public void testCreateMatrixWithInitialValue() {
    Matrix matrix = this.createRandomMatrix();
    int M = matrix.getNumRows();
    int N = matrix.getNumColumns();

    MatrixFactory<?> factory = this.createFactory();
    double initialValue = this.random.nextGaussian();
    Matrix m3 = factory.createMatrix(M, N, initialValue);
    assertNotNull(m3);
    assertEquals(M, m3.getNumRows());
    assertEquals(N, m3.getNumColumns());

    boolean hasNonZero = false;
    for (int i = 0; i < M; i++) {
      for (int j = 0; j < N; j++) {
        double value = m3.getElement(i, j);

        if (value != 0.0) {
          hasNonZero = true;
          assertEquals(initialValue, value, 0.0);
        }
      }
    }

    assertTrue(hasNonZero);
  }
 @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;
 }
 @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;
 }
示例#4
0
 public static Matrix vstack(MatrixFactory<? extends Matrix> matrixFactory, Matrix... matricies) {
   int nrows = 0;
   int ncols = 0;
   for (Matrix matrix : matricies) {
     nrows += matrix.getNumRows();
     ncols = matrix.getNumColumns();
   }
   Matrix ret = matrixFactory.createMatrix(nrows, ncols);
   int currentRow = 0;
   for (Matrix matrix : matricies) {
     ret.setSubMatrix(currentRow, 0, matrix);
     currentRow += matrix.getNumRows();
   }
   return ret;
 }
  /** Test of createMatrix method, of class MatrixFactory. */
  public void testCreateMatrix() {
    System.out.println("createMatrix");

    Matrix matrix = this.createRandomMatrix();
    int M = matrix.getNumRows();
    int N = matrix.getNumColumns();

    MatrixFactory<?> factory = this.createFactory();
    Matrix m2 = factory.createMatrix(M, N);
    assertNotNull(m2);
    assertNotSame(matrix, m2);

    assertEquals(M, m2.getNumRows());
    assertEquals(N, m2.getNumColumns());
    for (int i = 0; i < M; i++) {
      for (int j = 0; j < N; j++) {
        assertEquals(0.0, m2.getElement(i, j));
      }
    }
  }