Package org.apache.mahout.math.matrix

Examples of org.apache.mahout.math.matrix.DoubleMatrix1D


   */
  public DoubleMatrix2D getQ() {
    DoubleMatrix2D Q = QR.like();
    //double[][] Q = X.getArray();
    for (int k = n - 1; k >= 0; k--) {
      DoubleMatrix1D QRcolk = QR.viewColumn(k).viewPart(k, m - k);
      Q.setQuick(k, k, 1);
      for (int j = k; j < n; j++) {
        if (QR.getQuick(k, k) != 0) {
          DoubleMatrix1D Qcolj = Q.viewColumn(j).viewPart(k, m - k);
          double s = QRcolk.zDotProduct(Qcolj);
          s = -s / QR.getQuick(k, k);
          Qcolj.assign(QRcolk, Functions.plusMult(s));
        }
      }
    }
    return Q;
  }
View Full Code Here


    log.info("Lanczos iteration complete - now to diagonalize the tri-diagonal auxiliary matrix.");
    // at this point, have tridiag all filled out, and basis is all filled out, and orthonormalized
    EigenvalueDecomposition decomp = new EigenvalueDecomposition(triDiag);

    DoubleMatrix2D eigenVects = decomp.getV();
    DoubleMatrix1D eigenVals = decomp.getRealEigenvalues();
    endTime(TimingSection.TRIDIAG_DECOMP);
    startTime(TimingSection.FINAL_EIGEN_CREATE);

    for (int i = 0; i < basis.numRows() - 1; i++) {
      Vector realEigen = new DenseVector(corpus.numCols());
      // the eigenvectors live as columns of V, in reverse order.  Weird but true.
      DoubleMatrix1D ejCol = eigenVects.viewColumn(basis.numRows() - i - 1);
      for (int j = 0; j < ejCol.size(); j++) {
        double d = ejCol.getQuick(j);
        realEigen.assign(basis.getRow(j), new PlusMult(d));
      }
      realEigen = realEigen.normalize();
      eigenVectors.assignRow(i, realEigen);
      log.info("Eigenvector {} found with eigenvalue {}", i, eigenVals.get(i));
View Full Code Here

   * @throws IndexOutOfBoundsException if <tt>index<0 || width<0 || index+width>size()</tt>.
   */
  @Override
  public DoubleMatrix1D viewPart(final int index, int width) {
    checkRange(index, width);
    DoubleMatrix1D view = new WrapperDoubleMatrix1D(this) {
      @Override
      public double getQuick(int i) {
        return content.get(index + i);
      }

View Full Code Here

    }

    checkIndexes(indexes);
    final int[] idx = indexes;

    DoubleMatrix1D view = new WrapperDoubleMatrix1D(this) {
      @Override
      public double getQuick(int i) {
        return content.get(idx[i]);
      }
View Full Code Here

  @Override
  public DoubleMatrix1D viewStrides(final int theStride) {
    if (stride <= 0) {
      throw new IndexOutOfBoundsException("illegal stride: " + stride);
    }
    DoubleMatrix1D view = new WrapperDoubleMatrix1D(this) {
      @Override
      public double getQuick(int index) {
        return content.get(index * theStride);
      }

View Full Code Here

    if (isNoView && other.isNoView) { // quickest
      System.arraycopy(other.elements, 0, this.elements, 0, this.elements.length);
      return this;
    }
    if (haveSharedCells(other)) {
      DoubleMatrix1D c = other.copy();
      if (!(c instanceof DenseDoubleMatrix1D)) { // should not happen
        return super.assign(source);
      }
      other = (DenseDoubleMatrix1D) c;
    }
View Full Code Here

    log.info("Lanczos iteration complete - now to diagonalize the tri-diagonal auxiliary matrix.");
    // at this point, have tridiag all filled out, and basis is all filled out, and orthonormalized
    EigenvalueDecomposition decomp = new EigenvalueDecomposition(triDiag);

    DoubleMatrix2D eigenVects = decomp.getV();
    DoubleMatrix1D eigenVals = decomp.getRealEigenvalues();
    endTime(TimingSection.TRIDIAG_DECOMP);
    startTime(TimingSection.FINAL_EIGEN_CREATE);
    for (int row = 0; row < i; row++) {
      Vector realEigen = null;
      // the eigenvectors live as columns of V, in reverse order.  Weird but true.
      DoubleMatrix1D ejCol = eigenVects.viewColumn(i - row - 1);
      int size = Math.min(ejCol.size(), state.getBasisSize());
      for (int j = 0; j < size; j++) {
        double d = ejCol.get(j);
        Vector rowJ = state.getBasisVector(j);
        if(realEigen == null) {
          realEigen = rowJ.like();
        }
        realEigen.assign(rowJ, new PlusMult(d));
View Full Code Here

    LanczosState state = new LanczosState(m, desiredRank, initialVector);
    // set initial vector?
    solver.solve(state, desiredRank, true);

    EigenvalueDecomposition decomposition = new EigenvalueDecomposition(m);
    DoubleMatrix1D eigenvalues = decomposition.getRealEigenvalues();

    float fractionOfEigensExpectedGood = 0.6f;
    for(int i = 0; i < fractionOfEigensExpectedGood * desiredRank; i++) {
      double s = state.getSingularValue(desiredRank - i - 1);
      double e = eigenvalues.get(eigenvalues.size() - i - 1);
      log.info("{} : L = {}, E = {}", new Object[] {i, s, e});
      assertTrue("Singular value differs from eigenvalue", Math.abs((s-e)/e) < ERROR_TOLERANCE);
      Vector v = state.getRightSingularVector(i);
      Vector v2 = decomposition.getV().viewColumn(eigenvalues.size() - i - 1).toVector();
      double error = 1 - Math.abs(v.dot(v2)/(v.norm(2) * v2.norm(2)));
      log.info("error: {}", error);
      assertTrue(i + ": 1 - cosAngle = " + error, error < ERROR_TOLERANCE);
    }
  }
View Full Code Here

    if (isNoView && other.isNoView) { // quickest
      System.arraycopy(other.elements, 0, this.elements, 0, this.elements.length);
      return this;
    }
    if (haveSharedCells(other)) {
      DoubleMatrix1D c = other.copy();
      if (!(c instanceof DenseDoubleMatrix1D)) { // should not happen
        return super.assign(source);
      }
      other = (DenseDoubleMatrix1D) c;
    }
View Full Code Here

      luRows[i] = lu.viewRow(i);
    }

    IntArrayList nonZeroIndexes =
        new IntArrayList(); // sparsity
    DoubleMatrix1D luColj = lu.viewColumn(0).like()// blocked column j
    Mult multFunction = Mult.mult(0);

    // Outer loop.
    int cutOff = 10;
    for (int j = 0; j < n; j++) {
      // blocking (make copy of j-th column to localize references)
      luColj.assign(lu.viewColumn(j));

      // sparsity detection
      int maxCardinality = m / cutOff; // == heuristic depending on speedup
      luColj.getNonZeros(nonZeroIndexes, null, maxCardinality);
      int cardinality = nonZeroIndexes.size();
      boolean sparse = cardinality < maxCardinality;

      // Apply previous transformations.
      for (int i = 0; i < m; i++) {
        int kmax = Math.min(i, j);
        double s;
        if (sparse) {
          s = luRows[i].zDotProduct(luColj, 0, kmax, nonZeroIndexes);
        } else {
          s = luRows[i].zDotProduct(luColj, 0, kmax);
        }
        double before = luColj.getQuick(i);
        double after = before - s;
        luColj.setQuick(i, after); // LUcolj is a copy
        lu.setQuick(i, j, after);   // this is the original
        if (sparse) {
          if (before == 0 && after != 0) { // nasty bug fixed!
            int pos = nonZeroIndexes.binarySearch(i);
            pos = -pos - 1;
            nonZeroIndexes.beforeInsert(pos, i);
          }
          if (before != 0 && after == 0) {
            nonZeroIndexes.remove(nonZeroIndexes.binarySearch(i));
          }
        }
      }

      // Find pivot and exchange if necessary.
      int p = j;
      if (p < m) {
        double max = Math.abs(luColj.getQuick(p));
        for (int i = j + 1; i < m; i++) {
          double v = Math.abs(luColj.getQuick(i));
          if (v > max) {
            p = i;
            max = v;
          }
        }
View Full Code Here

TOP

Related Classes of org.apache.mahout.math.matrix.DoubleMatrix1D

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.