Ejemplo n.º 1
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.
         */
        //@Override
        public CMatrixRMaj getQ(CMatrixRMaj Q, bool compact)
        {
            if (compact)
            {
                Q = UtilDecompositons_CDRM.checkIdentity(Q, numRows, minLength);
            }
            else
            {
                Q = UtilDecompositons_CDRM.checkIdentity(Q, numRows, numRows);
            }

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

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

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

                QrHelperFunctions_CDRM.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 CMatrixRMaj getQ(CMatrixRMaj Q, bool compact)
        {
            if (compact)
            {
                Q = UtilDecompositons_CDRM.checkIdentity(Q, numRows, minLength);
            }
            else
            {
                Q = UtilDecompositons_CDRM.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;
                float realBefore = QR.data[diagIndex];
                float imagBefore = QR.data[diagIndex + 1];

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

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

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

            return(Q);
        }
Ejemplo n.º 3
0
        /**
         * <p>
         * Computes the householder vector "u" for the first column of submatrix j.  Note this is
         * a specialized householder for this problem.  There is some protection against
         * overfloaw and underflow.
         * </p>
         * <p>
         * Q = I - &gamma;uu<sup>T</sup>
         * </p>
         * <p>
         * This function finds the values of 'u' and '&gamma;'.
         * </p>
         *
         * @param j Which submatrix to work off of.
         */
        protected void householder(int j)
        {
            float[] u = dataQR[j];

            // find the largest value in this column
            // this is used to normalize the column and mitigate overflow/underflow
            float max = QrHelperFunctions_CDRM.findMax(u, j, numRows - j);

            if (max == 0.0f)
            {
                gamma = 0;
                error = true;
            }
            else
            {
                // computes tau and gamma, and normalizes u by max
                gamma = QrHelperFunctions_CDRM.computeTauGammaAndDivide(j, numRows, u, max, tau);

                // divide u by u_0
//            float u_0 = u[j] + tau;
                float real_u_0 = u[j * 2] + tau.real;
                float imag_u_0 = u[j * 2 + 1] + tau.imaginary;
                QrHelperFunctions_CDRM.divideElements(j + 1, numRows, u, 0, real_u_0, imag_u_0);

                tau.real      *= max;
                tau.imaginary *= max;

                u[j * 2]     = -tau.real;
                u[j * 2 + 1] = -tau.imaginary;
            }

            gammas[j] = gamma;
        }
        /**
         * <p>
         * Computes the householder vector "u" for the first column of submatrix j.  Note this is
         * a specialized householder for this problem.  There is some protection against
         * overflow and underflow.
         * </p>
         * <p>
         * Q = I - &gamma;uu<sup>H</sup>
         * </p>
         * <p>
         * This function finds the values of 'u' and '&gamma;'.
         * </p>
         *
         * @param j Which submatrix to work off of.
         */
        protected void householder(int j)
        {
            int startQR = j * numRows;
            int endQR   = startQR + numRows;

            startQR += j;

            float max = QrHelperFunctions_CDRM.findMax(QR.data, startQR, numRows - j);

            if (max == 0.0f)
            {
                gamma = 0;
                error = true;
            }
            else
            {
                // computes tau and normalizes u by max
                gamma = QrHelperFunctions_CDRM.computeTauGammaAndDivide(startQR, endQR, QR.data, max, tau);

                // divide u by u_0
                float realU0 = QR.data[startQR * 2] + tau.real;
                float imagU0 = QR.data[startQR * 2 + 1] + tau.imaginary;

                QrHelperFunctions_CDRM.divideElements(startQR + 1, endQR, QR.data, 0, realU0, imagU0);

                tau.real      *= max;
                tau.imaginary *= max;

                QR.data[startQR * 2]     = -tau.real;
                QR.data[startQR * 2 + 1] = -tau.imaginary;
            }

            gammas[j] = gamma;
        }
        /**
         * A = Q<sup>H</sup>*A
         *
         * @param A Matrix that is being multiplied by Q<sup>T</sup>.  Is modified.
         */
        public void applyTranQ(CMatrixRMaj A)
        {
            for (int j = 0; j < minLength; j++)
            {
                int   diagIndex  = (j * numRows + j) * 2;
                float realBefore = QR.data[diagIndex];
                float imagBefore = QR.data[diagIndex + 1];

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

                QrHelperFunctions_CDRM.rank1UpdateMultR(A, QR.data, j * numRows, gammas[j], 0, j, numRows, v);

                QR.data[diagIndex]     = realBefore;
                QR.data[diagIndex + 1] = imagBefore;
            }
        }
Ejemplo n.º 6
0
        /**
         * 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 CMatrixRMaj getQ(CMatrixRMaj Q, bool compact)
        {
            if (compact)
            {
                Q = UtilDecompositons_CDRM.checkIdentity(Q, numRows, minLength);
            }
            else
            {
                Q = UtilDecompositons_CDRM.checkIdentity(Q, numRows, numRows);
            }

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

            return(Q);
        }
Ejemplo n.º 7
0
        /**
         * <p>
         * Computes the householder vector "u" for the first column of submatrix j.  Note this is
         * a specialized householder for this problem.  There is some protection against
         * overflow and underflow.
         * </p>
         * <p>
         * Q = I - &gamma;uu<sup>H</sup>
         * </p>
         * <p>
         * This function finds the values of 'u' and '&gamma;'.
         * </p>
         *
         * @param j Which submatrix to work off of.
         */
        protected void householder(int j)
        {
            // find the element with the largest absolute value in the column and make a copy
            float max = QrHelperFunctions_CDRM.extractColumnAndMax(QR, j, numRows, j, u, 0);

            if (max <= 0.0f)
            {
                gammas[j] = 0;
                error     = true;
            }
            else
            {
                float gamma = QrHelperFunctions_CDRM.computeTauGammaAndDivide(j, numRows, u, max, tau);
                gammas[j] = gamma;

                // divide u by u_0
                float real_u_0 = u[j * 2] + tau.real;
                float imag_u_0 = u[j * 2 + 1] + tau.imaginary;
                QrHelperFunctions_CDRM.divideElements(j + 1, numRows, u, 0, real_u_0, imag_u_0);

                // write the reflector into the lower left column of the matrix
                for (int i = j + 1; i < numRows; i++)
                {
                    dataQR[(i * numCols + j) * 2]     = u[i * 2];
                    dataQR[(i * numCols + j) * 2 + 1] = u[i * 2 + 1];
                }

                u[j * 2]     = 1;
                u[j * 2 + 1] = 0;

                QrHelperFunctions_CDRM.rank1UpdateMultR(QR, u, 0, gamma, j + 1, j, numRows, v);

                // since the first element in the householder vector is known to be 1
                // store the full upper hessenberg
                if (j < numCols)
                {
                    dataQR[(j * numCols + j) * 2]     = -tau.real * max;
                    dataQR[(j * numCols + j) * 2 + 1] = -tau.imaginary * max;
                }
            }
        }
        /**
         * A = Q*A
         *
         * @param A Matrix that is being multiplied by Q.  Is modified.
         */
        public void applyQ(CMatrixRMaj A)
        {
            if (A.numRows != numRows)
            {
                throw new ArgumentException("A must have at least " + numRows + " rows.");
            }

            for (int j = minLength - 1; j >= 0; j--)
            {
                int   diagIndex  = (j * numRows + j) * 2;
                float realBefore = QR.data[diagIndex];
                float imagBefore = QR.data[diagIndex + 1];

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

                QrHelperFunctions_CDRM.rank1UpdateMultR(A, QR.data, j * numRows, gammas[j], 0, j, numRows, v);

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