public void PutInRightOrder(Matrica P) { for (int i = 0; i < P.NoOfColumns; i++) { for (int j = 0; j < P.NoOfRows; j++) { if (P.LoadedMatrix[j][i] == 1 && i != j) { this.SwitchRows(j, i); break; } } } }
/// <summary> /// Checks if two matrices are equal. /// </summary> /// <param name="matrix">One of the matrices used in comparing.</param> public void Equals(Matrica matrix) { this.NoOfRows = matrix.NoOfRows; this.NoOfColumns = matrix.NoOfColumns; for (int i = 0; i < this.NoOfRows; i++) { List <double> row = new List <double>(); for (int j = 0; j < this.NoOfColumns; j++) { row.Insert(j, matrix.LoadedMatrix[i][j]); } this.LoadedMatrix.Add(i, row); } }
/// <summary> /// Subtracts matrix B to matrix A and saves value to matrix A. /// </summary> /// <param name="matrix">Matrix being subtracted from another matrix.</param> public void SubtractValue(Matrica matrix) { if (this.NoOfRows != matrix.NoOfRows || this.NoOfColumns != matrix.NoOfColumns) { throw new ArgumentException("Matrice koje želite oduzeti nisu odgovarajućih dimenzija za ovu operaciju!"); } for (int i = 0; i < this.NoOfRows; i++) { for (int j = 0; j < this.NoOfColumns; j++) { this.LoadedMatrix[i][j] -= matrix.LoadedMatrix[i][j]; } } }
/// <summary> /// Subtracts matrix B to matrix A and saves value to matrix A. /// </summary> /// <param name="matrix">Matrix being subtracted from another matrix.</param> public void SubtractValue(Matrica matrix) { if (this.NoOfRows != matrix.NoOfRows || this.NoOfColumns != matrix.NoOfColumns) { throw new ArgumentException("Matrices you want to subtract do not have compatible dimensions for this operation!"); } for (int i = 0; i < this.NoOfRows; i++) { for (int j = 0; j < this.NoOfColumns; j++) { this.LoadedMatrix[i][j] -= matrix.LoadedMatrix[i][j]; } } }
public static Matrica ArrayToVector(double[] array) { Matrica result = new Matrica(); result.NoOfRows = array.Length; result.NoOfColumns = 1; for (int i = 0; i < array.Length; i++) { List <double> row = new List <double>(); row.Insert(0, array[i]); result.LoadedMatrix.Add(i, row); } return(result); }
public Matrica MultiplyByScalar2(double scalar) { Matrica result = new Matrica(); result.Equals(this); for (int i = 0; i < result.NoOfRows; i++) { for (int j = 0; j < result.NoOfColumns; j++) { result.LoadedMatrix[i][j] *= scalar; } } return(result); }
/// <summary> /// Solves forward substitution. /// </summary> /// <param name="LU">Matrix containing both L and U matrices obtained from decomposition.</param> /// <param name="b">Vector containing values from the right side of system of linear equations.</param> /// <returns>Vector y.</returns> public static Matrica ForwardSubstitution(Matrica LU, Matrica b) { if (LU.NoOfRows != LU.NoOfColumns || LU.NoOfRows != b.NoOfRows) { throw new ArgumentException("Dimenzije matrica nisu odgovarajuće! Provjerite i pokušajte ponovno."); } for (int i = 0; i < b.NoOfRows - 1; i++) { for (int j = i + 1; j < b.NoOfRows; j++) { b.LoadedMatrix[j][0] -= LU.LoadedMatrix[j][i] * b.LoadedMatrix[i][0]; } } return(b); }
/// <summary> /// Solves forward substitution. /// </summary> /// <param name="LU">Matrix containing both L and U matrices obtained from decomposition.</param> /// <param name="b">Vector containing values from the right side of system of linear equations.</param> /// <returns>Vector y.</returns> public static Matrica ForwardSubstitution(Matrica LU, Matrica b) { if (LU.NoOfRows != LU.NoOfColumns || LU.NoOfRows != b.NoOfRows) { throw new ArgumentException("Matrices do not have compatible dimensions! Check the matrices and try again."); } for (int i = 0; i < b.NoOfRows - 1; i++) { for (int j = i + 1; j < b.NoOfRows; j++) { b.LoadedMatrix[j][0] -= LU.LoadedMatrix[j][i] * b.LoadedMatrix[i][0]; } } return(b); }
public void PutVectorColumnInInverseMatrixAtIndex(int index, Matrica vector) { for (int i = 0; i < this.NoOfRows; i++) { List <double> row; if (index == 0) { row = new List <double>(); } else { row = this.LoadedMatrix[i]; } row.Insert(index, vector.LoadedMatrix[i][0]); this.LoadedMatrix[i] = row; } }
/// <summary> /// Transposes the matrix. /// </summary> /// <returns>Transposed matrix.</returns> public Matrica TransposeMatrix() { Matrica result = new Matrica(); result.NoOfRows = this.NoOfColumns; result.NoOfColumns = this.NoOfRows; for (int i = 0; i < this.NoOfColumns; i++) { List <double> row = new List <double>(); for (int j = 0; j < this.NoOfRows; j++) { row.Insert(j, this.LoadedMatrix[j][i]); } result.LoadedMatrix.Add(i, row); } return(result); }
/// <summary> /// Checks if two matrices are equal. /// </summary> /// <param name="matrix">Matrix used in comparing.</param> /// <param name="epsilon">Constant used in comparing two values.</param> /// <returns>Returns true if equal, false if not equal.</returns> public bool IsEqual(Matrica matrix, double epsilon) { if (this.NoOfRows != matrix.NoOfRows || this.NoOfColumns != matrix.NoOfColumns) { return(false); } for (int i = 0; i < this.NoOfRows; i++) { for (int j = 0; j < this.NoOfColumns; j++) { if (Math.Abs(this.LoadedMatrix[i][j] - matrix.LoadedMatrix[i][j]) > epsilon) { return(false); } } } return(true); }
/// <summary> /// Solves backward substitution. /// </summary> /// <param name="LU">Matrix containing both L and U matrices obtained from decomposition.</param> /// <param name="b">Vector containing values from the right side of system of linear equations.</param> /// <param name="epsilon">Constant used for checking if we are dividing value by zero.</param> /// <returns>Vector x (result of solving system of linear equations).</returns> public static Matrica BackwardSubstitution(Matrica LU, Matrica b, double epsilon) { if (LU.NoOfRows != LU.NoOfColumns || LU.NoOfRows != b.NoOfRows) { throw new ArgumentException("Matrices do not have compatible dimensions! Check the matrices and try again."); } for (int i = b.NoOfRows - 1; i >= 0; i--) { if (Math.Abs(LU.LoadedMatrix[i][i]) <= epsilon) { throw new DivideByZeroException("It is not possible to divide by zero!!"); } b.LoadedMatrix[i][0] /= LU.LoadedMatrix[i][i]; for (int j = 0; j <= i - 1; j++) { b.LoadedMatrix[j][0] -= LU.LoadedMatrix[j][i] * b.LoadedMatrix[i][0]; } } return(b); }
/// <summary> /// Solves backward substitution. /// </summary> /// <param name="LU">Matrix containing both L and U matrices obtained from decomposition.</param> /// <param name="b">Vector containing values from the right side of system of linear equations.</param> /// <param name="epsilon">Constant used for checking if we are dividing value by zero.</param> /// <returns>Vector x (result of solving system of linear equations).</returns> public static Matrica BackwardSubstitution(Matrica LU, Matrica b, double epsilon) { if (LU.NoOfRows != LU.NoOfColumns || LU.NoOfRows != b.NoOfRows) { throw new ArgumentException("Dimenzije matrica nisu odgovarajuće! Provjerite i pokušajte ponovno."); } for (int i = b.NoOfRows - 1; i >= 0; i--) { if (Math.Abs(LU.LoadedMatrix[i][i]) <= epsilon) { throw new DivideByZeroException("Nije moguće dijeliti s nulom!!"); } b.LoadedMatrix[i][0] /= LU.LoadedMatrix[i][i]; for (int j = 0; j <= i - 1; j++) { b.LoadedMatrix[j][0] -= LU.LoadedMatrix[j][i] * b.LoadedMatrix[i][0]; } } return(b); }
/// <summary> /// Executes LUP decomposition. /// </summary> /// <param name="epsilon">Constant used for checking if the pivot element is equal to zero.</param> /// <param name="b">Vector containing values from the right side of system of linear equations.</param> /// <returns>Permutated identity matrix P.</returns> public Matrica LUPDecomposition(double epsilon, Matrica b) { if (this.NoOfRows != this.NoOfColumns) { throw new ArgumentException("Ne možete izvršiti LU dekompoziciju nad matricom koja nije kvadratna!"); } Matrica identityMatrixP = GenerateIdentityMatrix(this.NoOfRows); int pivotElementRow; for (int i = 0; i < this.NoOfColumns - 1; i++) { pivotElementRow = this.ChoosePivotElement(i, i); if (Math.Abs(this.LoadedMatrix[pivotElementRow][i]) <= epsilon) { throw new DivideByZeroException("Stožerni element je manji od ili jednak vrijednosti " + epsilon + ". Nije moguće riješiti ovaj sustav pomoću LUP dekompozicije."); } identityMatrixP.SwitchRows(i, pivotElementRow); this.SwitchRows(i, pivotElementRow); b.SwitchRows(i, pivotElementRow); for (int j = i + 1; j < this.NoOfRows; j++) { if (Math.Abs(this.LoadedMatrix[i][i]) <= epsilon) { throw new DivideByZeroException("Nije moguće dijeliti s nulom!!"); } this.LoadedMatrix[j][i] /= this.LoadedMatrix[i][i]; for (int k = i + 1; k < this.NoOfColumns; k++) { this.LoadedMatrix[j][k] -= this.LoadedMatrix[j][i] * this.LoadedMatrix[i][k]; } } } return(identityMatrixP); }
/// <summary> /// Executes LUP decomposition. /// </summary> /// <param name="epsilon">Constant used for checking if the pivot element is equal to zero.</param> /// <param name="b">Vector containing values from the right side of system of linear equations.</param> /// <returns>Permutated identity matrix P.</returns> public Matrica LUPDecomposition(double epsilon, Matrica b) { if (this.NoOfRows != this.NoOfColumns) { throw new ArgumentException("It is not possible to execute LU decomposition over the nonsquare matrix!"); } Matrica identityMatrixP = GenerateIdentityMatrix(this.NoOfRows); int pivotElementRow; for (int i = 0; i < this.NoOfColumns - 1; i++) { pivotElementRow = this.ChoosePivotElement(i, i); if (Math.Abs(this.LoadedMatrix[pivotElementRow][i]) <= epsilon) { throw new DivideByZeroException("Pivot element is less than or equal to " + epsilon + ". Solve the linear equation system using the LUP decomposition."); } identityMatrixP.SwitchRows(i, pivotElementRow); this.SwitchRows(i, pivotElementRow); b.SwitchRows(i, pivotElementRow); for (int j = i + 1; j < this.NoOfRows; j++) { if (Math.Abs(this.LoadedMatrix[i][i]) <= epsilon) { throw new DivideByZeroException("It is not possible to divide by zero!!"); } this.LoadedMatrix[j][i] /= this.LoadedMatrix[i][i]; for (int k = i + 1; k < this.NoOfColumns; k++) { this.LoadedMatrix[j][k] -= this.LoadedMatrix[j][i] * this.LoadedMatrix[i][k]; } } } return(identityMatrixP); }
/// <summary> /// Subtracts two matrices. /// </summary> /// <param name="matrix">A matrix being subtracted from another matrix.</param> /// <returns>Result of subtraction of matrices.</returns> public Matrica SubtractMatrices(Matrica matrix) { Matrica result = new Matrica(); if (this.NoOfRows != matrix.NoOfRows || this.NoOfColumns != matrix.NoOfColumns) { throw new ArgumentException("Matrices you want to subtract do not have compatible dimensions for this operation!"); } result.NoOfRows = this.NoOfRows; result.NoOfColumns = this.NoOfColumns; for (int i = 0; i < this.NoOfRows; i++) { List <double> row = new List <double>(); for (int j = 0; j < this.NoOfColumns; j++) { row.Insert(j, this.LoadedMatrix[i][j] - matrix.LoadedMatrix[i][j]); } result.LoadedMatrix.Add(i, row); } return(result); }
/// <summary> /// Subtracts two matrices. /// </summary> /// <param name="matrix">A matrix being subtracted from another matrix.</param> /// <returns>Result of subtraction of matrices.</returns> public Matrica SubtractMatrices(Matrica matrix) { Matrica result = new Matrica(); if (this.NoOfRows != matrix.NoOfRows || this.NoOfColumns != matrix.NoOfColumns) { throw new ArgumentException("Matrice koje želite oduzeti nisu odgovarajućih dimenzija za ovu operaciju!"); } result.NoOfRows = this.NoOfRows; result.NoOfColumns = this.NoOfColumns; for (int i = 0; i < this.NoOfRows; i++) { List <double> row = new List <double>(); for (int j = 0; j < this.NoOfColumns; j++) { row.Insert(j, this.LoadedMatrix[i][j] - matrix.LoadedMatrix[i][j]); } result.LoadedMatrix.Add(i, row); } return(result); }
/// <summary> /// Solves system of linear equations using LU/LUP decomposition, forward and backward substitution. /// </summary> /// <param name="A">Values on the left side of system of linear equations which are multiplied by unknown vector x.</param> /// <param name="b">Vector containing values from the right side of system of linear equations.</param> /// <param name="epsilon">Constant used for checking if we are dividing value by zero.</param> /// <returns>Vector x (result of solving system of linear equations).</returns> public static Matrica SolveSystemOfLinearEquations(Matrica A, Matrica b, double epsilon) { Console.WriteLine("Matrica A:"); Console.WriteLine("============"); A.WriteMatrixInConsole(); Console.WriteLine(); Console.WriteLine("Vektor b:"); Console.WriteLine("==========="); b.WriteMatrixInConsole(); Console.WriteLine(); Matrica copyOfA = new Matrica(); copyOfA.Equals(A); Matrica copyOfb = new Matrica(); copyOfb.Equals(b); try { A.LUDecomposition(epsilon); Console.WriteLine("Matrica L:"); Console.WriteLine("============"); Matrica L = A.GetLMatrixFromLU(); L.WriteMatrixInConsole(); Console.WriteLine(); Console.WriteLine("Matrica U:"); Console.WriteLine("============"); Matrica U = A.GetUMatrixFromLU(); U.WriteMatrixInConsole(); Console.WriteLine(); Matrica y = ForwardSubstitution(A, b); Console.WriteLine("Vektor y:"); Console.WriteLine("==========="); y.WriteMatrixInConsole(); Console.WriteLine(); Matrica x = BackwardSubstitution(A, y, epsilon); Console.WriteLine("Vektor x:"); Console.WriteLine("==========="); x.WriteMatrixInConsole(); Console.WriteLine(); return(x); } catch (DivideByZeroException) { Console.WriteLine("Nije moguće riješiti ovaj sustav LU dekompozicijom. Izvođenje LUP dekompozicije..."); A = copyOfA; b = copyOfb; Matrica P = A.LUPDecomposition(epsilon, b); Console.WriteLine("Matrica P:"); Console.WriteLine("============"); P.WriteMatrixInConsole(); Console.WriteLine(); Console.WriteLine("Matrica L:"); Console.WriteLine("============"); Matrica L = A.GetLMatrixFromLU(); L.WriteMatrixInConsole(); Console.WriteLine(); Console.WriteLine("Matrica U:"); Console.WriteLine("============"); Matrica U = A.GetUMatrixFromLU(); U.WriteMatrixInConsole(); Console.WriteLine(); Matrica y = ForwardSubstitution(A, b); Console.WriteLine("Vektor y:"); Console.WriteLine("==========="); y.WriteMatrixInConsole(); Console.WriteLine(); Matrica x = BackwardSubstitution(A, y, epsilon); Console.WriteLine("Vektor x:"); Console.WriteLine("==========="); x.WriteMatrixInConsole(); Console.WriteLine(); return(x); } }
/// <summary> /// Solves system of linear equations using LU/LUP decomposition, forward and backward substitution. /// </summary> /// <param name="A">Values on the left side of system of linear equations which are multiplied by unknown vector x.</param> /// <param name="b">Vector containing values from the right side of system of linear equations.</param> /// <param name="epsilon">Constant used for checking if we are dividing value by zero.</param> /// <returns>Vector x (result of solving system of linear equations).</returns> public static Matrica SolveSystemOfLinearEquations(Matrica A, Matrica b, double epsilon) { Console.WriteLine("Matrix A:"); Console.WriteLine("============"); A.WriteMatrixInConsole(); Console.WriteLine(); Console.WriteLine("Vector b:"); Console.WriteLine("==========="); b.WriteMatrixInConsole(); Console.WriteLine(); Matrica copyOfA = new Matrica(); copyOfA.Equals(A); Matrica copyOfb = new Matrica(); copyOfb.Equals(b); try { A.LUDecomposition(epsilon); Console.WriteLine("Matrix L:"); Console.WriteLine("============"); Matrica L = A.GetLMatrixFromLU(); L.WriteMatrixInConsole(); Console.WriteLine(); Console.WriteLine("Matrix U:"); Console.WriteLine("============"); Matrica U = A.GetUMatrixFromLU(); U.WriteMatrixInConsole(); Console.WriteLine(); Matrica y = ForwardSubstitution(A, b); Console.WriteLine("Vector y:"); Console.WriteLine("==========="); y.WriteMatrixInConsole(); Console.WriteLine(); Matrica x = BackwardSubstitution(A, y, epsilon); Console.WriteLine("Vector x:"); Console.WriteLine("==========="); x.WriteMatrixInConsole(); Console.WriteLine(); return(x); } catch (DivideByZeroException) { Console.WriteLine("It is not possible to solve this system using LU decomposition. Executing LUP decomposition..."); A = copyOfA; b = copyOfb; Matrica P = A.LUPDecomposition(epsilon, b); Console.WriteLine("Matrix P:"); Console.WriteLine("============"); P.WriteMatrixInConsole(); Console.WriteLine(); Console.WriteLine("Matrix L:"); Console.WriteLine("============"); Matrica L = A.GetLMatrixFromLU(); L.WriteMatrixInConsole(); Console.WriteLine(); Console.WriteLine("Matrix U:"); Console.WriteLine("============"); Matrica U = A.GetUMatrixFromLU(); U.WriteMatrixInConsole(); Console.WriteLine(); Matrica y = ForwardSubstitution(A, b); Console.WriteLine("Vector y:"); Console.WriteLine("==========="); y.WriteMatrixInConsole(); Console.WriteLine(); Matrica x = BackwardSubstitution(A, y, epsilon); Console.WriteLine("Vector x:"); Console.WriteLine("==========="); x.WriteMatrixInConsole(); Console.WriteLine(); return(x); } }