Permutation Permutation_permuteRandomly (Permutation me, long from, long to) { try { autoPermutation thee = Data_copy (me); Permutation_permuteRandomly_inline (thee.peek(), from, to); return thee.transfer(); } catch (MelderError) { Melder_throw (me, ": not permuted."); } }
Collection Collection_permuteItems (Collection me) { try { autoPermutation p = Permutation_create (my size); Permutation_permuteRandomly_inline (p.peek(), 0, 0); autoCollection thee = Collection_and_Permutation_permuteItems (me, p.peek()); return thee.transfer(); } catch (MelderError) { Melder_throw (me, ": items not permuted."); } }
Permutation Permutation_permuteBlocksRandomly (Permutation me, long from, long to, long blocksize, int permuteWithinBlocks, int noDoublets) { try { long n = Permutation_checkRange (me, &from, &to); if (blocksize == 1 || (blocksize >= n && permuteWithinBlocks)) { autoPermutation thee = Permutation_permuteRandomly (me, from, to); return thee.transfer(); } autoPermutation thee = Data_copy (me); if (blocksize >= n) { return thee.transfer(); } long nblocks = n / blocksize, nrest = n % blocksize; if (nrest != 0) Melder_throw ("It is not possible to fit an integer number of blocks " "in the range.\n(The last block is only of size ", nrest, ")."); autoPermutation pblocks = Permutation_create (nblocks); Permutation_permuteRandomly_inline (pblocks.peek(), 1, nblocks); long first = from; for (long iblock = 1; iblock <= nblocks; iblock++, first += blocksize) { /* (n1,n2,n3,...) means: move block n1 to position 1 etc... */ long blocktomove = Permutation_getValueAtIndex (pblocks.peek(), iblock); for (long j = 1; j <= blocksize; j++) { thy p[first - 1 + j] = my p[from - 1 + (blocktomove - 1) * blocksize + j]; } if (permuteWithinBlocks) { long last = first + blocksize - 1; Permutation_permuteRandomly_inline (thee.peek(), first, last); if (noDoublets && iblock > 0 && (thy p[first - 1] % blocksize) == (thy p[first] % blocksize)) { Permutation_swapOneFromRange (thee.peek(), first + 1, last, first, 0); } } } return thee.transfer(); } catch (MelderError) { Melder_throw (me, ": not permuted block randomly."); } }
autoPermutation Index_to_Permutation_permuteRandomly (Index me, int permuteWithinClasses) { try { long numberOfClasses = my classes->size; autoPermutation thee = Permutation_create (my numberOfElements); autoPermutation classes = Permutation_create (numberOfClasses); Permutation_permuteRandomly_inline (classes.get(), 0, 0); autoPermutation classesinv = Permutation_invert (classes.get()); autoNUMmatrix<long> indices (0, numberOfClasses, 1, 4); for (long i = 1; i <= my numberOfElements; i++) { indices[my classIndex[i]][2]++; /* col 2: number of elements in class */ } /* Get some other indices ready */ for (long i = 1; i <= numberOfClasses; i++) { long klass = classes -> p[i]; indices[i][1] = klass; indices[i][3] = indices[i - 1][3] + indices[i - 1][2]; /* col 3: index at start of class */ } for (long i = 1; i <= my numberOfElements; i++) { long klass = my classIndex[i]; long newindex = classesinv -> p[klass]; indices[newindex][4]++; /* col 4: number of elements processed for class */ long newpos = indices[newindex][3] + indices[newindex][4]; thy p[newpos] = i; } if (permuteWithinClasses) { for (long i = 1; i <= numberOfClasses; i++) { long from = indices[i][3] + 1; long to = from + indices[i][2] - 1; if (to > from) { Permutation_permuteRandomly_inline (thee.get(), from, to); } } } return thee; } catch (MelderError) { Melder_throw (me, U": Permutation not created."); } }
void Graphics_matrixAsSquares (Graphics g, double **matrix, long numberOfRows, long numberOfColumns, double zmin, double zmax, double cellSizeFactor, int randomFillOrder) { long numberOfCells = numberOfRows * numberOfColumns; autoPermutation p = Permutation_create (numberOfCells); if (randomFillOrder) { Permutation_permuteRandomly_inline (p.peek(), 1, numberOfCells); } double zAbsMax = fabs (zmax) > fabs (zmin) ? fabs (zmax) : fabs (zmin); Graphics_Colour colour = Graphics_inqColour (g); double x1WC, x2WC, y1WC, y2WC; Graphics_inqWindow (g, &x1WC, &x2WC, &y1WC, &y2WC); double dx = fabs (x2WC - x1WC) / numberOfColumns; double dy = fabs (y2WC - y1WC) / numberOfRows; for (long i = 1; i <= numberOfCells; i++) { long index = Permutation_getValueAtIndex (p.peek(), i); long irow = (index - 1) / numberOfColumns + 1; long icol = (index - 1) % numberOfColumns + 1; double z = matrix[irow][icol]; z = z < zmin ? zmin : z; z = z > zmax ? zmax : z; double zweight = sqrt (fabs (z) / zAbsMax); // Area length^2) double xcenter = (icol - 0.5) * dx; double ycenter = (irow - 0.5) * dy; double x1 = x1WC + xcenter - zweight * 0.5 * dx * cellSizeFactor; x1 = x1 < x1WC ? x1WC : x1; double x2 = x1WC + xcenter + zweight * 0.5 * dx * cellSizeFactor; x2 = x2 > x2WC ? x2WC : x2; double y1 = y1WC + ycenter - zweight * 0.5 * dy * cellSizeFactor; y1 = y1 < y1WC ? y1WC : y1; double y2 = y1WC + ycenter + zweight * 0.5 * dy * cellSizeFactor; y2 = y2 > y2WC ? y2WC : y2; if (z > 0) { Graphics_setColour (g, Graphics_WHITE); } Graphics_fillRectangle (g, x1, x2, y1, y2); Graphics_setColour (g, colour); Graphics_rectangle (g, x1, x2 , y1, y2); } }