Debian Codegraph @

src/dr/math/matrixAlgebra/LinearEquations.java

raw symbols SCIP
1 /*
2 * LinearEquations.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.matrixAlgebra;
27
28 /**
29 * Class representing a system of linear equations.
30 *
31 * @author Didier H. Besset
32 */
33 public class LinearEquations {
34 /**
35 * components is a matrix build from the system's matrix and
36 * the constant vector
37 */
38 private double[][] rows;
39 /**
40 * Array containing the solution vectors.
41 */
42 private Vector[] solutions;
43
44 /**
45 * Construct a system of linear equation Ax = y1, y2,....
46 *
47 * @param m double[][]
48 * @param c double[][]
49 * @throws IllegalDimension if the system's matrix is not square
50 * if constant dimension does not match
51 * that of the matrix
52 */
53 public LinearEquations(double[][] m, double[][] c)
54 throws IllegalDimension {
55 int n = m.length;
56 if (m[0].length != n)
57 throw new IllegalDimension("Illegal system: a" + n + " by "
58 + m[0].length + " matrix is not a square matrix");
59 if (c[0].length != n)
60 throw new IllegalDimension("Illegal system: a " + n + " by " + n
61 + " matrix cannot build a system with a "
62 + c[0].length + "-dimensional vector");
63 rows = new double[n][n + c.length];
64 for (int i = 0; i < n; i++) {
65 for (int j = 0; j < n; j++)
66 rows[i][j] = m[i][j];
67 for (int j = 0; j < c.length; j++)
68 rows[i][n + j] = c[j][i];
69 }
70 }
71
72 /**
73 * Construct a system of linear equation Ax = y.
74 *
75 * @param m double[][] components of the system's matrix
76 * @param c double[] components of the constant vector
77 * @throws IllegalDimension if the system's matrix is not square
78 * if constant dimension does not match
79 * that of the matrix
80 */
81 public LinearEquations(double[][] m, double[] c)
82 throws IllegalDimension {
83 int n = m.length;
84 if (m[0].length != n)
85 throw new IllegalDimension("Illegal system: a" + n + " by "
86 + m[0].length + " matrix is not a square matrix");
87 if (c.length != n)
88 throw new IllegalDimension("Illegal system: a " + n + " by " + n
89 + " matrix cannot build a system with a "
90 + c.length + "-dimensional vector");
91 rows = new double[n][n + 1];
92 for (int i = 0; i < n; i++) {
93 for (int j = 0; j < n; j++)
94 rows[i][j] = m[i][j];
95 rows[i][n] = c[i];
96 }
97 }
98
99 /**
100 * Construct a system of linear equation Ax = y.
101 *
102 * @param a MatrixAlgebra.Matrix matrix A
103 * @param y MatrixAlgebra.Vector vector y
104 * @throws IllegalDimension if the system's matrix is not square
105 * if vector dimension does not match
106 * that of the matrix
107 */
108 public LinearEquations(Matrix a, Vector y)
109 throws IllegalDimension {
110 this(a.components, y.components);
111 }
112
113 /**
114 * Computes the solution for constant vector p applying
115 * backsubstitution.
116 *
117 * @param p int
118 * @throws java.lang.ArithmeticException if one diagonal element
119 * of the triangle matrix is zero.
120 */
121 private void backSubstitution(int p) throws ArithmeticException {
122 int n = rows.length;
123 double[] answer = new double[n];
124 double x;
125 for (int i = n - 1; i >= 0; i--) {
126 x = rows[i][n + p];
127 for (int j = i + 1; j < n; j++)
128 x -= answer[j] * rows[i][j];
129 answer[i] = x / rows[i][i];
130 }
131 solutions[p] = new Vector(answer);
132 return;
133 }
134
135 /**
136 * Finds the position of the largest pivot at step p.
137 *
138 * @param p int step of pivoting.
139 * @return int
140 */
141 private int largestPivot(int p) {
142 double pivot = Math.abs(rows[p][p]);
143 int answer = p;
144 double x;
145 for (int i = p + 1; i < rows.length; i++) {
146 x = Math.abs(rows[i][p]);
147 if (x > pivot) {
148 answer = i;
149 pivot = x;
150 }
151 }
152 return answer;
153 }
154
155 /**
156 * Perform pivot operation at location p.
157 *
158 * @param p int
159 * @throws java.lang.ArithmeticException if the pivot element
160 * is zero.
161 */
162 private void pivot(int p) throws ArithmeticException {
163 double inversePivot = 1 / rows[p][p];
164 double r;
165 int n = rows.length;
166 int m = rows[0].length;
167 for (int i = p + 1; i < n; i++) {
168 r = inversePivot * rows[i][p];
169 for (int j = p; j < m; j++)
170 rows[i][j] -= rows[p][j] * r;
171 }
172 return;
173 }
174
175 /**
176 * Perform optimum pivot operation at location p.
177 *
178 * @param p int
179 */
180 private void pivotingStep(int p) {
181 swapRows(p, largestPivot(p));
182 pivot(p);
183 return;
184 }
185
186 /**
187 * @return Vector solution for the 1st constant vector
188 */
189 public Vector solution() throws ArithmeticException {
190 return solution(0);
191 }
192
193 /**
194 * Return the vector solution of constants indexed by p.
195 *
196 * @param p int index of the constant vector fed into the system.
197 * @return matrixAlgebra.Vector
198 * @throws java.lang.ArithmeticException if the system cannot be solved.
199 */
200 public Vector solution(int p) throws ArithmeticException {
201 if (solutions == null)
202 solve();
203 if (solutions[p] == null)
204 backSubstitution(p);
205 return solutions[p];
206 }
207
208 /**
209 * @throws java.lang.ArithmeticException if the system cannot be solved.
210 */
211 private void solve() throws ArithmeticException {
212 int n = rows.length;
213 for (int i = 0; i < n; i++)
214 pivotingStep(i);
215 solutions = new Vector[rows[0].length - n];
216 }
217
218 /**
219 * Swaps rows p and q.
220 *
221 * @param p int
222 * @param q int
223 */
224 private void swapRows(int p, int q) {
225 if (p != q) {
226 double temp;
227 int m = rows[p].length;
228 for (int j = 0; j < m; j++) {
229 temp = rows[p][j];
230 rows[p][j] = rows[q][j];
231 rows[q][j] = temp;
232 }
233 }
234 return;
235 }
236
237 /**
238 * Returns a string representation of the system.
239 *
240 * @return java.lang.String
241 */
242 public String toString() {
243 StringBuffer sb = new StringBuffer();
244 char[] separator = {'[', ' '};
245 int n = rows.length;
246 int m = rows[0].length;
247 for (int i = 0; i < n; i++) {
248 separator[0] = '(';
249 for (int j = 0; j < n; j++) {
250 sb.append(separator);
251 sb.append(rows[i][j]);
252 separator[0] = ',';
253 }
254 separator[0] = ':';
255 for (int j = n; j < m; j++) {
256 sb.append(separator);
257 sb.append(rows[i][j]);
258 separator[0] = ',';
259 }
260 sb.append(')');
261 sb.append('\n');
262 }
263 return sb.toString();
264 }
265 }