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