private static QuadraticProgram createRandomQuadraticProgram(Random random, int objectiveSize, int solutionSize, int constraintSize) { DenseMatrix64F a = RandomMatrices.createRandom(objectiveSize, solutionSize, random); DenseMatrix64F b = RandomMatrices.createRandom(objectiveSize, 1, random); DenseMatrix64F c = RandomMatrices.createRandom(constraintSize, solutionSize, random); DenseMatrix64F d = RandomMatrices.createRandom(constraintSize, 1, random); return new QuadraticProgram(a, b, c, d); } }
/** * <p> * Sets each element in the matrix to a value drawn from an uniform distribution from 0 to 1 inclusive. * </p> * * @param mat The matrix who is to be randomized. Modified. * @param rand Random number generator used to fill the matrix. */ public static void setRandom( DenseMatrix64F mat , Random rand ) { setRandom(mat,0,1,rand); }
/** * 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); }
@ContinuousIntegrationTest(estimatedDuration = 0.0) @Test(timeout = 30000) public void testRemoveNullspaceComponent2() { int matrixSize = 6; boolean makeLargestComponentPositive = true; SVDNullspaceCalculator nullspaceCalculator = new SVDNullspaceCalculator(matrixSize, makeLargestComponentPositive); Random random = new Random(); double[] singularValues = RandomNumbers.nextDoubleArray(random, matrixSize, 1.0, 2.0); singularValues[matrixSize / 2] = 0.0; DenseMatrix64F matrix1 = RandomMatrices.createSingularValues(matrixSize, matrixSize, random, singularValues); nullspaceCalculator.setMatrix(matrix1, 1); DenseMatrix64F nullspace = nullspaceCalculator.getNullspace(); DenseMatrix64F matrix2 = RandomMatrices.createRandom(matrixSize, matrixSize, random); nullspaceCalculator.removeNullspaceComponent(matrix2); assertTrue(isNullspaceComponentZero(matrix2, nullspace)); }
@ContinuousIntegrationTest(estimatedDuration = 0.1) @Test(timeout = 30000) public void testCompareDifferentImplementations() { int numberOfStates = 30; int numberOfInputs = 10; StateSpaceSystemDiscretizer singleMatrixExponentialDiscretizer = new SingleMatrixExponentialStateSpaceSystemDiscretizer(numberOfStates, numberOfInputs); StateSpaceSystemDiscretizer splitUpMatrixExponentialDiscretizer = new SplitUpMatrixExponentialStateSpaceSystemDiscretizer(numberOfStates, numberOfInputs); StateSpaceSystemDiscretizer[] discretizers = new StateSpaceSystemDiscretizer[] { singleMatrixExponentialDiscretizer, splitUpMatrixExponentialDiscretizer }; Random random = new Random(125L); DenseMatrix64F A = RandomMatrices.createRandom(numberOfStates, numberOfStates, random); DenseMatrix64F B = RandomMatrices.createRandom(numberOfStates, numberOfInputs, random); DenseMatrix64F Q = RandomMatrices.createSymmPosDef(numberOfStates, random); DenseMatrix64F[] As = new DenseMatrix64F[] { new DenseMatrix64F(A), new DenseMatrix64F(A) }; DenseMatrix64F[] Bs = new DenseMatrix64F[] { new DenseMatrix64F(B), new DenseMatrix64F(B) }; DenseMatrix64F[] Qs = new DenseMatrix64F[] { new DenseMatrix64F(Q), new DenseMatrix64F(Q) }; double dt = 1.0; for (int i = 0; i < discretizers.length; i++) { discretizers[i].discretize(As[i], Bs[i], Qs[i], dt); } double tol = 1e-12; EjmlUnitTests.assertEquals(As[0], As[1], tol); EjmlUnitTests.assertEquals(Bs[0], Bs[1], tol); EjmlUnitTests.assertEquals(Qs[0], Qs[1], tol); }
/** * Creates a random diagonal matrix where the diagonal elements are selected from a uniform * distribution that goes from min to max. * * @param N Dimension of the matrix. * @param min Minimum value of a diagonal element. * @param max Maximum value of a diagonal element. * @param rand Random number generator. * @return A random diagonal matrix. */ public static DenseMatrix64F createDiagonal( int N , double min , double max , Random rand ) { return createDiagonal(N,N,min,max,rand); }
/** * Creates a random symmetric matrix whose values are selected from an uniform distribution * from min to max, inclusive. * * @param length Width and height of the matrix. * @param min Minimum value an element can have. * @param max Maximum value an element can have. * @param rand Random number generator. * @return A symmetric matrix. */ public static DenseMatrix64F createSymmetric(int length, double min, double max, Random rand) { DenseMatrix64F A = new DenseMatrix64F(length,length); createSymmetric(A,min,max,rand); return A; }
/** * <p> * Creates a random orthogonal or isometric matrix, depending on the number of rows and columns. * The number of rows must be more than or equal to the number of columns. * </p> * * @param numRows Number of rows in the generated matrix. * @param numCols Number of columns in the generated matrix. * @param rand Random number generator used to create matrices. * @return A new isometric matrix. */ public static DenseMatrix64F createOrthogonal( int numRows , int numCols , Random rand ) { if( numRows < numCols ) { throw new IllegalArgumentException("The number of rows must be more than or equal to the number of columns"); } DenseMatrix64F u[] = createSpan(numRows,numCols,rand); DenseMatrix64F ret = new DenseMatrix64F(numRows,numCols); for( int i = 0; i < numCols; i++ ) { SubmatrixOps.setSubMatrix(u[i], ret, 0, 0, 0, i, numRows, 1); } return ret; }
/** * Creates a random diagonal matrix where the diagonal elements are selected from a uniform * distribution that goes from min to max. * * @param N Dimension of the matrix. * @param min Minimum value of a diagonal element. * @param max Maximum value of a diagonal element. * @param rand Random number generator. * @return A random diagonal matrix. */ public static DenseMatrix64F createDiagonal( int N , double min , double max , Random rand ) { return createDiagonal(N,N,min,max,rand); }
/** * Creates a random symmetric matrix whose values are selected from an uniform distribution * from min to max, inclusive. * * @param length Width and height of the matrix. * @param min Minimum value an element can have. * @param max Maximum value an element can have. * @param rand Random number generator. * @return A symmetric matrix. */ public static DenseMatrix64F createSymmetric(int length, double min, double max, Random rand) { DenseMatrix64F A = new DenseMatrix64F(length,length); createSymmetric(A,min,max,rand); return A; }
/** * <p> * Creates a random orthogonal or isometric matrix, depending on the number of rows and columns. * The number of rows must be more than or equal to the number of columns. * </p> * * @param numRows Number of rows in the generated matrix. * @param numCols Number of columns in the generated matrix. * @param rand Random number generator used to create matrices. * @return A new isometric matrix. */ public static DenseMatrix64F createOrthogonal( int numRows , int numCols , Random rand ) { if( numRows < numCols ) { throw new IllegalArgumentException("The number of rows must be more than or equal to the number of columns"); } DenseMatrix64F u[] = createSpan(numRows,numCols,rand); DenseMatrix64F ret = new DenseMatrix64F(numRows,numCols); for( int i = 0; i < numCols; i++ ) { SubmatrixOps.setSubMatrix(u[i],ret,0,0,0,i,numRows,1); } return ret; }
public static void main(String[] args) { DenseMatrix64F m = RandomMatrices.createRandom(5,5,0,1,new Random(234)); List<String> labels = new ArrayList<>(); for (int i = 0; i < m.numRows; i++) { labels.add("Label "+i); } ConfusionMatrixPanel confusion = new ConfusionMatrixPanel(m,labels,300,false); confusion.setHighlightCategory(2); ShowImages.showWindow(confusion,"Window",true); }
/** * <p> * Sets each element in the matrix to a value drawn from an uniform distribution from 0 to 1 inclusive. * </p> * * @param mat The matrix who is to be randomized. Modified. * @param rand Random number generator used to fill the matrix. */ public static void setRandom( DenseMatrix64F mat , Random rand ) { setRandom(mat,0,1,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; }
/** * Creates a random diagonal matrix where the diagonal elements are selected from a uniform * distribution that goes from min to max. * * @param N Dimension of the matrix. * @param min Minimum value of a diagonal element. * @param max Maximum value of a diagonal element. * @param rand Random number generator. * @return A random diagonal matrix. */ public static DenseMatrix64F createDiagonal( int N , double min , double max , Random rand ) { return createDiagonal(N,N,min,max,rand); }
/** * Creates a random symmetric matrix whose values are selected from an uniform distribution * from min to max, inclusive. * * @param length Width and height of the matrix. * @param min Minimum value an element can have. * @param max Maximum value an element can have. * @param rand Random number generator. * @return A symmetric matrix. */ public static DenseMatrix64F createSymmetric(int length, double min, double max, Random rand) { DenseMatrix64F A = new DenseMatrix64F(length,length); createSymmetric(A,min,max,rand); return A; }
/** * <p> * Creates a random orthogonal or isometric matrix, depending on the number of rows and columns. * The number of rows must be more than or equal to the number of columns. * </p> * * @param numRows Number of rows in the generated matrix. * @param numCols Number of columns in the generated matrix. * @param rand Random number generator used to create matrices. * @return A new isometric matrix. */ public static DenseMatrix64F createOrthogonal( int numRows , int numCols , Random rand ) { if( numRows < numCols ) { throw new IllegalArgumentException("The number of rows must be more than or equal to the number of columns"); } DenseMatrix64F u[] = createSpan(numRows,numCols,rand); DenseMatrix64F ret = new DenseMatrix64F(numRows,numCols); for( int i = 0; i < numCols; i++ ) { SubmatrixOps.setSubMatrix(u[i],ret,0,0,0,i,numRows,1); } return ret; }
return RandomMatrices.createRandom(20, 20, random);
/** * <p> * Sets each element in the matrix to a value drawn from an uniform distribution from 0 to 1 inclusive. * </p> * * @param mat The matrix who is to be randomized. Modified. * @param rand Random number generator used to fill the matrix. */ public static void setRandom( DenseMatrix64F mat , Random rand ) { setRandom(mat,0,1,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; }