/**
         * Returns an upper triangular matrix which is the R in the QR decomposition.
         *
         * @param R An upper triangular matrix.
         * @param compact
         */
        //@Override
        public ZMatrixRMaj getR(ZMatrixRMaj R, bool compact)
        {
            if (compact)
            {
                R = UtilDecompositons_ZDRM.checkZerosLT(R, minLength, numCols);
            }
            else
            {
                R = UtilDecompositons_ZDRM.checkZerosLT(R, numRows, numCols);
            }

            for (int i = 0; i < minLength; i++)
            {
                for (int j = i; j < numCols; j++)
                {
                    int    indexQR = QR.getIndex(i, j);
                    double realQR  = QR.data[indexQR];
                    double imagQR  = QR.data[indexQR + 1];

                    R.set(i, j, realQR, imagQR);
                }
            }

            return(R);
        }
        /**
         * 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.
         */
        //@Override
        public ZMatrixRMaj getQ(ZMatrixRMaj Q, bool compact)
        {
            if (compact)
            {
                Q = UtilDecompositons_ZDRM.checkIdentity(Q, numRows, minLength);
            }
            else
            {
                Q = UtilDecompositons_ZDRM.checkIdentity(Q, numRows, numRows);
            }

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

                double vvReal = u[j * 2];
                double vvImag = u[j * 2 + 1];

                u[j * 2]     = 1;
                u[j * 2 + 1] = 0;
                double gammaReal = gammas[j];

                QrHelperFunctions_ZDRM.rank1UpdateMultR(Q, u, 0, gammaReal, j, j, numRows, v);

                u[j * 2]     = vvReal;
                u[j * 2 + 1] = vvImag;
            }

            return(Q);
        }
        /**
         * Computes the Q matrix from the information 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.
         */
        //@Override
        public ZMatrixRMaj getQ(ZMatrixRMaj Q, bool compact)
        {
            if (compact)
            {
                Q = UtilDecompositons_ZDRM.checkIdentity(Q, numRows, minLength);
            }
            else
            {
                Q = UtilDecompositons_ZDRM.checkIdentity(Q, numRows, numRows);
            }

            // Unlike applyQ() this takes advantage of zeros in the identity matrix
            // by not multiplying across all rows.
            for (int j = minLength - 1; j >= 0; j--)
            {
                int    diagIndex  = (j * numRows + j) * 2;
                double realBefore = QR.data[diagIndex];
                double imagBefore = QR.data[diagIndex + 1];

                QR.data[diagIndex]     = 1;
                QR.data[diagIndex + 1] = 0;

                QrHelperFunctions_ZDRM.rank1UpdateMultR(Q, QR.data, j * numRows, gammas[j], j, j, numRows, v);

                QR.data[diagIndex]     = realBefore;
                QR.data[diagIndex + 1] = imagBefore;
            }

            return(Q);
        }
        /**
         * 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 ZMatrixRMaj getQ(ZMatrixRMaj Q)
        {
            Q = UtilDecompositons_ZDRM.checkIdentity(Q, N, N);
            Array.Clear(u, 0, N * 2);
            for (int j = N - 2; j >= 0; j--)
            {
                QrHelperFunctions_ZDRM.extractHouseholderColumn(QH, j + 1, N, j, u, 0);
                QrHelperFunctions_ZDRM.rank1UpdateMultR(Q, u, 0, gammas[j], j + 1, j + 1, N, b);
            }

            return Q;
        }
        /**
         * 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 ZMatrixRMaj getH(ZMatrixRMaj H)
        {
            H = UtilDecompositons_ZDRM.checkZeros(H, N, N);

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

            for (int i = 1; i < N; i++)
            {
                Array.Copy(QH.data, (i * N + i - 1) * 2, H.data, (i * N + i - 1) * 2, (N - i + 1) * 2);
            }

            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.
         */
        //@Override
        public ZMatrixRMaj getT(ZMatrixRMaj T)
        {
            T = UtilDecompositons_ZDRM.checkZeros(T, N, N);

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

            for (int i = 1; i < N; i++)
            {
                T.set(i, i, QT.getReal(i, i), QT.getImag(i, i));
                double real = QT.getReal(i - 1, i);
                double imag = QT.getImag(i - 1, i);
                T.set(i - 1, i, real, imag);
                T.set(i, i - 1, real, -imag);
            }

            return(T);
        }
        /**
         * Computes the Q matrix from the information 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.
         */
        //@Override
        public ZMatrixRMaj getQ(ZMatrixRMaj Q, bool compact)
        {
            if (compact)
            {
                Q = UtilDecompositons_ZDRM.checkIdentity(Q, numRows, minLength);
            }
            else
            {
                Q = UtilDecompositons_ZDRM.checkIdentity(Q, numRows, numRows);
            }

            for (int j = minLength - 1; j >= 0; j--)
            {
                QrHelperFunctions_ZDRM.extractHouseholderColumn(QR, j, numRows, j, u, 0);
                QrHelperFunctions_ZDRM.rank1UpdateMultR(Q, u, 0, gammas[j], j, j, numRows, v);
            }

            return(Q);
        }
