Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
SparseVectorConverter |
|
| 0.0;0 |
1 | /* | |
2 | This file is part of Combean. | |
3 | ||
4 | Combean is free software; you can redistribute it and/or modify | |
5 | it under the terms of the GNU General Public License as published by | |
6 | the Free Software Foundation; either version 2 of the License, or | |
7 | (at your option) any later version. | |
8 | ||
9 | Combean is distributed in the hope that it will be useful, | |
10 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 | GNU General Public License for more details. | |
13 | ||
14 | You should have received a copy of the GNU General Public License | |
15 | along with Combean; if not, write to the Free Software | |
16 | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA | |
17 | */ | |
18 | /* | |
19 | * Created on 22.04.2005 | |
20 | * | |
21 | */ | |
22 | package net.sourceforge.combean.mathprog.linalg.statics; | |
23 | ||
24 | import net.sourceforge.combean.interfaces.mathprog.linalg.SparseVec; | |
25 | import net.sourceforge.combean.interfaces.mathprog.linalg.SparseVector; | |
26 | import net.sourceforge.combean.interfaces.mathprog.linalg.VectorIterator; | |
27 | import net.sourceforge.combean.interfaces.mathprog.linalg.VectorValue; | |
28 | import net.sourceforge.combean.interfaces.mathprog.lp.model.NoLabel; | |
29 | ||
30 | /** | |
31 | * Collection of static helper functions to convert SparseVectors to | |
32 | * plain Java arrays. | |
33 | * | |
34 | * @author schickin | |
35 | * | |
36 | */ | |
37 | 5 | public final class SparseVectorConverter { |
38 | ||
39 | /** | |
40 | * Convert a sparse vector given by an iterator over its components | |
41 | * to a plain double array | |
42 | * | |
43 | * @param dim the dimension of the dense output double array | |
44 | * @param itVec the iterator over the entries in the sparse vector | |
45 | * @return the entries represented by itVec as double array | |
46 | */ | |
47 | public static double[] convertToDoubleArray(int dim, | |
48 | VectorIterator<NoLabel> itVec) { | |
49 | 134 | double[] result = new double[dim]; |
50 | ||
51 | 1039 | for (int i = 0; i < dim; i++) { |
52 | 905 | result[i] = 0; |
53 | } | |
54 | ||
55 | 1012 | while (itVec.hasNext()) { |
56 | 878 | VectorValue<NoLabel> currValue = itVec.next(); |
57 | 878 | result[currValue.index()] = currValue.doubleValue(); |
58 | 878 | } |
59 | ||
60 | 134 | return result; |
61 | } | |
62 | ||
63 | /** | |
64 | * Convert SparseVec to plain double array | |
65 | * | |
66 | * @param dim desired dimension of the output array. must be big | |
67 | * enough such that all entries in vec fit into the output | |
68 | * @param vec the vector to be converted | |
69 | * @return vec as double array | |
70 | */ | |
71 | public static double[] convertToDoubleArray(int dim, | |
72 | SparseVec<NoLabel> vec) { | |
73 | 134 | return convertToDoubleArray(dim, vec.iterator()); |
74 | } | |
75 | ||
76 | /** | |
77 | * Convert SparseVec to plain double array, giving the double array the exact dimension of the | |
78 | * sparse vector. | |
79 | * | |
80 | * @param vec the vector to be converted | |
81 | * @return vec as double array | |
82 | */ | |
83 | public static double[] convertToDoubleArray( | |
84 | SparseVector vec) { | |
85 | 12 | return convertToDoubleArray(vec.getDimension(), vec); |
86 | } | |
87 | ||
88 | ||
89 | /** | |
90 | * Convert an SparseVec to two plain Java arrays, one containing the | |
91 | * indices of non-zero entries and a double array containing their | |
92 | * values. | |
93 | * | |
94 | * @param vec the vector to be converted | |
95 | * @param indexArr the index array to be filled. must be large enough | |
96 | * to accomodate all entries of vec. | |
97 | * @param valueArr the value array to be filled. must be large enough | |
98 | * to accomodate all entries of vec. | |
99 | */ | |
100 | public static <T> void convertToIndexAndDoubleArray(SparseVec<T> vec, | |
101 | int[] indexArr, double[] valueArr) { | |
102 | 725 | int currIdx = 0; |
103 | 725 | T label = null; |
104 | 725 | VectorIterator<T> itVec = vec.iterator(); |
105 | 2771 | while (itVec.hasNext()) { |
106 | 2051 | VectorValue<T> currValue = itVec.next(); |
107 | 2046 | if (label == null) { |
108 | 2046 | label = currValue.label(); |
109 | } | |
110 | else { | |
111 | 0 | assert label == currValue.label(); |
112 | } | |
113 | 2046 | indexArr[currIdx] = currValue.index(); |
114 | 2046 | valueArr[currIdx] = currValue.doubleValue(); |
115 | 2046 | currIdx++; |
116 | 2046 | } |
117 | 720 | } |
118 | ||
119 | } |