Package org.apache.mahout.math.matrix

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


    if (!isSymmetricPositiveDefinite) {
      throw new IllegalArgumentException("Matrix is not symmetric positive definite.");
    }

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

    // precompute and cache some views to avoid regenerating them time and again
    DoubleMatrix1D[] Xrows = new DoubleMatrix1D[n];
    for (int k = 0; k < n; k++) {
      Xrows[k] = X.viewRow(k);
    }

    // Solve L*Y = B;
    for (int k = 0; k < n; k++) {
      for (int i = k + 1; i < n; i++) {
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

   * @return the covariance matrix (<tt>n x n, n=matrix.columns</tt>).
   */
  public static DoubleMatrix2D covariance(DoubleMatrix2D matrix) {
    int rows = matrix.rows();
    int columns = matrix.columns();
    DoubleMatrix2D covariance = new DenseDoubleMatrix2D(columns, columns);

    double[] sums = new double[columns];
    DoubleMatrix1D[] cols = new DoubleMatrix1D[columns];
    for (int i = columns; --i >= 0;) {
      cols[i] = matrix.viewColumn(i);
      sums[i] = cols[i].zSum();
    }

    for (int i = columns; --i >= 0;) {
      for (int j = i + 1; --j >= 0;) {
        double sumOfProducts = cols[i].zDotProduct(cols[j]);
        double cov = (sumOfProducts - sums[i] * sums[j] / rows) / rows;
        covariance.setQuick(i, j, cov);
        covariance.setQuick(j, i, cov); // symmetric
      }
    }
    return covariance;
  }
View Full Code Here

   * @param distanceFunction (EUCLID, CANBERRA, ..., or any user defined distance function operating on two vectors).
   * @return the distance matrix (<tt>n x n, n=matrix.columns</tt>).
   */
  public static DoubleMatrix2D distance(DoubleMatrix2D matrix, VectorVectorFunction distanceFunction) {
    int columns = matrix.columns();
    DoubleMatrix2D distance = new DenseDoubleMatrix2D(columns, columns);

    // cache views
    DoubleMatrix1D[] cols = new DoubleMatrix1D[columns];
    for (int i = columns; --i >= 0;) {
      cols[i] = matrix.viewColumn(i);
    }

    // work out all permutations
    for (int i = columns; --i >= 0;) {
      for (int j = i; --j >= 0;) {
        double d = distanceFunction.apply(cols[i], cols[j]);
        distance.setQuick(i, j, d);
        distance.setQuick(j, i, d); // symmetric
      }
    }
    return distance;
  }
View Full Code Here

    catch (IllegalArgumentException exc) {
      buf.append(unknown).append(exc.getMessage());
    }

    buf.append("\n\ninverse(A) = ");
    DoubleMatrix2D identity = org.apache.mahout.math.matrix.DoubleFactory2D.dense.identity(LU.rows());
    try {
      this.solve(identity);
      buf.append(String.valueOf(identity));
    }
    catch (IllegalArgumentException exc) {
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

    Vector previousVector = new DenseVector(currentVector.size());
    Matrix basis = new SparseRowMatrix(new int[]{desiredRank, corpus.numCols()});
    basis.assignRow(0, currentVector);
    double alpha = 0;
    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
      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;
      }
      final double b = beta;
      nextVector.assign(new Scale(1 / b));
      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

   *
   * @return a new dice view.
   */
  @Override
  public DoubleMatrix2D viewDice() {
    DoubleMatrix2D view = new WrapperDoubleMatrix2D(this) {
      @Override
      public double getQuick(int row, int column) {
        return content.get(column, row);
      }

View Full Code Here

   *                                   row+height>rows()</tt>
   */
  @Override
  public DoubleMatrix2D viewPart(final int row, final int column, int height, int width) {
    checkBox(row, column, height, width);
    DoubleMatrix2D view = new WrapperDoubleMatrix2D(this) {
      @Override
      public double getQuick(int i, int j) {
        return content.get(row + i, column + j);
      }

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.