Computes the weighted dot product of ∑ ∀ i ∈ |w| w_i x_i y_i @param w the vector containing the weights, it is assumed to be random access @param x the first vector of the dot product @param y the second vector of the dot product @return the weighted dot product,
(final Vec w, final Vec x, final Vec y)
| 100 | * @return the weighted dot product, which is equivalent to the sum of the products of each index for each vector |
| 101 | */ |
| 102 | public static double weightedDot(final Vec w, final Vec x, final Vec y) |
| 103 | { |
| 104 | if(w.length() != x.length() || x.length() != y.length()) |
| 105 | throw new ArithmeticException("All 3 vector inputs must have equal lengths"); |
| 106 | |
| 107 | double sum = 0; |
| 108 | |
| 109 | if(x.isSparse() && y.isSparse()) |
| 110 | { |
| 111 | Iterator<IndexValue> xIter = x.iterator(); |
| 112 | Iterator<IndexValue> yIter = y.iterator(); |
| 113 | |
| 114 | IndexValue xiv = xIter.hasNext() ? xIter.next() : badIV; |
| 115 | IndexValue yiv = yIter.hasNext() ? yIter.next() : badIV; |
| 116 | |
| 117 | while(xiv != badIV && yiv != badIV) |
| 118 | { |
| 119 | if(xiv.getIndex() < yiv.getIndex()) |
| 120 | xiv = xIter.hasNext() ? xIter.next() : badIV; |
| 121 | else if(yiv.getIndex() > xiv.getIndex()) |
| 122 | yiv = yIter.hasNext() ? yIter.next() : badIV; |
| 123 | else//on the same page |
| 124 | { |
| 125 | sum += w.get(xiv.getIndex())*xiv.getValue()*yiv.getValue(); |
| 126 | xiv = xIter.hasNext() ? xIter.next() : badIV; |
| 127 | yiv = yIter.hasNext() ? yIter.next() : badIV; |
| 128 | } |
| 129 | } |
| 130 | } |
| 131 | else if(x.isSparse()) |
| 132 | { |
| 133 | for(IndexValue iv : x) |
| 134 | { |
| 135 | int indx = iv.getIndex(); |
| 136 | sum += w.get(indx)*iv.getValue()*y.get(indx); |
| 137 | } |
| 138 | } |
| 139 | else if(y.isSparse()) |
| 140 | return weightedDot(w, y, x); |
| 141 | else//all dense |
| 142 | { |
| 143 | for(int i = 0; i < w.length(); i++) |
| 144 | sum += w.get(i)*x.get(i)*y.get(i); |
| 145 | } |
| 146 | |
| 147 | return sum; |
| 148 | } |
| 149 | } |
no test coverage detected