// Methods public LUDecomposition(Matrix A) { this.LU = A.ToJaggedArray(); this.m = A.Rows; this.n = A.Columns; this.piv = new int[this.m]; for (int i = 0; i < this.m; i++) { this.piv[i] = i; } this.pivsign = 1; double[] numArray2 = new double[this.m]; for (int j = 0; j < this.n; j++) { for (int k = 0; k < this.m; k++) { numArray2[k] = this.LU[k][j]; } for (int m = 0; m < this.m; m++) { double[] numArray = this.LU[m]; int num5 = Math.Min(m, j); double num6 = 0.0; for (int num7 = 0; num7 < num5; num7++) { num6 += numArray[num7] * numArray2[num7]; } numArray[j] = numArray2[m] -= num6; } int index = j; for (int n = j + 1; n < this.m; n++) { if (Math.Abs(numArray2[n]) > Math.Abs(numArray2[index])) { index = n; } } if (index != j) { for (int num10 = 0; num10 < this.n; num10++) { double num11 = this.LU[index][num10]; this.LU[index][num10] = this.LU[j][num10]; this.LU[j][num10] = num11; } int num12 = this.piv[index]; this.piv[index] = this.piv[j]; this.piv[j] = num12; this.pivsign = -this.pivsign; } if ((j < this.m) & (this.LU[j][j] != 0.0)) { for (int num13 = j + 1; num13 < this.m; num13++) { this.LU[num13][j] /= this.LU[j][j]; } } } }
public virtual Matrix Solve(Matrix B) { if (B.Rows != this.m) { throw new ArgumentException("Matrix row dimensions must agree."); } if (!this.FullRank) { throw new SystemException("Matrix is rank deficient."); } int columns = B.Columns; double[][] array = B.ToJaggedArray(); for (int i = 0; i < this.n; i++) { for (int k = 0; k < columns; k++) { double num4 = 0.0; for (int m = i; m < this.m; m++) { num4 += this.QR[m][i] * array[m][k]; } num4 = -num4 / this.QR[i][i]; for (int n = i; n < this.m; n++) { array[n][k] += num4 * this.QR[n][i]; } } } for (int j = this.n - 1; j >= 0; j--) { for (int num8 = 0; num8 < columns; num8++) { array[j][num8] /= this.Rdiag[j]; } for (int num9 = 0; num9 < j; num9++) { for (int num10 = 0; num10 < columns; num10++) { array[num9][num10] -= array[j][num10] * this.QR[num9][j]; } } } B.FromJaggedArray(array); return B[0, this.n, 0, columns]; }
// Methods public QRDecomposition(Matrix A) { this.QR = A.ToJaggedArray(); this.m = A.Rows; this.n = A.Columns; this.Rdiag = new double[this.n]; for (int i = 0; i < this.n; i++) { double a = 0.0; for (int j = i; j < this.m; j++) { a = this.Hypot(a, this.QR[j][i]); } if (a != 0.0) { if (this.QR[i][i] < 0.0) { a = -a; } for (int k = i; k < this.m; k++) { this.QR[k][i] /= a; } this.QR[i][i]++; for (int m = i + 1; m < this.n; m++) { double num6 = 0.0; for (int n = i; n < this.m; n++) { num6 += this.QR[n][i] * this.QR[n][m]; } num6 = -num6 / this.QR[i][i]; for (int num8 = i; num8 < this.m; num8++) { this.QR[num8][m] += num6 * this.QR[num8][i]; } } } this.Rdiag[i] = -a; } }