public void MatrixTest() { double[,] Arr1 = new double[4, 4] { { 1, 1, 0, 3 }, { 2, 1, -1, 1 }, { 3, -1, -1, 2 }, { -1, 2, 3, -1 } }; double[,] Arr2 = new double[3, 3] { { 4, -1, 1 }, { -1, 4.25, 2.75 }, { 1, 2.75, 3.5 } }; MMatrix A = new MMatrix(Arr1); MMatrix B = new MMatrix(); MMatrix C = new MMatrix(); string s = ""; try { //A = MMatrix.RandomMatrix(size,0,10); s += "\nMatrix A=\n" + A.PrintToString(); s += "\nDet (A)= \n" + Convert.ToString(A.Determinant()) + "\n"; s += "\nA^-1= \n" + A.Inverse().PrintToString(); s += "\nA*A^-1= \n" + (A * A.Inverse()).PrintToString(); C = A * A.Transpose(); s += "\nC=A*A^T=\n" + C.PrintToString(); /////////////////Factor A = LDL* MMatrix.FactorLDLt(C); s += "\nFactor LDL*(C): L=\n" + (new MMatrix(C.LDL_L)).PrintToString(); s += "\nFactor LDL*(C): D=\n" + (new MMatrix(C.LDL_D)).PrintToString(); B = (new MMatrix(C.LDL_L)) * (new MMatrix(C.LDL_D)) * (new MMatrix(C.LDL_L)).Transpose(); s += "\nFactor LDL*: C=L*D*L^T\n" + B.PrintToString(); B = (new MMatrix(C.LDL_L)) * (new MMatrix(C.LDL_L)).Transpose(); s += "\nFactor LDL*: C=L*L^T\n" + B.PrintToString(); /////////////////Factor A = LU C.FactorLU(); s += "\nFactor LU(C): L=\n" + (new MMatrix(C.LU_L)).PrintToString(); s += "\nFactor LU(C): U=\n" + (new MMatrix(C.LU_U)).PrintToString(); s += "\nFactor LU: C=L*U\n" + ((new MMatrix(C.LU_L)) * (new MMatrix(C.LU_U))).PrintToString(); ///////////////////Factor A = PLU A.FactorLU_withP(); s += "\nFactor LU (A): P=\n" + (new MMatrix(A.LU_P)).PrintToString(); s += "\nFactor LU (A): L=\n" + (new MMatrix(A.LU_L)).PrintToString(); s += "\nFactor LU (A): U=\n" + (new MMatrix(A.LU_U)).PrintToString(); s += "\nFactor LU: A=PLU\n" + ((new MMatrix(A.LU_P)).Transpose() * (new MMatrix(A.LU_L)) * (new MMatrix(A.LU_U))).PrintToString(); this.RichText_Display(s); OutputMatrix = C; } catch (MMatrixException exp) { MessageBox.Show("INFO: " + exp.Message, sProjectTile, MessageBoxButtons.OK, MessageBoxIcon.Warning); } }
public static double Determinant_NonRecursive(MMatrix A) { double det = 1; if (!A.IsSquared()) throw new MMatrixException("Determinant of a non-square matrix doesn't exist"); A.FactorLU_withP(); for (int i = 0; i < A.row; ++i) det *= A.LU_L[i, i] * A.LU_U[i, i]; det *= Determinant(new MMatrix(A.LU_P)); return det; }