*/
public Vector permute(Vector v , int numPermutations) {
if (v instanceof TernaryVector)
return permute((TernaryVector) v, numPermutations, v.length());
Vector result = Vectors.instanceOf(v);
int[] dimensions = null;
int[] oldDims = null;
if (v instanceof SparseVector) {
oldDims = ((SparseVector) v).getNonZeroIndices();
dimensions = Arrays.copyOf(oldDims, oldDims.length);
} else {
dimensions = new int[v.length()];
for (int i = 0; i < v.length(); ++i)
dimensions[i] = i;
}
System.out.printf("input: %s%ninitial result: %s%n", v, result);
boolean isInverse = numPermutations < 0;
// NB: because we use the signum and !=, this loop will work for both
// positive and negative numbers of permutations
int totalPermutations = Math.abs(numPermutations);
// Loop through the number of permutation that we have to do and keep
// updating which indices are being assigned to which
for (int count = 1; count <= totalPermutations; ++count) {
// load the reordering funcion for this iteration of the permutation
Function function = getFunction(count, v.length());
// based on whether this is an inverse permutation, select whether
// to use the forward or backwards mapping.
int[] reordering = (isInverse)
? function.backward : function.forward;
oldDims = Arrays.copyOf(dimensions, dimensions.length);
for (int i = 0; i < oldDims.length; ++i) {
dimensions[i] = reordering[oldDims[i]];
}
}
for (int d : dimensions)
result.set(dimensions[d], v.getValue(d));
System.out.printf("input: %s%nfinal permuted result: %s%n", v, result);
return result;
}