@Override public Vector<Double> make(double[] data) { return new RealVector(data); }
@Override public Vector<Double> fromString(String s, int n) throws VectorFormatException { return RealVector.fromString(s, n); }
@Override public boolean containsKey(int idx) { return idx >= 0 && idx < length(); }
@Override public Vector<Double> row(int i) { final RealVector v = new RealVector(alpha.length); if (i > 0) { v.put(i - 1, beta[i - 1]); } v.put(i, alpha[i]); if (i + 1 < alpha.length) { v.put(i + 1, beta[i]); } return v; }
double[] beta = new double[n - 1]; for (int k = 0; k < n - 2; k++) { final RealVector x = new RealVector(n - k - 1); for (int j = k + 1; j < n; j++) { x.put(j - k - 1, A2.doubleValue(j, k)); final RealVector w = new RealVector(n - k - 1); for (int i = 0; i < n - k - 1; i++) { for (int j = 0; j < n - k - 1; j++) { w.data()[i] += 2 * A2.doubleValue(i + k + 1, j + k + 1) * v.doubleValue(j) / vtv; final double ptv = w.innerProduct(v); for (int j = 0; j < n - k - 1; j++) { w.sub(j, ptv * v.doubleValue(j) / vtv); A2.data()[i + k + 1][j + k + 1] -= v.doubleValue(i) * w.doubleValue(j) + w.doubleValue(i) * v.doubleValue(j);
q[k] = new RealVector(Arrays.copyOf(r, r.length)); q[k].multiply(1.0 / h[k][k]); h[i][k] = q[i].innerProduct(new RealVector(r)); r[j] -= h[i][k] * q[i].doubleValue(j);
@Override public <M extends Number> Vector<Double> multTransposed(Vector<M> x) { assert (x.length() == m); double[] product = new double[n]; if (x instanceof RealVector) { final double[] x2 = ((RealVector) x).data(); for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { product[i] += data[j][i] * x2[j]; } } } else if (x instanceof IntVector) { final int[] x2 = ((IntVector) x).data(); for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { product[i] += data[j][i] * x2[j]; } } } else { for (int i = 0; i < n; i++) { for (Map.Entry<Integer, M> e : x.entrySet()) { product[i] += data[e.getKey()][i] * e.getValue().doubleValue(); } } } return new RealVector(product); }
@Override public <M extends Number, O extends Number> Vector<O> mult(Vector<M> x, Vectors.Factory<O> using) { assert (x.length() == n); double[] product = new double[m]; if (x instanceof RealVector) { final double[] x2 = ((RealVector) x).data(); for (int i = 0; i < m; i++) { for (int j = 0; j < n; j++) { product[i] += data[i][j] * x2[j]; } } } else if (x instanceof IntVector) { final int[] x2 = ((IntVector) x).data(); for (int i = 0; i < m; i++) { for (int j = 0; j < n; j++) { product[i] += data[i][j] * x2[j]; } } } else { for (int i = 0; i < m; i++) { for (Map.Entry<Integer, M> e : x.entrySet()) { product[i] += data[i][e.getKey()] * e.getValue().doubleValue(); } } } return using.make(product); }
@Override public IntSet keySet() { return new IntStreamSet(size()); }
@Override public Vector<Double> simVecSource(Vector<Integer> termVec) { final double[] predicted = new double[W]; final double[] salience = calculateSalience(termVec, 0); for(int j = 0; j < J; j++) { for(int w : x[j][1].keySet()) { if((double)ctTotal[1][w] - alpha * x[j][1].get(w) != 0.0) { predicted[w] += salience[j] * x[j][1].get(w) / ((double)ctTotal[1][w] - alpha * x[j][1].get(w)); } } } return RealVector.make(predicted); // return RealVector.make(salience); }
/** * Calculate the eigenvalues of a non-symmetric matrix * @param A The matrix as a function y = Ax * @param W The number of columns in A * @param K The number of eigenvectors to calculate * @param epsilon The error threshold (recommended: {@code 1e-50}) * @return The top K eigenvectors and eigenvalues */ public static Solution nonsymmEigen(VectorFunction<Double, Double> A, int W, int K, double epsilon) { final ArnoldiAlgorithm.Solution arnoldi = ArnoldiAlgorithm.solve(A, randomUnitNormVector(W), K); final HessenbergQR.Solution soln = HessenbergQR.solve(arnoldi.K, arnoldi.h); final int[] order = order(soln.d); double[][] U = new double[arnoldi.K][W]; for (int i = 0; i < W; i++) { for (int j = 0; j < arnoldi.K; j++) { for (int k = 0; k < arnoldi.K; k++) { U[j][i] += arnoldi.q[k].doubleValue(i) * soln.V[k][order[j]]; } } } double[] S = new double[arnoldi.K]; for (int i = 0; i < arnoldi.K; i++) { S[i] = soln.d[order[i]]; } return new Solution(U, null, soln.d); } private static Random random;
@Override public <M extends Number, O extends Number> Vector<O> mult(Vector<M> x, Vectors.Factory<O> using) { assert (x.length() == n); double[] product = new double[m]; if (x instanceof RealVector) { final double[] x2 = ((RealVector) x).data(); for (int i = 0; i < m; i++) { for (int j = 0; j < n; j++) { product[i] += data[i][j] * x2[j]; } } } else if (x instanceof IntVector) { final int[] x2 = ((IntVector) x).data(); for (int i = 0; i < m; i++) { for (int j = 0; j < n; j++) { product[i] += data[i][j] * x2[j]; } } } else { for (int i = 0; i < m; i++) { for (Map.Entry<Integer, M> e : x.entrySet()) { product[i] += data[i][e.getKey()] * e.getValue().doubleValue(); } } } return using.make(product); }
@Override public Vector<Double> row(int i) { return new RealVector(data[i]); }
@Override public <M extends Number> Vector<Integer> multTransposed(Vector<M> x) { assert (x.length() == m); double[] product = new double[n]; if (x instanceof RealVector) { final double[] x2 = ((RealVector) x).data(); for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { product[i] += data[j][i] * x2[j]; } } } else if (x instanceof IntVector) { final int[] x2 = ((IntVector) x).data(); for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { product[i] += data[j][i] * x2[j]; } } } else { for (int i = 0; i < n; i++) { for (Map.Entry<Integer, M> e : x.entrySet()) { product[i] += data[e.getKey()][i] * e.getValue().doubleValue(); } } } return Vectors.AS_INTS.make(product); }
public static RealVector make(double... vals) { return new RealVector(vals); }
assert (n == y.length()); if (y instanceof RealVector) { double[] y2 = ((RealVector) y).data(); if (defaultValue == 0) { double innerProduct = 0;
public static RealVector make(double... data) { return new RealVector(data); }
assert (n == y.length()); if (y instanceof RealVector) { double[] y2 = ((RealVector) y).data(); if (defaultValue == 0) { double innerProduct = 0;
@Override public Vector<Double> make(int n, double defaultValue) { if (defaultValue == 0.0) { return new RealVector(n); } else { double[] data = new double[n]; Arrays.fill(data, defaultValue); return new RealVector(data); } }
assert (m == x.length()); if (x instanceof RealVector) { double[] x2 = ((RealVector) x).data();