Package org.apache.mahout.math.matrix

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


    double[] aggregates = new double[aggregatesRaw.length];
    for (int i = 0; i < aggregatesRaw.length; i++) {
      aggregates[i] = Double.longBitsToDouble(aggregatesRaw[i]);
    }

    DoubleMatrix2D VT = new DenseDoubleMatrix2D(convert(new long[][]{
        {4540286548932058396L, 4577864022762482570L, 4577700324701750780L, 4518480992251412349L, 4556435725689022825L,
            4488720014619835766L, 4335660689431780693L, 4502946434618942000L, 4531841594141675072L,
            4445484354574431038L, 4494181495103148132L, 4238500868784003636L, 4410983210480467520L,
            4378527886946299824L, 4535843255558462101L, 4533033844134633147L, 4588065853673790470L,
            4529714837625705005L, 4545016130375436933L, 4534987086323522839L, 4499740291369705403L,
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

   * Returns a string representation of the given matrix.
   *
   * @param matrix the matrix to convert.
   */
  public String toString(DoubleMatrix1D matrix) {
    DoubleMatrix2D easy = matrix.like2D(1, matrix.size());
    easy.viewRow(0).assign(matrix);
    return toString(easy);
  }
View Full Code Here

   *
   * @return a new independent matrix; inverse(matrix) if the matrix is square, pseudoinverse otherwise.
   */
  public DoubleMatrix2D inverse(DoubleMatrix2D A) {
    if (Property.isSquare(A) && property.isDiagonal(A)) {
      DoubleMatrix2D inv = A.copy();
      boolean isNonSingular = Diagonal.inverse(inv);
      if (!isNonSingular) {
        throw new IllegalArgumentException("A is singular.");
      }
      return inv;
View Full Code Here

      p = -p;
    }
    if (p == 0) {
      return DoubleFactory2D.dense.identity(A.rows());
    }
    DoubleMatrix2D T = A.like(); // temporary
    if (p == 1) {
      return T.assign(A);
    // safes one auxiliary matrix allocation
    if (p == 2) {
      blas.dgemm(false, false, 1, A, A, 0, T); // mult(A,A); // safes one auxiliary matrix allocation
      return T;
    }

    int k = QuickBitVector.mostSignificantBit(p); // index of highest bit in state "true"

    /*
    this is the naive version:
    DoubleMatrix2D B = A.copy();
    for (int i=0; i<p-1; i++) {
      B = mult(B,A);
    }
    return B;
    */

    // here comes the optimized version:
    //org.apache.mahout.math.Timer timer = new Timer().start();

    int i = 0;
    while (i <= k && (p & (1 << i)) == 0) { // while (bit i of p == false)
      // A = mult(A,A); would allocate a lot of temporary memory
      blas.dgemm(false, false, 1, A, A, 0, T); // A.zMult(A,T);
      DoubleMatrix2D swap = A;
      A = T;
      T = swap; // swap A with T
      i++;
    }

    DoubleMatrix2D B = A.copy();
    i++;
    for (; i <= k; i++) {
      // A = mult(A,A); would allocate a lot of temporary memory
      blas.dgemm(false, false, 1, A, A, 0, T); // A.zMult(A,T);
      DoubleMatrix2D swap = A;
      A = T;
      T = swap; // swap A with T

      if ((p & (1 << i)) != 0) { // if (bit i of p == true)
        // B = mult(B,A); would allocate a lot of temporary memory
View Full Code Here

   */
  private static DoubleMatrix2D subMatrix(DoubleMatrix2D A, int[] rowIndexes, int columnFrom, int columnTo) {
    int width = columnTo - columnFrom + 1;
    int rows = A.rows();
    A = A.viewPart(0, columnFrom, rows, width);
    DoubleMatrix2D sub = A.like(rowIndexes.length, width);

    for (int r = rowIndexes.length; --r >= 0;) {
      int row = rowIndexes[r];
      if (row < 0 || row >= rows) {
        throw new IndexOutOfBoundsException("Illegal Index");
      }
      sub.viewRow(r).assign(A.viewRow(row));
    }
    return sub;
  }
View Full Code Here

      throw new IndexOutOfBoundsException("Too many rows");
    }
    int height = rowTo - rowFrom + 1;
    int columns = A.columns();
    A = A.viewPart(rowFrom, 0, height, columns);
    DoubleMatrix2D sub = A.like(height, columnIndexes.length);

    for (int c = columnIndexes.length; --c >= 0;) {
      int column = columnIndexes[c];
      if (column < 0 || column >= columns) {
        throw new IndexOutOfBoundsException("Illegal Index");
      }
      sub.viewColumn(c).assign(A.viewColumn(column));
    }
    return sub;
  }
View Full Code Here

   *                              may be expensive, you may want to do it only every 2,4 or 8 iterations.)
   * @return the number of iterations actually executed.
   */
  public static int stencil9(DoubleMatrix2D A, org.apache.mahout.math.function.Double9Function function,
                             int maxIterations, DoubleMatrix2DProcedure hasConverged, int convergenceIterations) {
    DoubleMatrix2D B = A.copy();
    if (convergenceIterations <= 1) {
      convergenceIterations = 2;
    }
    if (convergenceIterations % 2 != 0) {
      convergenceIterations++;
    } // odd -> make it even

    int i = 0;
    while (i < maxIterations) { // do two steps at a time for efficiency
      A.zAssign8Neighbors(B, function);
      B.zAssign8Neighbors(A, function);
      i += 2;
      if (i % convergenceIterations == 0 && hasConverged != null) {
        if (hasConverged.apply(A)) {
          return i;
        }
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 <tt>B.rows() != A.rows()</tt>.
   * @throws IllegalArgumentException if <tt>!isSymmetricPositiveDefinite()</tt>.
   */
  public DoubleMatrix2D solve(DoubleMatrix2D B) {
    // Copy right hand side.
    DoubleMatrix2D X = B.copy();
    int nx = B.columns();

    // fix by MG Ferreira <mgf@webmail.co.za>
    // old code is in method xxxSolveBuggy()
    for (int c = 0; c < nx; c++) {
      // Solve L*Y = B;
      for (int i = 0; i < n; i++) {
        double sum = B.getQuick(i, c);
        for (int k = i - 1; k >= 0; k--) {
          sum -= L.getQuick(i, k) * X.getQuick(k, c);
        }
        X.setQuick(i, c, sum / L.getQuick(i, i));
      }

      // Solve L'*X = Y;
      for (int i = n - 1; i >= 0; i--) {
        double sum = X.getQuick(i, c);
        for (int k = i + 1; k < n; k++) {
          sum -= L.getQuick(k, i) * X.getQuick(k, c);
        }
        X.setQuick(i, c, sum / L.getQuick(i, i));
      }
    }

    return X;
  }
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.