| 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 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
public class SparseMatrixExponential { |
| 36 |
|
| 37 |
public static int maxKrylovBasisSize = 50; |
| 38 |
public static double tolerance = 1E-7; |
| 39 |
|
| 40 |
|
| 41 |
static { |
| 42 |
System.loadLibrary("ExpoKit"); |
| 43 |
} |
| 44 |
|
| 45 |
private native void executeDGEXPV(int order, int nz, int maxBasis, double time, |
| 46 |
double[] operandVector, double[] resultVector, |
| 47 |
double tolerance, double matrixNorm, |
| 48 |
double[] rate, int[] indexX, int[] indexY, |
| 49 |
double[] workspace, int lengthWorkspace, |
| 50 |
int[] intWorkspace, int lengethIntWorkspace, |
| 51 |
int flag |
| 52 |
); |
| 53 |
|
| 54 |
private int order; |
| 55 |
private int nonZeroEntries; |
| 56 |
private int krylovBasisSize; |
| 57 |
|
| 58 |
private double[] workSpace; |
| 59 |
private int[] intWorkSpace; |
| 60 |
private int lengthWorkspace; |
| 61 |
private int lengthIntWorkspace; |
| 62 |
|
| 63 |
|
| 64 |
private int[] fortranIndexX; |
| 65 |
private int[] fortranIndexY; |
| 66 |
private double[] rate; |
| 67 |
|
| 68 |
private double[] start; |
| 69 |
|
| 70 |
private double[] stop; |
| 71 |
|
| 72 |
private double norm; |
| 73 |
|
| 74 |
int index = 0; |
| 75 |
|
| 76 |
public SparseMatrixExponential(int order, int nonZeroEntries) { |
| 77 |
this.order = order; |
| 78 |
this.nonZeroEntries = nonZeroEntries; |
| 79 |
setUpWorkspace(); |
| 80 |
} |
| 81 |
|
| 82 |
private void setUpWorkspace() { |
| 83 |
|
| 84 |
if (order < maxKrylovBasisSize) |
| 85 |
krylovBasisSize = order - 10; |
| 86 |
else |
| 87 |
krylovBasisSize = maxKrylovBasisSize - 1; |
| 88 |
|
| 89 |
lengthIntWorkspace = krylovBasisSize + 2; |
| 90 |
lengthWorkspace = order * (lengthIntWorkspace) |
| 91 |
+ 5 * (lengthIntWorkspace) * (lengthIntWorkspace) + 7; |
| 92 |
|
| 93 |
intWorkSpace = new int[lengthIntWorkspace]; |
| 94 |
workSpace = new double[lengthWorkspace]; |
| 95 |
|
| 96 |
start = new double[order]; |
| 97 |
stop = new double[order]; |
| 98 |
|
| 99 |
fortranIndexX = new int[nonZeroEntries]; |
| 100 |
fortranIndexY = new int[nonZeroEntries]; |
| 101 |
rate = new double[nonZeroEntries]; |
| 102 |
|
| 103 |
} |
| 104 |
|
| 105 |
public void addEntry(int i, int j, double value) { |
| 106 |
fortranIndexX[index] = i + 1; |
| 107 |
fortranIndexY[index] = j + 1; |
| 108 |
rate[index] = value; |
| 109 |
index++; |
| 110 |
} |
| 111 |
|
| 112 |
public void setNorm(double norm) { |
| 113 |
this.norm = norm; |
| 114 |
} |
| 115 |
|
| 116 |
public void calculateInfinityNorm() { |
| 117 |
|
| 118 |
} |
| 119 |
|
| 120 |
public double getExponentialEntry(int x, int y, double time) { |
| 121 |
|
| 122 |
start[x] = 1.0; |
| 123 |
int flag = 0; |
| 124 |
|
| 125 |
executeDGEXPV(order, nonZeroEntries, krylovBasisSize, time, start, stop, tolerance, norm, |
| 126 |
rate, fortranIndexX, fortranIndexY, |
| 127 |
workSpace, lengthWorkspace, intWorkSpace, lengthIntWorkspace, flag |
| 128 |
); |
| 129 |
|
| 130 |
start[x] = 0.0; |
| 131 |
return stop[y]; |
| 132 |
} |
| 133 |
|
| 134 |
public String sparseRepresentation() { |
| 135 |
StringBuffer sb = new StringBuffer(); |
| 136 |
sb.append(order + " " + nonZeroEntries + "\n"); |
| 137 |
for (int i = 0; i < nonZeroEntries; i++) { |
| 138 |
sb.append(fortranIndexX[i]); |
| 139 |
sb.append(" "); |
| 140 |
sb.append(fortranIndexY[i]); |
| 141 |
sb.append(" "); |
| 142 |
sb.append(rate[i]); |
| 143 |
sb.append("\n"); |
| 144 |
} |
| 145 |
return sb.toString(); |
| 146 |
} |
| 147 |
|
| 148 |
} |