本文整理了Java中org.apache.mahout.math.Vector.getDistanceSquared()
方法的一些代码示例,展示了Vector.getDistanceSquared()
的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Vector.getDistanceSquared()
方法的具体详情如下:
包路径:org.apache.mahout.math.Vector
类名称:Vector
方法名:getDistanceSquared
[英]Get the square of the distance between this vector and the other vector.
[中]得到这个向量和另一个向量之间距离的平方。
代码示例来源:origin: apache/mahout
@Override
public double getDistanceSquared(Vector v) {
return delegate.getDistanceSquared(v);
}
代码示例来源:origin: apache/mahout
@Override
public double getDistanceSquared(Vector v) {
return delegate.getDistanceSquared(v);
}
代码示例来源: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 testSmallDistances() {
for (double fuzz : new double[]{1.0e-5, 1.0e-6, 1.0e-7, 1.0e-8, 1.0e-9, 1.0e-10}) {
MultiNormal x = new MultiNormal(fuzz, new ConstantVector(0, 20));
for (int i = 0; i < 10000; i++) {
final T v1 = vectorToTest(20);
Vector v2 = v1.plus(x.sample());
if (1 + fuzz * fuzz > 1) {
String msg = String.format("fuzz = %.1g, >", fuzz);
assertTrue(msg, v1.getDistanceSquared(v2) > 0);
assertTrue(msg, v2.getDistanceSquared(v1) > 0);
} else {
String msg = String.format("fuzz = %.1g, >=", fuzz);
assertTrue(msg, v1.getDistanceSquared(v2) >= 0);
assertTrue(msg, v2.getDistanceSquared(v1) >= 0);
}
}
}
}
代码示例来源: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
public void testEarlyStop() {
Matrix a = getA();
Vector b = getB();
ConjugateGradientSolver solver = new ConjugateGradientSolver();
// specifying a looser max error will result in few iterations but less accurate results
Vector x = solver.solve(a, b, null, 10, 0.1);
double distance = Math.sqrt(a.times(x).getDistanceSquared(b));
assertTrue(distance > EPSILON);
assertEquals(0.0, distance, 0.1); // should be equal to within the error specified
assertEquals(7, solver.getIterations()); // should have taken fewer iterations
// can get a similar effect by bounding the number of iterations
x = solver.solve(a, b, null, 7, ConjugateGradientSolver.DEFAULT_MAX_ERROR);
distance = Math.sqrt(a.times(x).getDistanceSquared(b));
assertTrue(distance > EPSILON);
assertEquals(0.0, distance, 0.1);
assertEquals(7, solver.getIterations());
}
代码示例来源:origin: apache/mahout
@Test
public void testConjugateGradientSolver() {
Matrix a = getA();
Vector b = getB();
ConjugateGradientSolver solver = new ConjugateGradientSolver();
Vector x = solver.solve(a, b);
assertEquals(0.0, Math.sqrt(a.times(x).getDistanceSquared(b)), EPSILON);
assertEquals(0.0, solver.getResidualNorm(), ConjugateGradientSolver.DEFAULT_MAX_ERROR);
assertEquals(10, solver.getIterations());
}
代码示例来源:origin: apache/mahout
@Test
public void testConditionedConjugateGradientSolver() {
Matrix a = getIllConditionedMatrix();
Vector b = getB();
Preconditioner conditioner = new JacobiConditioner(a);
ConjugateGradientSolver solver = new ConjugateGradientSolver();
Vector x = solver.solve(a, b, null, 100, ConjugateGradientSolver.DEFAULT_MAX_ERROR);
double distance = Math.sqrt(a.times(x).getDistanceSquared(b));
assertEquals(0.0, distance, EPSILON);
assertEquals(0.0, solver.getResidualNorm(), ConjugateGradientSolver.DEFAULT_MAX_ERROR);
assertEquals(16, solver.getIterations());
Vector x2 = solver.solve(a, b, conditioner, 100, ConjugateGradientSolver.DEFAULT_MAX_ERROR);
// the Jacobi preconditioner isn't very good, but it does result in one less iteration to converge
distance = Math.sqrt(a.times(x2).getDistanceSquared(b));
assertEquals(0.0, distance, EPSILON);
assertEquals(0.0, solver.getResidualNorm(), ConjugateGradientSolver.DEFAULT_MAX_ERROR);
assertEquals(15, solver.getIterations());
}
代码示例来源:origin: apache/mahout
assertEquals(0, dv1.plus(dv2).getDistanceSquared(v1.plus(v2)), FUZZ);
assertEquals(0, dv1.plus(dv2).getDistanceSquared(v1.plus(dv2)), FUZZ);
assertEquals(0, dv1.plus(dv2).getDistanceSquared(v1.plus(sv2)), FUZZ);
assertEquals(0, dv1.plus(dv2).getDistanceSquared(sv1.plus(v2)), FUZZ);
assertEquals(0, dv1.times(dv2).getDistanceSquared(v1.times(v2)), FUZZ);
assertEquals(0, dv1.times(dv2).getDistanceSquared(v1.times(dv2)), FUZZ);
assertEquals(0, dv1.times(dv2).getDistanceSquared(v1.times(sv2)), FUZZ);
assertEquals(0, dv1.times(dv2).getDistanceSquared(sv1.times(v2)), FUZZ);
assertEquals(0, dv1.minus(dv2).getDistanceSquared(v1.minus(v2)), FUZZ);
assertEquals(0, dv1.minus(dv2).getDistanceSquared(v1.minus(dv2)), FUZZ);
assertEquals(0, dv1.minus(dv2).getDistanceSquared(v1.minus(sv2)), FUZZ);
assertEquals(0, dv1.minus(dv2).getDistanceSquared(sv1.minus(v2)), FUZZ);
assertEquals(0, dv1.divide(z).getDistanceSquared(v1.divide(z)), 1.0e-12);
assertEquals(0, dv1.times(z).getDistanceSquared(v1.times(z)), 1.0e-12);
assertEquals(0, dv1.plus(z).getDistanceSquared(v1.plus(z)), 1.0e-12);
assertEquals(dv1.getDistanceSquared(dv2), v1.getDistanceSquared(v2), FUZZ);
assertEquals(dv1.getDistanceSquared(dv2), dv1.getDistanceSquared(v2), FUZZ);
assertEquals(dv1.getDistanceSquared(dv2), sv1.getDistanceSquared(v2), FUZZ);
assertEquals(dv1.getDistanceSquared(dv2), v1.getDistanceSquared(dv2), FUZZ);
assertEquals(dv1.getDistanceSquared(dv2), v1.getDistanceSquared(sv2), FUZZ);
assertEquals(dv1.getDistanceSquared(dv2), v1.getDistanceSquared(v2), FUZZ);
assertEquals(dv1.getDistanceSquared(dv2), dv1.getDistanceSquared(v2), FUZZ);
assertEquals(dv1.getDistanceSquared(dv2), sv1.getDistanceSquared(v2), FUZZ);
assertEquals(dv1.getDistanceSquared(dv2), v1.getDistanceSquared(dv2), FUZZ);
代码示例来源:origin: org.apache.mahout/mahout-math
@Override
public double getDistanceSquared(Vector v) {
return delegate.getDistanceSquared(v);
}
代码示例来源:origin: org.apache.mahout/mahout-mr
@Override
public double distance(Vector v1, Vector v2) {
return v2.getDistanceSquared(v1);
}
代码示例来源:origin: cloudera/mahout
@Override
public double getDistanceSquared(Vector v) {
return delegate.getDistanceSquared(v);
}
}
代码示例来源:origin: org.apache.mahout/mahout-core
@Override
public double distance(Vector v1, Vector v2) {
return v2.getDistanceSquared(v1);
}
代码示例来源:origin: org.apache.mahout/mahout-math
@Override
public double getDistanceSquared(Vector v) {
return delegate.getDistanceSquared(v);
}
代码示例来源:origin: org.apache.mahout/mahout-mrlegacy
@Override
public double distance(Vector v1, Vector v2) {
return v2.getDistanceSquared(v1);
}
代码示例来源:origin: org.apache.mahout/mahout-mrlegacy
@Test
public void testSolver() throws Exception {
File testData = getTestTempDir("testdata");
DistributedRowMatrix matrix = new TestDistributedRowMatrix().randomDistributedMatrix(
10, 10, 10, 10, 10.0, true, testData.getAbsolutePath());
matrix.setConf(getConfiguration());
Vector vector = randomVector(matrix.numCols(), 10.0);
DistributedConjugateGradientSolver solver = new DistributedConjugateGradientSolver();
Vector x = solver.solve(matrix, vector);
Vector solvedVector = matrix.times(x);
double distance = Math.sqrt(vector.getDistanceSquared(solvedVector));
assertEquals(0.0, distance, EPSILON);
}
}
代码示例来源:origin: cloudera/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: cloudera/mahout
@Test
public void testConjugateGradientSolver() {
Matrix a = getA();
Vector b = getB();
ConjugateGradientSolver solver = new ConjugateGradientSolver();
Vector x = solver.solve(a, b);
assertEquals(0.0, Math.sqrt(a.times(x).getDistanceSquared(b)), EPSILON);
assertEquals(0.0, solver.getResidualNorm(), ConjugateGradientSolver.DEFAULT_MAX_ERROR);
assertEquals(10, solver.getIterations());
}
代码示例来源:origin: org.apache.mahout/mahout-mrlegacy
@Test
public void testMatrixTimesVector() throws Exception {
Vector v = new RandomAccessSparseVector(50);
v.assign(1.0);
Matrix m = SolverTest.randomSequentialAccessSparseMatrix(100, 90, 50, 20, 1.0);
DistributedRowMatrix dm = randomDistributedMatrix(100, 90, 50, 20, 1.0, false);
dm.setConf(getConfiguration());
Vector expected = m.times(v);
Vector actual = dm.times(v);
assertEquals(0.0, expected.getDistanceSquared(actual), EPSILON);
}
代码示例来源:origin: org.apache.mahout/mahout-mrlegacy
@Test
public void testMatrixTimesSquaredVector() throws Exception {
Vector v = new RandomAccessSparseVector(50);
v.assign(1.0);
Matrix m = SolverTest.randomSequentialAccessSparseMatrix(100, 90, 50, 20, 1.0);
DistributedRowMatrix dm = randomDistributedMatrix(100, 90, 50, 20, 1.0, false);
dm.setConf(getConfiguration());
Vector expected = m.timesSquared(v);
Vector actual = dm.timesSquared(v);
assertEquals(0.0, expected.getDistanceSquared(actual), 1.0e-9);
}
内容来源于网络,如有侵权,请联系作者删除!