/// <summary> /// Main form constructor. /// </summary> public MainForm() { InitializeComponent(); Matrix T = new Matrix(4), resM = new Matrix(2); Vector F = new Vector(4), resV = new Vector(2); T[0, 0] = 1; T[0, 1] = 2; T[0, 2] = 3; T[0, 3] = 4; T[1, 0] = 2; T[1, 1] = 3; T[1, 2] = 4; T[1, 3] = 1; T[2, 0] = 3; T[2, 1] = 4; T[2, 2] = 1; T[2, 3] = 2; T[3, 0] = 4; T[3, 1] = 1; T[3, 2] = 2; T[3, 3] = 3; F[0] = 1; F[1] = 2; F[2] = 3; F[3] = 4; T.Condensate(F, out resM, out resV); Console.WriteLine("Original matrix\n{0}", T); Console.WriteLine("Original vector\n{0}", F); Console.WriteLine("Condensated matrix\n{0}", resM); Console.WriteLine("Condensated vector\n{0}", resV); Console.WriteLine("Inverted Original matrix\n{0}", T.GetInverse()); Console.WriteLine("Inverted Condensated matrix\n{0}", resM.GetInverse()); }
/// <summary> /// Gets sub-Vector of specified size, starting at specified position from this Vector. /// </summary> /// <param name="startPosition">Zero-based index of starting position.</param> /// <param name="size">Size of sub-Vector.</param> /// <returns>Returns Vector.</returns> public Vector GetSubVector(int startPosition, int size) { Vector result = new Vector(size); for (int i = 0; i < size; i++) { result[i] = this[i + startPosition]; } return result; }
/// <summary> /// Creates and returns new instance of this Vector (deep-copy). /// </summary> /// <returns> /// Returns Vector.</returns> public Vector Copy() { Vector result = new Vector(this.Size); for (int i = 0; i < this.Size; i++) { result[i] = this[i]; } return result; }
/// <summary> /// Adds two Vectors and returns resulting Vector. /// </summary> /// <param name="V1">First Vector.</param> /// <param name="V2">Second Vector.</param> /// <returns>Returns Vector.</returns> public static Vector operator +(Vector V1, Vector V2) { if (V1.Size != V2.Size) { throw new ArgumentException("Vector addition error. Inappropriate sizes. [" + V1.Size + "]+[" + V2.Size + "]"); } else { Vector result = new Vector(V1.Size); for (int i = 0; i < V1.Size; i++) { result[i] = V1[i] + V2[i]; } return result; } }
/// <summary> /// Gets local variables computed from given global variables, and local system of linear equations. /// </summary> /// <param name="F">Right part Vector of local system of linear equations.</param> /// <param name="G">Vector of global variables.</param> /// <returns>Vector.</returns> public Vector GetLocalVariables(Vector F, Vector G) { Vector result = new Vector(F.Size - G.Size), Fl = F.GetSubVector(2, result.Size); Matrix C, D; C = this.GetSubMatrix(2, 0, this.Rows - 2, 2); D = this.GetSubMatrix(2, 2, this.Rows - 2, this.Columns - 2); result = D.GetInverse() * (Fl - C * G); return result; }
/// <summary> /// Runs condensation on this Matrix. /// </summary> /// <param name="F">Right part Vector of local system of linear equations.</param> /// <param name="resM">Resulting Matrix.</param> /// <param name="resV">Resulting Vector.</param> public void Condensate(Vector F, out Matrix resM, out Vector resV) { if (this.Columns != this.Rows || this.Columns < 2 || this.Rows < 2) { throw new ArgumentException("Condensate method called from not square Matrix or one of it's dimensions is less then 2."); } else { resM = new Matrix(2); resV = new Vector(2); if (this.Columns == 2) { resM = this.Copy(); resV = F.Copy(); return; } Matrix A, B, C, D; Vector Fg, Fl; A = this.GetSubMatrix(0, 0, 2, 2); B = this.GetSubMatrix(0, 2, 2, this.Columns - 2); C = this.GetSubMatrix(2, 0, this.Rows - 2, 2); D = this.GetSubMatrix(2, 2, this.Rows - 2, this.Columns - 2); Fg = F.GetSubVector(0, 2); Fl = F.GetSubVector(2, F.Size - 2); resM = A - B * D.GetInverse() * C; resV = Fg - B * D.GetInverse() * Fl; } }
/// <summary> /// Multiplies given Matrix on given Vector and returns resulting Vector. /// </summary> /// <param name="M">Matrix to be multiplied.</param> /// <param name="V">Vector to be multiplied.</param> /// <returns>Returns Vector.</returns> public static Vector operator *(Matrix M, Vector V) { if (M.Columns != V.Size) { throw new ArgumentException("Inappropriate size of Matrix and Vector."); } else { Vector result = new Vector(M.Rows); for (int i = 0; i < M.Rows; i++) { for (int j = 0; j < V.Size; j++) { result[i] += M[i, j] * V[j]; } } return result; } }