Example #1
0
 public void testColsIndexeddense() {
   DenseMatrix A = new DenseMatrix("1 5 3; 2 8 5; 1 9 4; 2 5 3").t();
   DenseMatrix indexes = new DenseMatrix("2; 1; 1");
   DenseMatrix B = A.cols(indexes);
   System.out.println(B);
   assertTrue(B.equals(new DenseMatrix("1 9 4; 2 8 5; 2 8 5").t()));
 }
Example #2
0
 public void testConstructorByString() {
   DenseMatrix foo = new DenseMatrix("1 3; 5 7; 3 5");
   assertEquals(3, foo.rows);
   assertEquals(2, foo.cols);
   assertTrue(foo.equals(new DenseMatrix(new double[][] {{1, 3}, {5, 7}, {3, 5}})));
   assertEquals(5.0, foo.get(1, 0));
   assertEquals(7.0, foo.get(1, 1));
 }
Example #3
0
 public void testMultBasic() {
   DenseMatrix A = new DenseMatrix(new double[][] {{3, 4}, {5, 6}, {3, 9}});
   DenseMatrix B = new DenseMatrix(new double[][] {{2, 9, 1, 2}, {4, 1, 3, 2}});
   DenseMatrix C = A.mmul(B);
   System.out.println(C);
   DenseMatrix Ccorrect =
       new DenseMatrix(new double[][] {{22, 31, 15, 14}, {34, 51, 23, 22}, {42, 36, 30, 24}});
   assertTrue(C.equals(Ccorrect));
 }
Example #4
0
 public void testldltsolve() {
   int K = thousandconstant;
   DenseMatrix A_ = rand(K, K);
   DenseMatrix A = A_.t().mmul(A_);
   DenseMatrix B = rand(K, K);
   Timer timer = new Timer();
   DenseMatrix X = A.ldltSolve(B);
   timer.printTimeCheckMilliseconds();
   assertTrue(A.mmul(X).equals(B));
 }
Example #5
0
 public void testSvd() {
   DenseMatrix A = rand(5, 8);
   SvdResult result = A.svd();
   assertEquals(A, result.U.mmul(diag(result.S)).mmul(result.V.t()));
   Timer timer = new Timer();
   A = rand(500, thousandconstant);
   timer.printTimeCheckMilliseconds();
   result = A.svd();
   timer.printTimeCheckMilliseconds();
   assertEquals(A, result.U.mmul(diag(result.S)).mmul(result.V.t()));
 }
Example #6
0
 public void testfullpivhouseholderqrsolve() {
   int K = thousandconstant;
   DenseMatrix A = rand(K, K / 2);
   DenseMatrix B = rand(K, K / 3);
   Timer timer = new Timer();
   DenseMatrix X = A.fullPivHouseholderQRSolve(B);
   timer.printTimeCheckMilliseconds();
   DenseMatrix AX = A.mmul(X);
   double diff = AX.sub(B).abs().sum().s();
   assertTrue(diff < 0.01 * K * K / 3);
 }
Example #7
0
 public void testSparseDenseMmul() {
   int K = 60;
   int N = thousandconstant;
   SparseMatrixLil A = SparseMatrixLil.rand(K, N);
   DenseMatrix B = DenseMatrix.rand(N, K);
   Timer timer = new Timer();
   DenseMatrix C = A.mmul(B);
   timer.printTimeCheckMilliseconds();
   DenseMatrix C2 = A.toDense().mmul(B);
   assertTrue(C.equals(C2));
 }
Example #8
0
 public void testOne() {
   DenseMatrix A = ones(3, 3);
   System.out.println(A);
   DenseMatrix B = ones(3, 3);
   System.out.println(B);
   DenseMatrix C = A.mmul(B);
   System.out.println(C);
   assertEquals(3.0, C.get(0, 0));
   assertEquals(3.0, C.get(0, 2));
   assertEquals(3.0, C.get(2, 0));
   assertEquals(3.0, C.get(2, 2));
 }
Example #9
0
 public void testCreateSparse() {
   DenseMatrix A = rand(5, 5);
   SparseMatrixLil B = A.toSparseLil();
   assertTrue(B.equals(B.mmul(eye(5))));
   assertTrue(B.equals(B.t().mmul(eye(5)).t()));
   B = spzeros(2, 2);
   B.append(1, 0, 5);
   B.append(0, 1, 7);
   B.append(0, 0, 11);
   System.out.println(B.toDense());
   System.out.println(B.mmul(eye(2)));
   assertTrue(B.equals(B.mmul(eye(2))));
 }
Example #10
0
  public void testVarOverRows() {
    SparseMatrixLil A = new SparseMatrixLil("1 5 3; 2 8 5");
    DenseMatrix B = meanOverRows(A);
    assertEquals(3, B.cols);
    assertEquals(1, B.rows);
    assertEquals(1.5, B.get(0, 0));
    assertEquals(6.5, B.get(0, 1));

    B = varOverRows(A);
    assertEquals(3, B.cols);
    assertEquals(1, B.rows);
    assertEquals(0.5, B.get(0, 0));
    assertEquals(4.5, B.get(0, 1));
  }
Example #11
0
  public void testVarOverCols() {
    SparseMatrixLil A = new SparseMatrixLil("1 5 3; 2 8 5");
    DenseMatrix B = meanOverCols(A);
    assertEquals(1, B.cols);
    assertEquals(2, B.rows);
    assertEquals(3.0, B.get(0, 0));
    assertEquals(5.0, B.get(1, 0));

    B = varOverCols(A);
    assertEquals(1, B.cols);
    assertEquals(2, B.rows);
    assertEquals(4.0, B.get(0, 0));
    assertEquals(9.0, B.get(1, 0));
  }
