namespace Mapack { using System; /// QR decomposition for a rectangular matrix. /// /// For an m-by-n matrix A with m >= n, the QR decomposition is an m-by-n /// orthogonal matrix Q and an n-by-n upper triangular /// matrix R so that A = Q * R. /// The QR decompostion always exists, even if the matrix does not have /// full rank, so the constructor will never fail. The primary use of the /// QR decomposition is in the least squares solution of nonsquare systems /// of simultaneous linear equations. /// This will fail if returns . /// public class QrDecomposition { private Matrix QR; private double[] Rdiag; /// Construct a QR decomposition. public QrDecomposition(Matrix value) { if (value == null) { throw new ArgumentNullException("value"); } this.QR = (Matrix) value.Clone(); double[][] qr = this.QR.Array; int m = value.Rows; int n = value.Columns; this.Rdiag = new double[n]; for (int k = 0; k < n; k++) { // Compute 2-norm of k-th column without under/overflow. double nrm = 0; for (int i = k; i < m; i++) { nrm = Hypotenuse(nrm,qr[i][k]); } if (nrm != 0.0) { // Form k-th Householder vector. if (qr[k][k] < 0) { nrm = -nrm; } for (int i = k; i < m; i++) { qr[i][k] /= nrm; } qr[k][k] += 1.0; // Apply transformation to remaining columns. for (int j = k+1; j < n; j++) { double s = 0.0; for (int i = k; i < m; i++) { s += qr[i][k]*qr[i][j]; } s = -s/qr[k][k]; for (int i = k; i < m; i++) { qr[i][j] += s*qr[i][k]; } } } this.Rdiag[k] = -nrm; } } /// Least squares solution of A * X = B /// Right-hand-side matrix with as many rows as A and any number of columns. /// A matrix that minimized the two norm of Q * R * X - B. /// Matrix row dimensions must be the same. /// Matrix is rank deficient. public Matrix Solve(Matrix value) { if (value == null) { throw new ArgumentNullException("value"); } if (value.Rows != QR.Rows) { throw new ArgumentException("Matrix row dimensions must agree."); } if (!this.FullRank) { throw new InvalidOperationException("Matrix is rank deficient."); } // Copy right hand side int count = value.Columns; Matrix X = value.Clone(); int m = QR.Rows; int n = QR.Columns; double[][] qr = QR.Array; // Compute Y = transpose(Q)*B for (int k = 0; k < n; k++) { for (int j = 0; j < count; j++) { double s = 0.0; for (int i = k; i < m; i++) { s += qr[i][k] * X[i,j]; } s = -s / qr[k][k]; for (int i = k; i < m; i++) { X[i,j] += s * qr[i][k]; } } } // Solve R*X = Y; for (int k = n-1; k >= 0; k--) { for (int j = 0; j < count; j++) { X[k,j] /= Rdiag[k]; } for (int i = 0; i < k; i++) { for (int j = 0; j < count; j++) { X[i,j] -= X[k,j] * qr[i][k]; } } } return X.Submatrix(0, n-1, 0, count-1); } /// Shows if the matrix A is of full rank. /// The value is if R, and hence A, has full rank. public bool FullRank { get { int columns = this.QR.Columns; for (int i = 0; i < columns; i++) { if (this.Rdiag[i] == 0) { return false; } } return true; } } /// Returns the upper triangular factor R. public Matrix UpperTriangularFactor { get { int n = this.QR.Columns; Matrix X = new Matrix(n, n); double[][] x = X.Array; double[][] qr = QR.Array; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { if (i < j) { x[i][j] = qr[i][j]; } else if (i == j) { x[i][j] = Rdiag[i]; } else { x[i][j] = 0.0; } } } return X; } } /// Returns the orthogonal factor Q. public Matrix OrthogonalFactor { get { Matrix X = new Matrix(QR.Rows, QR.Columns); double[][] x = X.Array; double[][] qr = QR.Array; for (int k = QR.Columns - 1; k >= 0; k--) { for (int i = 0; i < QR.Rows; i++) { x[i][k] = 0.0; } x[k][k] = 1.0; for (int j = k; j < QR.Columns; j++) { if (qr[k][k] != 0) { double s = 0.0; for (int i = k; i < QR.Rows; i++) { s += qr[i][k] * x[i][j]; } s = -s / qr[k][k]; for (int i = k; i < QR.Rows; i++) { x[i][j] += s * qr[i][k]; } } } } return X; } } private static double Hypotenuse(double a, double b) { if (Math.Abs(a) > Math.Abs(b)) { double r = b / a; return Math.Abs(a) * Math.Sqrt(1 + r * r); } if (b != 0) { double r = a / b; return Math.Abs(b) * Math.Sqrt(1 + r * r); } return 0.0; } } }