1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
|
|
20 |
|
|
21 |
|
package jalview.math; |
22 |
|
|
23 |
|
import jalview.ext.android.SparseDoubleArray; |
24 |
|
|
25 |
|
|
26 |
|
|
27 |
|
|
28 |
|
|
29 |
|
|
30 |
|
|
31 |
|
@author |
32 |
|
|
|
|
| 81.6% |
Uncovered Elements: 18 (98) |
Complexity: 27 |
Complexity Density: 0.48 |
|
33 |
|
public class SparseMatrix extends Matrix |
34 |
|
{ |
35 |
|
|
36 |
|
|
37 |
|
|
38 |
|
|
39 |
|
SparseDoubleArray[] sparseColumns; |
40 |
|
|
41 |
|
|
42 |
|
|
43 |
|
|
44 |
|
@param |
45 |
|
|
|
|
| 94.1% |
Uncovered Elements: 1 (17) |
Complexity: 5 |
Complexity Density: 0.56 |
|
46 |
22 |
public SparseMatrix(double[][] v)... |
47 |
|
{ |
48 |
22 |
super(v.length, v.length > 0 ? v[0].length : 0); |
49 |
|
|
50 |
22 |
sparseColumns = new SparseDoubleArray[cols]; |
51 |
|
|
52 |
|
|
53 |
|
|
54 |
|
|
55 |
107 |
for (int col = 0; col < cols; col++) |
56 |
|
{ |
57 |
85 |
SparseDoubleArray sparseColumn = new SparseDoubleArray(); |
58 |
85 |
sparseColumns[col] = sparseColumn; |
59 |
522 |
for (int row = 0; row < rows; row++) |
60 |
|
{ |
61 |
437 |
double value = v[row][col]; |
62 |
437 |
if (value != 0d) |
63 |
|
{ |
64 |
184 |
sparseColumn.put(row, value); |
65 |
|
} |
66 |
|
} |
67 |
|
} |
68 |
|
} |
69 |
|
|
70 |
|
|
71 |
|
|
72 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
73 |
7019 |
@Override... |
74 |
|
public double getValue(int i, int j) |
75 |
|
{ |
76 |
7019 |
return sparseColumns[j].get(i); |
77 |
|
} |
78 |
|
|
79 |
|
|
80 |
|
|
81 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (5) |
Complexity: 2 |
Complexity Density: 0.67 |
|
82 |
870 |
@Override... |
83 |
|
public void setValue(int i, int j, double val) |
84 |
|
{ |
85 |
870 |
if (val == 0d) |
86 |
|
{ |
87 |
385 |
sparseColumns[j].delete(i); |
88 |
|
} |
89 |
|
else |
90 |
|
{ |
91 |
485 |
sparseColumns[j].put(i, val); |
92 |
|
} |
93 |
|
} |
94 |
|
|
|
|
| 0% |
Uncovered Elements: 7 (7) |
Complexity: 2 |
Complexity Density: 0.4 |
|
95 |
0 |
@Override... |
96 |
|
public double[] getColumn(int i) |
97 |
|
{ |
98 |
0 |
double[] col = new double[height()]; |
99 |
|
|
100 |
0 |
SparseDoubleArray vals = sparseColumns[i]; |
101 |
0 |
for (int nonZero = 0; nonZero < vals.size(); nonZero++) |
102 |
|
{ |
103 |
0 |
col[vals.keyAt(nonZero)] = vals.valueAt(nonZero); |
104 |
|
} |
105 |
0 |
return col; |
106 |
|
} |
107 |
|
|
|
|
| 0% |
Uncovered Elements: 6 (6) |
Complexity: 2 |
Complexity Density: 0.5 |
|
108 |
0 |
@Override... |
109 |
|
public MatrixI copy() |
110 |
|
{ |
111 |
0 |
double[][] vals = new double[height()][width()]; |
112 |
0 |
for (int i = 0; i < height(); i++) |
113 |
|
{ |
114 |
0 |
vals[i] = getRow(i); |
115 |
|
} |
116 |
0 |
return new SparseMatrix(vals); |
117 |
|
} |
118 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (10) |
Complexity: 3 |
Complexity Density: 0.5 |
|
119 |
1 |
@Override... |
120 |
|
public MatrixI transpose() |
121 |
|
{ |
122 |
1 |
double[][] out = new double[cols][rows]; |
123 |
|
|
124 |
|
|
125 |
|
|
126 |
|
|
127 |
4 |
for (int i = 0; i < cols; i++) |
128 |
|
{ |
129 |
|
|
130 |
|
|
131 |
|
|
132 |
|
|
133 |
3 |
SparseDoubleArray vals = sparseColumns[i]; |
134 |
7 |
for (int nonZero = 0; nonZero < vals.size(); nonZero++) |
135 |
|
{ |
136 |
4 |
out[i][vals.keyAt(nonZero)] = vals.valueAt(nonZero); |
137 |
|
} |
138 |
|
} |
139 |
|
|
140 |
1 |
return new SparseMatrix(out); |
141 |
|
} |
142 |
|
|
143 |
|
|
144 |
|
|
145 |
|
|
146 |
|
|
147 |
|
|
148 |
|
|
149 |
|
|
150 |
|
|
151 |
|
|
152 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (30) |
Complexity: 8 |
Complexity Density: 0.44 |
|
153 |
12 |
@Override... |
154 |
|
public MatrixI preMultiply(MatrixI in) |
155 |
|
{ |
156 |
12 |
if (in.width() != rows) |
157 |
|
{ |
158 |
2 |
throw new IllegalArgumentException("Can't pre-multiply " + this.rows |
159 |
|
+ " rows by " + in.width() + " columns"); |
160 |
|
} |
161 |
10 |
double[][] tmp = new double[in.height()][this.cols]; |
162 |
|
|
163 |
10 |
long count = 0L; |
164 |
34 |
for (int i = 0; i < in.height(); i++) |
165 |
|
{ |
166 |
98 |
for (int j = 0; j < this.cols; j++) |
167 |
|
{ |
168 |
|
|
169 |
|
|
170 |
|
|
171 |
|
|
172 |
|
|
173 |
74 |
SparseDoubleArray vals = sparseColumns[j]; |
174 |
74 |
boolean added = false; |
175 |
161 |
for (int nonZero = 0; nonZero < vals.size(); nonZero++) |
176 |
|
{ |
177 |
87 |
int myRow = vals.keyAt(nonZero); |
178 |
87 |
double myValue = vals.valueAt(nonZero); |
179 |
87 |
tmp[i][j] += (in.getValue(i, myRow) * myValue); |
180 |
87 |
added = true; |
181 |
|
} |
182 |
74 |
if (added && tmp[i][j] != 0d) |
183 |
|
{ |
184 |
41 |
count++; |
185 |
|
} |
186 |
|
} |
187 |
|
} |
188 |
|
|
189 |
|
|
190 |
|
|
191 |
|
|
192 |
|
|
193 |
10 |
if (count * 5 < in.height() * cols) |
194 |
|
{ |
195 |
1 |
return new SparseMatrix(tmp); |
196 |
|
} |
197 |
|
else |
198 |
|
{ |
199 |
9 |
return new Matrix(tmp); |
200 |
|
} |
201 |
|
} |
202 |
|
|
|
|
| 66.7% |
Uncovered Elements: 2 (6) |
Complexity: 2 |
Complexity Density: 0.5 |
|
203 |
146 |
@Override... |
204 |
|
protected double divideValue(int i, int j, double divisor) |
205 |
|
{ |
206 |
146 |
if (divisor == 0d) |
207 |
|
{ |
208 |
0 |
return getValue(i, j); |
209 |
|
} |
210 |
146 |
double v = sparseColumns[j].divide(i, divisor); |
211 |
146 |
return v; |
212 |
|
} |
213 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
214 |
1432 |
@Override... |
215 |
|
protected double addValue(int i, int j, double addend) |
216 |
|
{ |
217 |
1432 |
double v = sparseColumns[j].add(i, addend); |
218 |
1432 |
return v; |
219 |
|
} |
220 |
|
|
221 |
|
|
222 |
|
|
223 |
|
|
224 |
|
|
225 |
|
@return |
226 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (4) |
Complexity: 1 |
Complexity Density: 0.25 |
|
227 |
1 |
public float getFillRatio()... |
228 |
|
{ |
229 |
1 |
long count = 0L; |
230 |
1 |
for (SparseDoubleArray col : sparseColumns) |
231 |
|
{ |
232 |
5 |
count += col.size(); |
233 |
|
} |
234 |
1 |
return count / (float) (height() * width()); |
235 |
|
} |
236 |
|
} |