Ejemplo n.º 1
0
        /// <summary>Construct a QR decomposition.</summary>
        public QrDecomposition(MatrixV value)
        {
            if (value == null)
            {
                throw new ArgumentNullException("value");
            }

            this.QR = (MatrixV)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;
            }
        }
Ejemplo n.º 2
0
        /// <summary>Least squares solution of <c>A * X = B</c></summary>
        /// <param name="value">Right-hand-side MatrixV with as many rows as <c>A</c> and any number of columns.</param>
        /// <returns>A MatrixV that minimized the two norm of <c>Q * R * X - B</c>.</returns>
        /// <exception cref="T:System.ArgumentException">MatrixV row dimensions must be the same.</exception>
        /// <exception cref="T:System.InvalidOperationException">MatrixV is rank deficient.</exception>
        public MatrixV Solve(MatrixV value)
        {
            if (value == null)
            {
                throw new ArgumentNullException("value");
            }

            if (value.Rows != QR.Rows)
            {
                throw new ArgumentException("MatrixV row dimensions must agree.");
            }

            if (!this.FullRank)
            {
                throw new InvalidOperationException("MatrixV is rank deficient.");
            }

            // Copy right hand side
            int     count = value.Columns;
            MatrixV 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));
        }
Ejemplo n.º 3
0
        /// <summary>Construct a LU decomposition.</summary>
        public LuDecomposition(MatrixV value)
        {
            if (value == null)
            {
                throw new ArgumentNullException("value");
            }

            this.LU = (MatrixV)value.Clone();
            double[][] lu      = LU.Array;
            int        rows    = value.Rows;
            int        columns = value.Columns;

            pivotVector = new int[rows];
            for (int i = 0; i < rows; i++)
            {
                pivotVector[i] = i;
            }

            pivotSign = 1;
            double[] LUrowi;
            double[] LUcolj = new double[rows];

            // Outer loop.
            for (int j = 0; j < columns; 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++)
                {
                    LUrowi = lu[i];

                    // Most of the time is spent in the following dot product.
                    int    kmax = Math.Min(i, j);
                    double s    = 0.0;
                    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 < columns; k++)
                    {
                        double 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.0)
                {
                    for (int i = j + 1; i < rows; i++)
                    {
                        lu[i][j] /= lu[j][j];
                    }
                }
            }
        }