142 protected int numCols = 0;
143 protected int numRows = 0;
145 protected int outDigits = 0;
146 private int[][] originalMat;
147 protected transient int[][] genMat;
149 protected int[][] digitalShift;
152 protected double normFactor;
153 protected double[] factor;
155 protected int interlacing = 1;
160 private int[] primes = { 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67 };
165 private int[][] FaureFactor = { { 1 }, { 1, 2 }, { 2, 3, 1, 4 }, { 2, 3, 4, 5, 1, 6 },
166 { 3, 4, 7, 8, 2, 5, 6, 9, 1, 10 }, { 5, 8, 3, 4, 9, 10, 2, 6, 7, 11, 1, 12 },
167 { 5, 7, 10, 12, 3, 6, 11, 14, 4, 13, 2, 8, 9, 15, 1, 16 },
168 { 7, 8, 11, 12, 4, 5, 14, 15, 3, 6, 13, 16, 2, 9, 10, 17, 1, 18 },
169 { 5, 9, 14, 18, 7, 10, 13, 16, 4, 6, 17, 19, 3, 8, 15, 20, 2, 11, 12, 21, 1, 22 },
170 { 8, 11, 18, 21, 12, 17, 9, 13, 16, 20, 5, 6, 23, 24, 4, 7, 22, 25, 3, 10, 19, 26, 2, 14, 15, 27, 1, 28 },
171 { 12, 13, 18, 19, 11, 14, 17, 20, 7, 9, 22, 24, 4, 8, 23, 27, 5, 6, 25, 26, 3, 10, 21, 28, 2, 15, 16, 29, 1,
173 { 8, 14, 23, 29, 10, 11, 26, 27, 13, 17, 20, 24, 7, 16, 21, 30, 5, 15, 22, 32, 6, 31, 4, 9, 28, 33, 3, 12, 25,
174 34, 2, 18, 19, 35, 1, 36 },
175 { 16, 18, 23, 25, 11, 15, 26, 30, 12, 17, 24, 29, 9, 32, 13, 19, 22, 28, 6, 7, 34, 35, 5, 8, 33, 36, 4, 10, 31,
176 37, 3, 14, 27, 38, 2, 20, 21, 39, 1, 40 },
177 { 12, 18, 25, 31, 9, 19, 24, 34, 8, 16, 27, 35, 10, 13, 30, 33, 15, 20, 23, 28, 5, 17, 26, 38, 6, 7, 36, 37, 4,
178 11, 32, 39, 3, 14, 29, 40, 2, 21, 22, 41, 1, 42 },
179 { 13, 18, 29, 34, 11, 17, 30, 36, 10, 14, 33, 37, 7, 20, 27, 40, 9, 21, 26, 38, 15, 22, 25, 32, 6, 8, 39, 41,
180 5, 19, 28, 42, 4, 12, 35, 43, 3, 16, 31, 44, 2, 23, 24, 45, 1, 46 },
181 { 14, 19, 34, 39, 23, 30, 12, 22, 31, 41, 8, 11, 20, 24, 29, 33, 42, 45, 10, 16, 37, 43, 7, 15, 38, 46, 17, 25,
182 28, 36, 5, 21, 32, 48, 6, 9, 44, 47, 4, 13, 40, 49, 3, 18, 35, 50, 2, 26, 27, 51, 1, 52 },
183 { 25, 26, 33, 34, 18, 23, 36, 41, 14, 21, 38, 45, 24, 27, 32, 35, 11, 16, 43, 48, 9, 13, 46, 50, 8, 22, 37, 51,
184 7, 17, 42, 52, 19, 28, 31, 40, 6, 10, 49, 53, 5, 12, 47, 54, 4, 15, 44, 55, 3, 20, 39, 56, 2, 29, 30, 57,
186 { 22, 25, 36, 39, 17, 18, 43, 44, 24, 28, 33, 37, 13, 14, 47, 48, 16, 19, 42, 45, 9, 27, 34, 52, 8, 23, 38, 53,
187 11, 50, 7, 26, 35, 54, 21, 29, 32, 40, 6, 10, 51, 55, 5, 12, 49, 56, 4, 15, 46, 57, 3, 20, 41, 58, 2, 30,
189 { 18, 26, 41, 49, 14, 24, 43, 53, 12, 28, 39, 55, 29, 30, 37, 38, 10, 20, 47, 57, 16, 21, 46, 51, 8, 25, 42,
190 59, 13, 31, 36, 54, 9, 15, 52, 58, 7, 19, 48, 60, 23, 32, 35, 44, 5, 27, 40, 62, 6, 11, 56, 61, 4, 17,
191 50, 63, 3, 22, 45, 64, 2, 33, 34, 65, 1, 66 } };
210 this.interlacing = interlacing;
224 int[] bdigit =
new int[numCols];
225 int[] gdigit =
new int[numCols];
226 int idigits = intToDigitsGray(b, i, numCols, bdigit, gdigit);
228 if (digitalShift !=
null &&
dimShift < j)
231 for (l = 0; l < outDigits; l++) {
232 if (digitalShift ==
null)
235 sum = digitalShift[j][l];
237 for (c = 0; c < idigits; c++)
238 sum += genMat[j * numCols + c][l] * gdigit[c];
239 result += (sum % b) * factor[l];
241 if (digitalShift !=
null)
264 int[] bdigit =
new int[numCols];
266 for (c = 0; i > 0; c++) {
271 if (digitalShift !=
null &&
dimShift < j)
274 for (l = 0; l < outDigits; l++) {
275 if (digitalShift ==
null)
278 sum = digitalShift[j][l];
280 for (c = 0; c < idigits; c++)
281 sum += genMat[j * numCols + c][l] * bdigit[c];
282 result += (sum % b) * factor[l];
284 if (digitalShift !=
null)
312 throw new IllegalArgumentException(
PrintfFormat.
NEWLINE +
" Calling addRandomShift with null stream");
314 d2 = Math.max(1,
dim);
315 if (digitalShift ==
null) {
316 digitalShift =
new int[d2][outDigits];
322 int[][] temp =
new int[d3][outDigits];
324 for (
int i = 0; i < d1; i++)
325 for (
int j = 0; j < outDigits; j++)
326 temp[i][j] = digitalShift[i][j];
329 for (
int i = d1; i < d2; i++)
330 for (
int j = 0; j < outDigits; j++)
331 digitalShift[i][j] = stream.
nextInt(0, b - 1);
350 super.clearRandomShift();
360 StringBuffer sb =
new StringBuffer(100);
362 sb.append(
", base = ");
365 sb.append(
", Num cols = ");
367 sb.append(
", Num rows = ");
369 sb.append(
", outDigits = ");
370 sb.append(outDigits);
371 return sb.toString();
375 protected void printMat(
int N,
int[][][] A, String name) {
376 for (
int i = 0; i < N; i++) {
377 System.out.println(
"-------------------------------------" +
PrintfFormat.
NEWLINE + name +
" dim = " + i);
379 for (l = 0; l < numRows; l++) {
380 for (c = 0; c < numCols; c++) {
381 System.out.print(A[i][l][c] +
" ");
383 System.out.println(
"");
386 System.out.println(
"");
390 protected void printMat0(
int[][] A, String name) {
391 System.out.println(
"-------------------------------------" + PrintfFormat.NEWLINE + name);
393 for (l = 0; l < numCols; l++) {
394 for (c = 0; c < numCols; c++) {
395 System.out.print(A[l][c] +
" ");
397 System.out.println(
"");
399 System.out.println(
"");
405 private void leftMultiplyMat(
int j,
int[][] Mj) {
407 for (l = 0; l < numRows; l++) {
408 for (c = 0; c < numCols; c++) {
411 for (i = 0; i <= l; i++)
412 sum += Mj[l][i] * originalMat[j * numCols + c][i];
413 genMat[j * numCols + c][l] = sum % b;
421 private void leftMultiplyMatDiag(
int j,
int[][] Mj) {
423 for (l = 0; l < numRows; l++) {
424 for (c = 0; c < numCols; c++) {
426 sum = Mj[l][l] * originalMat[j * numCols + c][l];
427 genMat[j * numCols + c][l] = sum % b;
435 private void rightMultiplyMat(
int j,
int[][] Mj) {
437 for (l = 0; l < numRows; l++) {
438 for (c = 0; c < numCols; c++) {
441 for (i = 0; i <= c; i++)
442 sum += originalMat[j * numCols + i][l] * Mj[i][c];
443 genMat[j * numCols + c][l] = sum % b;
448 private int getFaureIndex(String method,
int sb,
int flag) {
452 throw new IllegalArgumentException(PrintfFormat.NEWLINE +
" sb >= base in " + method);
454 throw new IllegalArgumentException(PrintfFormat.NEWLINE +
" sb = 0 in " + method);
455 if ((flag > 2) || (flag < 0))
456 throw new IllegalArgumentException(PrintfFormat.NEWLINE +
" lowerFlag not in {0, 1, 2} in " + method);
460 while ((ib < primes.length) && (primes[ib] < b))
462 if (ib >= primes.length)
463 throw new IllegalArgumentException(
"base too large in " + method);
465 throw new IllegalArgumentException(
"Faure factors are not implemented for this base in " + method);
491 if (originalMat ==
null) {
492 originalMat = genMat;
493 genMat =
new int[
dim * numCols][numRows];
497 int[][][] scrambleMat =
new int[
dim][numRows][numRows];
498 for (j = 0; j <
dim; j++) {
499 for (l = 0; l < numRows; l++) {
500 for (c = 0; c < numRows; c++) {
502 scrambleMat[j][l][c] = stream.
nextInt(1, b - 1);
504 scrambleMat[j][l][c] = stream.
nextInt(0, b - 1);
506 scrambleMat[j][l][c] = 0;
511 for (j = 0; j <
dim; j++)
512 leftMultiplyMat(j, scrambleMat[j]);
526 if (originalMat ==
null) {
527 originalMat = genMat;
528 genMat =
new int[
dim * numCols][numRows];
531 int[][][] scrambleMat =
new int[
dim][numRows][numRows];
532 for (j = 0; j <
dim; j++) {
533 for (l = 0; l < numRows; l++) {
534 for (c = 0; c < numRows; c++) {
536 scrambleMat[j][l][c] = stream.
nextInt(1, b - 1);
538 scrambleMat[j][l][c] = 0;
543 for (j = 0; j <
dim; j++)
544 leftMultiplyMatDiag(j, scrambleMat[j]);
547 private void LMSFaurePermut(String method,
RandomStream stream,
int sb,
int lowerFlag) {
555 int ib = getFaureIndex(method, sb, lowerFlag);
558 if (originalMat ==
null) {
559 originalMat = genMat;
560 genMat =
new int[
dim * numCols][numRows];
566 int[][][] scrambleMat =
new int[
dim][numRows][numRows];
567 for (j = 0; j <
dim; j++) {
568 for (l = 0; l < numRows; l++) {
569 for (c = 0; c < numRows; c++) {
571 jb = stream.
nextInt(0, sb - 1);
572 scrambleMat[j][l][c] = FaureFactor[ib][jb];
574 if (lowerFlag == 2) {
575 scrambleMat[j][l][c] = stream.
nextInt(0, b - 1);
576 }
else if (lowerFlag == 1) {
577 jb = stream.
nextInt(0, sb - 1);
578 scrambleMat[j][l][c] = FaureFactor[ib][jb];
580 scrambleMat[j][l][c] = 0;
583 scrambleMat[j][l][c] = 0;
591 for (j = 0; j <
dim; j++)
592 leftMultiplyMatDiag(j, scrambleMat[j]);
594 for (j = 0; j <
dim; j++)
595 leftMultiplyMat(j, scrambleMat[j]);
611 LMSFaurePermut(
"leftMatrixScrambleFaurePermut", stream, sb, 2);
622 LMSFaurePermut(
"leftMatrixScrambleFaurePermutDiag", stream, sb, 0);
634 LMSFaurePermut(
"leftMatrixScrambleFaurePermutAll", stream, sb, 1);
653 if (originalMat ==
null) {
654 originalMat = genMat;
655 genMat =
new int[
dim * numCols][numRows];
659 int[][][] scrambleMat =
new int[
dim][numRows][numRows];
660 for (j = 0; j <
dim; j++) {
661 diag = stream.
nextInt(1, b - 1);
662 for (l = 0; l < numRows; l++) {
664 scrambleMat[j][l][l] = diag;
665 for (c = l + 1; c < numRows; c++)
666 scrambleMat[j][l][c] = 0;
668 for (l = 1; l < numRows; l++) {
669 col1 = stream.
nextInt(0, b - 1);
670 for (c = 0; l + c < numRows; c++)
671 scrambleMat[j][l + c][c] = col1;
675 for (j = 0; j <
dim; j++)
676 leftMultiplyMat(j, scrambleMat[j]);
679 private void iBMSFaurePermut(String method,
RandomStream stream,
int sb,
int lowerFlag) {
680 int ib = getFaureIndex(method, sb, lowerFlag);
683 if (originalMat ==
null) {
684 originalMat = genMat;
685 genMat =
new int[
dim * numCols][numRows];
693 int[][][] scrambleMat =
new int[
dim][numRows][numRows];
694 for (j = 0; j <
dim; j++) {
695 jb = stream.
nextInt(0, sb - 1);
696 diag = FaureFactor[ib][jb];
697 for (l = 0; l < numRows; l++) {
699 scrambleMat[j][l][l] = diag;
700 for (c = l + 1; c < numRows; c++)
701 scrambleMat[j][l][c] = 0;
703 for (l = 1; l < numRows; l++) {
704 if (lowerFlag == 2) {
705 col1 = stream.
nextInt(0, b - 1);
706 }
else if (lowerFlag == 1) {
707 jb = stream.
nextInt(0, sb - 1);
708 col1 = FaureFactor[ib][jb];
712 for (c = 0; l + c < numRows; c++)
713 scrambleMat[j][l + c][c] = col1;
719 for (j = 0; j <
dim; j++)
720 leftMultiplyMat(j, scrambleMat[j]);
722 for (j = 0; j <
dim; j++)
723 leftMultiplyMatDiag(j, scrambleMat[j]);
734 iBMSFaurePermut(
"iBinomialMatrixScrambleFaurePermut", stream, sb, 2);
745 iBMSFaurePermut(
"iBinomialMatrixScrambleFaurePermutDiag", stream, sb, 0);
757 iBMSFaurePermut(
"iBinomialMatrixScrambleFaurePermutAll", stream, sb, 1);
780 if (originalMat ==
null) {
781 originalMat = genMat;
782 genMat =
new int[
dim * numCols][numRows];
786 int[][][] scrambleMat =
new int[
dim][numRows][numRows];
787 for (j = 0; j <
dim; j++) {
788 for (c = 0; c < numRows; c++) {
789 diag = stream.
nextInt(1, b - 1);
790 for (l = 0; l < c; l++)
791 scrambleMat[j][l][c] = 0;
792 for (l = c; l < numRows; l++)
793 scrambleMat[j][l][c] = diag;
797 for (j = 0; j <
dim; j++)
798 leftMultiplyMat(j, scrambleMat[j]);
810 int ib = getFaureIndex(
"stripedMatrixScrambleFaurePermutAll", sb, 1);
813 if (originalMat ==
null) {
814 originalMat = genMat;
815 genMat =
new int[
dim * numCols][numRows];
823 int[][][] scrambleMat =
new int[
dim][numRows][numRows];
824 for (j = 0; j <
dim; j++) {
825 for (c = 0; c < numRows; c++) {
826 jb = stream.
nextInt(0, sb - 1);
827 diag = FaureFactor[ib][jb];
828 for (l = 0; l < c; l++)
829 scrambleMat[j][l][c] = 0;
830 for (l = c; l < numRows; l++)
831 scrambleMat[j][l][c] = diag;
835 for (j = 0; j <
dim; j++)
836 leftMultiplyMat(j, scrambleMat[j]);
859 if (originalMat ==
null) {
860 originalMat = genMat;
861 genMat =
new int[
dim * numCols][numRows];
866 int[][] scrambleMat =
new int[numCols][numCols];
867 for (c = 0; c < numCols; c++) {
868 for (l = 0; l < c; l++)
869 scrambleMat[l][c] = stream.
nextInt(0, b - 1);
870 scrambleMat[c][c] = stream.
nextInt(1, b - 1);
871 for (l = c + 1; l < numCols; l++)
872 scrambleMat[l][c] = 0;
877 for (j = 0; j <
dim; j++)
878 rightMultiplyMat(j, scrambleMat);
894 if (originalMat !=
null) {
895 genMat = originalMat;
914 for (
int j = 0; j < s; j++) {
916 for (
int l = 0; l < numRows; l++) {
917 for (
int c = 0; c < numCols; c++)
918 System.out.print(genMat[j * numCols + c][l] +
" ");
919 System.out.println(
"");
921 System.out.println(
"----------------------------------");
931 protected int intToDigitsGray(
int b,
int i,
int numDigits,
int[] bary,
int[] gray) {
937 for (c = 0; i > 0; c++) {
943 gray[idigits - 1] = bary[idigits - 1];
945 for (c = 0; c < idigits - 1; c++) {
946 diff = bary[c] - bary[c + 1];
952 for (c = idigits; c < numDigits; c++)
953 gray[c] = bary[c] = 0;
961 protected int idigits;
962 protected int[] bdigit;
963 protected int[] gdigit;
966 protected int[] cachedCurPoint;
971 public DigitalNetIterator() {
973 bdigit =
new int[numCols];
974 gdigit =
new int[numCols];
976 cachedCurPoint =
new int[(
dim + 1) * outDigits];
998 for (
int k = 0; k < outDigits; k++)
999 sum += cachedCurPoint[start + k] * factor[k];
1000 if (digitalShift !=
null)
1006 if (digitalShift ==
null)
1007 for (
int i = 0; i < cachedCurPoint.length; i++)
1008 cachedCurPoint[i] = 0;
1013 for (
int j = 0; j < dimS; j++)
1014 for (
int k = 0; k < outDigits; k++)
1015 cachedCurPoint[j * outDigits + k] = digitalShift[j][k];
1017 for (
int i = 0; i < numCols; i++)
1019 for (
int i = 0; i < numCols; i++)
1033 if (digitalShift !=
null &&
dimShift < dimS)
1037 idigits = intToDigitsGray(b, i, numCols, bdigit, gdigit);
1039 for (j = 0; j < dimS; j++) {
1040 for (l = 0; l < outDigits; l++) {
1041 if (digitalShift ==
null)
1044 sum = digitalShift[j][l];
1046 for (c = 0; c < idigits; c++)
1047 sum += genMat[j * numCols + c][l] * gdigit[c];
1048 cachedCurPoint[j * outDigits + l] = sum % b;
1064 for (pos = 0; gdigit[pos] == b - 1; pos++)
1073 if (outDigits < numRows)
1075 for (j = 0; j < dimS; j++) {
1076 for (l = 0; l < lsup; l++) {
1077 cachedCurPoint[j * outDigits + l] += genMat[j * numCols + pos][l];
1078 cachedCurPoint[j * outDigits + l] %= b;
1087 protected class DigitalNetIteratorNoGray
extends DigitalNetIterator {
1089 public DigitalNetIteratorNoGray() {
1104 idigits = intToDigitsGray(b, i, numCols, bdigit, gdigit);
1106 for (j = 0; j < dimS; j++) {
1107 for (l = 0; l < outDigits; l++) {
1108 if (digitalShift ==
null)
1111 sum = digitalShift[j][l];
1113 for (c = 0; c < idigits; c++)
1114 sum += genMat[j * numCols + c][l] * bdigit[c];
1115 cachedCurPoint[j * outDigits + l] = sum % b;
1129 for (pos = 0; bdigit[pos] == b - 1; pos++)
1140 if (outDigits < numRows)
1142 for (j = 0; j < dimS; j++) {
1143 for (l = 0; l < lsup; l++) {
1144 for (c = 0; c <= pos; c++)
1145 cachedCurPoint[j * outDigits + l] += genMat[j * numCols + c][l];
1146 cachedCurPoint[j * outDigits + l] %= b;