@Override public double doubleValue(int i, int j) { if (arr[i] == null) { return defaultValue; } else { return arr[i].doubleValue(j); } }
@Override public double doubleValue(int idx) { return v.doubleValue(idx); }
@Override public <M extends Number> double innerProduct(Vector<M> y) { final ByteBuffer d = data(); double ip = 0.0; for (int i = 0; i < length; i += SIZE_OF_DOUBLE) { d.position(i); double v = d.getDouble(); d.putDouble(v * y.doubleValue(i / SIZE_OF_DOUBLE)); } return ip; }
@Override public <M extends Number> void sub(Vector<M> vector) { assert (vector.length() != length / SIZE_OF_DOUBLE); final ByteBuffer d = data(); for (int i = 0; i < length; i += SIZE_OF_DOUBLE) { d.position(i); double v = d.getDouble(); d.position(i); d.putDouble(v - vector.doubleValue(i / SIZE_OF_DOUBLE)); } }
@Override public <M extends Number, O extends Number> Vector<O> mult(Vector<M> x, Vectors.Factory<O> using) { assert (x.length() == alpha.length); final Vector<O> product = using.make(alpha.length, 0.0); for (int i = 0; i < alpha.length; i++) { double value = 0.0; if (i > 0) { value += x.doubleValue(i - 1) * beta[i - 1]; } value += x.doubleValue(i) * alpha[i]; if (i < beta.length) { value += x.doubleValue(i + 1) * beta[i]; } product.put(i, value); } return product; }
@Override public <M extends Number> void add(Vector<M> vector) { assert (vector.length() != length / SIZE_OF_DOUBLE); final ByteBuffer d = data(); for (int i = 0; i < length; i += SIZE_OF_DOUBLE) { d.position(i); double v = d.getDouble(); d.position(i); d.putDouble(v + vector.doubleValue(i / SIZE_OF_DOUBLE)); } }
@Override public <M extends Number> Vector<N> multTransposed(Vector<M> x) { assert (m == cols()); double[] result = new double[m]; Arrays.fill(result, m * defaultValue); for (int i = 0; i < arr.length; i++) { for (Map.Entry<Integer, N> e : arr[i].entrySet()) { result[e.getKey().intValue()] += x.doubleValue(i) * e.getValue().doubleValue() - defaultValue; } } return using.make(result); }
@Override public <M extends Number> double innerProduct(Vector<M> y) { assert (y.length() == data.length); if (y instanceof RealVector) { final RealVector y2 = (RealVector) y; double innerProduct = 0.0; for (int i = 0; i < data.length; i++) { innerProduct += data[i] * y2.data[i]; } return innerProduct; } else if (y.defaultValue().doubleValue() == 0.0) { double innerProduct = 0.0; for (Map.Entry<Integer, M> e : y.entrySet()) { innerProduct += data[e.getKey()] * e.getValue().doubleValue(); } return innerProduct; } else { double innerProduct = 0.0; for (int i = 0; i < data.length; i++) { innerProduct += data[i] * y.doubleValue(i); } return innerProduct; } }
@Override public <M extends Number> double innerProduct(Vector<M> y) { assert (y.length() == data.length); if (y instanceof IntVector) { final IntVector y2 = (IntVector) y; int innerProduct = 0; for (int i = 0; i < data.length; i++) { innerProduct += data[i] * y2.data[i]; } return innerProduct; } else if (y.defaultValue().doubleValue() == 0.0) { double innerProduct = 0.0; for (Map.Entry<Integer, M> e : y.entrySet()) { innerProduct += data[e.getKey()] * e.getValue().doubleValue(); } return innerProduct; } else { double innerProduct = 0.0; for (int i = 0; i < data.length; i++) { innerProduct += data[i] * y.doubleValue(i); } return innerProduct; } }
public double[] calculateSalience(Vector<Integer> termVec, int l) { double[] salience = new double[J]; for (int j = 0; j < J; j++) { final double xDocLength = x[j][l].sum().doubleValue(); for (int w : x[j][l].keySet()) { if (ctTotal[l][w] != 0) { salience[j] += termVec.doubleValue(w) * x[j][l].doubleValue(w) / ctTotal[l][w]; } } if (xDocLength != 0.0) { salience[j] /= xDocLength; } } // print(topKwords(salience, 10)); return salience; } //
/** * Calculate AA^T where A is this matrix * * @param outerProduct The matrix to store the result in * @return */ public void selfOuterProduct(Matrix<N> outerProduct) { assert (outerProduct.cols() == m); assert (outerProduct.rows() == m); for (int i = 0; i < m; i++) { for (Map.Entry<Integer, N> e : arr[i].entrySet()) { for (int j = 0; j < m; j++) { final double v = arr[j].doubleValue(e.getKey()) * e.getValue().doubleValue(); if (v != 0.0) { outerProduct.add(i, j, v); } } } } }
/** * Calculate A^TA where A is this matrix * * @param innerProduct The matrix to store the result in * @return */ public void selfInnerProduct(Matrix<N> innerProduct) { assert (innerProduct.cols() == n); assert (innerProduct.rows() == n); for (int k = 0; k < n; k++) { for (Map.Entry<Integer, N> e : arr[k].entrySet()) { final int j = e.getKey(); for (int i = 0; i < n; i++) { final double v = arr[k].doubleValue(i) * e.getValue().doubleValue(); if (v != 0) { innerProduct.add(i, j, v); } } } } }
public static double dfDiceCoefficient(Vector<Integer> vec1, Vector<Integer> vec2, Vector<Double> df, final StopWordList stopWordList) { double num = 0.0, denom = 0.0; for (Integer i : vec1.keySet()) { if (stopWordList.contains(i)) { continue; } if (i >= df.size()) { continue; } if (vec2.containsKey(i)) { num += (1.0 - df.doubleValue(i)); } denom += (1.0 - df.doubleValue(i)); } for (int i : vec2.keySet()) { if (stopWordList.contains(i)) { continue; } if (i >= df.size()) { continue; } denom += (1.0 - df.doubleValue(i)); } return denom == 0.0 ? 0.0 : (2.0 * num / denom); }
/** * Compute the householder vector of a given vector * * Complexity: O(sparsity(n)) * * @return A vector with v(1) = 1 and (I - 2vv^T /v^Tv)x is zero in all but * first component */ public static Vector<Double> house(Vector<Double> x, int j) { final int n = x.length(); double mu = 0.0; final double[] x2 = x.toDoubleArray(); for (int i = j; i < n; i++) { mu += x2[i] * x2[i]; } mu = Math.sqrt(mu); final Vector<Double> v = x.clone(); if (mu != 0.0) { final double beta = x.doubleValue(0) + Math.signum(x.doubleValue(0)) * mu; for (int i = 1; i < v.length(); i++) { v.divide(i, beta); } } v.put(0, 1); return v; }
public static Vector<Double> solveT(SparseMatrix<Double> a, Vector<Double> b) { assert (a.cols() == b.length()); final int N = b.length(); Vector<Double> y = new SparseRealArray(N); for (int i = N - 1; i >= 0; i--) { double sum = b.doubleValue(i); for (int j = i + 1; j < N; j++) { sum -= a.doubleValue(j, i) * y.doubleValue(j); } y.put(i, sum / a.doubleValue(i, i)); } return y; }
public static Vector<Double> solve(SparseMatrix<Double> a, Vector<Double> b) { assert (a.cols() == b.length()); final int N = b.length(); Vector<Double> y = new SparseRealArray(N); for (int i = 0; i < N; i++) { double sum = b.doubleValue(i); for (int j = 0; j < i; j++) { sum -= a.doubleValue(i, j) * y.doubleValue(j); } y.put(i, sum / a.doubleValue(i, i)); } return y; }
public static void chol(SparseMatrix<Double> l, int k, int i, SparseMatrix<Double> a) throws IllegalArgumentException { double sum = 0; final Vector<Double> l_k = l.row(k); for (int j : l_k.keySet()) { sum += l.doubleValue(i, j) * l_k.doubleValue(j); } double a_ii = a.doubleValue(i, i); if (i == k) { if (a_ii - sum < 0) { throw new IllegalArgumentException("Matrix not positive definite"); } l.set(i, k, Math.sqrt(a_ii - sum)); } else { l.set(i, k, (a.doubleValue(i, k) - sum) / l.doubleValue(k, k)); } }
@Override @SuppressWarnings("unchecked") public <M extends Number, O extends Number> Matrix<O> outerProduct(Vector<M> y, Factory<O> using) { if (using == Vectors.AS_INTS) { int[][] data2 = new int[data.length][y.length()]; for (int i = 0; i < data.length; i++) { for (int j = 0; j < y.length(); j++) { data2[i][j] = data[i] * y.intValue(j); } } return (Matrix<O>) new IntArrayMatrix(data2); } else if (using == Vectors.AS_REALS) { double[][] data2 = new double[data.length][y.length()]; for (int i = 0; i < data.length; i++) { for (int j = 0; j < y.length(); j++) { data2[i][j] = y.doubleValue(j) * data[i]; } } return (Matrix<O>) new DoubleArrayMatrix(data2); } else { final SparseMatrix<O> matrix = new SparseMatrix<O>(data.length, y.length(), using); for (int i = 0; i < data.length; i++) { for (Map.Entry<Integer, M> e : y.entrySet()) { matrix.set(i, e.getKey(), e.getValue().doubleValue() * data[i]); } } return matrix; } }
@Override public <M extends Number> Matrix<N> product(Matrix<M> B) { if (this.cols() != B.rows()) { throw new IllegalArgumentException("Matrix dimensions not suitable for product"); } if (defaultValue != 0.0 || (B instanceof SparseMatrix && ((SparseMatrix) B).defaultValue != 0.0)) { throw new UnsupportedOperationException(); } Vector<N>[] res = new Vector[this.rows()]; for (int i = 0; i < this.rows(); i++) { res[i] = using.make(B.cols(), 0.0); for (int j : this.arr[i].keySet()) { final Vector<M> r = B.row(j); for (int k : r.keySet()) { res[i].add(k, this.arr[i].doubleValue(j) * B.doubleValue(j, k)); } } } return new SparseMatrix<N>(this.rows(), res, using); }
@Override public Vector<Double> apply(Vector<Double> v) { double[] mid = new double[W]; int n = 0; IntIterator data = corpus.iterator(); while (data.hasNext()) { int i = data.nextInt(); if (i != 0) { mid[i - 1] += v.doubleValue(n); } else { n++; } } n = 0; double[] a = new double[v.length()]; data = corpus.iterator(); while (data.hasNext()) { int i = data.nextInt(); if (i != 0) { a[n] += mid[i - 1]; } else { n++; } } return new RealVector(a); } }