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

x33g5p2x  于2022-02-01 转载在 其他  
字(10.4k)|赞(0)|评价(0)|浏览(116)

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

Vector.minus介绍

[英]Return a new vector containing the element by element difference of the recipient and the argument
[中]返回一个新向量,该向量包含收件人和参数的元素差

代码示例

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

@Override
public Vector minus(Vector x) {
 return delegate.minus(x);
}

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

@Override
public Vector minus(Vector that) {
 return delegate.minus(that);
}

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

private Vector localVOrtho(Vector v) {
 for (Vector old : localV) {
  if (old != null) {
   double x = v.dot(old);
   v = v.minus(old.times(x));
  }
 }
 return v;
}

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

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

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

private static void doTestGetDistanceSquared(Vector v, Vector w) {
 double expected = v.minus(w).getLengthSquared();
 assertEquals(expected, v.getDistanceSquared(w), 1.0e-6);
}

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

@Test
 public void testSetData() throws IOException {

  File f = File.createTempFile("matrix", ".m", getTestTempDir());
  f.deleteOnExit();

  Random gen = RandomUtils.getRandom();
  Matrix m0 = new SparseRowMatrix(10, 21);
  for (MatrixSlice row : m0) {
   int len = (int) Math.ceil(-15 * Math.log(1 - gen.nextDouble()));
   for (int i = 0; i < len; i++) {
    row.vector().set(gen.nextInt(21), 1);
   }
  }
  FileBasedSparseBinaryMatrix.writeMatrix(f, m0);

  FileBasedSparseBinaryMatrix m = new FileBasedSparseBinaryMatrix(10, 21);
  m.setData(f);

  for (MatrixSlice row : m) {
   Vector diff = row.vector().minus(m0.viewRow(row.index()));
   double error = diff.norm(1);
   if (error > 1.0e-14) {
    System.out.printf("%s\n", diff);
   }
   assertEquals(0, error, 1.0e-14);
  }
 }
}

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

@Test
public void testMinus() throws Exception {
 Vector val = test.minus(test);
 assertEquals("size", 3, val.size());
 for (int i = 0; i < test.size(); i++) {
  assertEquals("get [" + i + ']', 0.0, val.get(i), EPSILON);
 }
}

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

@Test
public void testMinus() {
 Vector val = test.minus(test);
 assertEquals("size", test.size(), val.size());
 for (int i = 0; i < test.size(); i++) {
  assertEquals("get [" + i + ']', 0.0, val.get(i), EPSILON);
 }
 val = test.minus(test).minus(test);
 assertEquals("cardinality", test.size(), val.size());
 for (int i = 0; i < test.size(); i++) {
  assertEquals("get [" + i + ']', 0.0, val.get(i) + test.get(i), EPSILON);
 }
 Vector val1 = test.plus(1);
 val = val1.minus(test);
 for (int i = 0; i < test.size(); i++) {
  assertEquals("get [" + i + ']', 1.0, val.get(i), EPSILON);
 }
 val1 = test.plus(-1);
 val = val1.minus(test);
 for (int i = 0; i < test.size(); i++) {
  assertEquals("get [" + i + ']', -1.0, val.get(i), EPSILON);
 }
}

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

@Test
public void testGetDistanceSquared() {
 Vector other = new RandomAccessSparseVector(test.size());
 other.set(1, -2);
 other.set(2, -5);
 other.set(3, -9);
 other.set(4, 1);
 double expected = test.minus(other).getLengthSquared();
 assertTrue("a.getDistanceSquared(b) != a.minus(b).getLengthSquared",
       Math.abs(expected - test.getDistanceSquared(other)) < 10.0E-7);
}

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

@Test(expected = CardinalityException.class)
public void testTimesVector() {
 Vector vectorA = new DenseVector(vectorAValues);
 Vector testTimesVectorA = test.times(vectorA);
 Vector expected = new DenseVector(new double[]{5.0, 11.0, 17.0});
 assertTrue("Matrix times vector not equals: " + vectorA + " != " + testTimesVectorA,
   expected.minus(testTimesVectorA).norm(2) < 1.0e-12);
 test.times(testTimesVectorA);
}

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

