/// <summary>
        ///   Constructs a new Cholesky Decomposition.
        /// </summary>
        ///
        /// <param name="value">
        ///   The symmetric matrix, given in upper triangular form, to be decomposed.</param>
        /// <param name="robust">
        ///   True to perform a square-root free LDLt decomposition, false otherwise.</param>
        /// <param name="inPlace">
        ///   True to perform the decomposition in place, storing the factorization in the
        ///   lower triangular part of the given matrix.</param>
        /// <param name="valueType">
        ///   How to interpret the matrix given to be decomposed. Using this parameter, a lower or
        ///   upper-triangular matrix can be interpreted as a symmetric matrix by assuming both lower
        ///   and upper parts contain the same elements. Use this parameter in conjunction with inPlace
        ///   to save memory by storing the original matrix and its decomposition at the same memory
        ///   location (lower part will contain the decomposition's L matrix, upper part will contains
        ///   the original matrix).</param>
        ///
        public CholeskyDecompositionF(Single[,] value, bool robust = false,
                                      bool inPlace = false, MatrixType valueType = MatrixType.UpperTriangular)
        {
            if (value.Rows() != value.Columns())
            {
                throw new DimensionMismatchException("value", "Matrix is not square.");
            }

            if (!inPlace)
            {
                value = value.Copy();
            }

            this.n      = value.Rows();
            this.L      = value.ToUpperTriangular(valueType, result: value);
            this.robust = robust;

            if (robust)
            {
                LDLt(); // Compute square-root free decomposition
            }
            else
            {
                LLt(); // Compute standard Cholesky decomposition
            }
        }
예제 #2
0
        /// <summary>Least squares solution of <c>A * X = B</c></summary>
        /// <param name="value">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 Single[,] Solve(Single[,] value)
        {
            if (value == null)
            {
                throw new ArgumentNullException("value", "Matrix cannot be null.");
            }

            if (value.Rows() != n)
            {
                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();
            var X     = value.Copy();

            // Compute Y = transpose(Q)*B
            for (int k = 0; k < p; k++)
            {
                for (int j = 0; j < count; j++)
                {
                    Single s = 0;
                    for (int i = k; i < n; i++)
                    {
                        s += qr[i, k] * X[i, j];
                    }

                    s = -s / qr[k, k];
                    for (int i = k; i < n; i++)
                    {
                        X[i, j] += s * qr[i, k];
                    }
                }
            }

            // Solve R*X = Y;
            for (int k = p - 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(Matrix.Create(p, count, X, transpose: false));
        }
예제 #3
0
        /// <summary>
        ///   Creates a new object that is a copy of the current instance.
        /// </summary>
        /// <returns>
        ///   A new object that is a copy of this instance.
        /// </returns>
        public object Clone()
        {
            var clone = new QrDecompositionF();

            clone.qr      = qr.Copy();
            clone.Rdiag   = Rdiag.Copy();
            clone.n       = n;
            clone.p       = p;
            clone.m       = m;
            clone.economy = economy;
            return(clone);
        }
예제 #4
0
        /// <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>
        /// <param name="inPlace">True if the decomposition should be done in place,
        /// overriding the given matrix <paramref name="value"/>. Default is false.</param>
        /// <param name="economy">True to perform the economy decomposition, where only
        ///.the information needed to solve linear systems is computed. If set to false,
        /// the full QR decomposition will be computed.</param>
        public QrDecompositionF(Single[,] value, bool transpose = false,
                                bool economy = true, bool inPlace = false)
        {
            if (value == null)
            {
                throw new ArgumentNullException("value", "Matrix cannot be null.");
            }

            if ((!transpose && value.Rows() < value.Columns()) ||
                (transpose && value.Columns() < value.Rows()))
            {
                throw new ArgumentException("Matrix has more columns than rows.", "value");
            }

            // https://www.inf.ethz.ch/personal/gander/papers/qrneu.pdf

            if (transpose)
            {
                this.p = value.Rows();

                if (economy)
                {
                    // Compute the faster, economy-sized QR decomposition
                    this.qr = value.Transpose(inPlace: inPlace);
                }
                else
                {
                    // Create room to store the full decomposition
                    this.qr = Matrix.Create(value.Columns(), value.Columns(), value, transpose: true);
                }
            }
            else
            {
                this.p = value.Columns();

                if (economy)
                {
                    // Compute the faster, economy-sized QR decomposition
                    this.qr = inPlace ? value : value.Copy();
                }
                else
                {
                    // Create room to store the full decomposition
                    this.qr = Matrix.Create(value.Rows(), value.Rows(), value, transpose: false);
                }
            }

            this.economy = economy;
            this.n       = qr.Rows();
            this.m       = qr.Columns();
            this.Rdiag   = new Single[m];

            for (int k = 0; k < m; k++)
            {
                // Compute 2-norm of k-th column without under/overflow.
                Single nrm = 0;
                for (int i = k; i < n; 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 < n; i++)
                    {
                        qr[i, k] /= nrm;
                    }

                    qr[k, k] += 1;

                    // Apply transformation to remaining columns.
                    for (int j = k + 1; j < m; j++)
                    {
                        Single s = 0;
                        for (int i = k; i < n; i++)
                        {
                            s += qr[i, k] * qr[i, j];
                        }

                        s = -s / qr[k, k];
                        for (int i = k; i < n; i++)
                        {
                            qr[i, j] += s * qr[i, k];
                        }
                    }
                }

                this.Rdiag[k] = -nrm;
            }
        }