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

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

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

Matrix.viewDiagonal介绍

[英]Returns a reference to the diagonal of a matrix. Changes to the view will change the original matrix.
[中]返回对矩阵对角线的引用。对视图的更改将更改原始矩阵。

代码示例

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

public DiagonalMatrix(Matrix values) {
 this(values.viewDiagonal());
}

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

/**
 * Return the block diagonal eigenvalue matrix
 *
 * @return D
 */
public Matrix getD() {
 Matrix x = new DenseMatrix(n, n);
 x.assign(0);
 x.viewDiagonal().assign(d);
 for (int i = 0; i < n; i++) {
  double v = e.getQuick(i);
  if (v > 0) {
   x.setQuick(i, i + 1, v);
  } else if (v < 0) {
   x.setQuick(i, i - 1, v);
  }
 }
 return x;
}

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

private static void decompositionSpeedCheck(Decomposer qrf, OnlineSummarizer s1, Matrix a, String label) {
 int n = 0;
 List<Integer> counts = Lists.newArrayList(10, 20, 50, 100, 200, 500);
 for (int k : counts) {
  double warmup = 0;
  double other = 0;
  n += k;
  for (int i = 0; i < k; i++) {
   QR qr = qrf.decompose(a);
   warmup = Math.max(warmup, qr.getQ().transpose().times(qr.getQ()).viewDiagonal().assign(Functions.plus(-1)).norm(1));
   Matrix z = qr.getQ().times(qr.getR()).minus(a);
   other = Math.max(other, z.aggregate(Functions.MIN, Functions.ABS));
  }
  double maxIdent = 0;
  double maxError = 0;
  long t0 = System.nanoTime();
  for (int i = 0; i < n; i++) {
   QR qr = qrf.decompose(a);
   maxIdent = Math.max(maxIdent, qr.getQ().transpose().times(qr.getQ()).viewDiagonal().assign(Functions.plus(-1)).norm(1));
   Matrix z = qr.getQ().times(qr.getR()).minus(a);
   maxError = Math.max(maxError, z.aggregate(Functions.MIN, Functions.ABS));
  }
  long t1 = System.nanoTime();
  if (k > 100) {
   s1.add(t1 - t0);
  }
  System.out.printf("%s %d\t%.1f\t%g\t%g\t%g\n", label, n, (t1 - t0) / 1.0e3 / n, maxIdent, maxError, warmup);
 }
}

代码示例来源: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 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 random() {
  Matrix m = new DenseMatrix(200, 30).assign(Functions.random());

  Vector b = new DenseVector(200).assign(1);

  LSMR r = new LSMR();
  Vector x1 = r.solve(m, b);

//    assertEquals(0, m.times(x1).minus(b).norm(2), 1.0e-2);
  double norm = new SingularValueDecomposition(m).getS().viewDiagonal().norm(2);
  double actual = m.transpose().times(m).times(x1).minus(m.transpose().times(b)).norm(2);
  System.out.printf("%.4f\n", actual / norm * 1.0e6);
  assertEquals(0, actual, norm * 1.0e-5);

  // and we need to check that the error estimates are pretty good.
  assertEquals(m.times(x1).minus(b).norm(2), r.getResidualNorm(), 1.0e-5);
  assertEquals(actual, r.getNormalEquationResidual(), 1.0e-9);
 }

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

v.viewDiagonal().assign(1);

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

@Test
public void testBasics() {
 DiagonalMatrix a = new DiagonalMatrix(new double[]{1, 2, 3, 4});
 assertEquals(0, a.viewDiagonal().minus(new DenseVector(new double[]{1, 2, 3, 4})).norm(1), 1.0e-10);
 assertEquals(0, a.viewPart(0, 3, 0, 3).viewDiagonal().minus(
  new DenseVector(new double[]{1, 2, 3})).norm(1), 1.0e-10);
 assertEquals(4, a.get(3, 3), 1.0e-10);
 Matrix m = new DenseMatrix(4, 4);
 m.assign(a);
 assertEquals(0, m.minus(a).aggregate(Functions.PLUS, Functions.ABS), 1.0e-10);
 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);
 m = new DenseMatrix(new double[][]{{1, 2, 3, 4}, {5, 6, 7, 8}});
 assertEquals(100, a.timesLeft(m).aggregate(Functions.PLUS, Functions.ABS), 1.0e-10);
 assertEquals(100, a.times(m.transpose()).aggregate(Functions.PLUS, Functions.ABS), 1.0e-10);
}

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

public DiagonalMatrix(Matrix values) {
 this(values.viewDiagonal());
}

代码示例来源:origin: org.apache.mahout/mahout-math

public DiagonalMatrix(Matrix values) {
 this(values.viewDiagonal());
}

代码示例来源:origin: org.apache.mahout/mahout-math

/**
 * Return the block diagonal eigenvalue matrix
 *
 * @return D
 */
public Matrix getD() {
 Matrix x = new DenseMatrix(n, n);
 x.assign(0);
 x.viewDiagonal().assign(d);
 for (int i = 0; i < n; i++) {
  double v = e.getQuick(i);
  if (v > 0) {
   x.setQuick(i, i + 1, v);
  } else if (v < 0) {
   x.setQuick(i, i - 1, v);
  }
 }
 return x;
}

代码示例来源:origin: org.apache.mahout/mahout-math

v.viewDiagonal().assign(1);

代码示例来源:origin: org.apache.mahout/mahout-mrlegacy

@Test
public void testInitialization() {
 // Start with super clusterable data.
 List<? extends WeightedVector> data = cubishTestData(0.01);
 // Just do initialization of ball k-means. This should drop a point into each of the clusters.
 BallKMeans r = new BallKMeans(new BruteSearch(new SquaredEuclideanDistanceMeasure()), 6, 20);
 r.cluster(data);
 // Put the centroids into a matrix.
 Matrix x = new DenseMatrix(6, 5);
 int row = 0;
 for (Centroid c : r) {
  x.viewRow(row).assign(c.viewPart(0, 5));
  row++;
 }
 // Verify that each column looks right. Should contain zeros except for a single 6.
 final Vector columnNorms = x.aggregateColumns(new VectorFunction() {
  @Override
  public double apply(Vector f) {
   // Return the sum of three discrepancy measures.
   return Math.abs(f.minValue()) + Math.abs(f.maxValue() - 6) + Math.abs(f.norm(1) - 6);
  }
 });
 // Verify all errors are nearly zero.
 assertEquals(0, columnNorms.norm(1) / columnNorms.size(), 0.1);
 // Verify that the centroids are a permutation of the original ones.
 SingularValueDecomposition svd = new SingularValueDecomposition(x);
 Vector s = svd.getS().viewDiagonal().assign(Functions.div(6));
 assertEquals(5, s.getLengthSquared(), 0.05);
 assertEquals(5, s.norm(1), 0.05);
}

相关文章