| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
package dr.math; |
| 27 |
|
| 28 |
import java.util.Arrays; |
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
public class KroneckerOperation { |
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
public static double[] sum(double[] A, double[] B) { |
| 41 |
final int dim = A.length * B.length; |
| 42 |
|
| 43 |
double[] out = new double[dim]; |
| 44 |
double[] tmpA = new double[dim]; |
| 45 |
double[] tmpB = new double[dim]; |
| 46 |
double[] Im = makeIdentityVector(A.length); |
| 47 |
double[] In = makeIdentityVector(B.length); |
| 48 |
|
| 49 |
sum(A, B, Im, In, tmpA, tmpB, out); |
| 50 |
return out; |
| 51 |
} |
| 52 |
|
| 53 |
public static void sum(double[] A, double[] B, |
| 54 |
double[] Im, double[] In, |
| 55 |
double[] tmpA, double[] tmpB, |
| 56 |
double[] C) { |
| 57 |
final int m = A.length; |
| 58 |
final int n = B.length; |
| 59 |
final int dim = m * n; |
| 60 |
|
| 61 |
if (C.length != dim |
| 62 |
|| Im.length != m || In.length != n |
| 63 |
|| tmpA.length != dim || tmpB.length != dim) { |
| 64 |
throw new RuntimeException("Wrong dimensions in Kronecker sum"); |
| 65 |
} |
| 66 |
|
| 67 |
product(A, m, 1, In, n, 1, tmpA); |
| 68 |
product(Im, m, 1, B, n, 1, tmpB); |
| 69 |
|
| 70 |
for (int i = 0; i < dim; i++) { |
| 71 |
C[i] = tmpA[i] + tmpB[i]; |
| 72 |
} |
| 73 |
} |
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
public static double[] sum(double[] A, int m, double[] B, int n) { |
| 78 |
final int dim = m * n; |
| 79 |
|
| 80 |
double[] out = new double[dim * dim]; |
| 81 |
double[] tmpA = new double[dim * dim]; |
| 82 |
double[] tmpB = new double[dim * dim]; |
| 83 |
double[] Im = makeIdentityMatrix(m); |
| 84 |
double[] In = makeIdentityMatrix(n); |
| 85 |
|
| 86 |
sum(A, m, B, n, Im, In, tmpA, tmpB, out); |
| 87 |
return out; |
| 88 |
} |
| 89 |
|
| 90 |
public static void sum(double[] A, int m, double[] B, int n, |
| 91 |
double[] Im, double[] In, |
| 92 |
double[] tmpA, double[] tmpB, |
| 93 |
double[] C) { |
| 94 |
|
| 95 |
final int dim = m * n; |
| 96 |
|
| 97 |
if (C.length != dim * dim || A.length != m * m || B.length != n * n |
| 98 |
|| Im.length != m * m || In.length != n * n |
| 99 |
|| tmpA.length != dim * dim || tmpB.length != dim * dim) { |
| 100 |
throw new RuntimeException("Wrong dimensions in Kronecker sum"); |
| 101 |
} |
| 102 |
|
| 103 |
product(A, m, m, In, n, n, tmpA); |
| 104 |
product(Im, m, m, B, n, n, tmpB); |
| 105 |
|
| 106 |
for (int i = 0; i < dim * dim; i++) { |
| 107 |
C[i] = tmpA[i] + tmpB[i]; |
| 108 |
} |
| 109 |
} |
| 110 |
|
| 111 |
private static double[] makeIdentityMatrix(int dim) { |
| 112 |
double[] out = new double[dim * dim]; |
| 113 |
for (int i = 0; i < dim; i++) { |
| 114 |
out[i * dim + i] = 1.0; |
| 115 |
} |
| 116 |
return out; |
| 117 |
} |
| 118 |
|
| 119 |
private static double[] makeIdentityVector(int dim) { |
| 120 |
double[] out = new double[dim]; |
| 121 |
Arrays.fill(out, 1.0); |
| 122 |
return out; |
| 123 |
} |
| 124 |
|
| 125 |
|
| 126 |
public static double[] product(double[] A, int m, int n, double[] B, int p, int q) { |
| 127 |
double[] out = new double[m * p * n * q]; |
| 128 |
product(A, m, n, B, p, q, out); |
| 129 |
return out; |
| 130 |
} |
| 131 |
|
| 132 |
public static void product(double[] A, int m, int n, double[] B, int p, int q, double[] out) { |
| 133 |
|
| 134 |
final int dimi = m * p; |
| 135 |
final int dimj = n * q; |
| 136 |
|
| 137 |
if (out.length != dimi * dimj || A.length != m * n || B.length != p * q) { |
| 138 |
throw new RuntimeException("Wrong dimensions in Kronecker product"); |
| 139 |
} |
| 140 |
|
| 141 |
for (int i = 0; i < m; i++) { |
| 142 |
|
| 143 |
final int iOffset = i * p; |
| 144 |
|
| 145 |
for (int j = 0; j < n; j++) { |
| 146 |
|
| 147 |
final int jOffset = j * q; |
| 148 |
|
| 149 |
final double aij = A[i * n + j]; |
| 150 |
|
| 151 |
for (int k = 0; k < p; k++) { |
| 152 |
for (int l = 0; l < q; l++) { |
| 153 |
out[(iOffset + k) * dimj + jOffset + l] = aij * B[k * q + l]; |
| 154 |
} |
| 155 |
} |
| 156 |
} |
| 157 |
} |
| 158 |
} |
| 159 |
|
| 160 |
public static double[][] product(double[][] A, double[][] B) { |
| 161 |
final int m = A.length; |
| 162 |
final int n = A[0].length; |
| 163 |
final int p = B.length; |
| 164 |
final int q = B[0].length; |
| 165 |
|
| 166 |
double[][] out = new double[m * p][n * q]; |
| 167 |
product(A, B, out); |
| 168 |
return out; |
| 169 |
} |
| 170 |
|
| 171 |
public static void product(double[][] A, double[][] B, double[][] out) { |
| 172 |
final int m = A.length; |
| 173 |
final int n = A[0].length; |
| 174 |
final int p = B.length; |
| 175 |
final int q = B[0].length; |
| 176 |
|
| 177 |
if (out == null || out.length != m * p || out[0].length != n * q) { |
| 178 |
throw new RuntimeException("Wrong dimensions in Kronecker product"); |
| 179 |
} |
| 180 |
|
| 181 |
for (int i = 0; i < m; i++) { |
| 182 |
final int iOffset = i * p; |
| 183 |
for (int j = 0; j < n; j++) { |
| 184 |
final int jOffset = j * q; |
| 185 |
final double aij = A[i][j]; |
| 186 |
|
| 187 |
for (int k = 0; k < p; k++) { |
| 188 |
for (int l = 0; l < q; l++) { |
| 189 |
out[iOffset + k][jOffset + l] = aij * B[k][l]; |
| 190 |
} |
| 191 |
} |
| 192 |
|
| 193 |
} |
| 194 |
} |
| 195 |
} |
| 196 |
|
| 197 |
public static double[] vectorize(double[][] A) { |
| 198 |
double[] out = new double[A.length * A[0].length]; |
| 199 |
vectorize(A, out); |
| 200 |
return out; |
| 201 |
} |
| 202 |
|
| 203 |
public static void vectorize(double[][] A, double[] out) { |
| 204 |
final int m = A.length; |
| 205 |
final int n = A[0].length; |
| 206 |
|
| 207 |
if (out == null || out.length != m * n) { |
| 208 |
throw new RuntimeException("Wrong dimensions in vectorize"); |
| 209 |
} |
| 210 |
|
| 211 |
int offset = 0; |
| 212 |
for (int i = 0; i < m; ++i) { |
| 213 |
for (int j = 0; j < n; ++j) { |
| 214 |
out[offset] = A[i][j]; |
| 215 |
++offset; |
| 216 |
} |
| 217 |
} |
| 218 |
} |
| 219 |
} |