org.apache.mahout.math.Matrix类的使用及代码示例

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

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

Matrix介绍

[英]The basic interface including numerous convenience functions
[中]基本界面包括许多方便的功能

代码示例

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

static Matrix addLambdaTimesNuiTimesE(Matrix matrix, double lambda, int nui) {
 Preconditions.checkArgument(matrix.numCols() == matrix.numRows(), "Must be a Square Matrix");
 double lambdaTimesNui = lambda * nui;
 int numCols = matrix.numCols();
 for (int n = 0; n < numCols; n++) {
  matrix.setQuick(n, n, matrix.getQuick(n, n) + lambdaTimesNui);
 }
 return matrix;
}

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

@Override
public void setQuick(int index, double value) {
 Vector v = rowToColumn ? matrix.viewColumn(index) : matrix.viewRow(index);
 if (v == null) {
  v = newVector(numCols);
  if (rowToColumn) {
   matrix.assignColumn(index, v);
  } else {
   matrix.assignRow(index, v);
  }
 }
 v.setQuick(transposeOffset, value);
}

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

@Test(expected = IndexException.class)
public void testGetIndexOver() {
 for (int row = 0; row < test.rowSize() + 1; row++) {
  for (int col = 0; col < test.columnSize(); col++) {
   test.get(row, col);
  }
 }
}

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

/**
 * Return the eigenvector matrix
 *
 * @return V
 */
public Matrix getV() {
 return v.like().assign(v);
}

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

protected TransposeViewVector(Matrix m, int offset, boolean rowToColumn) {
 super(rowToColumn ? m.numRows() : m.numCols());
 matrix = m;
 this.transposeOffset = offset;
 this.rowToColumn = rowToColumn;
 numCols = rowToColumn ? m.numCols() : m.numRows();
}

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

@Test
public void testAssignMatrix() {
 Matrix value = test.like();
 value.assign(test);
 for (int row = 0; row < test.rowSize(); row++) {
  for (int col = 0; col < test.columnSize(); col++) {
   assertEquals("value[" + row + "][" + col + ']',
     test.getQuick(row, col), value.getQuick(row, col), EPSILON);
  }
 }
}

代码示例来源: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 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 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(expected = IndexException.class)
public void testSetOver() {
 for (int row = 0; row < test.rowSize() + 1; row++) {
  for (int col = 0; col < test.columnSize(); col++) {
   test.set(row, col, 1.23);
  }
 }
}

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

@Test
public void testColumnView() {
 assertEquals(test.rowSize(), test.viewColumn(0).size());
 assertEquals(test.rowSize(), test.viewColumn(1).size());
 Random gen = RandomUtils.getRandom();
 for (int col = 0; col < test.columnSize(); col++) {
  int j = gen.nextInt(test.columnSize());
  double old = test.get(col, j);
  double v = gen.nextGaussian();
  test.viewColumn(col).set(j, v);
  assertEquals(v, test.get(j, col), 0);
  assertEquals(v, test.viewColumn(col).get(j), 0);
  test.set(j, col, old);
  assertEquals(old, test.get(j, col), 0);
  assertEquals(old, test.viewColumn(col).get(j), 0);
 }
}

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

@Test(expected = IllegalStateException.class)
public void testSettingLabelBindings() {
 assertNull("row bindings", test.getRowLabelBindings());
 assertNull("col bindings", test.getColumnLabelBindings());
 test.set("Fee", "Foo", 1, 1, 9);
 assertNotNull("row", test.getRowLabelBindings());
 assertNotNull("row", test.getRowLabelBindings());
 assertEquals("Fee", 1, test.getRowLabelBindings().get("Fee").intValue());
 assertEquals("Foo", 1, test.getColumnLabelBindings().get("Foo").intValue());
 assertEquals("FeeFoo", test.get(1, 1), test.get("Fee", "Foo"), EPSILON);
 test.get("Fie", "Foe");
}

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

private static void printMatrix(String name, Matrix m) {
 int rows = m.numRows();
 int columns = m.numCols();
 System.out.printf("%s - %d x %d\n", name, rows, columns);
 for (int i = 0; i < rows; i++) {
  for (int j = 0; j < columns; j++) {
   System.out.printf("%10.5f", m.get(i, j));
  }
  System.out.printf("\n");
 }
 System.out.printf("\n");
 System.out.printf("\n");
}

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

int cols = corpus.numCols();
Matrix eigens = new DenseMatrix(desiredRank, cols);
List<Double> eigenValues = new ArrayList<>();
log.info("Finding {} singular vectors of matrix with {} rows, via Hebbian", desiredRank, corpus.numRows());
Matrix corpusProjections = new DenseMatrix(corpus.numRows(), desiredRank);
TrainingState state = new TrainingState(eigens, corpusProjections);
for (int i = 0; i < desiredRank; i++) {
 while (hasNotConverged(currentEigen, corpus, state)) {
  int randomStartingIndex = getRandomStartingIndex(corpus, eigens);
  Vector initialTrainingVector = corpus.viewRow(randomStartingIndex);
  state.setTrainingIndex(randomStartingIndex);
  updater.update(currentEigen, initialTrainingVector, state);
  for (int corpusRow = 0; corpusRow < corpus.numRows(); corpusRow++) {
   state.setTrainingIndex(corpusRow);
   if (corpusRow != randomStartingIndex) {
    updater.update(currentEigen, corpus.viewRow(corpusRow), state);
 eigens.assignRow(i, currentEigen);
 eigenValues.add(eigenValue);
 state.setCurrentEigenValues(eigenValues);

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

@Override
public double getQuick(int index) {
 Vector v = rowToColumn ? matrix.viewColumn(index) : matrix.viewRow(index);
 return v == null ? 0.0 : v.getQuick(transposeOffset);
}

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

public Vector currentTrainingProjection() {
 if (trainingProjections.viewRow(trainingIndex) == null) {
  trainingProjections.assignRow(trainingIndex, new DenseVector(currentEigens.numCols()));
 }
 return trainingProjections.viewRow(trainingIndex);
}

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

public JacobiConditioner(Matrix a) {
 if (a.numCols() != a.numRows()) {
  throw new IllegalArgumentException("Matrix must be square.");
 }
 
 inverseDiagonal = new DenseVector(a.numCols());
 for (int i = 0; i < a.numCols(); ++i) {
  inverseDiagonal.setQuick(i, 1.0 / a.getQuick(i, i));
 }
}

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

@Override
public Matrix timesRight(Matrix that) {
 if (that.numRows() != diagonal.size()) {
  throw new IllegalArgumentException("Incompatible number of rows in the right operand of matrix multiplication.");
 }
 Matrix m = that.like();
 for (int row = 0; row < diagonal.size(); row++) {
  m.assignRow(row, that.viewRow(row).times(diagonal.getQuick(row)));
 }
 return m;
}

相关文章