/** * Returns the observations stored in this object as a <TT>String</TT>. * */ public String toString() { StringBuffer sb = new StringBuffer (); for (int i=0; i<numberObs(); i++) sb.append (i + " " + array.getQuick(i) + PrintfFormat.NEWLINE); return sb.toString(); }
/** * Returns the matrix cell value at coordinate <tt>[row,column]</tt>. * * <p>Provided with invalid parameters this method may return invalid objects without throwing any exception. * <b>You should only use this method when you are absolutely sure that the coordinate is within bounds.</b> * Precondition (unchecked): <tt>0 <= column < columns() && 0 <= row < rows()</tt>. * * @param row the index of the row-coordinate. * @param column the index of the column-coordinate. * @return the value at the specified coordinate. */ public double getQuick(int row, int column) { int k=-1; if (indexes[row] != null) k = indexes[row].binarySearch(column); if (k<0) return 0; return values[row].getQuick(k); } /**
/** * Returns the matrix cell value at coordinate <tt>[row,column]</tt>. * * <p>Provided with invalid parameters this method may return invalid objects without throwing any exception. * <b>You should only use this method when you are absolutely sure that the coordinate is within bounds.</b> * Precondition (unchecked): <tt>0 <= column < columns() && 0 <= row < rows()</tt>. * * @param row the index of the row-coordinate. * @param column the index of the column-coordinate. * @return the value at the specified coordinate. */ public double getQuick(int row, int column) { int k = indexes.binarySearchFromTo(column,starts[row],starts[row+1]-1); double v = 0; if (k>=0) v = values.getQuick(k); return v; } protected void insert(int row, int column, int index, double value) {
/** * Returns the matrix cell value at coordinate <tt>[row,column]</tt>. * * <p>Provided with invalid parameters this method may return invalid objects without throwing any exception. * <b>You should only use this method when you are absolutely sure that the coordinate is within bounds.</b> * Precondition (unchecked): <tt>0 <= column < columns() && 0 <= row < rows()</tt>. * * @param row the index of the row-coordinate. * @param column the index of the column-coordinate. * @return the value at the specified coordinate. */ public double getQuick(int row, int column) { int k=-1; if (indexes[row] != null) k = indexes[row].binarySearch(column); if (k<0) return 0; return values[row].getQuick(k); } /**
/** * Returns the matrix cell value at coordinate <tt>[row,column]</tt>. * * <p>Provided with invalid parameters this method may return invalid objects without throwing any exception. * <b>You should only use this method when you are absolutely sure that the coordinate is within bounds.</b> * Precondition (unchecked): <tt>0 <= column < columns() && 0 <= row < rows()</tt>. * * @param row the index of the row-coordinate. * @param column the index of the column-coordinate. * @return the value at the specified coordinate. */ public double getQuick(int row, int column) { int k = indexes.binarySearchFromTo(column,starts[row],starts[row+1]-1); double v = 0; if (k>=0) v = values.getQuick(k); return v; } protected void insert(int row, int column, int index, double value) {
/** * Returns the Euclid norm which is sum(x[i]^2) * @return */ public double getEuclidNorm() { DoubleArrayList dbls = new DoubleArrayList(this.cardinality()); this.getNonZeros(null, dbls); double norm = 0; for (int i = 0; i < dbls.size(); i++) { norm += Math.pow(dbls.getQuick(i), 2); } return Math.sqrt(norm); }
public double nextDouble() { if (!caching) return rvg.nextDouble(); else if (index >= values.size()) { double v = rvg.nextDouble(); values.add (v); ++index; return v; } else return values.getQuick (index++); }
public double nextDouble () { if (!caching) return stream.nextDouble(); else if (index >= values.size()) { double v = stream.nextDouble(); values.add (v); ++index; return v; } else return values.getQuick (index++); }
protected double productQuick(DoubleMatrix1D v1, DoubleMatrix1D v2) { IntArrayList indexList = new IntArrayList(); DoubleArrayList valueList = new DoubleArrayList(); v1.getNonZeros(indexList, valueList); double prod = 0.0; for (int i = 0; i < indexList.size(); ++i) { double temp = v2.getQuick(indexList.getQuick(i)); if (temp != 0.0) { prod += valueList.getQuick(i) * temp; } } // double prod = 0.0; // for (int i = 0; i < v1.size(); ++i) { // double temp1 = v1.getQuick(i); // double temp2 = v2.getQuick(i); // if (temp1 != 0.0 || temp2 != 0.0) { // prod += temp1 * temp2; // } // } return prod; }
/** * Normalization by dividing with Euclid norm. * @param vector */ public SparseVector normalizeVector(SparseVector vector) { //NORMALIZE HERE with norm1 so a unit vector is produced IntArrayList indexes = new IntArrayList(vector.cardinality()); DoubleArrayList dbls = new DoubleArrayList(vector.cardinality()); double norm = vector.getEuclidNorm(); vector.getNonZeros(indexes, dbls); for (int i = 0; i < indexes.size(); i++) { vector.setQuick(indexes.get(i), dbls.getQuick(i) / norm); } return vector; }
valueList.setSize(keyList.size()); for (int i = keyList.size(); --i >= 0;) { valueList.setQuick(i, get(keyList.getQuick(i)));
valueList.setSize(keyList.size()); for (int i=keyList.size(); --i >= 0; ) { valueList.setQuick(i,get(keyList.getQuick(i)));
valueList.setSize(keyList.size()); for (int i=keyList.size(); --i >= 0; ) { valueList.setQuick(i,get(keyList.getQuick(i)));
/** * Linear algebraic matrix-vector multiplication; <tt>z = A * y</tt>. * <tt>z[i] = alpha*Sum(A[i,j] * y[j]) + beta*z[i], i=0..A.rows()-1, j=0..y.size()-1</tt>. * Where <tt>A == this</tt>. * @param y the source vector. * @param z the vector where results are to be stored. * * @throws IllegalArgumentException if <tt>A.columns() != y.size() || A.rows() > z.size())</tt>. */ protected void zMult(final DoubleMatrix1D y, final DoubleMatrix1D z, cern.colt.list.IntArrayList nonZeroIndexes, DoubleMatrix1D[] allRows, final double alpha, final double beta) { if (columns != y.size() || rows > z.size()) throw new IllegalArgumentException("Incompatible args: "+toStringShort()+", "+y.toStringShort()+", "+z.toStringShort()); z.assign(cern.jet.math.Functions.mult(beta/alpha)); for (int i = indexes.length; --i >= 0; ) { if (indexes[i] != null) { for (int k = indexes[i].size(); --k >= 0; ) { int j = indexes[i].getQuick(k); double value = values[i].getQuick(k); z.setQuick(i,z.getQuick(i) + value * y.getQuick(j)); } } } z.assign(cern.jet.math.Functions.mult(alpha)); } }
/** * Linear algebraic matrix-vector multiplication; <tt>z = A * y</tt>. * <tt>z[i] = alpha*Sum(A[i,j] * y[j]) + beta*z[i], i=0..A.rows()-1, j=0..y.size()-1</tt>. * Where <tt>A == this</tt>. * @param y the source vector. * @param z the vector where results are to be stored. * * @throws IllegalArgumentException if <tt>A.columns() != y.size() || A.rows() > z.size())</tt>. */ protected void zMult(final DoubleMatrix1D y, final DoubleMatrix1D z, cern.colt.list.IntArrayList nonZeroIndexes, DoubleMatrix1D[] allRows, final double alpha, final double beta) { if (columns != y.size() || rows > z.size()) throw new IllegalArgumentException("Incompatible args: "+toStringShort()+", "+y.toStringShort()+", "+z.toStringShort()); z.assign(cern.jet.math.Functions.mult(beta/alpha)); for (int i = indexes.length; --i >= 0; ) { if (indexes[i] != null) { for (int k = indexes[i].size(); --k >= 0; ) { int j = indexes[i].getQuick(k); double value = values[i].getQuick(k); z.setQuick(i,z.getQuick(i) + value * y.getQuick(j)); } } } z.assign(cern.jet.math.Functions.mult(alpha)); } }