public static int Test_CramersRule() { RationalFactory rf = new RationalFactory(); //Create rational factory RationalSquareMatrix ACopy = rf[3, 3, //Matrix to solve "1", "2", "-2", "-1", "1", "3", "2", "-1", "2" ]; RationalVector rv = new RationalVector { -7, 3, 8 }; //values to solve for StringBuilder sb = new StringBuilder(); //Start building latex sb.Append(@"\begin{aligned}"); //Start system of linear equations sb.AppendFormat(@"&x_1 + 2x_2 - 2x_3 = -7 \\ \\"); sb.AppendFormat(@"&-x_1 + x_2 + 3x_3 = 3 \\ \\"); sb.AppendFormat(@"&2x_1 - x_2 + 2x_3 = 8 \\ \\"); RationalVector rvSolved = ACopy.CramersRule(rv); //solve system of linear equations sb.AppendFormat(@"&x_1 = {0}, x_2 = {1}, x_3 = {2}", rvSolved[0].ToLatex(), rvSolved[1].ToLatex(), rvSolved[2].ToLatex()); //output values sb.Append(@"\end{aligned}"); HtmlOutputMethods.WriteLatexEqToHtmlAndLaunch(sb.ToString(), "Test_CramersRule.html"); //display Latex via mathjax return(0); }
public static RationalSquareMatrix operator *(RationalSquareMatrix a, RationalSquareMatrix b) { RationalSquareMatrix retVal = new RationalSquareMatrix(a.Rows, a.Columns); for (int rowCount = 0; rowCount < retVal.Rows; rowCount++) { for (int colCount = 0; colCount < retVal.Columns; colCount++) { for (int retColCount = 0; retColCount < retVal.Columns; retColCount++) { retVal.InternalRep[rowCount, colCount] += a.InternalRep[rowCount, retColCount] * b.InternalRep[retColCount, colCount]; } } } StringBuilder sb = new StringBuilder(); sb.Append(a.ToLatex()); sb.Append(b.ToLatex()); sb.Append(" = "); sb.Append(retVal.ToLatex()); retVal.FullRep = sb.ToString(); return(retVal); }
public static RationalSquareMatrix FaddevasMethod(RationalSquareMatrix AIn, out RationalVector CharacteristicEquation) { RationalSquareMatrix A = AIn.Clone(); CharacteristicEquation = new RationalVector(); RationalSquareMatrix A_n = A; Rational b_n = 1; RationalSquareMatrix B_n = null; RationalSquareMatrix I = null; RationalSquareMatrix AInv = null; int i = 0; for (i = 1; i < A.Rows; i++) { b_n = -A_n.Trace() / (i); CharacteristicEquation.Add(b_n); I = RationalSquareMatrix.IdentityMatrix(A.Rows); B_n = A_n + b_n * I; A_n = A * B_n; } b_n = -A_n.Trace() / (i); CharacteristicEquation.Add(b_n); AInv = -1 / b_n * B_n; AInv.FullRep = A.ToLatex() + "^{-1} = " + AInv.ToLatex(); return(AInv); }
public static Rational Det2X2(RationalSquareMatrix rsm2X2, string CoFactor = "1") { Rational rCoFactor = Rational.Parse(CoFactor); Rational ret = (rsm2X2[0, 0] * rsm2X2[1, 1] - rsm2X2[0, 1] * rsm2X2[1, 0]) * rCoFactor; return(ret); }
public static RationalCoFactorInfo GetCoFactor(RationalSquareMatrix symIn, int Column) { RationalCoFactorInfo cfi = new RationalCoFactorInfo(); cfi.Sign = (int)Math.Pow(-1, Column + 1); RationalVector col = symIn[Column - 1]; cfi.CoFactor = col[0]; List <Rational> symList = new List <Rational>(); for (int i = 1; i < symIn.Rows; i++) { for (int j = 0; j < symIn.Columns; j++) { if (j + 1 != Column) { symList.Add(symIn[i, j]); } } } cfi.Minor = new RationalSquareMatrix(symIn.Rows - 1, symIn.Columns - 1, symList); cfi.Minor.MinorName = symIn.MinorName; return(cfi); }
public static int Test_EMatrix() { RationalFactory rf = new RationalFactory(); RationalSquareMatrix I = RationalSquareMatrix.IdentityMatrix(3); RationalSquareMatrix ACopy = rf[3, 3, "1", "2", "-2", "-1", "1", "3", "2", "-1", "2" ]; RationalSquareMatrix ACopy2 = rf[3, 3, "-7", "2", "-2", "-3", "1", "3", "8", "-1", "2" ]; RationalSquareMatrix ACopy3 = rf[4, 4, "4", "7", "2", "3", "1", "3", "1", "2", "2", "5", "3", "4", "1", "4", "2", "3" ]; string ll = RationalSquareMatrix.DetFullRep(ACopy); HtmlOutputMethods.WriteLatexEqToHtmlAndLaunch(ll, "Test_EMatrix.html"); //display Latex via mathjax return(0); }
public static int Test_RationalSquareMatrix() { RationalFactory rf = new RationalFactory(); RealFactory rff = new RealFactory(); RationalSquareMatrix A = rf[3, 3, "1", "2", "-2", "-1", "1", "3", "2", "-1", "2" ]; SquareRealMatrix SA = rff[3, 3, 7, 2, -2, 3, 1, 3, 8, -1, 2 ]; RationalVector rv = rf["7", "3", "8"]; StringBuilder sb = new StringBuilder();//Start building latex sb.Append(@"\begin{aligned}"); sb.AppendFormat(@"&A = {0}", A.ToLatex() + @" \\ \\"); //display A matrix A[0] = rv; sb.AppendFormat(@"&Ab = {0}", A.ToLatex()); //display Vec A sb.AppendFormat(@"&Det = {0}", SA.Determinant()); //display Vec A sb.Append(@"\end{aligned}"); HtmlOutputMethods.WriteLatexEqToHtmlAndLaunch(sb.ToString(), "Test_VecOperator.html"); //display Latex via mathjax return(0); }
public static string DetFullRep(RationalSquareMatrix A) { StringBuilder sb = new StringBuilder();//Start building latex Rational ret = 1; RationalSquareMatrix I = IdentityMatrix(A.Rows); int i = 0; int j = 0; sb.Append(@"\begin{aligned}"); sb.AppendFormat(@"&A = {0} I = {1}", A.ToLatex(), I.ToLatex() + @"\textrm{Initial matrix and identity matrix} \\ \\");//display A and I matrices before List <int[]> inds = LowerEchelonIndexes(I); foreach (int[] arr in inds) { i = arr[0]; j = arr[1]; Rational r = Rational.Abs(A[i, j]) / Rational.Abs(A[j, j]); if (A[i, j] > 0 && A[j, j] > 0) { r = r * -1; } if (A[i, j] < 0 && A[j, j] < 0) { r = r * -1; } string comment = string.Format(@"R_{0} \rightarrow R_{0} + {1}R_{2}", (i + 1), r.ToLatex(), (j + 1)); I[i, j] = r; sb.AppendFormat(@"&{0}{1} = ", A.ToLatex(), I.ToLatex());//display A matrix and augmented I matrix A = I * A; sb.AppendFormat(@"{0}", A.ToLatex() + comment + @" \\ \\");//display A matrix after elementary operation I[i, j] = 0; } string combo = string.Empty; for (i = 0; i < A.Rows; i++) { for (j = 0; j < A.Columns; j++) { if (i == j) { combo += A[i, j].ToLatex() + @"\cdot"; ret *= A[i, j]; } } } sb.AppendFormat(@"&Det = {0} = {1}", combo.Substring(0, combo.Length - 5), ret.ToLatex());//display det of A matrix sb.Append(@"\end{aligned}"); return(sb.ToString()); }
public RationalSquareMatrix Inverse() { RationalSquareMatrix Inv = null; RationalVector rv = new RationalVector(); Inv = FaddevasMethod(this, out rv); return(Inv); }
public static int Test_Rational_Determinant() { RationalFactory rf = new RationalFactory(); RationalSquareMatrix A = rf[3, 3, "1", "2", "-2", "-1", "1", "3", "2", "-1", "2" ]; RationalSquareMatrix ACopy3 = rf[4, 4, "4", "7", "2", "3", "1", "3", "1", "2", "2", "5", "3", "4", "1", "4", "2", "3" ]; RationalSquareMatrix ACopy3_2 = rf[3, 3, 1, 2, 3, 4, 1, 6, 7, 8, 1 ]; List <RationalCoFactorInfo> cfList = RationalSquareMatrix.GetAllMatrixCoFactors(ACopy3); StringBuilder sb = new StringBuilder();//Start building latex sb.Append(@"\begin{aligned}"); sb.AppendFormat(@"&{0} \\ \\", ACopy3.ToLatex()); //foreach(IGrouping<string, RationalCoFactorInfo> funk in q.ToList()) foreach (RationalCoFactorInfo ci in cfList) { sb.AppendFormat(@"&{0} \\ \\", ci.Minor.MinorName + " = " + ((ci.Sign < 0) ? "-" : "") + ci.CoFactor.ToLatex() + ci.Minor.ToLatex()); foreach (List <RationalCoFactorInfo> lstChild in ci.ListOfLists) { foreach (RationalCoFactorInfo ci2 in lstChild) { //if (ci2.Minor.Rows == 4) { sb.AppendFormat(@"&{0} \\ \\", ci.Minor.MinorName + " = " + ((ci.Sign < 0) ? "-" : "") + ci.CoFactor.ToLatex() + ci2.CoFactor.ToLatex() + ci2.Minor.ToLatex()); } } } } sb.Append(@"&Det = " + RationalSquareMatrix.Det(ACopy3_2)); sb.Append(@"\end{aligned}"); HtmlOutputMethods.WriteLatexEqToHtmlAndLaunch(sb.ToString(), "Test_Rational_Determinant.html"); //display Latex via mathjax return(0); }
public static List <RationalCoFactorInfo> GetCoFactors(RationalSquareMatrix ParentMatrix) { List <RationalCoFactorInfo> cfiL = new List <RationalCoFactorInfo>(); int Order = ParentMatrix.Rows; for (int i = 0; i < ParentMatrix.Columns; i++) { cfiL.Add(GetCoFactor(ParentMatrix, i + 1)); } return(cfiL); }
public RationalSquareMatrix Clone() { RationalSquareMatrix ret = new RationalSquareMatrix(this.Rows, this.Columns); for (int i = 0; i < Rows; i++) { for (int j = 0; j < Columns; j++) { ret.InternalRep[i, j] = this.InternalRep[i, j]; } } ret.Name = this.Name; ret.LatexName = this.LatexName; return(ret); }
public RationalVector CramersRule(RationalVector VectorToSolve) { RationalVector Deltas = new RationalVector(); RationalSquareMatrix A = this.Clone(); Rational Delta = RationalSquareMatrix.Det(A); for (int i = 0; i < this.Rows; i++) { RationalVector rvSave = A[i]; A[i] = VectorToSolve; Deltas.Add(RationalSquareMatrix.Det(A) / Delta); A[i] = rvSave; } return(Deltas); }
public static RationalSquareMatrix IdentityMatrix(int Order) { RationalSquareMatrix retVal = new RationalSquareMatrix(Order, Order); for (int rowCount = 0; rowCount < retVal.Rows; rowCount++) { for (int colCount = 0; colCount < retVal.Columns; colCount++) { if (rowCount == colCount) { retVal[rowCount, colCount] = 1; } } } return(retVal); }
public static Rational Det(RationalSquareMatrix A) { Rational ret = 1; RationalSquareMatrix I = IdentityMatrix(A.Rows); int i = 0; int j = 0; List <int[]> inds = LowerEchelonIndexes(I); foreach (int[] arr in inds) { i = arr[0]; j = arr[1]; Rational r = Rational.Abs(A[i, j]) / Rational.Abs(A[j, j]); if (A[i, j] > 0 && A[j, j] > 0) { r = r * -1; } if (A[i, j] < 0 && A[j, j] < 0) { r = r * -1; } I[i, j] = r; A = I * A; I[i, j] = 0; } for (i = 0; i < A.Rows; i++) { for (j = 0; j < A.Columns; j++) { if (i == j) { ret *= A[i, j]; } } } return(ret); }
public RationalSquareMatrix this[int Rows, int Columns, params string[] exps] { get { RationalSquareMatrix ret = new RationalSquareMatrix(Rows, Columns); ret.Parent = this; int cnt = 0; for (int i = 0; i < Rows; i++) { for (int j = 0; j < Columns; j++) { ret[i, j] = Rational.Parse(exps[cnt++]); } } return(ret); } }
public static List <int[]> UpperEchelonIndexes(RationalSquareMatrix A) { List <int[]> Indexes = new List <int[]>(); for (int i = 0; i < A.Rows; i++) { for (int j = 1; j < A.Columns; j++) { if (i == j) { int ind = j + 1; while (ind < A.Columns) { Indexes.Add(new int[] { i, ind }); ind++; } } } } return(Indexes); }
public static RationalSquareMatrix KroneckerProduct(RationalSquareMatrix a, RationalSquareMatrix b) { int Rows = a.Rows * b.Rows; // calculate number of rows. int Columns = a.Columns * b.Rows; // calculate number of columns int incC = 0; // increment variable for column of b matrix int incR = 0; // increment variable for row of b matrix int incAMC = 0; // increment variable for column of a matrix int incAMR = 0; // increment variable for row of a matrix RationalSquareMatrix retVal = new RationalSquareMatrix(Rows, Columns); int rowCount; int colCount; string exp = string.Empty; for (rowCount = 0; rowCount < retVal.Rows; rowCount++) { if (incR > b.Rows - 1) // reached end of rows of b matrix { incR = 0; incAMR++; } incAMC = 0; for (colCount = 0; colCount < retVal.Columns; colCount++) { incC++; if (incC > b.Columns - 1) // reached end of columns of b matrix { incC = 0; incAMC++; } retVal[rowCount, colCount] = a[incAMR, incAMC] * b[incR, incC]; } incR++; } retVal.FullRep = a.ToLatex() + @"\;\otimes\;" + b.ToLatex() + " = " + retVal.ToLatex(); //produce latex string return(retVal); }
public static RationalSquareMatrix operator *(RationalSquareMatrix a, Rational b) { RationalSquareMatrix retVal = new RationalSquareMatrix(a.Rows, a.Columns); for (int i = 0; i < retVal.Rows; i++) { for (int j = 0; j < retVal.Columns; j++) { retVal.InternalRep[i, j] = a.InternalRep[i, j] * b; } } StringBuilder sb = new StringBuilder(); sb.Append(b.ToLatex()); sb.Append(a.ToLatex()); sb.Append(" = "); sb.Append(retVal.ToLatex()); retVal.FullRep = sb.ToString(); return(retVal); }
public static List <RationalCoFactorInfo> GetAllMatrixCoFactors(RationalSquareMatrix ParentMatrix) { List <RationalCoFactorInfo> cfList = RationalSquareMatrix.GetCoFactors(ParentMatrix); ParentMatrix.MinorName = "M1"; if (cfList[0].Minor.Rows == 2) //At two go back { return(cfList); } int inc = 0; RationalCoFactorInfo cfi = null; RationalCoFactorInfo cfiChild = null; List <RationalCoFactorInfo> cfListChild = null; string TopMinorName = string.Empty; while (inc < cfList.Count) { if (cfi == null) { cfi = cfList[inc]; TopMinorName = "M" + (inc + 1).ToString(); cfi.Minor.MinorName = TopMinorName; } else if (cfi != null && cfi.ListOfLists.Count == 0) //init value { List <RationalCoFactorInfo> cfListTmp = RationalSquareMatrix.GetCoFactors(cfi.Minor); cfi.ListOfLists.Add(cfListTmp); cfListChild = new List <RationalCoFactorInfo>(cfListTmp); if (cfListChild[0].Minor.Rows == 2) //end of line { cfList[inc] = cfi; cfi = null; inc++; } } else if (cfi != null && cfi.ListOfLists.Count > 0) //have value { List <RationalCoFactorInfo> cfListTmp = null; cfiChild = new RationalCoFactorInfo(); foreach (RationalCoFactorInfo cfiC in cfListChild) { cfListTmp = RationalSquareMatrix.GetCoFactors(cfiC.Minor); cfi.ListOfLists.Add(cfListTmp); cfiChild.ListOfLists.Add(cfListTmp); } cfListChild = new List <RationalCoFactorInfo>(); foreach (List <RationalCoFactorInfo> cfl in cfiChild.ListOfLists) { foreach (RationalCoFactorInfo cfiC in cfl) { cfListChild.Add(cfiC); } } cfiChild = null; if (cfListChild[0].Minor.Rows == 2) //end of line { cfList[inc] = cfi; cfi = null; inc++; } } } return(cfList); }
public static int Test_FaddevasMethod() { RationalFactory rf = new RationalFactory(); RationalSquareMatrix A = rf[3, 3, "1", "0", "-2", "4", "1", "0", "0", "2", "1" ]; RationalVector rv = new RationalVector(); RationalSquareMatrix AInv = RationalSquareMatrix.FaddevasMethod(A, out rv); StringBuilder sb = new StringBuilder();//Start building latex sb.Append(@"A^{-1} = " + AInv.ToLatex()); HtmlOutputMethods.WriteLatexEqToHtmlAndLaunch(AInv.ToString("F"), "Test_FaddevasMethod_Determinant.html"); //display Latex via mathjax /* * RationalSquareMatrix A1 = A.Clone(); * StringBuilder sb = new StringBuilder();//Start building latex * sb.Append(@"\begin{aligned}"); * sb.AppendFormat(@"&A1 = {0} \\ \\", A1.ToLatex()); * * Rational b1 = -A1.Trace(); * sb.AppendFormat(@"&b1 = {0} \\ \\", b1.ToLatex()); * RationalSquareMatrix I = RationalSquareMatrix.IdentityMatrix(A.Rows); * * RationalSquareMatrix B1 = A1 + (b1 * I); * sb.AppendFormat(@"&B1 = {0} \\ \\", B1.ToLatex()); * * RationalSquareMatrix A2 = A * B1; * sb.AppendFormat(@"&A2 = {0} \\ \\", A2.ToLatex()); * * Rational b2 = A2.Trace() / -2; * sb.AppendFormat(@"&b2 = {0} \\ \\", b2.ToLatex()); * * I = RationalSquareMatrix.IdentityMatrix(A.Rows); * RationalSquareMatrix B2 = A2 + b2 * I; * sb.AppendFormat(@"&B2 = {0} \\ \\", B2.ToLatex()); * * RationalSquareMatrix A3 = A * B2; * sb.AppendFormat(@"&A3 = {0} \\ \\", A3.ToLatex()); * * Rational b3 = A3.Trace() / -3; * sb.AppendFormat(@"&b3 = {0} \\ \\", b3.ToLatex()); * * RationalSquareMatrix AInv = 1/b3 * B2; * sb.Append(@"&A^{-1} = " + AInv.ToLatex() + @" \\ \\"); * * * RationalSquareMatrix A_n = A; * Rational b_n = 1; * RationalSquareMatrix B_n = null; * int i = 0; * for(i = 0; i < A.Rows - 1; i++) * { * b_n = -A_n.Trace() / (i + 1); * I = RationalSquareMatrix.IdentityMatrix(A.Rows); * * B_n = A_n + b_n * I; * sb.Append(@"&Bn = " + B_n.ToLatex() + @" \\ \\"); * * A_n = A * B_n; * sb.Append(@"&An = " + A_n.ToLatex() + @" \\ \\"); * } * * b_n = -A_n.Trace() / (i + 1); * sb.Append(@"&bn = " + b_n.ToLatex() + @" \\ \\"); * * AInv = 1/b_n * B_n; * sb.Append(@"&A^{-1} = " + AInv.ToLatex() + @" \\ \\"); * * sb.Append(@"\end{aligned}"); * HtmlOutputMethods.WriteLatexEqToHtmlAndLaunch(sb.ToString(), "Test_FaddevasMethod_Determinant.html"); //display Latex via mathjax */ return(0); }