/** * LU Decomposition Back Solve; this method takes the LU matrix and the permutation vector * produced by the GMatrix method LUD and solves the equation (LU)*x = b by placing the solution * vector x into this vector. This vector should be the same length or longer than b. * * @param LU The matrix into which the lower and upper decompostions have been placed * @param b The b vector in the equation (LU)*x = b * @param permutation The row permuations that were necessary to produce the LU matrix parameter */ public final void LUDBackSolve(GMatrix LU, GVector b, GVector permutation) { int size = LU.nRow * LU.nCol; double[] temp = new double[size]; double[] result = new double[size]; int[] row_perm = new int[b.getSize()]; int i, j; if (LU.nRow != b.getSize()) { throw new MismatchedSizeException(VecMathI18N.getString("GVector16")); } if (LU.nRow != permutation.getSize()) { throw new MismatchedSizeException(VecMathI18N.getString("GVector24")); } if (LU.nRow != LU.nCol) { throw new MismatchedSizeException(VecMathI18N.getString("GVector25")); } for (i = 0; i < LU.nRow; i++) { for (j = 0; j < LU.nCol; j++) { temp[i * LU.nCol + j] = LU.values[i][j]; } } for (i = 0; i < size; i++) result[i] = 0.0; for (i = 0; i < LU.nRow; i++) result[i * LU.nCol] = b.values[i]; for (i = 0; i < LU.nCol; i++) row_perm[i] = (int) permutation.values[i]; GMatrix.luBacksubstitution(LU.nRow, temp, row_perm, result); for (i = 0; i < LU.nRow; i++) this.values[i] = result[i * LU.nCol]; }
/** * Solves for x in Ax = b, where x is this vector (nx1), A is mxn, b is mx1, and A = * U*W*transpose(V); U,W,V must be precomputed and can be found by taking the singular value * decomposition (SVD) of A using the method SVD found in the GMatrix class. * * @param U The U matrix produced by the GMatrix method SVD * @param W The W matrix produced by the GMatrix method SVD * @param V The V matrix produced by the GMatrix method SVD * @param b The b vector in the linear equation Ax = b */ public final void SVDBackSolve(GMatrix U, GMatrix W, GMatrix V, GVector b) { if (!(U.nRow == b.getSize() && U.nRow == U.nCol && U.nRow == W.nRow)) { throw new MismatchedSizeException(VecMathI18N.getString("GVector15")); } if (!(W.nCol == values.length && W.nCol == V.nCol && W.nCol == V.nRow)) { throw new MismatchedSizeException(VecMathI18N.getString("GVector23")); } GMatrix tmp = new GMatrix(U.nRow, W.nCol); tmp.mul(U, V); tmp.mulTransposeRight(U, W); tmp.invert(); mul(tmp, b); }