public class SparseRowRandomInitStrategy implements InitStrategy { MatrixFactory<? extends Matrix> smf = MatrixFactory.getSparseDefault(); private double min; private double max; private Random random; private double sparcity; public SparseRowRandomInitStrategy(double min, double max, double sparcity, Random random) { this.min = min; this.max = max; this.random = random; this.sparcity = sparcity; } @Override public Matrix init(int rows, int cols) { final SparseMatrix rand = (SparseMatrix) smf.createUniformRandom(rows, cols, min, max, random); final Matrix ret = smf.createMatrix(rows, cols); for (int i = 0; i < rows; i++) { if (this.random.nextDouble() > sparcity) { ret.setRow(i, rand.getRow(i)); } } return ret; } }
public class SparseOnesInitStrategy implements InitStrategy { MatrixFactory<? extends Matrix> smf = MatrixFactory.getSparseDefault(); private Random random; private double sparcity; public SparseOnesInitStrategy(double sparcity, Random random) { this.random = random; this.sparcity = sparcity; } @Override public Matrix init(int rows, int cols) { final Matrix ret = smf.createMatrix(rows, cols); for (int i = 0; i < rows; i++) { for (int j = 0; j < cols; j++) { if (this.random.nextDouble() > sparcity) ret.setElement(i, j, 1d); } } return ret; } }
/** Test of getSparseDefault method, of class MatrixFactory. */ public void testGetSparseDefault() { System.out.println("getDefault"); MatrixFactory<? extends Matrix> result = MatrixFactory.getSparseDefault(); assertSame(MatrixFactory.DEFAULT_SPARSE_INSTANCE, result); }