Debian Codegraph @

src/dr/math/SparseMatrixExponential.java

raw symbols SCIP
1 /*
2 * SparseMatrixExponential.java
3 *
4 * Copyright (c) 2002-2015 Alexei Drummond, Andrew Rambaut and Marc Suchard
5 *
6 * This file is part of BEAST.
7 * See the NOTICE file distributed with this work for additional
8 * information regarding copyright ownership and licensing.
9 *
10 * BEAST is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU Lesser General Public License as
12 * published by the Free Software Foundation; either version 2
13 * of the License, or (at your option) any later version.
14 *
15 * BEAST is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Lesser General Public License for more details.
19 *
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with BEAST; if not, write to the
22 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
23 * Boston, MA 02110-1301 USA
24 */
25
26 package dr.math;
27
28 /**
29 * Created by IntelliJ IDEA.
30 * User: msuchard
31 * Date: Jul 28, 2007
32 * Time: 11:05:44 AM
33 * To change this template use File | Settings | File Templates.
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; // todo determine correct cut-off, consider Pade approximation for small order
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 // todo
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; // recycle
131 return stop[y]; // stop gets overwritten with each call, no need to reset values
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 }