Package org.apache.mahout.math.matrix

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


    return Math.sqrt(Algebra.norm2(x));
  }

  public void drot(DoubleMatrix1D x, DoubleMatrix1D y, double c, double s) {
    x.checkSize(y);
    DoubleMatrix1D tmp = x.copy();

    x.assign(Functions.mult(c));
    x.assign(y, Functions.plusMult(s));

    y.assign(Functions.mult(c));
View Full Code Here


    Property.checkSquare(A);
    int size = A.rows();
    if (size != x.size() && size != y.size()) {
      throw new IllegalArgumentException(A.toStringShort() + ", " + x.toStringShort() + ", " + y.toStringShort());
    }
    DoubleMatrix1D tmp = x.like();
    for (int i = 0; i < size; i++) {
      double sum = 0;
      for (int j = 0; j <= i; j++) {
        sum += A.getQuick(i, j) * x.getQuick(j);
      }
      for (int j = i + 1; j < size; j++) {
        sum += A.getQuick(j, i) * x.getQuick(j);
      }
      tmp.setQuick(i, alpha * sum + beta * y.getQuick(i));
    }
    y.assign(tmp);
  }
View Full Code Here

    int size = A.rows();
    if (size != x.size()) {
      throw new IllegalArgumentException(A.toStringShort() + ", " + x.toStringShort());
    }

    DoubleMatrix1D b = x.like();
    DoubleMatrix1D y = x.like();
    if (isUnitTriangular) {
      y.assign(1);
    } else {
      for (int i = 0; i < size; i++) {
        y.setQuick(i, A.getQuick(i, i));
      }
    }

    for (int i = 0; i < size; i++) {
      double sum = 0;
      if (!isUpperTriangular) {
        for (int j = 0; j < i; j++) {
          sum += A.getQuick(i, j) * x.getQuick(j);
        }
        sum += y.getQuick(i) * x.getQuick(i);
      } else {
        sum += y.getQuick(i) * x.getQuick(i);
        for (int j = i + 1; j < size; j++) {
          sum += A.getQuick(i, j) * x.getQuick(j);
        }
      }
      b.setQuick(i, sum);
View Full Code Here

    int[] rowIndexes = new int[matrix.rows()]; // row indexes to reorder instead of matrix itself
    for (int i = rowIndexes.length; --i >= 0;) {
      rowIndexes[i] = i;
    }

    final DoubleMatrix1D col = matrix.viewColumn(column);
    IntComparator comp = new IntComparator() {
      public int compare(int a, int b) {
        double av = col.getQuick(a);
        double bv = col.getQuick(b);
        if (av != av || bv != bv) {
          return compareNaN(av, bv);
        } // swap NaNs to the end
        return av < bv ? -1 : (av == bv ? 0 : 1);
      }
View Full Code Here

    int[] sliceIndexes = new int[matrix.slices()]; // indexes to reorder instead of matrix itself
    for (int i = sliceIndexes.length; --i >= 0;) {
      sliceIndexes[i] = i;
    }

    final DoubleMatrix1D sliceView = matrix.viewRow(row).viewColumn(column);
    IntComparator comp = new IntComparator() {
      public int compare(int a, int b) {
        double av = sliceView.getQuick(a);
        double bv = sliceView.getQuick(b);
        if (av != av || bv != bv) {
          return compareNaN(av, bv);
        } // swap NaNs to the end
        return av < bv ? -1 : (av == bv ? 0 : 1);
      }
View Full Code Here

        g[c] = tmp;
      }
    };

    // compare splitter[a] with columnView[rowIndexes[b]]
    final DoubleMatrix1D columnView = matrix.viewColumn(column);
    IntComparator comp = new IntComparator() {
      public int compare(int a, int b) {
        double av = splitters[a];
        double bv = columnView.getQuick(g[b]);
        return av < bv ? -1 : (av == bv ? 0 : 1);
      }
    };

    // compare columnView[rowIndexes[a]] with columnView[rowIndexes[b]]
    IntComparator comp2 = new IntComparator() {
      public int compare(int a, int b) {
        double av = columnView.getQuick(g[a]);
        double bv = columnView.getQuick(g[b]);
        return av < bv ? -1 : (av == bv ? 0 : 1);
      }
    };

    // compare splitter[a] with splitter[b]
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 CUT_OFF = 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 / CUT_OFF; // == 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

    Mult div = Mult.div(0);
    PlusMult minusMult = PlusMult.minusMult(0);

    IntArrayList nonZeroIndexes =
        new IntArrayList(); // sparsity
    DoubleMatrix1D Browk = org.apache.mahout.math.matrix.DoubleFactory1D.dense.make(nx); // blocked row k

    // Solve L*Y = B(piv,:)
    int CUT_OFF = 10;
    for (int k = 0; k < n; k++) {
      // blocking (make copy of k-th row to localize references)
      Browk.assign(Brows[k]);

      // sparsity detection
      int maxCardinality = nx / CUT_OFF; // == heuristic depending on speedup
      Browk.getNonZeros(nonZeroIndexes, null, maxCardinality);
      int cardinality = nonZeroIndexes.size();
      boolean sparse = (cardinality < maxCardinality);

      for (int i = k + 1; i < n; i++) {
        //for (int j = 0; j < nx; j++) B[i][j] -= B[k][j]*LU[i][k];
        //for (int j = 0; j < nx; j++) B.set(i,j, B.get(i,j) - B.get(k,j)*LU.get(i,k));

        minusMult.setMultiplicator(-LU.getQuick(i, k));
        if (minusMult.getMultiplicator() != 0) {
          if (sparse) {
            Brows[i].assign(Browk, minusMult, nonZeroIndexes);
          } else {
            Brows[i].assign(Browk, minusMult);
          }
        }
      }
    }

    // Solve U*B = Y;
    for (int k = n - 1; k >= 0; k--) {
      // for (int j = 0; j < nx; j++) B[k][j] /= LU[k][k];
      // for (int j = 0; j < nx; j++) B.set(k,j, B.get(k,j) / LU.get(k,k));
      div.setMultiplicator(1 / LU.getQuick(k, k));
      Brows[k].assign(div);

      // blocking
      if (Browk == null) {
        Browk = org.apache.mahout.math.matrix.DoubleFactory1D.dense.make(B.columns());
      }
      Browk.assign(Brows[k]);

      // sparsity detection
      int maxCardinality = nx / CUT_OFF; // == heuristic depending on speedup
      Browk.getNonZeros(nonZeroIndexes, null, maxCardinality);
      int cardinality = nonZeroIndexes.size();
      boolean sparse = (cardinality < maxCardinality);

      //Browk.getNonZeros(nonZeroIndexes,null);
      //boolean sparse = nonZeroIndexes.size() < nx/10;
View Full Code Here

        QR.setQuick(k, k, QR.getQuick(k, k) + 1);

        // Apply transformation to remaining columns.
        for (int j = k + 1; j < n; j++) {
          DoubleMatrix1D QRcolj = QR.viewColumn(j).viewPart(k, m - k);
          double s = QRcolumnsPart[k].zDotProduct(QRcolj);
          /*
          // fixes bug reported by John Chambers
          DoubleMatrix1D QRcolj = QR.viewColumn(j).viewPart(k,m-k);
          double s = QRcolumnsPart[k].zDotProduct(QRcolumns[j]);
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.