Package org.apache.mahout.math.matrix

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


  @Override
  public DoubleMatrix2D viewStrides(final int theRowStride, final int theColumnStride) {
    if (theRowStride <= 0 || theColumnStride <= 0) {
      throw new IndexOutOfBoundsException("illegal stride");
    }
    DoubleMatrix2D view = new WrapperDoubleMatrix2D(this) {
      @Override
      public double getQuick(int row, int column) {
        return content.get(theRowStride * row, theColumnStride * column);
      }

View Full Code Here


      System.arraycopy(other.elements, 0, this.elements, 0, this.elements.length);
      return this;
    }

    if (haveSharedCells(other)) {
      DoubleMatrix2D c = other.copy();
      if (!(c instanceof DenseDoubleMatrix2D)) { // should not happen
        return super.assign(other);
      }
      other = (DenseDoubleMatrix2D) c;
    }
View Full Code Here

   * Generates and returns the (economy-sized) orthogonal factor <tt>Q</tt>.
   *
   * @return <tt>Q</tt>
   */
  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));
        }
      }
View Full Code Here

   * Returns the upper triangular factor, <tt>R</tt>.
   *
   * @return <tt>R</tt>
   */
  public DoubleMatrix2D getR() {
    DoubleMatrix2D R = QR.like(n, n);
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < n; j++) {
        if (i < j) {
          R.setQuick(i, j, QR.getQuick(i, j));
        } else if (i == j) {
          R.setQuick(i, j, Rdiag.getQuick(i));
        } else {
          R.setQuick(i, j, 0);
        }
      }
    }
    return R;
  }
View Full Code Here

      throw new IllegalArgumentException("Matrix is rank deficient.");
    }

    // Copy right hand side
    int nx = B.columns();
    DoubleMatrix2D X = B.copy();

    // Compute Y = transpose(Q)*B
    for (int k = 0; k < n; k++) {
      for (int j = 0; j < nx; j++) {
        double s = 0.0;
        for (int i = k; i < m; i++) {
          s += QR.getQuick(i, k) * X.getQuick(i, j);
        }
        s = -s / QR.getQuick(k, k);
        for (int i = k; i < m; i++) {
          X.setQuick(i, j, X.getQuick(i, j) + s * QR.getQuick(i, k));
        }
      }
    }
    // Solve R*X = Y;
    for (int k = n - 1; k >= 0; k--) {
      for (int j = 0; j < nx; j++) {
        X.setQuick(k, j, X.getQuick(k, j) / Rdiag.getQuick(k));
      }
      for (int i = 0; i < k; i++) {
        for (int j = 0; j < nx; j++) {
          X.setQuick(i, j, X.getQuick(i, j) - X.getQuick(k, j) * QR.getQuick(i, k));
        }
      }
    }
    return X.viewPart(0, 0, n, nx);
  }
View Full Code Here

   * @throws IllegalArgumentException if A is singular, that is, if <tt>!this.isNonsingular()</tt>.
   * @throws IllegalArgumentException if <tt>A.rows() < A.columns()</tt>.
   */

  public DoubleMatrix2D solve(DoubleMatrix2D b) {
    DoubleMatrix2D x = b.copy();
    quick.solve(x);
    return x;
  }
View Full Code Here

    Vector currentVector = getInitialVector(corpus);
    Vector previousVector = new DenseVector(currentVector.size());
    Matrix basis = new SparseRowMatrix(new int[]{desiredRank, corpus.numCols()});
    basis.assignRow(0, currentVector);
    double beta = 0;
    DoubleMatrix2D triDiag = new DenseDoubleMatrix2D(desiredRank, desiredRank);
    for (int i = 1; i < desiredRank; i++) {
      startTime(TimingSection.ITERATE);
      Vector nextVector = isSymmetric ? corpus.times(currentVector) : corpus.timesSquared(currentVector);
      log.info("{} passes through the corpus so far...", i);
      calculateScaleFactor(nextVector);
      nextVector.assign(new Scale(1 / scaleFactor));
      nextVector.assign(previousVector, new PlusMult(-beta));
      // now orthogonalize
      double alpha = currentVector.dot(nextVector);
      nextVector.assign(currentVector, new PlusMult(-alpha));
      endTime(TimingSection.ITERATE);
      startTime(TimingSection.ORTHOGANLIZE);
      orthoganalizeAgainstAllButLast(nextVector, basis);
      endTime(TimingSection.ORTHOGANLIZE);
      // and normalize
      beta = nextVector.norm(2);
      if (outOfRange(beta) || outOfRange(alpha)) {
        log.warn("Lanczos parameters out of range: alpha = {}, beta = {}.  Bailing out early!", alpha, beta);
        break;
      }
      nextVector.assign(new Scale(1 / beta));
      basis.assignRow(i, nextVector);
      previousVector = currentVector;
      currentVector = nextVector;
      // save the projections and norms!
      triDiag.set(i - 1, i - 1, alpha);
      if (i < desiredRank - 1) {
        triDiag.set(i - 1, i, beta);
        triDiag.set(i, i - 1, beta);
      }
    }
    startTime(TimingSection.TRIDIAG_DECOMP);

    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();
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 = ejCol.size();
      for (int j = 0; j < size; j++) {
        double d = ejCol.get(j);
        Vector rowJ = state.getBasisVector(j);
        if(realEigen == null) {
View Full Code Here

    this.elements = new double[rows * columns];
  }

  /** Constructs an identity matrix (having ones on the diagonal and zeros elsewhere). */
  public static DoubleMatrix2D identity(int rowsAndColumns) {
    DoubleMatrix2D matrix = new DenseDoubleMatrix2D(rowsAndColumns, rowsAndColumns);
    for (int i = rowsAndColumns; --i >= 0;) {
      matrix.setQuick(i, i, 1);
    }
    return matrix;
  }
View Full Code Here

      System.arraycopy(other.elements, 0, this.elements, 0, this.elements.length);
      return this;
    }

    if (haveSharedCells(other)) {
      DoubleMatrix2D c = other.copy();
      if (!(c instanceof DenseDoubleMatrix2D)) { // should not happen
        return super.assign(other);
      }
      other = (DenseDoubleMatrix2D) c;
    }
View Full Code Here

TOP

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

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.