| 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.matrixAlgebra; |
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
public class LinearEquations { |
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
private double[][] rows; |
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
private Vector[] solutions; |
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 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 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 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 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
public LinearEquations(Matrix a, Vector y) |
| 109 |
throws IllegalDimension { |
| 110 |
this(a.components, y.components); |
| 111 |
} |
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 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 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 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 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 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 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
private void pivotingStep(int p) { |
| 181 |
swapRows(p, largestPivot(p)); |
| 182 |
pivot(p); |
| 183 |
return; |
| 184 |
} |
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
public Vector solution() throws ArithmeticException { |
| 190 |
return solution(0); |
| 191 |
} |
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 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 |
|
| 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 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 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 |
|
| 239 |
|
| 240 |
|
| 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 |
} |