/** * Constructor that initializes a random rotation matrix using the EJML library. * * @param seed * The seed used for generating the random rotation matrix * @param dim * The dimensionality of the vectors that we want to randomly rotate */ public RandomRotation(int seed, int dim) { Random rand = new Random(seed); // create a random rotation matrix randomMatrix = new DenseMatrix64F(dim, dim); randomMatrix = RandomMatrices.createOrthogonal(dim, dim, rand); }
/** * <p> * Creates a random matrix which will have the provided singular values. The length 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 rand Random number generator. * @param sv Singular values of the matrix. * @return A new matrix with the specified singular values. */ public static DenseMatrix64F createSingularValues(int numRows, int numCols, Random rand, double ...sv) { DenseMatrix64F U = RandomMatrices.createOrthogonal(numRows,numRows,rand); DenseMatrix64F V = RandomMatrices.createOrthogonal(numCols,numCols,rand); DenseMatrix64F S = new DenseMatrix64F(numRows,numCols); int min = Math.min(numRows,numCols); min = Math.min(min,sv.length); for( int i = 0; i < min; i++ ) { S.set(i,i,sv[i]); } DenseMatrix64F tmp = new DenseMatrix64F(numRows,numCols); CommonOps.mult(U,S,tmp); CommonOps.multTransB(tmp,V,S); return S; }
/** * <p> * Creates a random matrix which will have the provided singular values. The length 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 rand Random number generator. * @param sv Singular values of the matrix. * @return A new matrix with the specified singular values. */ public static DenseMatrix64F createSingularValues(int numRows, int numCols, Random rand, double ...sv) { DenseMatrix64F U = RandomMatrices.createOrthogonal(numRows,numRows,rand); DenseMatrix64F V = RandomMatrices.createOrthogonal(numCols,numCols,rand); DenseMatrix64F S = new DenseMatrix64F(numRows,numCols); int min = Math.min(numRows,numCols); min = Math.min(min,sv.length); for( int i = 0; i < min; i++ ) { S.set(i,i,sv[i]); } DenseMatrix64F tmp = new DenseMatrix64F(numRows,numCols); CommonOps.mult(U,S,tmp); CommonOps.multTransB(tmp,V,S); return S; }
/** * <p> * Creates a random matrix which will have the provided singular values. The length 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 rand Random number generator. * @param sv Singular values of the matrix. * @return A new matrix with the specified singular values. */ public static DenseMatrix64F createSingularValues(int numRows, int numCols, Random rand, double ...sv) { DenseMatrix64F U = RandomMatrices.createOrthogonal(numRows,numRows,rand); DenseMatrix64F V = RandomMatrices.createOrthogonal(numCols,numCols,rand); DenseMatrix64F S = new DenseMatrix64F(numRows,numCols); int min = Math.min(numRows,numCols); min = Math.min(min,sv.length); for( int i = 0; i < min; i++ ) { S.set(i,i,sv[i]); } DenseMatrix64F tmp = new DenseMatrix64F(numRows,numCols); CommonOps.mult(U,S,tmp); CommonOps.multTransB(tmp,V,S); return S; }
/** * Creates a new random symmetric matrix that will have the specified real eigenvalues. * * @param num Dimension of the resulting matrix. * @param rand Random number generator. * @param eigenvalues Set of real eigenvalues that the matrix will have. * @return A random matrix with the specified eigenvalues. */ public static DenseMatrix64F createEigenvaluesSymm( int num, Random rand , double ...eigenvalues ) { DenseMatrix64F V = RandomMatrices.createOrthogonal(num,num,rand); DenseMatrix64F D = CommonOps.diag(eigenvalues); DenseMatrix64F temp = new DenseMatrix64F(num,num); CommonOps.mult(V,D,temp); CommonOps.multTransB(temp,V,D); return D; }
/** * Creates a new random symmetric matrix that will have the specified real eigenvalues. * * @param num Dimension of the resulting matrix. * @param rand Random number generator. * @param eigenvalues Set of real eigenvalues that the matrix will have. * @return A random matrix with the specified eigenvalues. */ public static DenseMatrix64F createEigenvaluesSymm( int num, Random rand , double ...eigenvalues ) { DenseMatrix64F V = RandomMatrices.createOrthogonal(num,num,rand); DenseMatrix64F D = CommonOps.diag(eigenvalues); DenseMatrix64F temp = new DenseMatrix64F(num,num); CommonOps.mult(V,D,temp); CommonOps.multTransB(temp,V,D); return D; }
/** * Creates a new random symmetric matrix that will have the specified real eigenvalues. * * @param num Dimension of the resulting matrix. * @param rand Random number generator. * @param eigenvalues Set of real eigenvalues that the matrix will have. * @return A random matrix with the specified eigenvalues. */ public static DenseMatrix64F createEigenvaluesSymm( int num, Random rand , double ...eigenvalues ) { DenseMatrix64F V = RandomMatrices.createOrthogonal(num,num,rand); DenseMatrix64F D = CommonOps.diag(eigenvalues); DenseMatrix64F temp = new DenseMatrix64F(num,num); CommonOps.mult(V,D,temp); CommonOps.multTransB(temp,V,D); return D; }