Example #8
0
        /**
         * Writes the lower triangular matrix into the specified matrix.
         *
         * @param lower Where the lower triangular matrix is written to.
         */
        //@Override
        public ZMatrixRMaj getLower(ZMatrixRMaj lower)
        {
            int numRows = LU.numRows;
            int numCols = LU.numRows < LU.numCols ? LU.numRows : LU.numCols;

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

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

                for (int j = 0; j < i; j++)
                {
                    int indexLU = LU.getIndex(i, j);
                    int indexL  = lower.getIndex(i, j);

                    double real      = LU.data[indexLU];
                    double imaginary = LU.data[indexLU + 1];

                    lower.data[indexL]     = real;
                    lower.data[indexL + 1] = imaginary;
                }
            }

            if (numRows > numCols)
            {
                for (int i = numCols; i < numRows; i++)
                {
                    for (int j = 0; j < numCols; j++)
                    {
                        int indexLU = LU.getIndex(i, j);
                        int indexL  = lower.getIndex(i, j);

                        double real      = LU.data[indexLU];
                        double imaginary = LU.data[indexLU + 1];

                        lower.data[indexL]     = real;
                        lower.data[indexL + 1] = imaginary;
                    }
                }
            }
            return(lower);
        }
        /**
         * Returns an upper triangular matrix which is the R in the QR decomposition.
         *
         * @param R An upper triangular matrix.
         * @param compact
         */
        //@Override
        public ZMatrixRMaj getR(ZMatrixRMaj R, bool compact)
        {
            if (compact)
            {
                R = UtilDecompositons_ZDRM.checkZerosLT(R, minLength, numCols);
            }
            else
            {
                R = UtilDecompositons_ZDRM.checkZerosLT(R, numRows, numCols);
            }

            for (int i = 0; i < R.numRows; i++)
            {
                for (int j = i; j < R.numCols; j++)
                {
                    int index = QR.getIndex(j, i);
                    R.set(i, j, QR.data[index], QR.data[index + 1]);
                }
            }

            return(R);
        }
        /**
         * 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.
         */
        //@Override
        public ZMatrixRMaj getR(ZMatrixRMaj R, bool compact)
        {
            if (compact)
            {
                R = UtilDecompositons_ZDRM.checkZerosLT(R, minLength, numCols);
            }
            else
            {
                R = UtilDecompositons_ZDRM.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++)
                {
                    R.set(i, j, colR[i * 2], colR[i * 2 + 1]);
                }
            }

            return(R);
        }
        /**
         * An orthogonal matrix that has the following property: T = Q<sup>H</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 ZMatrixRMaj getQ(ZMatrixRMaj Q, bool transposed)
        {
            Q = UtilDecompositons_ZDRM.checkIdentity(Q, N, N);
            Array.Clear(w, 0, N * 2);

            if (transposed)
            {
                for (int j = N - 2; j >= 0; j--)
                {
                    QrHelperFunctions_ZDRM.extractHouseholderRow(QT, j, j + 1, N, w, 0);
                    QrHelperFunctions_ZDRM.rank1UpdateMultL(Q, w, 0, gammas[j], j + 1, j + 1, N);
                }
            }
            else
            {
                for (int j = N - 2; j >= 0; j--)
                {
                    QrHelperFunctions_ZDRM.extractHouseholderRow(QT, j, j + 1, N, w, 0);
                    QrHelperFunctions_ZDRM.rank1UpdateMultR(Q, w, 0, gammas[j], j + 1, j + 1, N, b);
                }
            }

            return(Q);
        }
        //@Override
        public ZMatrixRMaj getT(ZMatrixRMaj T)
        {
            // write the values to T
            if (lower)
            {
                T = UtilDecompositons_ZDRM.checkZerosUT(T, n, n);
                for (int i = 0; i < n; i++)
                {
                    int index = i * n * 2;
                    for (int j = 0; j <= i; j++)
                    {
                        T.data[index] = this.T.data[index];
                        index++;
                        T.data[index] = this.T.data[index];
                        index++;
                    }
                }
            }
            else
            {
                T = UtilDecompositons_ZDRM.checkZerosLT(T, n, n);
                for (int i = 0; i < n; i++)
                {
                    int index = (i * n + i) * 2;
                    for (int j = i; j < n; j++)
                    {
                        T.data[index] = this.T.data[index];
                        index++;
                        T.data[index] = this.T.data[index];
                        index++;
                    }
                }
            }

            return(T);
        }
Example #13
0
        /**
         * Writes the upper triangular matrix into the specified matrix.
         *
         * @param upper Where the upper triangular matrix is writen to.
         */
        //@Override
        public ZMatrixRMaj getUpper(ZMatrixRMaj upper)
        {
            int numRows = LU.numRows < LU.numCols ? LU.numRows : LU.numCols;
            int numCols = LU.numCols;

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

            for (int i = 0; i < numRows; i++)
            {
                for (int j = i; j < numCols; j++)
                {
                    int indexLU = LU.getIndex(i, j);
                    int indexU  = upper.getIndex(i, j);

                    double real      = LU.data[indexLU];
                    double imaginary = LU.data[indexLU + 1];

                    upper.data[indexU]     = real;
                    upper.data[indexU + 1] = imaginary;
                }
            }

            return(upper);
        }