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 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 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 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 RationalSquareMatrix Inverse() { RationalSquareMatrix Inv = null; RationalVector rv = new RationalVector(); Inv = FaddevasMethod(this, out rv); return(Inv); }
public static RationalVector operator +(Rational value, RationalVector v) { RationalVector vM = new RationalVector(); for (int i = 0; i < v.Count; i++) { vM.Add(value + v[i]); } return(vM); }
public static RationalVector operator /(RationalVector v, Rational value) { RationalVector vM = new RationalVector(); for (int i = 0; i < v.Count; i++) { vM.Add(v[i] / value); } return(vM); }
public static RationalVector operator -(RationalVector v1, RationalVector v2) { RationalVector vM = new RationalVector(); for (int i = 0; i < v1.Count; i++) { vM.Add(v1[i] - v2[i]); } return(vM); }
public RationalVector this[params string[] exps] { get { RationalVector rv = new RationalVector(); foreach (string r in exps) { rv.Add(Rational.Parse(r)); } return(rv); } }
public static Rational DotProduct(RationalVector v1, RationalVector v2) { Rational ret = 0; if (v1.Count != v2.Count) { throw new Exception("Vectors must be equal in length"); } for (int i = 0; i < v1.Count; i++) { ret += (v1[i] * v2[i]); } 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 RationalVector this[int Column] { get { RationalVector ret = new RationalVector(); for (int i = 0; i < this.Rows; i++) { ret.Add(this[i, Column]); } return(ret); } set { for (int i = 0; i < this.Rows; i++) { this[i, Column] = value[i]; } } }
public static RationalVector Normalize(RationalVector v) { return(v / new Rational(Math.Sqrt(DotProduct(v, v).ToDouble()))); }
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); }