Beispiel #1
0
        /// <summary>Least squares solution of <c>A * X = B</c></summary>
        /// <param name="rhs">Right-hand-side matrix with as many rows as <c>A</c> and any number of columns.</param>
        /// <returns>A matrix that minimized the two norm of <c>Q * R * X - B</c>.</returns>
        /// <exception cref="T:System.ArgumentException">Matrix row dimensions must be the same.</exception>
        /// <exception cref="T:System.InvalidOperationException">Matrix is rank deficient.</exception>
        public ArrayMatrix Solve(ArrayMatrix rhs)
        {
            if (rhs.RowCount != QR.RowCount)
            {
                throw new ArgumentException("Matrix row dimensions must agree.");
            }
            if (!IsFullRank)
            {
                throw new InvalidOperationException("Matrix is rank deficient.");
            }

            // Copy right hand side
            int         count = rhs.Columns;
            ArrayMatrix X     = (ArrayMatrix)rhs.Clone();
            int         m     = QR.RowCount;
            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));
        }
Beispiel #2
0
        /// <summary>Solves a set of equation systems of type <c>A * X = B</c>.</summary>
        /// <param name="B">Right hand side matrix with as many rows as <c>A</c> and any number of columns.</param>
        /// <returns>Matrix <c>X</c> so that <c>L * U * X = B</c>.</returns>
        public ArrayMatrix Solve(ArrayMatrix B)
        {
            if (B.RowCount != LU.RowCount)
            {
                throw new ArgumentException("Invalid matrix dimensions.");
            }
            if (!IsNonSingular)
            {
                throw new InvalidOperationException("Matrix is singular");
            }

            // Copy right hand side with pivoting
            int         count = B.Columns;
            ArrayMatrix X     = B.Submatrix(pivotVector, 0, count - 1);

            int rows    = LU.RowCount;
            int columns = LU.Columns;

            double[][] lu = LU.Array;

            // Solve L*Y = B(piv,:)
            for (int k = 0; k < columns; k++)
            {
                for (int i = k + 1; i < columns; i++)
                {
                    for (int j = 0; j < count; j++)
                    {
                        X[i, j] -= X[k, j] * lu[i][k];
                    }
                }
            }

            // Solve U*X = Y;
            for (int k = columns - 1; k >= 0; k--)
            {
                for (int j = 0; j < count; j++)
                {
                    X[k, j] /= lu[k][k];
                }

                for (int i = 0; i < k; i++)
                {
                    for (int j = 0; j < count; j++)
                    {
                        X[i, j] -= X[k, j] * lu[i][k];
                    }
                }
            }
            return(X);
        }