/** * 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; }
/** * 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]; } }
/** * 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]; } }