Package cern.colt.matrix

Examples of cern.colt.matrix.DoubleMatrix2D


  cern.colt.Timer timer3 = new cern.colt.Timer();
  cern.colt.Timer timer4 = new cern.colt.Timer();
  cern.colt.Timer timer5 = new cern.colt.Timer();
  cern.colt.Timer timer6 = new cern.colt.Timer();

  DoubleMatrix2D  matrix = null;
  if (kind.equals("sparse")) matrix = new SparseDoubleMatrix2D(size,size,initialCapacity,minLoadFactor,maxLoadFactor);
  else if (kind.equals("dense")) matrix = cern.colt.matrix.DoubleFactory2D.dense.make(size,size);
  //else if (kind.equals("denseArray")) matrix = new DoubleArrayMatrix2D(size,size);
  else throw new RuntimeException("unknown kind");
 
  System.out.println("\nNow initializing...");
  //Matrix AJ = new Matrix(columnwise,3);
  //Basic.random(matrix, new cern.jet.random.Uniform(new cern.jet.random.engine.MersenneTwister()));
  double value = 2;
  DoubleMatrix2D tmp = DoubleFactory2D.dense.sample(matrix.rows(), matrix.columns(), value, percentNonZero);
  matrix.assign(tmp);
  tmp = null;
  /*
  long NN = matrix.size();
  int nn = (int) (NN*percentNonZero);
  long[] nonZeroIndexes = new long[nn];
  cern.jet.random.sampling.RandomSampler sampler = new cern.jet.random.sampling.RandomSampler(nn,NN,0,new cern.jet.random.engine.MersenneTwister());
  sampler.nextBlock(nn,nonZeroIndexes,0);
  for (int i=nn; --i >=0; ) {
    int row = (int) (nonZeroIndexes[i]/size);
    int column = (int) (nonZeroIndexes[i]%size);
    matrix.set(row,column, value);
  }
  */

  /*
  timer1.start();
  for (int i=0; i<runs; i++) {
    LUDecomposition LU = new LUDecomposition(matrix);
  }
  timer1.stop();
  timer1.display();

  {
    Jama.Matrix jmatrix = new Jama.Matrix(matrix.toArray());
    timer2.start();
    for (int i=0; i<runs; i++) {
      Jama.LUDecomposition LU = new Jama.LUDecomposition(jmatrix);
    }
    timer2.stop();
    timer2.display();
  }
  */
  System.out.println("\ntesting...");
  if (print) System.out.println(matrix);
  DoubleMatrix2D dense = DoubleFactory2D.dense.make(size,size);
  dense.assign(matrix);
  if (! dense.equals(matrix)) throw new InternalError();
  DoubleMatrix2D ADense = dense.copy();
  DoubleMatrix2D BDense = dense.copy();
  DoubleMatrix2D CDense = dense.copy();
  ADense.zMult(BDense,CDense);
  System.out.println("\nNext testing...");
  /*
  {
    timer6.start();
    double a = cubicLoop(runs,size);
    timer6.stop();
    timer6.display();
    System.out.println(a);
  }
  */
 

  {
    DoubleMatrix2D A = matrix.copy();
    DoubleMatrix2D B = matrix.copy();
    //DoubleMatrix2D C = Basic.product(A,B);
    DoubleMatrix2D C = matrix.copy();
    A.zMult(B,C);
    if (! (C.equals(CDense))) throw new InternalError();
    C.assign(matrix);
    System.out.println("\nNow benchmarking...");
   
    timer3.start();
    for (int i=0; i<runs; i++) {
      A.zMult(B,C);
View Full Code Here


 

  long before = Runtime.getRuntime().freeMemory();
  long size = (((long)rows)*columns)*runs;

  DoubleMatrix2D  matrix = null;
  if (kind.equals("sparse")) matrix = new SparseDoubleMatrix2D(rows,columns,initialCapacity,minLoadFactor,maxLoadFactor);
  else if (kind.equals("dense")) matrix = new DenseDoubleMatrix2D(rows,columns);
  //else if (kind.equals("denseArray")) matrix = new DoubleArrayMatrix2D(rows,columns);
  else throw new RuntimeException("unknown kind");
 
  System.out.println("\nNow filling...");
  //if (kind.equals("sparse")) ((SparseDoubleMatrix2D)matrix).elements.hashCollisions = 0;
  for (int i=0; i<runs; i++) {
    matrix.assign(0);
    matrix.ensureCapacity(initialCapacity);
    if (kind.equals("sparse")) ((SparseDoubleMatrix2D)matrix).ensureCapacity(initialCapacity);
    timer1.start();
    int value = 0;
    for (int row=0; row < rows; row++) {
      for (int column=0; column < columns; column++) {
        matrix.setQuick(row,column,value++);
      }
    }
    timer1.stop();
  }
  timer1.display();
  timer1.minus(emptyLoop).display();
  System.out.println(size / timer1.minus(emptyLoop).seconds() +" elements / sec");
 
  Runtime.getRuntime().gc(); // invite gc
  try { Thread.currentThread().sleep(1000); } catch (InterruptedException exc) {};
  long after = Runtime.getRuntime().freeMemory();
  System.out.println("KB needed="+(before-after) / 1024);
  System.out.println("bytes needed per non-zero="+(before-after) / (double)matrix.cardinality());
  if (print) {
    System.out.println(matrix);
    if (kind.equals("sparse")) System.out.println("map="+((SparseDoubleMatrix2D)matrix).elements);
  }
  /*
  if (kind.equals("sparse")) {
    int hashCollisions = ((SparseDoubleMatrix2D)matrix).elements.hashCollisions;
    System.out.println("hashCollisions="+hashCollisions);
    System.out.println("--> "+ ((double)hashCollisions / (rows*columns)) +" hashCollisions/element on average.");
  }
  */
 
  System.out.println("\nNow reading...");
  //if (kind.equals("sparse")) ((SparseDoubleMatrix2D)matrix).elements.hashCollisions = 0;
  timer2.start();
  double element=0;
  for (int i=0; i<runs; i++) {
    for (int row=0; row < rows; row++) {
      for (int column=0; column < columns; column++) {
        element += matrix.getQuick(row,column);
      }
    }   
  }
  timer2.stop().display()
  timer2.minus(emptyLoop2).display();
  System.out.println(size / timer2.minus(emptyLoop2).seconds() +" elements / sec");
  if (print) System.out.println(matrix);
  //if (kind.equals("sparse")) System.out.println("hashCollisions="+((SparseDoubleMatrix2D)matrix).elements.hashCollisions);
  System.out.println(element); // !!! so that the jitter can't optimize away the whole loop
 
  System.out.println("\nNow reading view...");
  DoubleMatrix2D view = matrix.viewPart(0,0,rows,columns);
  timer4.start();
  element=0;
  for (int i=0; i<runs; i++) {
    for (int row=0; row < rows; row++) {
      for (int column=0; column < columns; column++) {
        element += view.getQuick(row,column);
      }
    }   
  }
  timer4.stop().display()
  timer4.minus(emptyLoop2).display();
View Full Code Here

  cern.colt.Timer timer1 = new cern.colt.Timer();
  cern.colt.Timer timer2 = new cern.colt.Timer();

  long size = (((long)rows)*columns)*runs;

  DoubleMatrix2D  matrix = null;
  if (kind.equals("sparse")) matrix = new SparseDoubleMatrix2D(rows,columns,initialCapacity,minLoadFactor,maxLoadFactor);
  else if (kind.equals("dense")) matrix = new DenseDoubleMatrix2D(rows,columns);
  //else if (kind.equals("denseArray")) matrix = new DoubleArrayMatrix2D(rows,columns);
  else throw new RuntimeException("unknown kind");
 
  System.out.println("\nNow multiplying...");
  matrix.assign(1);
  //if (kind.equals("sparse")) ((SparseDoubleMatrix2D)matrix).elements.hashCollisions = 0;
  for (int i=0; i<runs; i++) {
    timer1.start();
    cern.colt.matrix.doublealgo.Transform.mult(matrix, 3);
    timer1.stop();
  }
  timer1.display();
  System.out.println(size / timer1.seconds() +" elements / sec");
 
  if (print) {
    System.out.println(matrix);
  }
  /*
  if (kind.equals("sparse")) {
    int hashCollisions = ((SparseDoubleMatrix2D)matrix).elements.hashCollisions;
    System.out.println("hashCollisions="+hashCollisions);
    System.out.println("--> "+ ((double)hashCollisions / (rows*columns)) +" hashCollisions/element on average.");
  }
  */
 
  System.out.println("\nNow multiplying2...");
  matrix.assign(1);
  //if (kind.equals("sparse")) ((SparseDoubleMatrix2D)matrix).elements.hashCollisions = 0;
  for (int i=0; i<runs; i++) {
    timer2.start();
    cern.colt.matrix.doublealgo.Transform.mult(matrix,3);
    timer2.stop();
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

@return the new view.
   
*/
public DoubleMatrix2D viewPart(final int row, final int column, int height, int width) {
  checkBox(row,column,height,width);
  DoubleMatrix2D view = new WrapperDoubleMatrix2D(this) {
    public double getQuick(int i, int j) {
      return content.get(row+i,column+j);
    }
    public void setQuick(int i, int j, double value) {
      content.set(row+i,column+j,value);
View Full Code Here

@return a new flip view.
@see #viewColumnFlip()
*/
public DoubleMatrix2D viewRowFlip() {
  if (rows==0) return this;
  DoubleMatrix2D view = new WrapperDoubleMatrix2D(this) {
    public double getQuick(int row, int column) {
      return content.get(rows-1-row,column);
    }
    public void setQuick(int row, int column, double value) {
      content.set(rows-1-row,column,value);
View Full Code Here

  checkRowIndexes(rowIndexes);
  checkColumnIndexes(columnIndexes);
  final int[] rix = rowIndexes;
  final int[] cix = columnIndexes;
 
  DoubleMatrix2D view = new WrapperDoubleMatrix2D(this) {
    public double getQuick(int i, int j) {
      return content.get(rix[i],cix[j]);
    }
    public void setQuick(int i, int j, double value) {
      content.set(rix[i],cix[j],value);
View Full Code Here

@return a new view.
@throws  IndexOutOfBoundsException if <tt>rowStride<=0 || columnStride<=0</tt>.
*/
public DoubleMatrix2D viewStrides(final int _rowStride, final int _columnStride) {
  if (_rowStride<=0 || _columnStride<=0) throw new IndexOutOfBoundsException("illegal stride");
  DoubleMatrix2D view = new WrapperDoubleMatrix2D(this) {
    public double getQuick(int row, int column) {
      return content.get(_rowStride*row, _columnStride*column);
    }
    public void setQuick(int row, int column, double value) {
      content.set(_rowStride*row, _columnStride*column, value);
View Full Code Here

/**
*/
public static void doubleTest() {
int rows = 4;
int columns = 5; // make a 4*5 matrix
DoubleMatrix2D master = new DenseDoubleMatrix2D(rows,columns);
System.out.println(master);
master.assign(1); // set all cells to 1
System.out.println("\n"+master);
master.viewPart(2,1,2,3).assign(2); // set [2,1] .. [3,3] to 2
System.out.println("\n"+master);

DoubleMatrix2D copyPart = master.viewPart(2,1,2,3).copy();
copyPart.assign(3); // modify an independent copy
copyPart.set(0,0,4);
System.out.println("\n"+copyPart); // has changed
System.out.println("\n"+master); // master has not changed

DoubleMatrix2D view1 = master.viewPart(0,3,4,2); // [0,3] .. [3,4]
DoubleMatrix2D view2 = view1.viewPart(0,0,4,1); // a view from a view
System.out.println("\n"+view1);
System.out.println("\n"+view2);
}
View Full Code Here

System.out.println("\n"+view2);
}
/**
*/
public static void doubleTest(int rows, int columns, int initialCapacity, double minLoadFactor, double maxLoadFactor) {
  DoubleMatrix2D matrix = new SparseDoubleMatrix2D(rows,columns,initialCapacity,minLoadFactor,maxLoadFactor);
  System.out.println(matrix);
 
  System.out.println("adding...");
  int i=0;
  for (int column=0; column < columns; column++) {
    for (int row=0; row < rows; row++) {
      //if (i%1000 == 0) {
         matrix.set(row,column,i);
      //}
      i++;
    }
  }
  System.out.println(matrix);

  System.out.println("removing...");
  for (int column=0; column < columns; column++) {
    for (int row=0; row < rows; row++) {
      //if (i%1000 == 0) {
        matrix.set(row,column,0);
      //}
    }
  }

  System.out.println(matrix);
View Full Code Here

TOP

Related Classes of cern.colt.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.