public virtual FMatrixRMaj getT(FMatrixRMaj T)
        {
            // write the values to T
            if (lower)
            {
                T = UtilDecompositons_FDRM.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_FDRM.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);
        }
예제 #2
0
        /**
         * Writes the lower triangular matrix into the specified matrix.
         *
         * @param lower Where the lower triangular matrix is written to.
         */
        public virtual FMatrixRMaj getLower(FMatrixRMaj lower)
        {
            int numRows = LU.numRows;
            int numCols = LU.numRows < LU.numCols ? LU.numRows : LU.numCols;

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

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

                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);
        }
예제 #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 FMatrixRMaj getQ(FMatrixRMaj Q)
        {
            Q = UtilDecompositons_FDRM.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_FDRM.rank1UpdateMultR(Q, u, gammas[j], j + 1, j + 1, N, b);
            }

            return(Q);
        }
예제 #4
0
        /**
         * Writes the upper triangular matrix into the specified matrix.
         *
         * @param upper Where the upper triangular matrix is writen to.
         */
        public virtual FMatrixRMaj getUpper(FMatrixRMaj upper)
        {
            int numRows = LU.numRows < LU.numCols ? LU.numRows : LU.numCols;
            int numCols = LU.numCols;

            upper = UtilDecompositons_FDRM.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);
        }
예제 #5
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 FMatrixRMaj getH(FMatrixRMaj H)
        {
            H = UtilDecompositons_FDRM.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);
        }
        /**
         * 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 FMatrixRMaj getT(FMatrixRMaj T)
        {
            T = UtilDecompositons_FDRM.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 FMatrixRMaj getQ(FMatrixRMaj Q)
        {
            Q = UtilDecompositons_FDRM.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_FDRM.rank1UpdateMultR(Q, w, gammas[j + 1], j + 1, j + 1, N, b);
//            Q.print();
            }

            return(Q);
        }
        /**
         * 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 virtual FMatrixRMaj getT(FMatrixRMaj T)
        {
            T = UtilDecompositons_FDRM.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));
                float 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);
        }
        /**
         * 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 FMatrixRMaj getQ(FMatrixRMaj Q, bool compact)
        {
            if (compact)
            {
                Q = UtilDecompositons_FDRM.checkIdentity(Q, numRows, minLength);
            }
            else
            {
                Q = UtilDecompositons_FDRM.checkIdentity(Q, numRows, numRows);
            }

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

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

            return(Q);
        }
        /**
         * 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 virtual FMatrixRMaj getQ(FMatrixRMaj Q, bool transposed)
        {
            Q = UtilDecompositons_FDRM.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_FDRM.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_FDRM.rank1UpdateMultR(Q, w, gammas[j + 1], j + 1, j + 1, N, b);
                }
            }

            return(Q);
        }
        /**
         * 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 FMatrixRMaj getR(FMatrixRMaj R, bool compact)
        {
            if (compact)
            {
                R = UtilDecompositons_FDRM.checkZerosLT(R, minLength, numCols);
            }
            else
            {
                R = UtilDecompositons_FDRM.checkZerosLT(R, numRows, numCols);
            }

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

            return(R);
        }