org.apache.mahout.math.Matrix.aggregate()方法的使用及代码示例

x33g5p2x  于2022-01-25 转载在 其他  
字(9.4k)|赞(0)|评价(0)|浏览(111)

本文整理了Java中org.apache.mahout.math.Matrix.aggregate()方法的一些代码示例,展示了Matrix.aggregate()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Matrix.aggregate()方法的具体详情如下:
包路径:org.apache.mahout.math.Matrix
类名称:Matrix
方法名:aggregate

Matrix.aggregate介绍

[英]Collects the results of a function applied to each element of a matrix and then aggregated.
[中]收集应用于矩阵每个元素的函数结果,然后进行聚合。

代码示例

代码示例来源:origin: apache/mahout

@Test
public void testAggregate() {
 double total = test.aggregate(Functions.PLUS, Functions.IDENTITY);
 assertEquals(test.aggregateRows(new VectorFunction() {
  @Override
  public double apply(Vector v) {
   return v.zSum();
  }
 }).zSum(), total, EPSILON);
}

代码示例来源:origin: apache/mahout

private static void assertEquals(Matrix u1, Matrix u2) {
 assertEquals(0, u1.minus(u2).aggregate(Functions.MAX, Functions.ABS), 1.0e-10);
}

代码示例来源:origin: apache/mahout

private static void assertEquals(Matrix ref, Matrix actual, double epsilon) {
 assertEquals(0, ref.minus(actual).aggregate(Functions.MAX, Functions.ABS), epsilon);
}

代码示例来源:origin: apache/mahout

private static void check(String msg, Matrix a, Matrix b) {
 Assert.assertEquals(msg, 0, a.minus(b).aggregate(Functions.PLUS, Functions.ABS), 1.0e-10);
}

代码示例来源:origin: apache/mahout

private static void assertEquals(Matrix ref, Matrix actual, double epsilon) {
 assertEquals(0, ref.minus(actual).aggregate(Functions.MAX, Functions.ABS), epsilon);
}

代码示例来源:origin: apache/mahout

@Test
public void testFunctionalView() {
 Matrix m = Matrices.functionalMatrixView(5, 6, new IntIntFunction() {
  @Override
  public double apply(int row, int col) {
   assertTrue(row < 5);
   assertTrue(col < 6);
   return row + col;
  }
 });
 // row-wise sums are 15, 15+ 6, 15 +12, 15+18, 15+24
 // so total sum is 1/2*(15+15+24)*5 =27*5 = 135
 assertEquals(135, m.aggregate(Functions.PLUS, Functions.IDENTITY), 1e-10);
}

代码示例来源:origin: apache/mahout

@Test
public void testTransposeView() {
 Matrix m = Matrices.gaussianView(5, 6, 1234L);
 Matrix controlM = new DenseMatrix(5, 6).assign(m);
 System.out.printf("M=\n%s\n", m);
 System.out.printf("controlM=\n%s\n", controlM);
 Matrix mtm = Matrices.transposedView(m).times(m);
 Matrix controlMtm = controlM.transpose().times(controlM);
 System.out.printf("M'M=\n%s\n", mtm);
 Matrix diff = mtm.minus(controlMtm);
 assertEquals(0, diff.aggregate(Functions.PLUS, Functions.ABS), 1e-10);
}

代码示例来源:origin: apache/mahout

@Test
public void testGaussianView() {
 Matrix m1 = Matrices.gaussianView(5, 6, 1234);
 Matrix m2 = Matrices.gaussianView(5, 6, 1234);
 Matrix diff = m1.minus(m2);
 assertEquals(0, diff.aggregate(Functions.PLUS, Functions.ABS), 1e-10);
}

代码示例来源:origin: apache/mahout

@Test
public void testSvdHang() throws IOException, InterruptedException, ExecutionException, TimeoutException {
 System.out.printf("starting hanging-svd\n");
 final Matrix m = readTsv("hanging-svd.tsv");
 SingularValueDecomposition svd = new SingularValueDecomposition(m);
 assertEquals(0, m.minus(svd.getU().times(svd.getS()).times(svd.getV().transpose())).aggregate(Functions.PLUS, Functions.ABS), 1e-10);
 System.out.printf("No hang\n");
}

代码示例来源:origin: apache/mahout

