Package mikera.matrixx.impl

Examples of mikera.matrixx.impl.SparseRowMatrix


  }
 
  public static void main(String[] args) {
    // We want a SparseRowMatrix, because we are going to multiply it with a second dense matrix
    // This means that a row-oriented sparse format is better for the first matrix
    SparseRowMatrix m=SparseRowMatrix.create(SIZE,SIZE);
   
    // First task is to construct the large sparse matrix
    startTimer();
   
    for (int i=0; i<SIZE; i++) {
      double[] data=new double[DSIZE];
      for (int j=0; j<DSIZE; j++) {
        data[j]=Rand.nextDouble();
      }
      Index indy=Indexz.createRandomChoice(DSIZE, SIZE);
      m.replaceRow(i,SparseIndexedVector.create(SIZE, indy, data));
    }
   
    printTime("Construct sparse matrix: ");
   
    // System.out.println("First row sum = "+m.getRow(0).elementSum());
   
    // Now we normalise each row to element sum = 1.0
    // This demonstrates both the mutability of rows and the setRow functionality
   
    startTimer();
   
    for (int i=0; i<SIZE; i++) {
      AVector row=m.getRow(i);
      double sum=row.elementSum();
      if (sum>0) {
        row.divide(sum);
      } else {
        m.setRow(i, RepeatedElementVector.create(SIZE,1.0/SIZE));
      }
    }
   
    printTime("Normalise all rows: ");

    //System.out.println("First row sum = "+m.getRow(0).elementSum());
   
    // We construct a dense matrix for later multiplication
   
    startTimer();
   
    AMatrix t=Matrixx.createRandomMatrix(SIZE, CSIZE);
    printTime("Construct dense matrix: ");
   
    System.out.println("Dense element sum = "+t.elementSum());

    // Finally compute the innerProduct (matrix multiplication) of
    // sparse matrix with dense matrix
   
    startTimer();
   
    AMatrix result=m.innerProduct(t);
   
    printTime("Multiply with dense matrix: ");
   
    System.out.println("Result element sum = "+result.elementSum());
    // if this demo is working, the element sum should be roughly the same before and after transformation
    // (modulo some small numerical errors)

        // ----------------------------------------------------------------------
    // Construct another (smaller) sparse matrix.
    SparseRowMatrix M=SparseRowMatrix.create(SSIZE,SSIZE);
   
    // First task is to construct the large sparse matrix
    startTimer();
   
    for (int i=0; i<SSIZE; i++) {
      double[] data=new double[DSIZE];
      for (int j=0; j<DSIZE; j++) {
        data[j]=Rand.nextDouble();
      }
      Index indy=Indexz.createRandomChoice(DSIZE, SSIZE);
      M.replaceRow(i,SparseIndexedVector.create(SSIZE, indy, data));
    }
   
    printTime("Construct small sparse matrix: ");

   
        // ----------------------------------------------------------------------
    // Convert this sparse matrix into a dense matrix.
    startTimer();
    Matrix D = Matrix.create(M);
    printTime("Convert small sparse matrix to dense: ");

   
        // ----------------------------------------------------------------------
    // Check equality from M.
    startTimer();
    boolean eq = M.equals(D);
    printTime("Equality check result (" + eq + "): ");
   
   
        // ----------------------------------------------------------------------
    // Check equality from D.
    startTimer();
    eq = D.epsilonEquals(M, 0.000001);
    printTime("epsilonEquals check result (" + eq + ", should be true): ");
   
   
        // ----------------------------------------------------------------------
    // Change sparse matrix and test equality again (shouldn't be equal)
    startTimer();
        M.addAt(SSIZE-1, SSIZE-1, 3.14159);
    eq = M.equals(D);
    printTime("Equality check result (" + eq + ", should be false): ");
   
   
        // ----------------------------------------------------------------------
    // Change dense matrix also; should be equal again.
    startTimer();
        D.addAt(SSIZE-1, SSIZE-1, 3.14159);
    eq = M.equals(D);
    printTime("Equality check result (" + eq + ", should be true): ");
   
  }
View Full Code Here


    return v;
  }
 
  public static SparseRowMatrix createMatrix() {
    SparseRowMatrix sm=SparseRowMatrix.create(SIZE,SIZE);
   
    for (int i=0; i<1000; i++) {
      sm.replaceRow(r.nextInt(SIZE), createRow());
    }
    return sm;
  }
View Full Code Here

    }
    return sm;
  }
 
  public static void main(String[] args) {
    SparseRowMatrix sm=createMatrix();
    System.out.println(sm.nonZeroCount() +" elements are non-zero out of " + sm.elementCount()+" total elements");
   
    AMatrix smm=sm.innerProduct(sm);
    System.out.println(smm.nonZeroCount() +" elements are non-zero in the product.");
  }
View Full Code Here

  public static AMatrix createSparse(int columnCount, Index[] indexes,
      AVector[] weights) {
    int rowCount = indexes.length;
    if (rowCount != weights.length)
      throw new IllegalArgumentException("Length of indexes array must match length of weights array");
    SparseRowMatrix sm=SparseRowMatrix.create(rowCount, columnCount);
    for (int i = 0; i < rowCount; i++) {
      sm.replaceRow(i, SparseIndexedVector.wrap(columnCount, indexes[i].clone(), weights[i].toDoubleArray()));
    }
    return sm;
  }
View Full Code Here

   */
  public static SparseRowMatrix createSparseRows(INDArray a) {
    if (!(a.dimensionality()==2)) throw new IllegalArgumentException(ErrorMessages.incompatibleShape(a));
    int rc=a.getShape(0);
    int cc=a.getShape(1);
    SparseRowMatrix m=SparseRowMatrix.create(rc,cc);
    for (int i=0; i<rc; i++) {
      AVector v=a.slice(i).sparseClone().asVector();
      if (!v.isZero()) {
        m.replaceRow(i, v);
      }
    }
    return m;
  }
View Full Code Here

TOP

Related Classes of mikera.matrixx.impl.SparseRowMatrix

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.