Exemple #1
0
        /**
         * Performs QR decomposition on A
         *
         * @param A not modified.
         */
        //@Override
        public override bool setA(ZMatrixRMaj A)
        {
            if (A.numRows < A.numCols)
            {
                throw new ArgumentException("Can't solve for wide systems.  More variables than equations.");
            }
            if (A.numRows > maxRows || A.numCols > maxCols)
            {
                setMaxSize(A.numRows, A.numCols);
            }

            R.reshape(A.numCols, A.numCols);
            a.reshape(A.numRows, 1);
            temp.reshape(A.numRows, 1);

            _setA(A);
            if (!decomposer.decompose(A))
            {
                return(false);
            }

            gammas = decomposer.getGammas();
            QR     = decomposer.getQR();
            decomposer.getR(R, true);
            return(true);
        }
        public static void invert(LinearSolverDense <ZMatrixRMaj> solver, ZMatrixRMaj A, ZMatrixRMaj A_inv)
        {
            A_inv.reshape(A.numRows, A.numCols);

            CommonOps_ZDRM.setIdentity(A_inv);

            solver.solve(A_inv, A_inv);
        }
 /**
  * <p>Performs an "in-place" conjugate transpose.</p>
  *
  * @param mat The matrix that is to be transposed. Modified.
  * @see #transpose(ZMatrixRMaj)
  */
 public static void transposeConjugate(ZMatrixRMaj mat)
 {
     if (mat.numCols == mat.numRows)
     {
         TransposeAlgs_ZDRM.squareConjugate(mat);
     }
     else
     {
         ZMatrixRMaj b = new ZMatrixRMaj(mat.numCols, mat.numRows);
         transposeConjugate(mat, b);
         mat.reshape(b.numRows, b.numCols);
         mat.setTo(b);
     }
 }
        /**
         * <p>
         * Extracts the diagonal elements 'src' write it to the 'dst' vector.  'dst'
         * can either be a row or column vector.
         * <p>
         *
         * @param src Matrix whose diagonal elements are being extracted. Not modified.
         * @param dst A vector the results will be written into. Modified.
         */
        public static void extractDiag(ZMatrixRMaj src, ZMatrixRMaj dst)
        {
            int N = Math.Min(src.numRows, src.numCols);

            // reshape if it's not the right size
            if (!MatrixFeatures_ZDRM.isVector(dst) || dst.numCols * dst.numCols != N)
            {
                dst.reshape(N, 1);
            }

            for (int i = 0; i < N; i++)
            {
                int index = src.getIndex(i, i);
                dst.data[i * 2]     = src.data[index];
                dst.data[i * 2 + 1] = src.data[index + 1];
            }
        }
        public override /**/ double quality()
        {
            return(SpecializedOps_ZDRM.qualityTriangular(R));
        }

        /**
         * Solves for X using the QR decomposition.
         *
         * @param B A matrix that is n by m.  Not modified.
         * @param X An n by m matrix where the solution is written to.  Modified.
         */
        //@Override
        public override void solve(ZMatrixRMaj B, ZMatrixRMaj X)
        {
            if (X.numRows != numCols)
            {
                throw new ArgumentException("Unexpected dimensions for X");
            }
            else if (B.numRows != numRows || B.numCols != X.numCols)
            {
                throw new ArgumentException("Unexpected dimensions for B");
            }

            int BnumCols = B.numCols;

            Y.reshape(numRows, 1);
            Z.reshape(numRows, 1);

            // solve each column one by one
            for (int colB = 0; colB < BnumCols; colB++)
            {
                // make a copy of this column in the vector
                for (int i = 0; i < numRows; i++)
                {
                    int indexB = B.getIndex(i, colB);
                    Y.data[i * 2]     = B.data[indexB];
                    Y.data[i * 2 + 1] = B.data[indexB + 1];
                }

                // Solve Qa=b
                // a = Q'b
                CommonOps_ZDRM.mult(Qt, Y, Z);

                // solve for Rx = b using the standard upper triangular solver
                TriangularSolver_ZDRM.solveU(R.data, Z.data, numCols);

                // save the results
                for (int i = 0; i < numCols; i++)
                {
                    X.set(i, colB, Z.data[i * 2], Z.data[i * 2 + 1]);
                }
            }
        }
        /**
         * Performs QR decomposition on A
         *
         * @param A not modified.
         */
        //@Override
        public override bool setA(ZMatrixRMaj A)
        {
            if (A.numRows > maxRows || A.numCols > maxCols)
            {
                setMaxSize(A.numRows, A.numCols);
            }

            _setA(A);
            if (!decomposer.decompose(A))
            {
                return(false);
            }

            Q.reshape(numRows, numRows);
            R.reshape(numRows, numCols);
            decomposer.getQ(Q, false);
            decomposer.getR(R, false);
            CommonOps_ZDRM.transposeConjugate(Q, Qt);

            return(true);
        }