MCPcopy Create free account
hub / github.com/EdwardRaff/JSAT / accumulateSum

Method accumulateSum

JSAT/src/jsat/linear/VecOps.java:31–93  ·  view source on GitHub ↗

Computes the result of ∑ ∀ i ∈ |w| w i f(x i -y i ) @param w the vector of weight values to multiply on the results @param x the first vector of values in the difference @param y the second vector of values in the difference @param f t

(final Vec w, final Vec x, final Vec y, final Function f)

Source from the content-addressed store, hash-verified

29 * @return the accumulated sum of the evaluations
30 */
31 public static double accumulateSum(final Vec w, final Vec x, final Vec y, final Function f)
32 {
33 if(w.length() != x.length() || x.length() != y.length())
34 throw new ArithmeticException("All 3 vector inputs must have equal lengths");
35
36 double val = 0;
37 final boolean skipZeros = f.f(0) == 0;
38 final boolean wSparse = w.isSparse();
39 final boolean xSparse = x.isSparse();
40 final boolean ySparse = y.isSparse();
41
42 //skip zeros applied to (x_i-y_i) == 0. We can always skip zeros in w
43
44 if (wSparse && !xSparse && !ySparse)
45 {
46 for (IndexValue wiv : w)
47 {
48 final int idx = wiv.getIndex();
49 val += wiv.getValue() * f.f(x.get(idx) - y.get(idx));
50 }
51 }
52 else if (!wSparse && !xSparse && !ySparse)//w is dense
53 {
54 for (int i = 0; i < w.length(); i++)
55 val += w.get(i) * f.f(x.get(i) - y.get(i));
56 }
57 else //Best for all sparse, but also works well in general
58 {
59 Iterator<IndexValue> xIter = x.iterator();
60 Iterator<IndexValue> yIter = y.iterator();
61
62 IndexValue xiv = xIter.hasNext() ? xIter.next() : badIV;
63 IndexValue yiv = yIter.hasNext() ? yIter.next() : badIV;
64
65 for (IndexValue wiv : w)
66 {
67 int index = wiv.getIndex();
68 double w_i = wiv.getValue();
69
70 while (xiv.getIndex() < index && xIter.hasNext())
71 xiv = xIter.next();
72 while (yiv.getIndex() < index && yIter.hasNext())
73 yiv = yIter.next();
74
75
76 final double x_i, y_i;
77 if (xiv.getIndex() == index)
78 x_i = xiv.getValue();
79 else
80 x_i = 0;
81 if (yiv.getIndex() == index)
82 y_i = yiv.getValue();
83 else
84 y_i = 0;
85
86 if (skipZeros && x_i == 0 && y_i == 0)
87 continue;
88 val += w_i * f.f(x_i - y_i);

Callers 2

distMethod · 0.95
distMethod · 0.95

Calls 9

getIndexMethod · 0.95
getValueMethod · 0.95
fMethod · 0.65
lengthMethod · 0.45
isSparseMethod · 0.45
getMethod · 0.45
iteratorMethod · 0.45
hasNextMethod · 0.45
nextMethod · 0.45

Tested by

no test coverage detected