Package mikera.indexz

Examples of mikera.indexz.Index


  /**
   * Creates a SparseIndexedVector with the specified index and data values.
   * Performs no checking - Index must be distinct and sorted.
   */
  public static SparseIndexedVector wrap(int length, int[] indices, double[] data) {
    Index index=Index.wrap(indices);
    assert(index.length()==data.length);
    assert(index.isDistinctSorted());
    return new SparseIndexedVector(length, index,data);
  }
View Full Code Here


  }
 
  public static SparseIndexedVector create(ASparseVector source) {
    int length = source.length();
    if (length==0) throw new IllegalArgumentException("Can't create a length 0 SparseIndexedVector");
    Index ixs=source.nonSparseIndex();
    int n=ixs.length();
    double[] vals=new double[n];
    for (int i=0; i<n; i++) {
      vals[i]=source.unsafeGet(ixs.unsafeGet(i));
    }
    return wrap(length,ixs,vals);
  }
View Full Code Here

  }
 
  public static SparseIndexedVector create(SparseHashedVector source) {
    int length = source.length();
    if (length==0) throw new IllegalArgumentException("Can't create a length 0 SparseIndexedVector");
    Index ixs=source.nonSparseIndex();
    int n=ixs.length();
    double[] vals=new double[n];
    for (int i=0; i<n; i++) {
      vals[i]=source.unsafeGet(ixs.unsafeGet(i));
    }
    return wrap(length,ixs,vals);
  }
View Full Code Here

    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: ");
View Full Code Here

  }
 
  @Override
  public boolean isRangeZero(int start, int length) {
    int end=start+length;
    Index index=internalIndex();
    double[] data=internalData();
    int si=index.seekPosition(start);
    int di=index.seekPosition(end);
    for (int i=si; i<di; i++) {
      if (data[i]!=0.0) return false;
    }
    return true;
  }
View Full Code Here

  @Override
  public int[] nonZeroIndices() {
    int n=(int)nonZeroCount();
    double[] data=internalData();
    Index index=internalIndex();
    int[] ret=new int[n];
    int di=0;
    for (int i=0; i<data.length; i++) {
      if (data[i]!=0.0) ret[di++]=index.get(i);
    }
    if (di!=n) throw new VectorzException("Invalid non-zero index count. Maybe concurrent modification of vector?");
    return ret;
  }
View Full Code Here

     
  @Override
  public void addToArray(int offset, double[] array, int arrayOffset, int length) {
    assert((offset>=0)&&(offset+length<=this.length));
    double[] data=internalData();
    Index index=internalIndex();
   
    int start=index.seekPosition(offset);
    int[] ixs=index.data;
    int dataLength=data.length;
    for (int j=start; j<dataLength; j++) {
      int di=ixs[j]-offset; // index relative to offset
      if (di>=length) return;
View Full Code Here

  @Override
  public void addMultipleToArray(double factor,int offset, double[] array, int arrayOffset, int length) {
    int aOffset=arrayOffset-offset;
   
    double[] data=internalData();
    Index index=internalIndex();
    int[] ixs=index.data;
    int start=index.seekPosition(offset);
    for (int i=start; i<data.length; i++) {
      int di=ixs[i];
      // if (di<offset) continue; not needed because of seekPosition!
      if (di>=(offset+length)) return;
      array[di+aOffset]+=factor*data[i];
 
View Full Code Here

    }
    assert(offset>=0);
    assert(offset+length<=length());
   
    double[] data=internalData();
    Index index=internalIndex();
    int[] ixs=index.data;
    int dataLength=data.length;
    for (int j=index.seekPosition(offset); j<dataLength; j++) {
      int i =ixs[j]-offset; // index relative to offset
      if (i>=length) return;
      array[i+arrayOffset]+=factor*data[j]*other.get(i+otherOffset);
    }   
  }
 
View Full Code Here

    assert(offset+length<=length());
    double[] otherArray=other.getArray();
    otherOffset+=other.getArrayOffset();
   
    double[] data=internalData();
    Index index=internalIndex();
    int[] ixs=index.data;
    int dataLength=data.length;
    for (int j=index.seekPosition(offset); j<dataLength; j++) {
      int i =ixs[j]-offset; // index relative to offset
      if (i>=length) return;
      array[i+arrayOffset]+=factor*data[j]*otherArray[i+otherOffset];
    }   
  }
 
View Full Code Here

TOP

Related Classes of mikera.indexz.Index

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.