Solves for the vector x such that U x = y @param U an upper triangular matrix @param y a vector whos length is equal to the rows in U @return x such that U x = y
(Matrix U, Vec y)
| 252 | * @return x such that U x = y |
| 253 | */ |
| 254 | public static Vec backSub(Matrix U, Vec y) |
| 255 | { |
| 256 | if (y.length() != U.rows()) |
| 257 | throw new ArithmeticException("Vector and matrix sizes do not agree"); |
| 258 | |
| 259 | Vec x = y instanceof SparseVector ? new SparseVector(U.cols()) : new DenseVector(U.cols()); |
| 260 | |
| 261 | final int start = Math.min(U.rows(), U.cols())-1; |
| 262 | |
| 263 | for (int i = start; i >= 0; i--) |
| 264 | { |
| 265 | double x_i = y.get(i); |
| 266 | for (int j = i + 1; j <= start; j++) |
| 267 | x_i -= U.get(i, j) * x.get(j); |
| 268 | x_i /= U.get(i, i); |
| 269 | if(Double.isInfinite(x_i))//Occurs when U_(i,i) = 0 |
| 270 | x_i = 0; |
| 271 | x.set(i, x_i); |
| 272 | } |
| 273 | |
| 274 | return x; |
| 275 | } |
| 276 | |
| 277 | /** |
| 278 | * Solves for the matrix x such that U x = y |