@Override public int getNumNonZeroElements() { final DoubleIterator iterator = values.values().iterator(); int numNonZeros = 0; for( int i = values.size(); i-- != 0; ) if ( iterator.nextDouble() != 0 ) numNonZeros++; return numNonZeros; }
/** * Compute the sum of the elements of a map. * @param v The vector * @return The sum of the values of {@code v}. */ public static double sumAbs(Long2DoubleMap v) { double sum = 0; DoubleIterator iter = v.values().iterator(); while (iter.hasNext()) { sum += Math.abs(iter.nextDouble()); } return sum; }
/** * Compute the sum of the elements of a map. * @param v The vector * @return The sum of the values of {@code v}. */ public static double sum(Long2DoubleMap v) { double sum = 0; DoubleIterator iter = v.values().iterator(); while (iter.hasNext()) { sum += iter.nextDouble(); } return sum; }
/** * Compute the sum of the squares of elements of a map. * @param v The vector * @return The sum of the squares of the values of {@code v}. */ public static double sumOfSquares(Long2DoubleMap v) { if (v instanceof Long2DoubleSortedArrayMap) { return sumOfSquares((Long2DoubleSortedArrayMap) v); } else { double sum = 0; DoubleIterator iter = v.values().iterator(); while (iter.hasNext()) { double d = iter.nextDouble(); sum += d * d; } return sum; } }
@Override public InvertibleFunction<Long2DoubleMap,Long2DoubleMap> makeTransformation(Long2DoubleMap reference) { if (reference.isEmpty()) { return new IdentityVectorNormalizer().makeTransformation(reference); } else { final double mean = Vectors.mean(reference); double var = 0; DoubleIterator iter = reference.values().iterator(); while (iter.hasNext()) { final double v = iter.nextDouble(); final double diff = v - mean; var += diff * diff; } if (Scalars.isZero(var) && Scalars.isZero(damping)) { logger.warn("found zero variance for {}, and no damping is enabled", reference); } /* damping calculation as described in Hofmann '04 * $\sigma_u^2 = \frac{\sigma^2 + q * \={\sigma}^2}{n_u + q}$ */ double stdev = Math.sqrt((var + damping * globalVariance) / (reference.size() + damping)); return new Transform(mean, stdev); } }
@Override public DoubleIterator iterator() { return collection.iterator(); } @Override
@Override public DoubleIterator iterator() { return DoubleIterators.unmodifiable(collection.iterator()); } @Override
/** * Creates a new array list and fills it with a given type-specific collection. * * @param c * a type-specific collection that will be used to fill the array * list. */ public DoubleArrayList(final DoubleCollection c) { this(c.size()); size = DoubleIterators.unwrap(c.iterator(), a); } /**
/** * Compute and return the L2 norm (Euclidian length) of the vector. * * @return The L2 norm of the vector */ public double norm() { double ssq = 0; DoubleIterator iter = values().iterator(); while (iter.hasNext()) { double v = iter.nextDouble(); ssq += v * v; } return Math.sqrt(ssq); }
/** * Compute the sum of the elements of a map. * @param v The vector * @return The sum of the values of {@code v}. */ public static double sumAbs(Long2DoubleMap v) { double sum = 0; DoubleIterator iter = v.values().iterator(); while (iter.hasNext()) { sum += Math.abs(iter.nextDouble()); } return sum; }
@Override public boolean containsAll(final DoubleCollection c) { for (final DoubleIterator i = c.iterator(); i.hasNext();) if (!contains(i.nextDouble())) return false; return true; } @Override
@Override public boolean addAll(final DoubleCollection c) { boolean retVal = false; for (final DoubleIterator i = c.iterator(); i.hasNext();) if (add(i.nextDouble())) retVal = true; return retVal; } @Override
@Override public boolean removeAll(final DoubleCollection c) { boolean retVal = false; for (final DoubleIterator i = c.iterator(); i.hasNext();) if (rem(i.nextDouble())) retVal = true; return retVal; } @Override
@Override public int getNumNonZeroElements() { final DoubleIterator iterator = values.values().iterator(); int numNonZeros = 0; for( int i = values.size(); i-- != 0; ) if ( iterator.nextDouble() != 0 ) numNonZeros++; return numNonZeros; }
/** * Compute the sum of the elements of a map. * @param v The vector * @return The sum of the values of {@code v}. */ public static double sum(Long2DoubleMap v) { double sum = 0; DoubleIterator iter = v.values().iterator(); while (iter.hasNext()) { sum += iter.nextDouble(); } return sum; }
@Override public boolean addAll(int index, final DoubleCollection c) { ensureIndex(index); int n = c.size(); if (n == 0) return false; grow(size + n); if (index != size) System.arraycopy(a, index, a, index + n, size - index); final DoubleIterator i = c.iterator(); size += n; while (n-- != 0) a[index++] = i.nextDouble(); assert size <= a.length; return true; } @Override
/** * Creates a new big-array big list and fills it with a given type-specific * collection. * * @param c * a type-specific collection that will be used to fill the array * list. */ public DoubleBigArrayBigList(final DoubleCollection c) { this(c.size()); for (DoubleIterator i = c.iterator(); i.hasNext();) add(i.nextDouble()); } /**
@Override public boolean addAll(int index, final DoubleCollection c) { ensureIndex(index); final DoubleIterator i = c.iterator(); final boolean retVal = i.hasNext(); while (i.hasNext()) add(index++, i.nextDouble()); return retVal; } /**
/** * Compute the sum of the squares of elements of a map. * @param v The vector * @return The sum of the squares of the values of {@code v}. */ public static double sumOfSquares(Long2DoubleMap v) { if (v instanceof Long2DoubleSortedArrayMap) { return sumOfSquares((Long2DoubleSortedArrayMap) v); } else { double sum = 0; DoubleIterator iter = v.values().iterator(); while (iter.hasNext()) { double d = iter.nextDouble(); sum += d * d; } return sum; } }
/** Copies the argument internally, rescaling weights so they sum up to one. * * @param index2Weight the new map from indices to weights. * @return true. */ public synchronized boolean setWeights( final Reference2DoubleMap<Index> index2Weight ) { this.index2Weight.clear(); this.index2Weight.defaultReturnValue( 0 ); // Since we're setting up values, we must assume missing indices have weight 0. double weightSum = 0; for( DoubleIterator i = index2Weight.values().iterator(); i.hasNext(); ) weightSum += i.nextDouble(); if ( weightSum == 0 ) weightSum = 1; // No positive weights. for( ObjectIterator<Entry<Index, Double>> i = index2Weight.entrySet().iterator(); i.hasNext(); ) { Reference2DoubleMap.Entry<Index> e = (Reference2DoubleMap.Entry<Index>)i.next(); this.index2Weight.put( e.getKey(), e.getDoubleValue() / weightSum ); } this.index2Weight.trim(); LOGGER.debug( "New weight map for " + this + ": " + this.index2Weight ); return true; }