Debian Codegraph @

src/test/dr/math/KroneckerOperationTest.java

raw symbols SCIP
1 package test.dr.math;
2
3 import dr.math.matrixAlgebra.Vector;
4 import dr.math.matrixAlgebra.Matrix;
5 import dr.math.KroneckerOperation;
6
7 /**
8 * @author Marc A. Suchard
9 */
10 public class KroneckerOperationTest extends MathTestCase {
11
12 private void testKroneckerSum(double[] A, int m, double[] B, int n, double[] trueC) {
13
14 double[] C;
15 if (m == -1 || n == -1) {
16 C = KroneckerOperation.sum(A, B);
17 } else {
18 C = KroneckerOperation.sum(A, m, B, n);
19 }
20 printMatrix(A, m, m, "A = ");
21 printMatrix(B, n, n, "B = ");
22 printMatrix(C, m * n, m * n, "A %s% B = ");
23 didPass(C, trueC);
24 }
25
26 private void testKroneckerProduct(double[] A, int m, int n, double[] B, int p, int q, double[] trueC) {
27
28 double[] C = KroneckerOperation.product(A, m, n, B, p, q);
29 printMatrix(A, m, n, "A = ");
30 printMatrix(B, p, q, "B = ");
31 printMatrix(C, m * p, n * q, "A %x% B = ");
32 didPass(C, trueC);
33 }
34
35 private void testKroneckerProduct(double[][] A, double[][] B, double[][] trueC) {
36 double[][] C = KroneckerOperation.product(A, B);
37 printMatrix(A, "A = ");
38 printMatrix(B, "B = ");
39 printMatrix(C, "A %x% B = ");
40 didPass(C, trueC);
41 }
42
43 private void printMatrix(double[] A, int m, int n, String text) {
44 System.out.println(text + new Vector(A) + " (" + m + " x " + n + ")");
45 }
46
47 private void printMatrix(double[][] A, String text) {
48 System.out.println(text + "\n" + new Matrix(A));
49 }
50
51 private void didPass(double[][] C, double[][] trueC) {
52 boolean passed = true;
53 for (int i = 0; i < C.length; i++) {
54 for (int j = 0; j < C[i].length; j++) {
55 assertEquals(C[i][j], trueC[i][j], 1E-10);
56 if (C[i][j] != trueC[i][j]) {
57 passed = false;
58 break;
59 }
60 }
61 }
62 System.out.println("Passed = " + passed + "\n");
63 }
64
65 private void didPass(double[] C, double[] trueC) {
66 boolean passed = true;
67 for (int i = 0; i < C.length; i++) {
68 assertEquals(C[i], trueC[i], 1E-10);
69 if (C[i] != trueC[i]) {
70 passed = false;
71 break;
72 }
73 }
74 System.out.println("Passed = " + passed + "\n");
75 }
76
77 public void testKroneckerSum() {
78 double[] A = {
79 1, 2,
80 3, 4};
81 double[] B = {
82 0, 5,
83 6, 7};
84 double[] trueC = {
85 1, 5, 2, 0,
86 6, 8, 0, 2,
87 3, 0, 4, 5,
88 0, 3, 6, 11};
89 testKroneckerSum(A, 2, B, 2, trueC);
90
91 double[] D = {
92 -4, 2,
93 5, 6
94 };
95 double[] E = {
96 1, 2, 3,
97 4, 5, 6,
98 7, 8, 9
99 };
100 double[] trueF = {
101 -3, 2, 3, 2, 0, 0,
102 4, 1, 6, 0, 2, 0,
103 7, 8, 5, 0, 0, 2,
104 5, 0, 0, 7, 2, 3,
105 0, 5, 0, 4, 11, 6,
106 0, 0, 5, 7, 8, 15
107 };
108 testKroneckerSum(D, 2, E, 3, trueF);
109
110 // test sum for column-vectors
111 double[] V1 = {1, 2, 3};
112 double[] V2 = {4, 5};
113 double[] Vtrue = {5, 6, 6, 7, 7, 8};
114 testKroneckerSum(V1, -1, V2, -1, Vtrue);
115 }
116
117 public void testKroneckerProduct() {
118
119 double[] A = {1, 2, 3, 4};
120 double[] B = {0, 5, 6, 7};
121 double[] trueC = {0, 5, 0, 10, 6, 7, 12, 14, 0, 15, 0, 20, 18, 21, 24, 28};
122 testKroneckerProduct(A, 2, 2, B, 2, 2, trueC);
123
124 double[] D = {
125 1, 2, 3,
126 3, 2, 1};
127 double[] E = {
128 2, 1,
129 2, 3};
130 double[] trueF = {
131 2, 1, 4, 2, 6, 3,
132 2, 3, 4, 6, 6, 9,
133 6, 3, 4, 2, 2, 1,
134 6, 9, 4, 6, 2, 3};
135 testKroneckerProduct(D, 2, 3, E, 2, 2, trueF);
136
137 // test product for column-vectors
138 double[] V1 = {1, 2, 3};
139 double[] V2 = {4, 5};
140 double[] Vtrue = {4, 5, 8, 10, 12, 15};
141 testKroneckerProduct(V1, 3, 1, V2, 2, 1, Vtrue);
142
143 double[][] DD = {
144 {1, 2, 3},
145 {3, 2, 1}};
146 double[][] EE = {
147 {2, 1},
148 {2, 3}};
149 double[][] trueFF = {
150 {2, 1, 4, 2, 6, 3},
151 {2, 3, 4, 6, 6, 9},
152 {6, 3, 4, 2, 2, 1},
153 {6, 9, 4, 6, 2, 3}};
154 testKroneckerProduct(DD, EE, trueFF);
155
156 }
157 }