/// <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); }