コード例 #1
0
        /**
         * Writes the lower triangular matrix into the specified matrix.
         *
         * @param lower Where the lower triangular matrix is written to.
         */

        public virtual DMatrixRMaj getLower(DMatrixRMaj lower)
        {
            int numRows = LU.numRows;
            int numCols = LU.numRows < LU.numCols ? LU.numRows : LU.numCols;

            lower = UtilDecompositons_DDRM.checkZerosUT(lower, numRows, numCols);

            for (int i = 0; i < numCols; i++)
            {
                lower.unsafe_set(i, i, 1.0);

                for (int j = 0; j < i; j++)
                {
                    lower.unsafe_set(i, j, LU.unsafe_get(i, j));
                }
            }

            if (numRows > numCols)
            {
                for (int i = numCols; i < numRows; i++)
                {
                    for (int j = 0; j < numCols; j++)
                    {
                        lower.unsafe_set(i, j, LU.unsafe_get(i, j));
                    }
                }
            }
            return(lower);
        }
コード例 #2
0
        public virtual DMatrixRMaj getT(DMatrixRMaj T)
        {
            // write the values to T
            if (lower)
            {
                T = UtilDecompositons_DDRM.checkZerosUT(T, n, n);
                for (int i = 0; i < n; i++)
                {
                    for (int j = 0; j <= i; j++)
                    {
                        T.unsafe_set(i, j, this.T.unsafe_get(i, j));
                    }
                }
            }
            else
            {
                T = UtilDecompositons_DDRM.checkZerosLT(T, n, n);
                for (int i = 0; i < n; i++)
                {
                    for (int j = i; j < n; j++)
                    {
                        T.unsafe_set(i, j, this.T.unsafe_get(i, j));
                    }
                }
            }

            return(T);
        }
コード例 #3
0
        /**
         * An orthogonal matrix that has the following property: H = Q<sup>T</sup>AQ
         *
         * @param Q If not null then the results will be stored here.  Otherwise a new matrix will be created.
         * @return The extracted Q matrix.
         */
        public DMatrixRMaj getQ(DMatrixRMaj Q)
        {
            Q = UtilDecompositons_DDRM.checkIdentity(Q, N, N);

            for (int j = N - 2; j >= 0; j--)
            {
                u[j + 1] = 1;
                for (int i = j + 2; i < N; i++)
                {
                    u[i] = QH.get(i, j);
                }
                QrHelperFunctions_DDRM.rank1UpdateMultR(Q, u, gammas[j], j + 1, j + 1, N, b);
            }

            return(Q);
        }
コード例 #4
0
        /**
         * An upper Hessenberg matrix from the decomposition.
         *
         * @param H If not null then the results will be stored here.  Otherwise a new matrix will be created.
         * @return The extracted H matrix.
         */
        public DMatrixRMaj getH(DMatrixRMaj H)
        {
            H = UtilDecompositons_DDRM.checkZeros(H, N, N);

            // copy the first row
            Array.Copy(QH.data, 0, H.data, 0, N);

            for (int i = 1; i < N; i++)
            {
                for (int j = i - 1; j < N; j++)
                {
                    H.set(i, j, QH.get(i, j));
                }
            }

            return(H);
        }
コード例 #5
0
        /**
         * Writes the upper triangular matrix into the specified matrix.
         *
         * @param upper Where the upper triangular matrix is writen to.
         */
        public virtual DMatrixRMaj getUpper(DMatrixRMaj upper)
        {
            int numRows = LU.numRows < LU.numCols ? LU.numRows : LU.numCols;
            int numCols = LU.numCols;

            upper = UtilDecompositons_DDRM.checkZerosLT(upper, numRows, numCols);

            for (int i = 0; i < numRows; i++)
            {
                for (int j = i; j < numCols; j++)
                {
                    upper.unsafe_set(i, j, LU.unsafe_get(i, j));
                }
            }

            return(upper);
        }
        /**
         * Extracts the tridiagonal matrix found in the decomposition.
         *
         * @param T If not null then the results will be stored here.  Otherwise a new matrix will be created.
         * @return The extracted T matrix.
         */
        public DMatrixRMaj getT(DMatrixRMaj T)
        {
            T = UtilDecompositons_DDRM.checkZeros(T, N, N);

            T.data[0] = QT.data[0];
            T.data[1] = QT.data[1];


            for (int i = 1; i < N - 1; i++)
            {
                T.set(i, i, QT.get(i, i));
                T.set(i, i + 1, QT.get(i, i + 1));
                T.set(i, i - 1, QT.get(i - 1, i));
            }

            T.data[(N - 1) * N + N - 1] = QT.data[(N - 1) * N + N - 1];
            T.data[(N - 1) * N + N - 2] = QT.data[(N - 2) * N + N - 1];

            return(T);
        }
        /**
         * An orthogonal matrix that has the following property: T = Q<sup>T</sup>AQ
         *
         * @param Q If not null then the results will be stored here.  Otherwise a new matrix will be created.
         * @return The extracted Q matrix.
         */
        public DMatrixRMaj getQ(DMatrixRMaj Q)
        {
            Q = UtilDecompositons_DDRM.checkIdentity(Q, N, N);

            for (int i = 0; i < N; i++)
            {
                w[i] = 0;
            }

            for (int j = N - 2; j >= 0; j--)
            {
                w[j + 1] = 1;
                for (int i = j + 2; i < N; i++)
                {
                    w[i] = QT.get(j, i);
                }
                QrHelperFunctions_DDRM.rank1UpdateMultR(Q, w, gammas[j + 1], j + 1, j + 1, N, b);
//            Q.print();
            }

            return(Q);
        }
