Example #1
0
        /**
         * Creates a newJava.Util.Random symmetric matrix that will have the specified real eigenvalues.
         *
         * @param num Dimension of the resulting matrix.
         * @param randJava.Util.Random number generator.
         * @param eigenvalues Set of real eigenvalues that the matrix will have.
         * @return AJava.Util.Random matrix with the specified eigenvalues.
         */
        public static DMatrixRMaj symmetricWithEigenvalues(int num, Java.Util.Random rand, double[] eigenvalues)
        {
            DMatrixRMaj V = RandomMatrices_DDRM.orthogonal(num, num, rand);
            DMatrixRMaj D = CommonOps_DDRM.diag(eigenvalues);

            DMatrixRMaj temp = new DMatrixRMaj(num, num);

            CommonOps_DDRM.mult(V, D, temp);
            CommonOps_DDRM.multTransB(temp, V, D);

            return(D);
        }
Example #2
0
        /**
         * <p>
         * Creates aJava.Util.Random matrix which will have the provided singular values.  The Count() of sv
         * is assumed to be the rank of the matrix.  This can be useful for testing purposes when one
         * needs to ensure that a matrix is not singular but randomly generated.
         * </p>
         *
         * @param numRows Number of rows in generated matrix.
         * @param numCols NUmber of columns in generated matrix.
         * @param randJava.Util.Random number generator.
         * @param sv Singular values of the matrix.
         * @return A new matrix with the specified singular values.
         */
        public static DMatrixRMaj singular(int numRows, int numCols,
                                           Java.Util.Random rand, double[] sv)
        {
            DMatrixRMaj U, V, S;

            // speed it up in compact format
            if (numRows > numCols)
            {
                U = RandomMatrices_DDRM.orthogonal(numRows, numCols, rand);
                V = RandomMatrices_DDRM.orthogonal(numCols, numCols, rand);
                S = new DMatrixRMaj(numCols, numCols);
            }
            else
            {
                U = RandomMatrices_DDRM.orthogonal(numRows, numRows, rand);
                V = RandomMatrices_DDRM.orthogonal(numCols, numCols, rand);
                S = new DMatrixRMaj(numRows, numCols);
            }

            int min = Math.Min(numRows, numCols);

            min = Math.Min(min, sv.Count());

            for (int i = 0; i < min; i++)
            {
                S.set(i, i, sv[i]);
            }

            DMatrixRMaj tmp = new DMatrixRMaj(numRows, numCols);

            CommonOps_DDRM.mult(U, S, tmp);
            S.reshape(numRows, numCols);
            CommonOps_DDRM.multTransB(tmp, V, S);

            return(S);
        }