@Test
 public void testSwap() {
  Matrix m = new DenseMatrix(10, 10);
  for (int i = 0; i < 10; i++) {
   for (int j = 0; j < 10; j++) {
    m.set(i, j, 10 * i + j);
   }
  }

  PivotedMatrix pm = new PivotedMatrix(m);

  pm.swap(3, 5);

  assertEquals(0, pm.viewDiagonal().minus(
    new DenseVector(new double[]{0, 11, 22, 55, 44, 33, 66, 77, 88, 99})).norm(1), 1.0e-10);

  pm.swap(2, 7);
  assertEquals(0, pm.viewDiagonal().minus(
    new DenseVector(new double[]{0, 11, 77, 55, 44, 33, 66, 22, 88, 99})).norm(1), 1.0e-10);

  pm.swap(5, 8);
  assertEquals(0, pm.viewColumn(4).minus(
    new DenseVector(new double[]{4.0,14.0,74.0,54.0,44.0,84.0,64.0,24.0,34.0,94.0})).norm(1), 1.0e-10);
  assertEquals(0, pm.viewDiagonal().minus(
    new DenseVector(new double[]{0, 11, 77, 55, 44, 88, 66, 22, 33, 99})).norm(1), 1.0e-10);
 }
}

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

@Test
public void testTimesSquaredTimesVector() {
 Vector vectorA = new DenseVector(vectorAValues);
 Vector ttA = test.timesSquared(vectorA);
 Vector ttASlow = test.transpose().times(test.times(vectorA));
 assertTrue("M'Mv != M.timesSquared(v): " + ttA + " != " + ttASlow,
   ttASlow.minus(ttA).norm(2) < 1.0e-12);
}

代码示例来源: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 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

@Test
 public void testSetData() throws IOException {
  File f = File.createTempFile("matrix", ".m", getTestTempDir());
  f.deleteOnExit();

  Matrix m0 = new DenseMatrix(100000, 30);
  MultiNormal gen = new MultiNormal(30);
  for (MatrixSlice row : m0) {
   row.vector().assign(gen.sample());
  }
  FileBasedMatrix.writeMatrix(f, m0);

  FileBasedMatrix m = new FileBasedMatrix(100000, 30);
  m.setData(f, true);

  assertEquals(0, m0.minus(m).aggregate(Functions.MAX, Functions.ABS), 1.0e-8);

  int i = 0;
  for (MatrixSlice row : m) {
   assertEquals(0, row.vector().minus(m0.viewRow(i++)).norm(1), 1.0e-8);
  }
 }
}

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

private static void doTestAggregation(Vector v, Vector w) {
 assertEquals("aggregate(plus, pow(2)) not equal to " + v.getLengthSquared(),
   v.getLengthSquared(),
   v.aggregate(Functions.PLUS, Functions.pow(2)), EPSILON);
 assertEquals("aggregate(plus, abs) not equal to " + v.norm(1),
   v.norm(1),
   v.aggregate(Functions.PLUS, Functions.ABS), EPSILON);
 assertEquals("aggregate(max, abs) not equal to " + v.norm(Double.POSITIVE_INFINITY),
   v.norm(Double.POSITIVE_INFINITY),
   v.aggregate(Functions.MAX, Functions.ABS), EPSILON);
 assertEquals("v.dot(w) != v.aggregate(w, plus, mult)",
   v.dot(w),
   v.aggregate(w, Functions.PLUS, Functions.MULT), EPSILON);
 assertEquals("|(v-w)|^2 != v.aggregate(w, plus, chain(pow(2), minus))",
   v.minus(w).dot(v.minus(w)),
   v.aggregate(w, Functions.PLUS, Functions.chain(Functions.pow(2), Functions.MINUS)), EPSILON);
}

代码示例来源: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
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 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: apache/mahout

@Test
public void testProjection() {
 Vector v1 = new DenseVector(10).assign(Functions.random());
 WeightedVector v2 = new WeightedVector(v1, v1, 31);
 assertEquals(v1.dot(v1), v2.getWeight(), 1.0e-13);
 assertEquals(31, v2.getIndex());
 Matrix y = new DenseMatrix(10, 4).assign(Functions.random());
 Matrix q = new QRDecomposition(y.viewPart(0, 10, 0, 3)).getQ();
 Vector nullSpace = y.viewColumn(3).minus(q.times(q.transpose().times(y.viewColumn(3))));
 WeightedVector v3 = new WeightedVector(q.viewColumn(0).plus(q.viewColumn(1)), nullSpace, 1);
 assertEquals(0, v3.getWeight(), 1.0e-13);
 Vector qx = q.viewColumn(0).plus(q.viewColumn(1)).normalize();
 WeightedVector v4 = new WeightedVector(qx, q.viewColumn(0), 2);
 assertEquals(Math.sqrt(0.5), v4.getWeight(), 1.0e-13);
 WeightedVector v5 = WeightedVector.project(q.viewColumn(0), qx);
 assertEquals(Math.sqrt(0.5), v5.getWeight(), 1.0e-13);
}

相关文章