@Override public Vector like(int cardinality) { return new SequentialAccessSparseVector(cardinality); }
@Test public void testNumNonZerosSequentialAccessSparse() { SequentialAccessSparseVector vector = new SequentialAccessSparseVector(10); vector.setQuick(3, 1); vector.set(5, 1); vector.setQuick(7, 0); vector.set(9, 0); assertEquals(2, vector.getNumNonZeroElements()); }
@SuppressWarnings("CloneDoesntCallSuperClone") @Override public SequentialAccessSparseVector clone() { return new SequentialAccessSparseVector(size(), values.clone()); }
public SequentialAccessSparseVector(SequentialAccessSparseVector other) { this(other.size(), other.getNumNondefaultElements()); values = other.values.clone(); }
protected Vector sparseItemRatingVector(final PreferenceArray prefs) { final SequentialAccessSparseVector ratings = new SequentialAccessSparseVector( Integer.MAX_VALUE, prefs.length()); for (final Preference preference : prefs) { ratings.set((int) preference.getUserID(), preference.getValue()); } return ratings; }
@Override public SequentialAccessSparseVector vectorToTest(int size) { SequentialAccessSparseVector r = new SequentialAccessSparseVector(size); Random gen = RandomUtils.getRandom(); for (int i = 0; i < 3; i++) { r.set(gen.nextInt(r.size()), gen.nextGaussian()); } return r; }
@Test public void testAll() { SequentialAccessSparseVector x = new SequentialAccessSparseVector(CARDINALITY); for (int i = 0; i < x.size(); ++i) { x.setQuick(i, i); } SequentialAccessSparseVector y = new SequentialAccessSparseVector(x); System.out.printf("function %s; operation %s\n", function, operation); operation.assign(x, y, function); for (int i = 0; i < x.size(); ++i) { assertEquals(x.getQuick(i), function.apply(i, i), 0.0); } } }
@Test public void testSparseVectorFullIteration() { int[] index = {0, 1, 2, 3, 4, 5}; double[] values = {1, 2, 3, 4, 5, 6}; assertEquals(index.length, values.length); int n = index.length; Vector vector = new SequentialAccessSparseVector(n); for (int i = 0; i < n; i++) { vector.set(index[i], values[i]); } for (int i = 0; i < n; i++) { assertEquals(vector.get(i), values[i], EPSILON); } int elements = 0; for (Element ignore : vector.all()) { elements++; } assertEquals(n, elements); assertFalse(new SequentialAccessSparseVector(0).iterator().hasNext()); }
public SequentialAccessSparseVector(Vector other) { this(other.size(), other.getNumNondefaultElements()); if (other.isSequentialAccess()) { for (Element e : other.nonZeroes()) { set(e.index(), e.get()); } } else { // If the incoming Vector to copy is random, then adding items // from the Iterator can degrade performance dramatically if // the number of elements is large as this Vector tries to stay // in order as items are added, so it's better to sort the other // Vector's elements by index and then add them to this copySortedRandomAccessSparseVector(other); } }
/** * must be synchronized, otherwise may get ArrayIndexOutOfBoundsException * @param dataPointIndex * @param featureIndex * @param featureValue */ @Override public synchronized void setFeatureValue(int dataPointIndex, int featureIndex, double featureValue) { if ((!this.hasMissingValue()) && Double.isNaN(featureValue)){ throw new IllegalArgumentException("missing value is not allowed in this data set"); } this.featureRows[dataPointIndex].set(featureIndex, featureValue); this.featureColumns[featureIndex].set(dataPointIndex, featureValue); }
@Override public boolean hasNext() { return element.getNextIndex() < SequentialAccessSparseVector.this.size(); }
@Override public Vector minus(Vector that) { if (size() != that.size()) { throw new CardinalityException(size(), that.size()); } // Here we compute "that - this" since it's not fast to randomly access "this" // and then invert at the end Vector result = that.clone(); Iterator<Element> iter = this.iterateNonZero(); while (iter.hasNext()) { Element thisElement = iter.next(); int index = thisElement.index(); result.setQuick(index, that.getQuick(index) - thisElement.get()); } result.assign(Functions.NEGATE); return result; }
@Override public Vector assign(Vector other) { int size = size(); if (size != other.size()) { throw new CardinalityException(size, other.size()); } if (other instanceof SequentialAccessSparseVector) { values = ((SequentialAccessSparseVector)other).values.clone(); } else { values = new OrderedIntDoubleMapping(); Iterator<Element> othersElems = other.iterateNonZero(); while (othersElems.hasNext()) { Element elem = othersElems.next(); setQuick(elem.index(), elem.get()); } } return this; }
@Override public double getLookupCost() { return Math.max(1, Math.round(Functions.LOG2.apply(getNumNondefaultElements()))); }
protected Vector sparseUserRatingVector(PreferenceArray prefs) { SequentialAccessSparseVector ratings = new SequentialAccessSparseVector(Integer.MAX_VALUE, prefs.length()); for (Preference preference : prefs) { ratings.set((int) preference.getItemID(), preference.getValue()); } return ratings; } }
public SequentialAccessSparseVector(Vector other) { this(other.size(), other.getNumNondefaultElements()); if (other.isSequentialAccess()) { Iterator<Element> it = other.iterateNonZero(); Element e; while (it.hasNext() && (e = it.next()) != null) { set(e.index(), e.get()); } } else { // If the incoming Vector to copy is random, then adding items // from the Iterator can degrade performance dramatically if // the number of elements is large as this Vector tries to stay // in order as items are added, so it's better to sort the other // Vector's elements by index and then add them to this copySortedRandomAccessSparseVector(other); } }
public SequentialAccessSparseVector(SequentialAccessSparseVector other) { this(other.size(), other.getNumNondefaultElements()); values = other.values.clone(); }
public SequentialAccessSparseVector(SequentialAccessSparseVector other, boolean shallowCopy) { super(other.size()); values = shallowCopy ? other.values : other.values.clone(); }
@Override public double getLookupCost() { return Math.max(1, Math.round(Functions.LOG2.apply(getNumNondefaultElements()))); }
@Override Vector generateTestVector(int cardinality) { return new SequentialAccessSparseVector(cardinality); }