|
1
|
|
package baseCode.dataStructure.matrix;
|
|
2
|
|
|
|
3
|
|
import java.text.NumberFormat;
|
|
4
|
|
|
|
5
|
|
import cern.colt.list.DoubleArrayList;
|
|
6
|
|
import cern.colt.matrix.DoubleMatrix1D;
|
|
7
|
|
import cern.colt.matrix.impl.SparseDoubleMatrix2D;
|
|
8
|
|
|
|
9
|
|
|
|
10
|
|
|
|
11
|
|
|
|
12
|
|
|
|
13
|
|
|
|
14
|
|
|
|
15
|
|
|
|
16
|
|
|
|
17
|
|
|
|
18
|
|
|
|
19
|
|
|
|
20
|
|
|
|
21
|
|
|
|
22
|
|
|
|
23
|
|
|
|
24
|
|
|
|
25
|
|
|
|
26
|
|
public class SparseDoubleMatrix2DNamed extends AbstractNamedDoubleMatrix implements
|
|
27
|
|
NamedMatrix {
|
|
28
|
|
|
|
29
|
|
private SparseDoubleMatrix2D matrix;
|
|
30
|
|
|
|
31
|
|
|
|
32
|
|
|
|
33
|
|
|
|
34
|
0
|
public SparseDoubleMatrix2DNamed( double T[][] ) {
|
|
35
|
0
|
super();
|
|
36
|
0
|
matrix = new SparseDoubleMatrix2D( T );
|
|
37
|
|
}
|
|
38
|
|
|
|
39
|
|
|
|
40
|
|
|
|
41
|
|
|
|
42
|
|
|
|
43
|
2
|
public SparseDoubleMatrix2DNamed( int rows, int cols ) {
|
|
44
|
2
|
super();
|
|
45
|
2
|
matrix = new SparseDoubleMatrix2D( rows, cols );
|
|
46
|
|
}
|
|
47
|
|
|
|
48
|
0
|
public void set( int row, int col, Object value ) {
|
|
49
|
0
|
set( row, col, ( ( Double ) value ).doubleValue() );
|
|
50
|
|
}
|
|
51
|
|
|
|
52
|
|
|
|
53
|
|
|
|
54
|
|
|
|
55
|
|
|
|
56
|
|
|
|
57
|
|
|
|
58
|
0
|
public double[] getRow( int row ) {
|
|
59
|
0
|
return viewRow( row ).toArray();
|
|
60
|
|
}
|
|
61
|
|
|
|
62
|
|
|
|
63
|
|
|
|
64
|
|
|
|
65
|
|
|
|
66
|
|
|
|
67
|
|
|
|
68
|
0
|
public double[] getCol( int col ) {
|
|
69
|
0
|
double[] result = new double[rows()];
|
|
70
|
0
|
for ( int i = 0; i < rows(); i++ ) {
|
|
71
|
0
|
result[i] = get( i, col );
|
|
72
|
|
}
|
|
73
|
0
|
return result;
|
|
74
|
|
}
|
|
75
|
|
|
|
76
|
0
|
public Object[] getRowObj( int row ) {
|
|
77
|
0
|
Double[] result = new Double[columns()];
|
|
78
|
0
|
for ( int i = 0; i < columns(); i++ ) {
|
|
79
|
0
|
result[i] = new Double( get( row, i ) );
|
|
80
|
|
}
|
|
81
|
0
|
return result;
|
|
82
|
|
}
|
|
83
|
|
|
|
84
|
0
|
public Object[] getColObj( int col ) {
|
|
85
|
0
|
Double[] result = new Double[rows()];
|
|
86
|
0
|
for ( int i = 0; i < rows(); i++ ) {
|
|
87
|
0
|
result[i] = new Double( get( i, col ) );
|
|
88
|
|
}
|
|
89
|
0
|
return result;
|
|
90
|
|
}
|
|
91
|
|
|
|
92
|
|
|
|
93
|
|
|
|
94
|
|
|
|
95
|
2
|
public String toString() {
|
|
96
|
2
|
NumberFormat nf = NumberFormat.getInstance();
|
|
97
|
2
|
String result = "";
|
|
98
|
2
|
if ( this.hasColNames() || this.hasRowNames() ) {
|
|
99
|
2
|
result = "label";
|
|
100
|
|
}
|
|
101
|
|
|
|
102
|
2
|
if ( this.hasColNames() ) {
|
|
103
|
2
|
for ( int i = 0; i < columns(); i++ ) {
|
|
104
|
6
|
result = result + "\t" + getColName( i );
|
|
105
|
|
}
|
|
106
|
2
|
result += "\n";
|
|
107
|
|
}
|
|
108
|
|
|
|
109
|
2
|
for ( int i = 0; i < rows(); i++ ) {
|
|
110
|
6
|
if ( this.hasRowNames() ) {
|
|
111
|
6
|
result += getRowName( i );
|
|
112
|
|
}
|
|
113
|
6
|
for ( int j = 0; j < columns(); j++ ) {
|
|
114
|
|
|
|
115
|
18
|
double value = get( i, j );
|
|
116
|
|
|
|
117
|
18
|
if ( value == 0.0 ) {
|
|
118
|
5
|
result = result + "\t";
|
|
119
|
|
} else {
|
|
120
|
|
|
|
121
|
13
|
result = result + "\t" + nf.format( value );
|
|
122
|
|
}
|
|
123
|
|
}
|
|
124
|
6
|
result += "\n";
|
|
125
|
|
}
|
|
126
|
2
|
return result;
|
|
127
|
|
}
|
|
128
|
|
|
|
129
|
|
|
|
130
|
|
|
|
131
|
|
|
|
132
|
|
|
|
133
|
0
|
public double[] getRowByName( String s ) {
|
|
134
|
0
|
return getRow( getRowIndexByName( s ) );
|
|
135
|
|
}
|
|
136
|
|
|
|
137
|
0
|
public boolean isMissing( int i, int j ) {
|
|
138
|
0
|
return Double.isNaN( get( i, j ) );
|
|
139
|
|
}
|
|
140
|
|
|
|
141
|
|
|
|
142
|
|
|
|
143
|
|
|
|
144
|
36
|
public int columns() {
|
|
145
|
36
|
return matrix.columns();
|
|
146
|
|
}
|
|
147
|
|
|
|
148
|
|
|
|
149
|
|
|
|
150
|
|
|
|
151
|
|
|
|
152
|
|
|
|
153
|
18
|
public double get( int row, int column ) {
|
|
154
|
18
|
return matrix.get( row, column );
|
|
155
|
|
}
|
|
156
|
|
|
|
157
|
|
|
|
158
|
|
|
|
159
|
|
|
|
160
|
|
|
|
161
|
|
|
|
162
|
0
|
public double getQuick( int row, int column ) {
|
|
163
|
0
|
return matrix.getQuick( row, column );
|
|
164
|
|
}
|
|
165
|
|
|
|
166
|
|
|
|
167
|
|
|
|
168
|
|
|
|
169
|
14
|
public int rows() {
|
|
170
|
14
|
return matrix.rows();
|
|
171
|
|
}
|
|
172
|
|
|
|
173
|
|
|
|
174
|
|
|
|
175
|
|
|
|
176
|
|
|
|
177
|
|
|
|
178
|
0
|
public void set( int row, int column, double value ) {
|
|
179
|
0
|
matrix.set( row, column, value );
|
|
180
|
|
}
|
|
181
|
|
|
|
182
|
|
|
|
183
|
|
|
|
184
|
|
|
|
185
|
|
|
|
186
|
|
|
|
187
|
30
|
public void setQuick( int row, int column, double value ) {
|
|
188
|
30
|
matrix.setQuick( row, column, value );
|
|
189
|
|
}
|
|
190
|
|
|
|
191
|
|
|
|
192
|
|
|
|
193
|
|
|
|
194
|
|
|
|
195
|
0
|
public DoubleMatrix1D viewColumn( int column ) {
|
|
196
|
0
|
return matrix.viewColumn( column );
|
|
197
|
|
}
|
|
198
|
|
|
|
199
|
|
|
|
200
|
|
|
|
201
|
|
|
|
202
|
|
|
|
203
|
0
|
public DoubleMatrix1D viewRow( int row ) {
|
|
204
|
0
|
return matrix.viewRow( row );
|
|
205
|
|
}
|
|
206
|
|
|
|
207
|
|
|
|
208
|
|
|
|
209
|
|
|
|
210
|
0
|
public int cardinality() {
|
|
211
|
0
|
return matrix.cardinality();
|
|
212
|
|
}
|
|
213
|
|
|
|
214
|
|
|
|
215
|
|
|
|
216
|
0
|
public void ensureCapacity( int minNonZeros ) {
|
|
217
|
0
|
matrix.ensureCapacity( minNonZeros );
|
|
218
|
|
}
|
|
219
|
|
|
|
220
|
|
|
|
221
|
|
|
|
222
|
0
|
public int size() {
|
|
223
|
0
|
return matrix.size();
|
|
224
|
|
}
|
|
225
|
|
|
|
226
|
|
|
|
227
|
|
|
|
228
|
0
|
public void trimToSize() {
|
|
229
|
0
|
matrix.trimToSize();
|
|
230
|
|
}
|
|
231
|
|
|
|
232
|
|
|
|
233
|
|
|
|
234
|
|
|
|
235
|
0
|
public DoubleArrayList getRowArrayList( int i ) {
|
|
236
|
0
|
return new DoubleArrayList(getRow(i));
|
|
237
|
|
}
|
|
238
|
|
|
|
239
|
|
|
|
240
|
|
}
|