/** * Add a dense vector back into the dense vector itself * <p> * * w = w + alpha * dv * * @param dv * the dense vector * @param alpha * the scalar */ public synchronized void addDenseVector(DenseVector dv, double alpha) { if (this.isExtendable() && this.needAllocateSpace(dv)) { this.allocateSpace(dv.getVectorLength()); } int n = dv.getVectorLength(); for (int i = 0; i < n; i++) { u[i] += alpha * dv.u[i]; } }
/** * return the dot product of a sparse feature vector and the dense vector * itself. Note that if the sparse vector contains some elements (feature * indexes) that do not exist in the dense vector, the dot product function * will ignore them (instead of throwing an exception) * * @param fv * Sparse feature vector * @return */ public double dotProduct(FeatureVector fv) { double ret = dotProduct(u, fv); return ret; }
/** * should avoid using this function, currently only for liblinear * @return */ public double[] getWeightArray(){ return super.getInternalArray(); } }
/** * Add a sparse vector back into the dense vector itself * <p> * * w = w + alpha * fv * * @param fv * A sparse feature vector * @param alpha * The scalar */ public synchronized void addSparseFeatureVector(FeatureVector fv, double alpha) { if (this.isExtendable() && this.needAllocateSpace(fv)) { this.allocateSpace(fv.maxIdx()); } int[] idx = fv.getIdx(); double[] value = fv.getValue(); for (int i = 0; i < idx.length; i++) { u[idx[i]] += alpha * value[i]; } }
/** * Decide if the dense vector contains features which are not covered by the * dense vector * * @param fv * @return */ public boolean needAllocateSpace(DenseVector dv) { boolean ret = (dv.getVectorLength() >= u.length); return ret; }
/** * Please use the addFeatureVector function instead ! * @param fv * @param alpha */ @Deprecated public synchronized void addToW(FeatureVector fv, double alpha) { super.addSparseFeatureVector(fv, alpha); }
/** * Initialize an element of the weight vector, if the weight vector does not * contain this item, allocate space internally * * @param index * The index of the initialized item * @param v * The value of the initialized item */ public synchronized void setElement(int index, double v) { assert extendable == true; if (index >= u.length) { System.out.println(index + " " + u.length); allocateSpace(index); } u[index] = v; }
public int getWeightVectorLength(){ return super.getVectorLength(); }
/** * Return the dot product of a dense feature vector and the dense vector * itself * * @param df * @return */ public double dotProduct(DenseVector df) { double res = 0.0; int l = df.getVectorLength(); if (u.length < l) { l = u.length; } for (int i = 0; i < l; i++) { res += u[i] * df.u[i]; } return res; }