| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
package net.sourceforge.combean.mathprog.linalg.statics; |
| 6 | |
|
| 7 | |
import java.util.Set; |
| 8 | |
import java.util.TreeSet; |
| 9 | |
|
| 10 | |
import net.sourceforge.combean.interfaces.mathprog.linalg.SparseVec; |
| 11 | |
import net.sourceforge.combean.interfaces.mathprog.linalg.VectorIterator; |
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
|
| 17 | |
|
| 18 | |
|
| 19 | 0 | public class SparseVectorUtil { |
| 20 | |
|
| 21 | |
|
| 22 | |
|
| 23 | |
|
| 24 | |
|
| 25 | |
|
| 26 | |
|
| 27 | |
public static final <Label> double sum(SparseVec<Label> vec) { |
| 28 | 36 | double result = 0.0; |
| 29 | |
|
| 30 | 36 | VectorIterator<Label> itVec = vec.iterator(); |
| 31 | 144 | while (itVec.hasNext()) { |
| 32 | 108 | result += itVec.next().doubleValue(); |
| 33 | |
} |
| 34 | |
|
| 35 | 36 | return result; |
| 36 | |
} |
| 37 | |
|
| 38 | |
public static final <Label> Set<Label> collectLabels(SparseVec<Label> vec) { |
| 39 | 0 | Set<Label> labels = new TreeSet<Label>(); |
| 40 | 0 | VectorIterator<Label> itVec = vec.iterator(); |
| 41 | 0 | while (itVec.hasNext()) { |
| 42 | 0 | labels.add(itVec.next().label()); |
| 43 | |
} |
| 44 | 0 | return labels; |
| 45 | |
} |
| 46 | |
|
| 47 | |
} |