本文整理了Java中org.apache.mahout.math.Matrix.times()
方法的一些代码示例,展示了Matrix.times()
的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Matrix.times()
方法的具体详情如下:
包路径:org.apache.mahout.math.Matrix
类名称:Matrix
方法名:times
[英]Return a new matrix containing the product of each value of the recipient and the argument
[中]返回一个新矩阵,其中包含接收者的每个值与参数的乘积
代码示例来源:origin: apache/mahout
public Matrix getU() {
// U = (Y inv(R)) U_0
return cd1.solveRight(y).times(svd.getU());
}
代码示例来源: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
public SequentialBigSvd(Matrix A, int p) {
// Y = A * \Omega
y = A.times(new RandomTrinaryMatrix(A.columnSize(), p));
// R'R = Y' Y
cd1 = new CholeskyDecomposition(y.transpose().times(y));
// B = Q" A = (Y R^{-1} )' A
b = cd1.solveRight(y).transpose().times(A);
// L L' = B B'
cd2 = new CholeskyDecomposition(b.times(b.transpose()));
// U_0 D V_0' = L
svd = new SingularValueDecomposition(cd2.getL());
}
代码示例来源:origin: apache/mahout
public Matrix getV() {
// V = (B' inv(L')) V_0
return cd2.solveRight(b.transpose()).times(svd.getV());
}
}
代码示例来源:origin: apache/mahout
private static Matrix createTestMatrix(Random r, int rows, int columns, double[] singularValues) {
Matrix u = createOrthogonalMatrix(r, rows);
Matrix d = createDiagonalMatrix(singularValues, rows, columns);
Matrix v = createOrthogonalMatrix(r, columns);
return u.times(d).times(v);
}
代码示例来源:origin: apache/mahout
@Test(expected = CardinalityException.class)
public void testTimesMatrixCardinality() {
Matrix other = test.like(5, 8);
test.times(other);
}
代码示例来源:origin: apache/mahout
@Test(expected = CardinalityException.class)
public void testTimesMatrixCardinality() {
Matrix other = test.like(5, 8);
test.times(other);
}
代码示例来源:origin: apache/mahout
private static Matrix lowRankMatrix() {
Matrix u = new RandomTrinaryMatrix(1, 20, 4, false);
Matrix d = new DiagonalMatrix(new double[]{5, 3, 1, 0.5});
Matrix v = new RandomTrinaryMatrix(2, 23, 4, false);
return u.times(d).times(v.transpose());
}
}
代码示例来源: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(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 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 testTimesDouble() {
Matrix value = test.times(4.53);
for (int row = 0; row < test.rowSize(); row++) {
for (int col = 0; col < test.columnSize(); col++) {
assertEquals("value[" + row + "][" + col + ']',
values[row][col] * 4.53, value.getQuick(row, col), EPSILON);
}
}
}
代码示例来源:origin: apache/mahout
@Test
public void testTimesDouble() {
Matrix value = test.times(4.53);
for (int row = 0; row < test.rowSize(); row++) {
for (int col = 0; col < test.columnSize(); col++) {
assertEquals("value[" + row + "][" + col + ']',
values[row + 1][col + 1] * 4.53, value.getQuick(row, col), EPSILON);
}
}
}
代码示例来源:origin: apache/mahout
@Test
public void testSingularValues() {
Matrix A = lowRankMatrix();
SequentialBigSvd s = new SequentialBigSvd(A, 8);
SingularValueDecomposition svd = new SingularValueDecomposition(A);
Vector reference = new DenseVector(svd.getSingularValues()).viewPart(0, 8);
assertEquals(reference, s.getSingularValues());
assertEquals(A, s.getU().times(new DiagonalMatrix(s.getSingularValues())).times(s.getV().transpose()));
}
代码示例来源: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 testTimesMatrix() {
Matrix transpose = test.transpose();
Matrix value = test.times(transpose);
assertEquals("rows", test.rowSize(), value.rowSize());
assertEquals("cols", test.rowSize(), value.columnSize());
// TODO: check the math too, lazy
}
代码示例来源: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
public static void checkOrthogonal(Matrix m) {
Matrix mTm = m.transpose().times(m);
Matrix id = new DenseMatrix(mTm.numRows(),mTm.numRows());
for (int i = 0; i < mTm.numRows(); i++) {
id.set(i, i, 1);
}
assertEquals(0, Algebra.getNorm(mTm.minus(id)), NORM_TOLERANCE);
}
代码示例来源: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
private void validateYtY(Matrix matrixToTest, int numThreads) {
OpenIntObjectHashMap<Vector> matrixToTestAsRowVectors = asRowVectors(matrixToTest);
ImplicitFeedbackAlternatingLeastSquaresSolver solver = new ImplicitFeedbackAlternatingLeastSquaresSolver(
matrixToTest.columnSize(), 1, 1, matrixToTestAsRowVectors, numThreads);
Matrix yTy = matrixToTest.transpose().times(matrixToTest);
Matrix shouldMatchyTy = solver.getYtransposeY(matrixToTestAsRowVectors);
for (int row = 0; row < yTy.rowSize(); row++) {
for (int column = 0; column < yTy.columnSize(); column++) {
assertEquals(yTy.getQuick(row, column), shouldMatchyTy.getQuick(row, column), 0);
}
}
}
内容来源于网络,如有侵权,请联系作者删除!