/** * Transforms the given vector into this vector. * * @param vec a double vector. */ public DenseDoubleVector(DoubleVector vec) { this.vector = new double[vec.getDimension()]; if (vec.isSparse()) { Iterator<DoubleVectorElement> iterateNonZero = vec.iterateNonZero(); while (iterateNonZero.hasNext()) { DoubleVectorElement next = iterateNonZero.next(); this.vector[next.getIndex()] = next.getValue(); } } else { System.arraycopy(vec.toArray(), 0, this.vector, 0, this.vector.length); } }
@Override public DoubleVector apply(DoubleVector vector) { DoubleVector newInstance = newInstance(vector); if (vector.isSparse()) { Iterator<DoubleVectorElement> iterateNonZero = vector.iterateNonZero(); while (iterateNonZero.hasNext()) { DoubleVectorElement next = iterateNonZero.next(); newInstance.set(next.getIndex(), apply(next.getValue())); } } else { for (int i = 0; i < vector.getDimension(); i++) { newInstance.set(i, apply(vector.get(i))); } } return newInstance; }
@Override public DoubleVector pow(double x) { DoubleVector v = new SparseBitVector(getDimension()); Iterator<DoubleVectorElement> it = iterateNonZero(); while (it.hasNext()) { DoubleVectorElement e = it.next(); double value = 0.0d; if (x == 2d) { value = e.getValue() * e.getValue(); } else { value = FastMath.pow(e.getValue(), x); } v.set(e.getIndex(), value); } return v; }
@Override public double dot(DoubleVector s) { double dotProduct = 0.0d; if (s.isSparse()) { Iterator<DoubleVectorElement> iterateNonZero = s.iterateNonZero(); while (iterateNonZero.hasNext()) { DoubleVectorElement next = iterateNonZero.next(); dotProduct += this.get(next.getIndex()) * next.getValue(); } } else { for (int i = 0; i < getLength(); i++) { dotProduct += this.get(i) * s.get(i); } } return dotProduct; }
@Override public DoubleMatrix multiplyElementWise(DoubleMatrix other) { DoubleMatrix result = new SparseDoubleRowMatrix(this.getRowCount(), this.getColumnCount()); for (int row : this.matrix.keys()) { Iterator<DoubleVectorElement> iterateNonZero = matrix.get(row) .iterateNonZero(); while (iterateNonZero.hasNext()) { DoubleVectorElement e = iterateNonZero.next(); result.set(row, e.getIndex(), get(row, e.getIndex()) * other.get(row, e.getIndex())); } } return result; }
@Override protected final DoubleVectorElement computeNext() { // look for the element that has a bit set while (index < getDimension() && !vector.get(index)) { index++; } if (index < getDimension()) { element.setIndex(index); element.setValue(get(index)); index++; return element; } else { return endOfData(); } }
@Override public double dot(DoubleVector s) { double dotProduct = 0.0d; // take a shortcut by just iterating over the non-zero elements of the // smaller vector of both multiplicants. DoubleVector smallestVector = s.getLength() < getLength() ? s : this; DoubleVector largerVector = smallestVector == this ? s : this; Iterator<DoubleVectorElement> it = smallestVector.iterateNonZero(); while (it.hasNext()) { DoubleVectorElement next = it.next(); double d = largerVector.get(next.getIndex()); dotProduct += d * next.getValue(); } return dotProduct; }
@Override public DoubleVector gradient(DoubleVector vector) { DoubleVector newInstance = newInstance(vector); if (vector.isSparse()) { Iterator<DoubleVectorElement> iterateNonZero = vector.iterateNonZero(); while (iterateNonZero.hasNext()) { DoubleVectorElement next = iterateNonZero.next(); newInstance.set(next.getIndex(), gradient(next.getValue())); } } else { for (int i = 0; i < vector.getDimension(); i++) { newInstance.set(i, gradient(vector.get(i))); } } return newInstance; }
@Override public DoubleVector pow(double x) { DoubleVector v = new SparseDoubleVector(this.dimension, this.vector.size()); Iterator<DoubleVectorElement> it = iterateNonZero(); while (it.hasNext()) { DoubleVectorElement e = it.next(); double value = 0.0d; if (x == 2d) { value = e.getValue() * e.getValue(); } else { value = FastMath.pow(e.getValue(), x); } v.set(e.getIndex(), value); } return v; }
@Override public double dot(DoubleVector s) { double dotProduct = 0.0d; // take a shortcut by just iterating over the non-zero elements of the // smaller vector of both multiplicants. DoubleVector smallestVector = s.getLength() < getLength() ? s : this; DoubleVector largerVector = smallestVector == this ? s : this; Iterator<DoubleVectorElement> it = smallestVector.iterateNonZero(); while (it.hasNext()) { DoubleVectorElement next = it.next(); double d = largerVector.get(next.getIndex()); dotProduct += d * next.getValue(); } return dotProduct; }
@Override public DoubleVector multiply(DoubleVector s) { DoubleVector vec = new SparseBitVector(s.getDimension()); // take a shortcut by just iterating over the non-zero elements of the // smaller vector of both multiplicants. DoubleVector smallestVector = s.getLength() < getLength() ? s : this; DoubleVector largerVector = smallestVector == this ? s : this; Iterator<DoubleVectorElement> it = smallestVector.iterateNonZero(); while (it.hasNext()) { DoubleVectorElement next = it.next(); double otherValue = largerVector.get(next.getIndex()); vec.set(next.getIndex(), next.getValue() * otherValue); } return vec; }
@Override public double dot(DoubleVector s) { double dotProduct = 0.0d; // take a shortcut by just iterating over the non-zero elements of the // smaller vector of both multiplicants. DoubleVector smallestVector = s.getLength() < getLength() ? s : this; DoubleVector largerVector = smallestVector == this ? s : this; Iterator<DoubleVectorElement> it = smallestVector.iterateNonZero(); while (it.hasNext()) { DoubleVectorElement next = it.next(); double d = largerVector.get(next.getIndex()); dotProduct += d * next.getValue(); } return dotProduct; }
@Override public DoubleVector multiplyVectorRow(DoubleVector v) { DoubleVector result = new SparseDoubleVector(this.getRowCount()); for (int row : matrix.keys()) { Iterator<DoubleVectorElement> iterateNonZero = matrix.get(row) .iterateNonZero(); double sum = 0.0d; while (iterateNonZero.hasNext()) { DoubleVectorElement e = iterateNonZero.next(); sum += (e.getValue() * v.get(e.getIndex())); } result.set(row, sum); } return result; }
@Override public DoubleVector multiply(DoubleVector s) { // take a shortcut by just iterating over the non-zero elements of the // smaller vector of both multiplicants. DoubleVector smallestVector = s.getLength() < getLength() ? s : this; DoubleVector vec = new SparseDoubleVector(s.getDimension(), smallestVector.getLength()); DoubleVector largerVector = smallestVector == this ? s : this; Iterator<DoubleVectorElement> it = smallestVector.iterateNonZero(); while (it.hasNext()) { DoubleVectorElement next = it.next(); double otherValue = largerVector.get(next.getIndex()); vec.set(next.getIndex(), next.getValue() * otherValue); } return vec; }
@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; }
private double getProbabilityForClass(DoubleVector document, int classIndex) { double probabilitySum = 0.0d; Iterator<DoubleVectorElement> iterateNonZero = document.iterateNonZero(); while (iterateNonZero.hasNext()) { DoubleVectorElement next = iterateNonZero.next(); double wordCount = next.getValue(); double probabilityOfToken = probabilityMatrix.get(classIndex, next.getIndex()); if (probabilityOfToken == 0d) { probabilityOfToken = LOW_PROBABILITY; } probabilitySum += (wordCount * probabilityOfToken); } return probabilitySum; }
@Override public DoubleVector multiply(DoubleVector s) { // take a shortcut by just iterating over the non-zero elements of the // smaller vector of both multiplicants. DoubleVector smallestVector = s.getLength() < getLength() ? s : this; DoubleVector vec = new SequentialSparseDoubleVector(s.getDimension()); DoubleVector largerVector = smallestVector == this ? s : this; Iterator<DoubleVectorElement> it = smallestVector.iterateNonZero(); while (it.hasNext()) { DoubleVectorElement next = it.next(); double otherValue = largerVector.get(next.getIndex()); vec.set(next.getIndex(), next.getValue() * otherValue); } return vec; }
@Override public DoubleMatrix add(DoubleMatrix other) { SparseDoubleRowMatrix result = new SparseDoubleRowMatrix( other.getRowCount(), other.getColumnCount()); for (int row : this.matrix.keys()) { Iterator<DoubleVectorElement> iterate = matrix.get(row).iterate(); while (iterate.hasNext()) { DoubleVectorElement e = iterate.next(); result.set(row, e.getIndex(), e.getValue() + other.get(row, e.getIndex())); } } return result; }
@Override public DoubleMatrix subtract(DoubleMatrix other) { SparseDoubleRowMatrix result = new SparseDoubleRowMatrix( other.getRowCount(), other.getColumnCount()); for (int row : this.matrix.keys()) { Iterator<DoubleVectorElement> iterate = matrix.get(row).iterate(); while (iterate.hasNext()) { DoubleVectorElement e = iterate.next(); result.set(row, e.getIndex(), e.getValue() - other.get(row, e.getIndex())); } } return result; }