Example #12
0
 public void testElements() {
   DenseMatrix A = rand(5, 8);
   DenseMatrix B = rand(5, 8);
   assertTrue(zeros(5, 8).equals(A.sub(A)));
   assertTrue(A.equals(A.add(A).div(2)));
   assertTrue(ones(5, 8).equals(A.div(A)));
   assertTrue(A.equals(A.mul(A).div(A)));
   assertTrue(A.neg().abs().equals(A));
   assertTrue(A.neg().add(A).equals(zeros(5, 8)));
 }
Example #13
0
 public void testTranspose() {
   DenseMatrix A = rand(5, 8);
   DenseMatrix At = A.t();
   assertEquals(5, At.cols);
   assertEquals(8, At.rows);
   assertTrue(A.equals(At.t()));
   SparseMatrixLil B = A.toSparseLil();
   SparseMatrixLil Bt = B.t();
   assertEquals(5, Bt.cols);
   assertEquals(8, Bt.rows);
   assertTrue(At.equals(Bt));
   assertTrue(A.equals(Bt.t()));
 }
Example #14
0
 public void testSum() {
   DenseMatrix A = new DenseMatrix(new double[][] {{1, 9}, {7, 3}});
   assertTrue(new DenseMatrix(new double[][] {{8, 12}}).equals(A.sumOverRows()));
   assertTrue(new DenseMatrix(new double[][] {{10}, {10}}).equals(A.sumOverCols()));
   SparseMatrixLil B = A.toSparseLil();
   assertTrue(new DenseMatrix(new double[][] {{8, 12}}).toSparseLil().equals(B.sumOverRows()));
   assertTrue(new DenseMatrix(new double[][] {{10}, {10}}).toSparseLil().equals(B.sumOverCols()));
   assertTrue(B.sumOverRows().equals(A.sumOverRows()));
   assertTrue(B.sumOverCols().equals(A.sumOverCols()));
   assertEquals(B.sum().sum().s(), A.sum().sum().s());
   SparseMatrixLil C = spzeros(4, 5);
   C.append(1, 2, 5);
   C.append(3, 1, 3);
   C.append(3, 2, 7);
   C.append(1, 4, 11);
   System.out.println(C.sumOverRows());
   assertTrue(new DenseMatrix(new double[][] {{0, 3, 12, 0, 11}}).equals(C.sumOverRows()));
   System.out.println(C.sumOverCols());
   assertTrue(new DenseMatrix(new double[][] {{0}, {16}, {0}, {10}}).equals(C.sumOverCols()));
   assertEquals(26.0, C.sumOverCols().sumOverRows().s());
   assertEquals(26.0, C.sumOverRows().sumOverCols().s());
 }
Example #15
0
 public void testmaxovercols() {
   DenseMatrix A = new DenseMatrix("1 5 3; 2 8 5; 1 9 4; 2 5 3");
   DenseMatrix B = A.maxOverCols();
   assertTrue(B.equals(new DenseMatrix("5; 8; 9; 5")));
 }
Example #16
0
 public void testmaxoverrows() {
   DenseMatrix A = new DenseMatrix("1 5 3; 2 8 5; 1 9 4; 2 5 3");
   DenseMatrix B = A.maxOverRows();
   assertTrue(B.equals(new DenseMatrix("2 9 5")));
 }
Example #17
0
 public void testnonzerocols() {
   DenseMatrix A = new DenseMatrix("1 2 0 3 5 0");
   DenseMatrix B = A.nonZeroCols();
   assertTrue(B.equals(new DenseMatrix("0; 1; 3; 4")));
 }
Example #18
0
 public void testnonzerorows() {
   DenseMatrix A = new DenseMatrix("1; 2; 0; 3; 5; 0");
   DenseMatrix B = A.nonZeroRows();
   assertTrue(B.equals(new DenseMatrix("0; 1; 3; 4")));
 }
Example #19
0
 public static final DenseMatrix varOverRows(DenseMatrix mat) {
   return mat.pow(2).sumOverRows().sub(mat.sumOverRows().pow(2).div(mat.rows)).div(mat.rows - 1);
 }
Example #20
0
 public void testMin() {
   SparseMatrixLil A = speye(5);
   DenseMatrix B = A.minOverCols();
   System.out.println(B);
   assertEquals(5.0, B.sumOverRows().s());
 }
Example #21
0
 public static final DenseMatrix meanOverRows(DenseMatrix mat) {
   return mat.sumOverRows().div(mat.rows);
 }
Example #22
0
 public void testToSparse() {
   DenseMatrix A = rand(5, 8);
   assertTrue(A.equals(A.toSparseLil().toDense()));
 }
Example #23
0
 public void testminovercols() {
   DenseMatrix A = new DenseMatrix("1 5 3; 2 8 5; 1 9 4; 2 5 3");
   DenseMatrix B = A.minOverCols();
   assertTrue(B.equals(new DenseMatrix("1; 2; 1; 2")));
 }
Example #24
0
 public static final DenseMatrix meanOverCols(DenseMatrix mat) {
   return mat.sumOverCols().div(mat.cols);
 }
Example #25
0
 public void testSlice() {
   DenseMatrix A = rand(5, 8);
   DenseMatrix B = rand(5, 12);
   DenseMatrix C = rand(4, 8);
   DenseMatrix AB = A.concatRight(B);
   assertTrue(A.equals(AB.cols(0, 8)));
   assertTrue(B.equals(AB.cols(8, 20)));
   DenseMatrix AC = A.concatDown(C);
   assertTrue(A.equals(AC.rows(0, 5)));
   assertTrue(C.equals(AC.rows(5, 9)));
 }