iterative permutation library update
This commit is contained in:
@ -4,6 +4,61 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <math.h>
|
||||
#include <new>
|
||||
|
||||
extern "C"
|
||||
{
|
||||
|
||||
////////////////////////////////////////////
|
||||
//Basic operations on preallocated buffers//
|
||||
//for C library export //
|
||||
////////////////////////////////////////////
|
||||
|
||||
static const int amsperm1_success = 0;
|
||||
static const int amsperm1_failure = -1;
|
||||
|
||||
//basic factorial found with integer loop
|
||||
int amsperm1_factorial(int n);
|
||||
|
||||
//Is the multi-index in the appropriate factoriadic range?
|
||||
// position 0 can range from [0 to length)
|
||||
// position 1 can range from [0 to (length-1))
|
||||
//parameters:
|
||||
// int mindex[length] - multi-index input
|
||||
// int length - multi-index length output
|
||||
bool amsperm1_mindex_valid(int *mindex, int length);
|
||||
|
||||
|
||||
bool amsperm1_perm_valid(int *perm, int *wrk, int length);
|
||||
|
||||
//returns a factoriadic multi-index for a given index.
|
||||
//Each factoriadic multi-index corresponds to a permutation according to a simple algorithm
|
||||
// factoriadic indices range from (ex 4 length case) {0,0,0,0} to {3,2,1,0}
|
||||
// parameters:
|
||||
// index - [0 to factorial(length)) index of permutation
|
||||
// mindex - factoriadic multi-index output (size=length)
|
||||
// length - length of the permutation (number of elements to permute)
|
||||
// return value:
|
||||
// 0: success
|
||||
// -1 : privided index was out of the valid range
|
||||
int amsperm1_index_to_mindex(int index, int *mindex, int length);
|
||||
|
||||
int amsperm1_mindex_to_index(int *mindex, int length);
|
||||
|
||||
|
||||
int amsperm1_mindex_to_perm(int *mindex, int *perm, int *wrk, int length);
|
||||
|
||||
|
||||
int amsperm1_perm_to_mindex(int *perm, int *mindex, int *wrk, int length);
|
||||
|
||||
int amsperm1_index_to_perm(int index, int *perm, int *mindex, int *wrk, int length);
|
||||
|
||||
int amsperm1_perm_to_index(int *perm, int *mindex, int *wrk, int length);
|
||||
|
||||
void amsperm1_test_basicperm1();
|
||||
|
||||
}; //end extern C
|
||||
|
||||
|
||||
|
||||
namespace ams
|
||||
@ -11,42 +66,83 @@ namespace ams
|
||||
namespace perm
|
||||
{
|
||||
|
||||
//Basic operations on preallocated buffers
|
||||
//for later C library export
|
||||
|
||||
static const int perm_success = 0;
|
||||
static const int perm_failure = -1;
|
||||
|
||||
int factorial(int n);
|
||||
|
||||
int index_to_mindex(int index, int *mindex, int ndim);
|
||||
|
||||
int mindex_to_index(int *mindex, int ndim);
|
||||
int mindex_to_perm(int *mindex, int *perm, int *wrk, int ndim);
|
||||
int perm_to_mindex(int *perm, int *mindex, int *wrk, int ndim);
|
||||
|
||||
bool perm_valid(int *perm, int ndim);
|
||||
|
||||
bool mindex_valid(int *mindex, int ndim);
|
||||
|
||||
void index_to_perm(int index, int *perm, int ndim);
|
||||
int perm_to_index(int *perm, int ndim);
|
||||
|
||||
|
||||
|
||||
|
||||
//Permutation class
|
||||
class permutation
|
||||
//Iteratable Permutation Class
|
||||
//Provides an object for iterating over permutations of low dimension
|
||||
//These can be stepped along
|
||||
class ipermutation
|
||||
{
|
||||
public:
|
||||
int dim;
|
||||
int *data;
|
||||
int length;
|
||||
int *perm; //permutation
|
||||
|
||||
int index; //do I want these also stored with the permutation variable?
|
||||
int *mind; //factoriadic multi-index
|
||||
int *wrk; //working memory buffer
|
||||
|
||||
ipermutation();
|
||||
ipermutation(const int _length);
|
||||
ipermutation(const int _index, const int _length);
|
||||
ipermutation(const int *parray, const int _length);
|
||||
~ipermutation();
|
||||
|
||||
ipermutation(const ipermutation& other);
|
||||
|
||||
int resize(const int _nlength);
|
||||
|
||||
ipermutation operator=(const ipermutation &other);
|
||||
int& operator[](const int ind);
|
||||
const int& operator[](const int ind) const;
|
||||
|
||||
bool valid() const;
|
||||
bool operator==(const ipermutation other) const;
|
||||
|
||||
//pre-increment operator
|
||||
ipermutation& operator++();
|
||||
ipermutation& operator--();
|
||||
|
||||
//post-increment operators
|
||||
ipermutation operator++(int);
|
||||
ipermutation operator--(int);
|
||||
|
||||
ipermutation operator*(const ipermutation b) const; //compose permutations
|
||||
ipermutation inverse() const; //invert permutation
|
||||
|
||||
//returns first and last permutations of a given lengthension
|
||||
static ipermutation first(int _nlength);
|
||||
static ipermutation last(int _nlength);
|
||||
|
||||
int levi_civita() const;
|
||||
|
||||
void print(int style=0);
|
||||
|
||||
permutation();
|
||||
permutation(int _dim);
|
||||
~permutation();
|
||||
};
|
||||
|
||||
ipermutation ipermutation_first(int _nlength);
|
||||
ipermutation ipermutation_last(int _nlength);
|
||||
int levi_civita(const ipermutation p);
|
||||
|
||||
void test_ipermutation1();
|
||||
|
||||
// non-indexable permutation class
|
||||
// the same permutation logic, but for larger permutation sets
|
||||
// can't index, can't increment or decrement a sequence, but can still
|
||||
// compose, test validity, test levi_civita sign, apply to arrays, etc.
|
||||
class permutation
|
||||
{
|
||||
public:
|
||||
int length;
|
||||
int *data;
|
||||
|
||||
|
||||
};
|
||||
|
||||
|
||||
}; //end namespace perm
|
||||
}; //end namespace ams
|
||||
|
||||
|
||||
Reference in New Issue
Block a user