@Test
public void testBasics() {
 Matrix a = new DenseSymmetricMatrix(new double[]{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, false);
 System.out.println(a.toString());
 assertEquals(0, a.viewDiagonal().minus(new DenseVector(new double[]{1, 5, 8, 10})).norm(1), 1.0e-10);
 assertEquals(0, a.viewPart(0, 3, 1, 3).viewDiagonal().minus(
   new DenseVector(new double[]{2, 6, 9})).norm(1), 1.0e-10);
 assertEquals(4, a.get(0, 3), 1.0e-10);
 System.out.println(a);
 Matrix m = new DenseMatrix(4, 4).assign(a);
 assertEquals(0, m.minus(a).aggregate(Functions.PLUS, Functions.ABS), 1.0e-10);
 System.out.println(m);
 assertEquals(0, m.transpose().times(m).minus(a.transpose().times(a)).aggregate(
   Functions.PLUS, Functions.ABS), 1.0e-10);
 System.out.println(a.plus(a));
 assertEquals(0, m.plus(m).minus(a.plus(a)).aggregate(Functions.PLUS, Functions.ABS), 1.0e-10);
}

代码示例来源:origin: apache/mahout

@Test
public void testEigen() {
 Matrix a = new DenseSymmetricMatrix(new double[]{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, false);
 Matrix b = new DenseMatrix(a.numRows(), a.numCols());
 b.assign(a);
 assertEquals(0, a.minus(b).aggregate(Functions.PLUS, Functions.ABS), 1.0e-10);
 EigenDecomposition edA = new EigenDecomposition(a);
 EigenDecomposition edB = new EigenDecomposition(b);
 System.out.println(edA.getV());
 assertEquals(0, edA.getV().minus(edB.getV()).aggregate(Functions.PLUS, Functions.ABS), 1.0e-10);
 assertEquals(0, edA.getRealEigenvalues().minus(edA.getRealEigenvalues()).aggregate(Functions.PLUS, Functions.ABS), 1.0e-10);
}

代码示例来源:origin: apache/mahout

@Test(timeout=50000)
 public void testTimesCorrect() {
  Random raw = RandomUtils.getRandom();

  // build two large sequential sparse matrices and multiply them
  Matrix x = new SparseRowMatrix(100, 2000, false)
   .assign(Functions.random());

  Matrix y = new SparseRowMatrix(2000, 100, false)
   .assign(Functions.random());

  Matrix xd = new DenseMatrix(100, 2000).assign(x);
  Matrix yd = new DenseMatrix(2000, 100).assign(y);
  assertEquals(0, xd.times(yd).minus(x.times(y)).aggregate(Functions.PLUS, Functions.ABS), 1e-15);
  assertEquals(0, x.times(yd).minus(x.times(y)).aggregate(Functions.PLUS, Functions.ABS), 1e-15);
  assertEquals(0, xd.times(y).minus(x.times(y)).aggregate(Functions.PLUS, Functions.ABS), 1e-15);
 }
}

代码示例来源:origin: apache/mahout

@Test
public void test2() {
 // Test matrix from Nicholas Higham's paper at http://eprints.ma.man.ac.uk/1199/01/covered/MIMS_ep2008_116.pdf
 double[][] values = new double[3][];
 values[0] = new double[]{1, -1, 1};
 values[1] = new double[]{-1, 1, -1};
 values[2] = new double[]{1, -1, 2};
 Matrix A = new DenseMatrix(values);
 // without pivoting
 CholeskyDecomposition cd = new CholeskyDecomposition(A, false);
 assertEquals(0, cd.getL().times(cd.getL().transpose()).minus(A).aggregate(Functions.PLUS, Functions.ABS), 1.0e-10);
 // with pivoting
 cd = new CholeskyDecomposition(A);
 assertEquals(0, cd.getL().times(cd.getL().transpose()).minus(A).aggregate(Functions.PLUS, Functions.ABS), 1.0e-10);
}

代码示例来源:origin: apache/mahout

@Test
public void testRankDeficient() {
 Matrix A = rank4Matrix();
 CholeskyDecomposition cd = new CholeskyDecomposition(A);
 PivotedMatrix Ax = new PivotedMatrix(A, cd.getPivot());
 CholeskyDecomposition cd2 = new CholeskyDecomposition(Ax, false);
 assertEquals(0, cd2.getL().times(cd2.getL().transpose()).minus(Ax).aggregate(Functions.PLUS, Functions.ABS), 1.0e-10);
 assertEquals(0, cd.getL().times(cd.getL().transpose()).minus(A).aggregate(Functions.PLUS, Functions.ABS), 1.0e-10);
 Assert.assertFalse(cd.isPositiveDefinite());
 Matrix L = cd.getL();
 Matrix Abar = L.times(L.transpose());
 double error = A.minus(Abar).aggregate(Functions.MAX, Functions.ABS);
 Assert.assertEquals(0, error, 1.0e-10);
}

代码示例来源:origin: apache/mahout

@Test
public void testLeftVectors() {
 Matrix A = lowRankMatrix();
 SequentialBigSvd s = new SequentialBigSvd(A, 8);
 SingularValueDecomposition svd = new SingularValueDecomposition(A);
 // can only check first few singular vectors because once the singular values
 // go to zero, the singular vectors are not uniquely determined
 Matrix u1 = svd.getU().viewPart(0, 20, 0, 4).assign(Functions.ABS);
 Matrix u2 = s.getU().viewPart(0, 20, 0, 4).assign(Functions.ABS);
 assertEquals(0, u1.minus(u2).aggregate(Functions.PLUS, Functions.ABS), 1.0e-9);
}

代码示例来源:origin: apache/mahout

@Test
public void testSymmetricUniformView() {
 Matrix m1 = Matrices.symmetricUniformView(5, 6, 1234);
 Matrix m2 = Matrices.symmetricUniformView(5, 6, 1234);
 for (int row = 0; row < m1.numRows(); row++) {
  for (int col = 0; col < m1.numCols(); col++) {
   assertTrue(m1.getQuick(row, col) >= -1.0);
   assertTrue(m1.getQuick(row, col) < 1.0);
  }
 }
 Matrix diff = m1.minus(m2);
 assertEquals(0, diff.aggregate(Functions.PLUS, Functions.ABS), 1e-10);
}

代码示例来源:origin: apache/mahout

@Test
public void testUniformView() {
 Matrix m1 = Matrices.uniformView(5, 6, 1234);
 Matrix m2 = Matrices.uniformView(5, 6, 1234);
 for (int row = 0; row < m1.numRows(); row++) {
  for (int col = 0; col < m1.numCols(); col++) {
   assertTrue(m1.getQuick(row, col) >= 0.0);
   assertTrue(m1.getQuick(row, col) < 1.0);
  }
 }
 Matrix diff = m1.minus(m2);
 assertEquals(0, diff.aggregate(Functions.PLUS, Functions.ABS), 1e-10);
}

代码示例来源:origin: apache/mahout

@Test
public void testBasics() {
 Matrix a = new UpperTriangular(new double[]{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, false);
 assertEquals(0, a.viewDiagonal().minus(new DenseVector(new double[]{1, 5, 8, 10})).norm(1), 1.0e-10);
 assertEquals(0, a.viewPart(0, 3, 1, 3).viewDiagonal().minus(
   new DenseVector(new double[]{2, 6, 9})).norm(1), 1.0e-10);
 assertEquals(4, a.get(0, 3), 1.0e-10);
 print(a);
 Matrix m = new DenseMatrix(4, 4).assign(a);
 assertEquals(0, m.minus(a).aggregate(Functions.PLUS, Functions.ABS), 1.0e-10);
 print(m);
 assertEquals(0, m.transpose().times(m).minus(a.transpose().times(a)).aggregate(
   Functions.PLUS, Functions.ABS), 1.0e-10);
 assertEquals(0, m.plus(m).minus(a.plus(a)).aggregate(Functions.PLUS, Functions.ABS), 1.0e-10);
}

代码示例来源:origin: apache/mahout

@Test
public void randomMatrix() {
 Matrix a = new DenseMatrix(60, 60).assign(Functions.random());
 QRDecomposition qr = new QRDecomposition(a);
 // how close is Q to actually being orthornormal?
 double maxIdent = qr.getQ().transpose().times(qr.getQ()).viewDiagonal().assign(Functions.plus(-1)).norm(1);
 assertEquals(0, maxIdent, 1.0e-13);
 // how close is Q R to the original value of A?
 Matrix z = qr.getQ().times(qr.getR()).minus(a);
 double maxError = z.aggregate(Functions.MIN, Functions.ABS);
 assertEquals(0, maxError, 1.0e-13);
}

代码示例来源:origin: apache/mahout

@Test
public void rank1() {
 Matrix x = new DenseMatrix(3, 3);
 x.viewRow(0).assign(new double[]{1, 2, 3});
 x.viewRow(1).assign(new double[]{2, 4, 6});
 x.viewRow(2).assign(new double[]{3, 6, 9});
 CholeskyDecomposition rr = new CholeskyDecomposition(x.transpose().times(x), false);
 assertEquals(0, new DenseVector(new double[]{3.741657, 7.483315, 11.22497}).aggregate(rr.getL().transpose().viewRow(0), Functions.PLUS, new DoubleDoubleFunction() {
  @Override
  public double apply(double arg1, double arg2) {
   return Math.abs(arg1) - Math.abs(arg2);
  }
 }), 1.0e-5);
 assertEquals(0, rr.getL().viewPart(0, 3, 1, 2).aggregate(Functions.PLUS, Functions.ABS), 1.0e-9);
}

相关文章