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