Esempio n. 1
0
 /**
  * Create a {@code CompactSparseVector} from an array, saving only the non zero entries.
  *
  * @param array The double array to produce a sparse vector from.
  */
 public CompactSparseVector(SparseDoubleVector v) {
   int length = v.length();
   int[] nz = v.getNonZeroIndices();
   double[] values = new double[nz.length];
   for (int i = 0; i < nz.length; ++i) values[i] = v.get(nz[i]);
   vector = new SparseDoubleArray(nz, values, length);
   magnitude = -1;
 }
 @Override
 public DoubleMatrix subtract(DoubleVector vec) {
   DoubleMatrix result = new SparseDoubleRowMatrix(this.getRowCount(), this.getColumnCount());
   for (int row : this.matrix.keys()) {
     SparseDoubleVector rowVec = matrix.get(row);
     result.setRowVector(row, rowVec.subtract(vec.get(row)));
   }
   return result;
 }
 @Override
 public double get(int row, int col) {
   SparseDoubleVector vector = matrix.get(row);
   if (vector == null) {
     return NOT_FLAGGED;
   } else {
     return vector.get(col);
   }
 }
 public void handleContextVector(String primaryKey, String secondaryKey, SparseDoubleVector v) {
   assertEquals("foxes.n.123", secondaryKey);
   assertEquals("foxes", primaryKey);
   assertEquals(0, v.get(0), .001);
   assertEquals(1.0, v.get(1), .001);
   assertEquals(4.0, v.get(2), .001);
   assertEquals(4.5, v.get(3), .001);
   assertEquals(0, v.get(4), .001);
   assertEquals(2, v.get(5), .001);
 }
 @Override
 public void set(int row, int col, double value) {
   if (value != 0.0d) {
     SparseDoubleVector sparseDoubleVector = matrix.get(row);
     if (sparseDoubleVector == null) {
       sparseDoubleVector = new SparseDoubleVector(getColumnCount());
       matrix.put(row, sparseDoubleVector);
     }
     sparseDoubleVector.set(col, value);
   }
 }
 @Override
 public DoubleMatrix divide(DoubleVector vec) {
   DoubleMatrix result = new SparseDoubleRowMatrix(this.getRowCount(), this.getColumnCount());
   for (int row : this.matrix.keys()) {
     SparseDoubleVector rowVector = matrix.get(row);
     Iterator<DoubleVectorElement> iterateNonZero = rowVector.iterateNonZero();
     while (iterateNonZero.hasNext()) {
       DoubleVectorElement next = iterateNonZero.next();
       result.set(row, next.getIndex(), next.getValue() / vec.get(row));
     }
   }
   return result;
 }
Esempio n. 7
0
 public static DoubleVector scaleByMagnitude(SparseDoubleVector vector) {
   return new ScaledSparseDoubleVector(vector, 1d / vector.magnitude());
 }