update
parent
41a3fbcdea
commit
11975cebfb
Binary file not shown.
Binary file not shown.
@ -0,0 +1,106 @@
|
|||||||
|
#include <amscppperm1/amscppperm1.hpp>
|
||||||
|
|
||||||
|
namespace ams
|
||||||
|
{
|
||||||
|
namespace perm
|
||||||
|
{
|
||||||
|
|
||||||
|
//basic factorial found with integer loop
|
||||||
|
int factorial(int n)
|
||||||
|
{
|
||||||
|
int I;
|
||||||
|
int ret = 0;
|
||||||
|
if(n>=0)
|
||||||
|
{
|
||||||
|
ret = 1;
|
||||||
|
for(I=0;I<n;I++)
|
||||||
|
{
|
||||||
|
ret = ret*(I+1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
//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 dim case) {0,0,0,0} to {3,2,1,0}
|
||||||
|
// parameters:
|
||||||
|
// index - [0 to factorial(ndim)) index of permutation
|
||||||
|
// mindex - factoriadic multi-index output (size=ndim)
|
||||||
|
// ndim - dimension of the permutation (number of elements to permute)
|
||||||
|
// return value:
|
||||||
|
// 0: success
|
||||||
|
// -1 : privided index was out of the valid range
|
||||||
|
int index_to_mindex(int index, int *mindex, int ndim)
|
||||||
|
{
|
||||||
|
int ret = -1;
|
||||||
|
int I,J,K;
|
||||||
|
if(index>=0 && index<factorial(ndim))
|
||||||
|
{
|
||||||
|
ret = 0; //success
|
||||||
|
K = ndim;
|
||||||
|
J = index;
|
||||||
|
for(I=0;I<ndim;I++)
|
||||||
|
{
|
||||||
|
mindex[I] = J%K;
|
||||||
|
J = (J - mindex[I])/K;
|
||||||
|
K = K-1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
ret = -1; //error
|
||||||
|
//invalid index
|
||||||
|
for(I=0;I<ndim;I++)
|
||||||
|
mindex[I] = -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
//Is the multi-index in the appropriate factoriadic range?
|
||||||
|
// position 0 can range from [0 to ndim)
|
||||||
|
// position 1 can range from [0 to (ndim-1))
|
||||||
|
bool mindex_valid(int *mindex, int ndim)
|
||||||
|
{
|
||||||
|
int I;
|
||||||
|
bool ret = 1;
|
||||||
|
for(I=0;I<ndim;I++)
|
||||||
|
{
|
||||||
|
if(mindex[I]<0 || mindex[I]>=(ndim-I))
|
||||||
|
{
|
||||||
|
ret = 0;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
int mindex_to_index(int *mindex, int ndim)
|
||||||
|
{
|
||||||
|
int ret = -1;
|
||||||
|
int I;
|
||||||
|
int K;
|
||||||
|
if(mindex_valid(mindex,ndim))
|
||||||
|
{
|
||||||
|
K = 1;
|
||||||
|
ret = 0;
|
||||||
|
for(I=0;I<ndim;I++)
|
||||||
|
{
|
||||||
|
ret = ret + mindex[I]*K;
|
||||||
|
K = K*(ndim-I);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
ret = -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}; //end namespace perm
|
||||||
|
}; //end namespace ams
|
Loading…
Reference in New Issue