コード例 #8
0
        /**
         * Extracts the tridiagonal matrix found in the decomposition.
         *
         * @param T If not null then the results will be stored here.  Otherwise a new matrix will be created.
         * @return The extracted T matrix.
         */
        //@Override
        public DMatrixRMaj getT(DMatrixRMaj T)
        {
            T = UtilDecompositons_DDRM.checkZeros(T, N, N);

            T.data[0] = QT.data[0];

            for (int i = 1; i < N; i++)
            {
                T.set(i, i, QT.get(i, i));
                double a = QT.get(i - 1, i);
                T.set(i - 1, i, a);
                T.set(i, i - 1, a);
            }

            if (N > 1)
            {
                T.data[(N - 1) * N + N - 1] = QT.data[(N - 1) * N + N - 1];
                T.data[(N - 1) * N + N - 2] = QT.data[(N - 2) * N + N - 1];
            }

            return(T);
        }
コード例 #9
0
        /**
         * Computes the Q matrix from the imformation stored in the QR matrix.  This
         * operation requires about 4(m<sup>2</sup>n-mn<sup>2</sup>+n<sup>3</sup>/3) flops.
         *
         * @param Q The orthogonal Q matrix.
         */
        public virtual DMatrixRMaj getQ(DMatrixRMaj Q, bool compact)
        {
            if (compact)
            {
                Q = UtilDecompositons_DDRM.checkIdentity(Q, numRows, minLength);
            }
            else
            {
                Q = UtilDecompositons_DDRM.checkIdentity(Q, numRows, numRows);
            }

            for (int j = minLength - 1; j >= 0; j--)
            {
                double[] u = dataQR[j];

                double vv = u[j];
                u[j] = 1;
                QrHelperFunctions_DDRM.rank1UpdateMultR(Q, u, gammas[j], j, j, numRows, v);
                u[j] = vv;
            }

            return(Q);
        }
コード例 #10
0
        /**
         * Returns an upper triangular matrix which is the R in the QR decomposition.  If compact then the input
         * expected to be size = [min(rows,cols) , numCols] otherwise size = [numRows,numCols].
         *
         * @param R Storage for upper triangular matrix.
         * @param compact If true then a compact matrix is expected.
         */
        public virtual DMatrixRMaj getR(DMatrixRMaj R, bool compact)
        {
            if (compact)
            {
                R = UtilDecompositons_DDRM.checkZerosLT(R, minLength, numCols);
            }
            else
            {
                R = UtilDecompositons_DDRM.checkZerosLT(R, numRows, numCols);
            }

            for (int j = 0; j < numCols; j++)
            {
                double[] colR = dataQR[j];
                int      l    = Math.Min(j, numRows - 1);
                for (int i = 0; i <= l; i++)
                {
                    double val = colR[i];
                    R.set(i, j, val);
                }
            }

            return(R);
        }
コード例 #11
0
        /**
         * An orthogonal matrix that has the following property: T = Q<sup>T</sup>AQ
         *
         * @param Q If not null then the results will be stored here.  Otherwise a new matrix will be created.
         * @return The extracted Q matrix.
         */
        //@Override
        public DMatrixRMaj getQ(DMatrixRMaj Q, bool transposed)
        {
            Q = UtilDecompositons_DDRM.checkIdentity(Q, N, N);

            for (int i = 0; i < N; i++)
            {
                w[i] = 0;
            }

            if (transposed)
            {
                for (int j = N - 2; j >= 0; j--)
                {
                    w[j + 1] = 1;
                    for (int i = j + 2; i < N; i++)
                    {
                        w[i] = QT.data[j * N + i];
                    }
                    QrHelperFunctions_DDRM.rank1UpdateMultL(Q, w, gammas[j + 1], j + 1, j + 1, N);
                }
            }
            else
            {
                for (int j = N - 2; j >= 0; j--)
                {
                    w[j + 1] = 1;
                    for (int i = j + 2; i < N; i++)
                    {
                        w[i] = QT.get(j, i);
                    }
                    QrHelperFunctions_DDRM.rank1UpdateMultR(Q, w, gammas[j + 1], j + 1, j + 1, N, b);
                }
            }

            return(Q);
        }