/// <summary> /// The function returns the reduced echelon form of the current MatrixClass /// </summary> public MatrixClass ReducedEchelonForm() { try { MatrixClass ReducedEchelonMatrixClass = this.Duplicate(); for (int i = 0; i < this.Rows; i++) { if (ReducedEchelonMatrixClass[i, i] == 0) // if diagonal entry is zero, { for (int j = i + 1; j < ReducedEchelonMatrixClass.Rows; j++) { if (ReducedEchelonMatrixClass[j, i] != 0) //check if some below entry is non-zero { ReducedEchelonMatrixClass.InterchangeRow(i, j); // then interchange the two rows } } } if (ReducedEchelonMatrixClass[i, i] == 0) // if not found any non-zero diagonal entry { continue; // increment i; } if (ReducedEchelonMatrixClass[i, i] != 1) // if diagonal entry is not 1 , { for (int j = i + 1; j < ReducedEchelonMatrixClass.Rows; j++) { if (ReducedEchelonMatrixClass[j, i] == 1) //check if some below entry is 1 { ReducedEchelonMatrixClass.InterchangeRow(i, j); // then interchange the two rows } } } ReducedEchelonMatrixClass.MultiplyRow(i, Fraction.Inverse(ReducedEchelonMatrixClass[i, i])); for (int j = i + 1; j < ReducedEchelonMatrixClass.Rows; j++) { ReducedEchelonMatrixClass.AddRow(j, i, -ReducedEchelonMatrixClass[j, i]); } for (int j = i - 1; j >= 0; j--) { ReducedEchelonMatrixClass.AddRow(j, i, -ReducedEchelonMatrixClass[j, i]); } } return(ReducedEchelonMatrixClass); } catch (Exception) { throw new MatrixClassException("MatrixClass can not be reduced to Echelon form"); } }
/// <summary> /// The function returns the inverse of the current MatrixClass using Reduced Echelon Form method /// The function is very fast and efficient but may raise overflow exceptions in some cases. /// In such cases use the Inverse() method which computes inverse in the traditional way(using adjoint). /// </summary> public MatrixClass InverseFast() { if (this.DeterminentFast() == 0) { throw new MatrixClassException("Inverse of a singular MatrixClass is not possible"); } try { MatrixClass IdentityMatrixClass = MatrixClass.IdentityMatrixClass(this.Rows, this.Cols); MatrixClass ReducedEchelonMatrixClass = this.Duplicate(); for (int i = 0; i < this.Rows; i++) { if (ReducedEchelonMatrixClass[i, i] == 0) // if diagonal entry is zero, { for (int j = i + 1; j < ReducedEchelonMatrixClass.Rows; j++) { if (ReducedEchelonMatrixClass[j, i] != 0) //check if some below entry is non-zero { ReducedEchelonMatrixClass.InterchangeRow(i, j); // then interchange the two rows IdentityMatrixClass.InterchangeRow(i, j); // then interchange the two rows } } } IdentityMatrixClass.MultiplyRow(i, Fraction.Inverse(ReducedEchelonMatrixClass[i, i])); ReducedEchelonMatrixClass.MultiplyRow(i, Fraction.Inverse(ReducedEchelonMatrixClass[i, i])); for (int j = i + 1; j < ReducedEchelonMatrixClass.Rows; j++) { IdentityMatrixClass.AddRow(j, i, -ReducedEchelonMatrixClass[j, i]); ReducedEchelonMatrixClass.AddRow(j, i, -ReducedEchelonMatrixClass[j, i]); } for (int j = i - 1; j >= 0; j--) { IdentityMatrixClass.AddRow(j, i, -ReducedEchelonMatrixClass[j, i]); ReducedEchelonMatrixClass.AddRow(j, i, -ReducedEchelonMatrixClass[j, i]); } } return(IdentityMatrixClass); } catch (Exception) { throw new MatrixClassException("Inverse of the given MatrixClass could not be calculated"); } }
/// <summary> /// The function returns the determinent of the current MatrixClass object as Fraction /// It computes the determinent by reducing the MatrixClass to reduced echelon form using row operations /// The function is very fast and efficient but may raise overflow exceptions in some cases. /// In such cases use the Determinent() function which computes determinent in the traditional /// manner(by using minors) /// </summary> public Fraction DeterminentFast() { if (this.Rows != this.Cols) { throw new MatrixClassException("Determinent of a non-square MatrixClass doesn't exist"); } Fraction det = new Fraction(1); try { MatrixClass ReducedEchelonMatrixClass = this.Duplicate(); for (int i = 0; i < this.Rows; i++) { if (ReducedEchelonMatrixClass[i, i] == 0) // if diagonal entry is zero, { for (int j = i + 1; j < ReducedEchelonMatrixClass.Rows; j++) { if (ReducedEchelonMatrixClass[j, i] != 0) //check if some below entry is non-zero { ReducedEchelonMatrixClass.InterchangeRow(i, j); // then interchange the two rows det *= -1; //interchanging two rows negates the determinent } } } det *= ReducedEchelonMatrixClass[i, i]; ReducedEchelonMatrixClass.MultiplyRow(i, Fraction.Inverse(ReducedEchelonMatrixClass[i, i])); for (int j = i + 1; j < ReducedEchelonMatrixClass.Rows; j++) { ReducedEchelonMatrixClass.AddRow(j, i, -ReducedEchelonMatrixClass[j, i]); } for (int j = i - 1; j >= 0; j--) { ReducedEchelonMatrixClass.AddRow(j, i, -ReducedEchelonMatrixClass[j, i]); } } return(det); } catch (Exception) { throw new MatrixClassException("Determinent of the given MatrixClass could not be calculated"); } }