/// <summary>Constructs a QR decomposition.</summary> /// <param name="value">The matrix A to be decomposed.</param> /// <param name="transpose">True if the decomposition should be performed on /// the transpose of A rather than A itself, false otherwise. Default is false.</param> public QrDecompositionD(decimal[,] value, bool transpose) { if (value == null) { throw new ArgumentNullException("value", "Matrix cannot be null."); } if ((!transpose && value.GetLength(0) < value.GetLength(1)) || (transpose && value.GetLength(1) < value.GetLength(0))) { throw new ArgumentException("Matrix has more columns than rows.", "value"); } this.qr = transpose ? value.Transpose() : (decimal[, ])value.Clone(); int rows = qr.GetLength(0); int cols = qr.GetLength(1); this.Rdiag = new decimal[cols]; for (int k = 0; k < cols; k++) { // Compute 2-norm of k-th column without under/overflow. decimal nrm = 0; for (int i = k; i < rows; i++) { nrm = Tools.Hypotenuse(nrm, qr[i, k]); } if (nrm != 0) { // Form k-th Householder vector. if (qr[k, k] < 0) { nrm = -nrm; } for (int i = k; i < rows; i++) { qr[i, k] /= nrm; } qr[k, k] += 1; // Apply transformation to remaining columns. for (int j = k + 1; j < cols; j++) { decimal s = 0; for (int i = k; i < rows; i++) { s += qr[i, k] * qr[i, j]; } s = -s / qr[k, k]; for (int i = k; i < rows; i++) { qr[i, j] += s * qr[i, k]; } } } this.Rdiag[k] = -nrm; } }
/// <summary> /// Constructs a new LU decomposition. /// </summary> /// <param name="value">The matrix A to be decomposed.</param> /// <param name="transpose">True if the decomposition should be performed on /// the transpose of A rather than A itself, false otherwise. Default is false.</param> /// <param name="inPlace">True if the decomposition should be performed over the /// <paramref name="value"/> matrix rather than on a copy of it. If true, the /// matrix will be destroyed during the decomposition. Default is false.</param> /// public LuDecompositionD(decimal[,] value, bool transpose, bool inPlace) { if (value == null) { throw new ArgumentNullException("value", "Matrix cannot be null."); } if (transpose) { this.lu = value.Transpose(inPlace); } else { this.lu = inPlace ? value : (decimal[, ])value.Clone(); } this.rows = lu.GetLength(0); this.cols = lu.GetLength(1); this.pivotSign = 1; this.pivotVector = new int[rows]; for (int i = 0; i < rows; i++) { pivotVector[i] = i; } var LUcolj = new decimal[rows]; unsafe { fixed(decimal *LU = lu) { // Outer loop. for (int j = 0; j < cols; j++) { // Make a copy of the j-th column to localize references. for (int i = 0; i < rows; i++) { LUcolj[i] = lu[i, j]; } // Apply previous transformations. for (int i = 0; i < rows; i++) { decimal s = 0; // Most of the time is spent in // the following dot product: int kmax = Math.Min(i, j); decimal *LUrowi = &LU[i * cols]; for (int k = 0; k < kmax; k++) { s += LUrowi[k] * LUcolj[k]; } LUrowi[j] = LUcolj[i] -= s; } // Find pivot and exchange if necessary. int p = j; for (int i = j + 1; i < rows; i++) { if (Math.Abs(LUcolj[i]) > Math.Abs(LUcolj[p])) { p = i; } } if (p != j) { for (int k = 0; k < cols; k++) { var t = lu[p, k]; lu[p, k] = lu[j, k]; lu[j, k] = t; } int v = pivotVector[p]; pivotVector[p] = pivotVector[j]; pivotVector[j] = v; pivotSign = -pivotSign; } // Compute multipliers. if (j < rows && lu[j, j] != 0) { for (int i = j + 1; i < rows; i++) { lu[i, j] /= lu[j, j]; } } } } } }
/// <summary>Least squares solution of <c>X * A = B</c></summary> /// <param name="value">Right-hand-side matrix with as many columns as <c>A</c> and any number of rows.</param> /// <returns>A matrix that minimized the two norm of <c>X * Q * R - B</c>.</returns> /// <exception cref="T:System.ArgumentException">Matrix column dimensions must be the same.</exception> /// <exception cref="T:System.InvalidOperationException">Matrix is rank deficient.</exception> public decimal[,] SolveTranspose(decimal[,] value) { if (value == null) { throw new ArgumentNullException("value", "Matrix cannot be null."); } if (value.GetLength(1) != qr.GetLength(0)) { 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.GetLength(0); var X = value.Transpose(); int m = qr.GetLength(0); int n = qr.GetLength(1); // Compute Y = transpose(Q)*B for (int k = 0; k < n; k++) { for (int j = 0; j < count; j++) { decimal s = 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]; } } } var r = new decimal[count, n]; for (int i = 0; i < n; i++) { for (int j = 0; j < count; j++) { r[j, i] = X[i, j]; } } return(r); }