public void CalculatingLowerUpperMatrices() { MatrixMB mat = new MatrixMB(2, 2); mat.Data[0][0] = 4; mat.Data[0][1] = 7; mat.Data[1][0] = 2; mat.Data[1][1] = 6; mat.MakeLU(); Assert.AreEqual(4, mat.Data[0][0]); Assert.AreEqual(7, mat.Data[0][1]); Assert.AreEqual(2, mat.Data[1][0]); Assert.AreEqual(6, mat.Data[1][1]); var L = mat.L; Assert.AreEqual(1, L.Data[0][0]); Assert.AreEqual(0, L.Data[0][1]); Assert.AreEqual(0.5, L.Data[1][0]); Assert.AreEqual(1, L.Data[1][1]); var U = mat.U; Assert.AreEqual(4, U.Data[0][0]); Assert.AreEqual(7, U.Data[0][1]); Assert.AreEqual(0, U.Data[1][0]); Assert.AreEqual(2.5, U.Data[1][1]); }
/// <summary> /// Function solves Ax = b for A as an upper triangular matrix /// </summary> /// <param name="A">MatrixMB - upper triangular matrix</param> /// <param name="b">MatrixMB</param> /// <returns>MatrixMB</returns> public static MatrixMB SubsBack(MatrixMB A, MatrixMB b) { if (A.L == null) { A.MakeLU(); } int n = A.Rows; MatrixMB x = new MatrixMB(n, 1); for (int i = n - 1; i > -1; i--) { x[i, 0] = b[i, 0]; for (int j = n - 1; j > i; j--) { x[i, 0] -= A[i, j] * x[j, 0]; } x[i, 0] = x[i, 0] / A[i, i]; } return(x); }
/// <summary> /// Function solves Ax = b for A as a lower triangular matrix /// </summary> /// <param name="A">MatrixMB - lower triangular matrix</param> /// <param name="b">MatrixMB</param> /// <returns>MatrixMB</returns> public static MatrixMB SubsForth(MatrixMB A, MatrixMB b) { if (A.L == null) { A.MakeLU(); } int n = A.Rows; MatrixMB x = new MatrixMB(n, 1); for (int i = 0; i < n; i++) { x[i, 0] = b[i, 0]; for (int j = 0; j < i; j++) { x[i, 0] -= A[i, j] * x[j, 0]; } x[i, 0] = x[i, 0] / A[i, i]; } return(x); }
/// <summary> /// Function solves Ax = b for A as a lower triangular matrix /// </summary> /// <param name="A">MatrixMB - lower triangular matrix</param> /// <param name="b">MatrixMB</param> /// <returns>MatrixMB</returns> public static MatrixMB SubsForth(MatrixMB A, MatrixMB b) { if (A.L == null) A.MakeLU(); int n = A.Rows; MatrixMB x = new MatrixMB(n, 1); for (int i = 0; i < n; i++) { x[i, 0] = b[i, 0]; for (int j = 0; j < i; j++) x[i, 0] -= A[i, j] * x[j, 0]; x[i, 0] = x[i, 0] / A[i, i]; } return x; }
/// <summary> /// Function solves Ax = b for A as an upper triangular matrix /// </summary> /// <param name="A">MatrixMB - upper triangular matrix</param> /// <param name="b">MatrixMB</param> /// <returns>MatrixMB</returns> public static MatrixMB SubsBack(MatrixMB A, MatrixMB b) { if (A.L == null) A.MakeLU(); int n = A.Rows; MatrixMB x = new MatrixMB(n, 1); for (int i = n - 1; i > -1; i--) { x[i, 0] = b[i, 0]; for (int j = n - 1; j > i; j--) x[i, 0] -= A[i, j] * x[j, 0]; x[i, 0] = x[i, 0] / A[i, i]; } return x; }