mdarray 0.5.3-java → 0.5.4-java

Sign up to get free protection for your applications and to get access to all the features.
Files changed (43) hide show
  1. data/README.md +325 -70
  2. data/doc/20130625 MDArray Internals.docx +0 -0
  3. data/lib/colt/colt.rb +2 -0
  4. data/lib/colt/matrix/colt_matrix.rb +365 -0
  5. data/lib/colt/matrix/matrix2D_floating_algebra.rb +325 -0
  6. data/lib/colt/matrix/matrix_hierarchy.rb +258 -0
  7. data/lib/mdarray.rb +54 -3
  8. data/lib/mdarray/access.rb +16 -0
  9. data/lib/mdarray/counter.rb +16 -0
  10. data/lib/mdarray/creation.rb +13 -1
  11. data/lib/mdarray/lazy_mdarray.rb +6 -2
  12. data/lib/mdarray/lazy_operators.rb +8 -0
  13. data/lib/mdarray/printing.rb +18 -3
  14. data/lib/mdarray/section.rb +101 -0
  15. data/lib/netcdf/attribute.rb +154 -0
  16. data/lib/netcdf/cdm_node.rb +71 -0
  17. data/lib/netcdf/dimension.rb +146 -0
  18. data/lib/netcdf/file.rb +253 -0
  19. data/lib/netcdf/file_writer.rb +474 -0
  20. data/lib/netcdf/group.rb +205 -0
  21. data/lib/netcdf/netcdf.rb +151 -0
  22. data/lib/netcdf/variable.rb +520 -0
  23. data/test/colt/test_complete.rb +1 -2
  24. data/test/colt/test_double_matrix2d.rb +186 -0
  25. data/test/colt/test_float_matrix2d.rb +171 -0
  26. data/test/colt/test_math.rb +21 -0
  27. data/test/colt/test_matrix.rb +172 -0
  28. data/test/complete.rb +9 -1
  29. data/test/env.rb +11 -1
  30. data/test/mdarray/test_complete.rb +2 -0
  31. data/test/mdarray/test_creation.rb +19 -28
  32. data/test/mdarray/test_non_numeric.rb +97 -0
  33. data/test/mdarray/test_sections.rb +94 -0
  34. data/test/mdarray/test_views.rb +23 -1
  35. data/test/netcdf/netcdfwriter.rb +197 -0
  36. data/test/netcdf/test_complete.rb +27 -0
  37. data/test/netcdf/test_netcdf.rb +331 -0
  38. data/test/netcdf/test_redefine.rb +120 -0
  39. data/vendor/incanter.jar +0 -0
  40. data/vendor/{netcdfAll-4.3.16.jar → netcdfAll-4.3.18.jar} +0 -0
  41. data/version.rb +1 -1
  42. metadata +44 -26
  43. data/vendor/parallelcolt-0.10.0.jar +0 -0
@@ -0,0 +1,325 @@
1
+ # -*- coding: utf-8 -*-
2
+
3
+ ##########################################################################################
4
+ # Copyright © 2013 Rodrigo Botafogo. All Rights Reserved. Permission to use, copy, modify,
5
+ # and distribute this software and its documentation for educational, research, and
6
+ # not-for-profit purposes, without fee and without a signed licensing agreement, is hereby
7
+ # granted, provided that the above copyright notice, this paragraph and the following two
8
+ # paragraphs appear in all copies, modifications, and distributions. Contact Rodrigo
9
+ # Botafogo - rodrigo.a.botafogo@gmail.com for commercial licensing opportunities.
10
+ #
11
+ # IN NO EVENT SHALL RODRIGO BOTAFOGO BE LIABLE TO ANY PARTY FOR DIRECT, INDIRECT, SPECIAL,
12
+ # INCIDENTAL, OR CONSEQUENTIAL DAMAGES, INCLUDING LOST PROFITS, ARISING OUT OF THE USE OF
13
+ # THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF RODRIGO BOTAFOGO HAS BEEN ADVISED OF THE
14
+ # POSSIBILITY OF SUCH DAMAGE.
15
+ #
16
+ # RODRIGO BOTAFOGO SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
17
+ # THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE
18
+ # SOFTWARE AND ACCOMPANYING DOCUMENTATION, IF ANY, PROVIDED HEREUNDER IS PROVIDED "AS IS".
19
+ # RODRIGO BOTAFOGO HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS,
20
+ # OR MODIFICATIONS.
21
+ ##########################################################################################
22
+
23
+ # require 'jruby/core_ext'
24
+
25
+ require 'java'
26
+
27
+ ##########################################################################################
28
+ #
29
+ ##########################################################################################
30
+
31
+ module Matrix2DFloatingAlgebra
32
+ include_package "cern.colt.matrix.tdouble.algo"
33
+ include_package "cern.colt.matrix.tfloat.algo"
34
+
35
+ #------------------------------------------------------------------------------------
36
+ # Solves the upper triangular system U*x=b;
37
+ #------------------------------------------------------------------------------------
38
+
39
+ def backward_solve(matrix1D)
40
+ result = @algebra.backwardSolve(@colt_matrix, matrix1D.colt_matrix)
41
+ MDMatrix.from_colt_matrix(result)
42
+ end
43
+
44
+ #------------------------------------------------------------------------------------
45
+ # Constructs and returns the cholesky-decomposition of the given matrix. For a
46
+ # symmetric, positive definite matrix A, the Cholesky decomposition is a lower
47
+ # triangular matrix L so that A = L*L'; If the matrix is not symmetric positive
48
+ # definite, the IllegalArgumentException is thrown.
49
+ #------------------------------------------------------------------------------------
50
+
51
+ def chol
52
+ result = @algebra.chol(@colt_matrix).getL()
53
+ MDMatrix.from_colt_matrix(result)
54
+ end
55
+
56
+ #------------------------------------------------------------------------------------
57
+ # Returns the condition of matrix A, which is the ratio of largest to smallest
58
+ # singular value.
59
+ #------------------------------------------------------------------------------------
60
+
61
+ def cond
62
+ @algebra.cond(@colt_matrix)
63
+ end
64
+
65
+ #------------------------------------------------------------------------------------
66
+ # Returns the determinant of matrix A.
67
+ #------------------------------------------------------------------------------------
68
+
69
+ def det
70
+ @algebra.det(@colt_matrix)
71
+ end
72
+
73
+ #------------------------------------------------------------------------------------
74
+ #
75
+ #------------------------------------------------------------------------------------
76
+
77
+ def eig
78
+ eig = @algebra.eig(@colt_matrix)
79
+ [MDMatrix.from_colt_matrix(eig.getD),
80
+ MDMatrix.from_colt_matrix(eig.getImagEigenvalues),
81
+ MDMatrix.from_colt_matrix(eig.getRealEigenvalues),
82
+ MDMatrix.from_colt_matrix(eig.getV)]
83
+ end
84
+
85
+ #------------------------------------------------------------------------------------
86
+ # Solves the lower triangular system L*x=b;
87
+ #------------------------------------------------------------------------------------
88
+
89
+ def forward_solve(matrix1D)
90
+ result = @algebra.forwardSolve(@colt_matrix, matrix1D.colt_matrix)
91
+ MDMatrix.from_colt_matrix(result)
92
+ end
93
+
94
+ #------------------------------------------------------------------------------------
95
+ # Returns the inverse or pseudo-inverse of matrix A.
96
+ #------------------------------------------------------------------------------------
97
+
98
+ def inverse
99
+ result = @algebra.inverse(@colt_matrix)
100
+ MDMatrix.from_colt_matrix(result)
101
+ end
102
+
103
+ #------------------------------------------------------------------------------------
104
+ # Computes the Kronecker product of two real matrices.
105
+ #------------------------------------------------------------------------------------
106
+
107
+ def kron(matrix)
108
+
109
+ if (matrix.rank != 2)
110
+ raise "Rank should be 2"
111
+ end
112
+ result = @algebra.kron(@colt_matrix, matrix.colt_matrix)
113
+ MDMatrix.from_colt_matrix(result)
114
+
115
+ end
116
+
117
+ #------------------------------------------------------------------------------------
118
+ # Constructs and returns the LU-decomposition of the given matrix.
119
+ #------------------------------------------------------------------------------------
120
+
121
+ def lu
122
+ result = @algebra.lu(@colt_matrix)
123
+ [result.isNonsingular(), result.det(), result.getPivot.to_a(),
124
+ MDMatrix.from_colt_matrix(result.getL()),
125
+ MDMatrix.from_colt_matrix(result.getU())]
126
+ end
127
+
128
+ #------------------------------------------------------------------------------------
129
+ # Multiplies this matrix by another matrix
130
+ #------------------------------------------------------------------------------------
131
+
132
+ def mult(matrix)
133
+
134
+ if (matrix.rank > 2)
135
+ raise "Rank should be 1 or 2"
136
+ end
137
+
138
+ result = @colt_matrix.like
139
+ @colt_matrix.zMult(matrix.colt_matrix, result)
140
+ MDMatrix.from_colt_matrix(result)
141
+
142
+ end
143
+
144
+ alias :* :mult
145
+
146
+ #------------------------------------------------------------------------------------
147
+ # Returns the one-norm of vector x, which is Sum(abs(x[i])).
148
+ #------------------------------------------------------------------------------------
149
+
150
+ def norm1
151
+ @algebra.norm1(@colt_matrix)
152
+ end
153
+
154
+ #------------------------------------------------------------------------------------
155
+ # Returns the two-norm of matrix A, which is the maximum singular value; obtained
156
+ # from SVD.
157
+ #------------------------------------------------------------------------------------
158
+
159
+ def norm2
160
+ @algebra.norm2(@colt_matrix)
161
+ end
162
+
163
+ #------------------------------------------------------------------------------------
164
+ # Returns the Frobenius norm of matrix A, which is Sqrt(Sum(A[i,j]^2))
165
+ #------------------------------------------------------------------------------------
166
+
167
+ def normF
168
+ @algebra.normF(@colt_matrix)
169
+ end
170
+
171
+ #------------------------------------------------------------------------------------
172
+ # Returns the infinity norm of matrix A, which is the maximum absolute row sum.
173
+ #------------------------------------------------------------------------------------
174
+
175
+ def norm_infinity
176
+ @algebra.normInfinity(@colt_matrix)
177
+ end
178
+
179
+ #------------------------------------------------------------------------------------
180
+ # Linear algebraic matrix power; B = A^k <==> B = A*A*...
181
+ #------------------------------------------------------------------------------------
182
+
183
+ def power(val)
184
+ result = @algebra.pow(@colt_matrix, val)
185
+ MDMatrix.from_colt_matrix(result)
186
+ end
187
+
188
+ alias :** :power
189
+
190
+ #------------------------------------------------------------------------------------
191
+ # Returns the effective numerical rank of matrix A, obtained from Singular Value
192
+ # Decomposition.
193
+ #------------------------------------------------------------------------------------
194
+
195
+ def numerical_rank
196
+ @algebra.rank(@colt_matrix)
197
+ end
198
+
199
+ #------------------------------------------------------------------------------------
200
+ # Solves A*X = B
201
+ #------------------------------------------------------------------------------------
202
+
203
+ def solve(matrix)
204
+ result = @algebra.solve(@colt_matrix, matrix.colt_matrix)
205
+ MDMatris.from_colt_matrix(resul)
206
+ end
207
+
208
+ #------------------------------------------------------------------------------------
209
+ # Solves X*A = B, which is also A'*X' = B'.
210
+ #------------------------------------------------------------------------------------
211
+
212
+ def solve_transpose(matrix)
213
+ result = @algebra.solveTranspose(@colt_matrix, matrix.colt_matrix)
214
+ MDMatris.from_colt_matrix(resul)
215
+ end
216
+
217
+ #------------------------------------------------------------------------------------
218
+ # Constructs and returns the SingularValue-decomposition of the given matrix.
219
+ #------------------------------------------------------------------------------------
220
+
221
+ def svd
222
+ result = @algebra.svd(@colt_matrix)
223
+ [result.getInfo().val, result.cond(), result.norm2(), result.rank(),
224
+ result.getSingularValues().to_a(),
225
+ MDMatrix.from_colt_matrix(result.getS()),
226
+ MDMatrix.from_colt_matrix(result.getU()),
227
+ MDMatrix.from_colt_matrix(result.getV())]
228
+ end
229
+
230
+ #------------------------------------------------------------------------------------
231
+ # Returns the sum of the diagonal elements of matrix A; Sum(A[i,i]).
232
+ #------------------------------------------------------------------------------------
233
+
234
+ def trace
235
+ @algebra.trace(@colt_matrix)
236
+ end
237
+
238
+ #------------------------------------------------------------------------------------
239
+ # Modifies the matrix to be a lower trapezoidal matrix.
240
+ #------------------------------------------------------------------------------------
241
+
242
+ def trapezoidal_lower
243
+ result = @algebra.trapezoidalLower(@colt_matrix)
244
+ MDMatrix.from_colt_matrix(result)
245
+ end
246
+
247
+ #------------------------------------------------------------------------------------
248
+ # Returns the two-norm (aka euclidean norm) of vector X.vectorize();
249
+ #------------------------------------------------------------------------------------
250
+
251
+ def vector_norm2
252
+ @algebra.vectorNorm2(@colt_matrix)
253
+ end
254
+
255
+ end #
256
+
257
+ ##########################################################################################
258
+ #
259
+ ##########################################################################################
260
+
261
+ module Matrix2DDoubleAlgebra
262
+ include_package "cern.colt.matrix.tdouble.algo"
263
+
264
+ #------------------------------------------------------------------------------------
265
+ # Constructs and returns the QR-decomposition of the given matrix.
266
+ #------------------------------------------------------------------------------------
267
+
268
+ def qr(economy_size = true)
269
+ result = @algebra.qr(@colt_matrix)
270
+ [result.hasFullRank(), MDMatrix.from_colt_matrix(result.getQ(economy_size)),
271
+ MDMatrix.from_colt_matrix(result.getR(economy_size))]
272
+ end
273
+
274
+ end
275
+
276
+ ##########################################################################################
277
+ #
278
+ ##########################################################################################
279
+
280
+ module Matrix2DFloatAlgebra
281
+ include_package "cern.colt.matrix.tfloat.algo"
282
+
283
+ #------------------------------------------------------------------------------------
284
+ # Constructs and returns the QR-decomposition of the given matrix.
285
+ #------------------------------------------------------------------------------------
286
+
287
+ def qr
288
+ result = @algebra.qr(@colt_matrix)
289
+ [result.hasFullRank(),
290
+ MDMatrix.from_colt_matrix(result.getH()),
291
+ MDMatrix.from_colt_matrix(result.getQ()),
292
+ MDMatrix.from_colt_matrix(result.getR())]
293
+ end
294
+
295
+ end
296
+
297
+ ##########################################################################################
298
+ #
299
+ ##########################################################################################
300
+
301
+ class FloatingMDMatrix2D
302
+
303
+ include Matrix2DFloatingAlgebra
304
+
305
+ end # MDMatrix
306
+
307
+ ##########################################################################################
308
+ #
309
+ ##########################################################################################
310
+
311
+ class DoubleMDMatrix2D
312
+
313
+ include Matrix2DDoubleAlgebra
314
+
315
+ end
316
+
317
+ ##########################################################################################
318
+ #
319
+ ##########################################################################################
320
+
321
+ class FloatMDMatrix2D
322
+
323
+ include Matrix2DFloatAlgebra
324
+
325
+ end
@@ -0,0 +1,258 @@
1
+ # -*- coding: utf-8 -*-
2
+
3
+ ##########################################################################################
4
+ # Copyright © 2013 Rodrigo Botafogo. All Rights Reserved. Permission to use, copy, modify,
5
+ # and distribute this software and its documentation for educational, research, and
6
+ # not-for-profit purposes, without fee and without a signed licensing agreement, is hereby
7
+ # granted, provided that the above copyright notice, this paragraph and the following two
8
+ # paragraphs appear in all copies, modifications, and distributions. Contact Rodrigo
9
+ # Botafogo - rodrigo.a.botafogo@gmail.com for commercial licensing opportunities.
10
+ #
11
+ # IN NO EVENT SHALL RODRIGO BOTAFOGO BE LIABLE TO ANY PARTY FOR DIRECT, INDIRECT, SPECIAL,
12
+ # INCIDENTAL, OR CONSEQUENTIAL DAMAGES, INCLUDING LOST PROFITS, ARISING OUT OF THE USE OF
13
+ # THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF RODRIGO BOTAFOGO HAS BEEN ADVISED OF THE
14
+ # POSSIBILITY OF SUCH DAMAGE.
15
+ #
16
+ # RODRIGO BOTAFOGO SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
17
+ # THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE
18
+ # SOFTWARE AND ACCOMPANYING DOCUMENTATION, IF ANY, PROVIDED HEREUNDER IS PROVIDED "AS IS".
19
+ # RODRIGO BOTAFOGO HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS,
20
+ # OR MODIFICATIONS.
21
+ ##########################################################################################
22
+
23
+
24
+ ##########################################################################################
25
+ # Floating point 1D Matrices
26
+ ##########################################################################################
27
+
28
+ class FloatingMDMatrix1D < MDMatrix
29
+
30
+ end # FloatingMDMatrix1D
31
+
32
+ ##########################################################################################
33
+ #
34
+ ##########################################################################################
35
+
36
+ class DoubleMDMatrix1D < FloatingMDMatrix1D
37
+ include_package "cern.colt.matrix.tdouble.algo"
38
+
39
+ #------------------------------------------------------------------------------------
40
+ #
41
+ #------------------------------------------------------------------------------------
42
+
43
+ def initialize(mdarray, colt_matrix)
44
+ super(mdarray, colt_matrix)
45
+ @algebra = DenseDoubleAlgebra.new
46
+ end
47
+
48
+ end # DoubleMDMatrix1D
49
+
50
+ ##########################################################################################
51
+ #
52
+ ##########################################################################################
53
+
54
+ class FloatMDMatrix1D < FloatingMDMatrix1D
55
+ include_package "cern.colt.matrix.tfloat.algo"
56
+
57
+ #------------------------------------------------------------------------------------
58
+ #
59
+ #------------------------------------------------------------------------------------
60
+
61
+ def initialize(mdarray, colt_matrix)
62
+ super(mdarray, colt_matrix)
63
+ @algebra = DenseFloatAlgebra.new
64
+ end
65
+
66
+ end # FloatMDMatrix1D
67
+
68
+
69
+
70
+ ##########################################################################################
71
+ # Floating point 2D Matrices
72
+ ##########################################################################################
73
+
74
+ class FloatingMDMatrix2D < MDMatrix
75
+
76
+ end # FloatingMDMatrix2D
77
+
78
+ ##########################################################################################
79
+ #
80
+ ##########################################################################################
81
+
82
+ class DoubleMDMatrix2D < FloatingMDMatrix2D
83
+ include_package "cern.colt.matrix.tdouble.algo"
84
+
85
+ #------------------------------------------------------------------------------------
86
+ #
87
+ #------------------------------------------------------------------------------------
88
+
89
+ def initialize(mdarray, colt_matrix)
90
+ super(mdarray, colt_matrix)
91
+ @algebra = DenseDoubleAlgebra.new
92
+ end
93
+
94
+ end # DoubleMDMatrix
95
+
96
+ ##########################################################################################
97
+ #
98
+ ##########################################################################################
99
+
100
+ class FloatMDMatrix2D < FloatingMDMatrix2D
101
+ include_package "cern.colt.matrix.tfloat.algo"
102
+
103
+ #------------------------------------------------------------------------------------
104
+ #
105
+ #------------------------------------------------------------------------------------
106
+
107
+ def initialize(mdarray, colt_matrix)
108
+ super(mdarray, colt_matrix)
109
+ @algebra = DenseFloatAlgebra.new
110
+ end
111
+
112
+ end # FloatMDMatrix
113
+
114
+
115
+
116
+
117
+
118
+ ##########################################################################################
119
+ #
120
+ ##########################################################################################
121
+
122
+ class DoubleMDMatrix3D < MDMatrix
123
+ include_package "cern.colt.matrix.tdouble.algo"
124
+
125
+ #------------------------------------------------------------------------------------
126
+ #
127
+ #------------------------------------------------------------------------------------
128
+
129
+ def initialize(mdarray, colt_matrix)
130
+ super(mdarray, colt_matrix)
131
+ @algebra = DenseDoubleAlgebra.new
132
+ end
133
+
134
+ end # DoubleMDMatrix
135
+
136
+ ##########################################################################################
137
+ #
138
+ ##########################################################################################
139
+
140
+ class FloatMDMatrix3D < MDMatrix
141
+ include_package "cern.colt.matrix.tfloat.algo"
142
+
143
+ #------------------------------------------------------------------------------------
144
+ #
145
+ #------------------------------------------------------------------------------------
146
+
147
+ def initialize(mdarray, colt_matrix)
148
+ super(mdarray, colt_matrix)
149
+ @algebra = DenseFloatAlgebra.new
150
+ end
151
+
152
+ end # FloatMDMatrix
153
+
154
+
155
+
156
+
157
+
158
+ ##########################################################################################
159
+ #
160
+ ##########################################################################################
161
+
162
+ class LongMDMatrix1D < MDMatrix
163
+ include_package "cern.colt.matrix.tlong.algo"
164
+
165
+ #------------------------------------------------------------------------------------
166
+ #
167
+ #------------------------------------------------------------------------------------
168
+
169
+ def initialize(mdarray, colt_matrix)
170
+ super(mdarray, colt_matrix)
171
+ end
172
+
173
+ end # LongMDMatrix
174
+
175
+ ##########################################################################################
176
+ #
177
+ ##########################################################################################
178
+
179
+ class LongMDMatrix2D < MDMatrix
180
+ include_package "cern.colt.matrix.tlong.algo"
181
+
182
+ #------------------------------------------------------------------------------------
183
+ #
184
+ #------------------------------------------------------------------------------------
185
+
186
+ def initialize(mdarray, colt_matrix)
187
+ super(mdarray, colt_matrix)
188
+ end
189
+
190
+ end # LongMDMatrix
191
+
192
+ ##########################################################################################
193
+ #
194
+ ##########################################################################################
195
+
196
+ class LongMDMatrix3D < MDMatrix
197
+ include_package "cern.colt.matrix.tlong.algo"
198
+
199
+ #------------------------------------------------------------------------------------
200
+ #
201
+ #------------------------------------------------------------------------------------
202
+
203
+ def initialize(mdarray, colt_matrix)
204
+ super(mdarray, colt_matrix)
205
+ end
206
+
207
+ end # LongMDMatrix
208
+
209
+ ##########################################################################################
210
+ #
211
+ ##########################################################################################
212
+
213
+ class IntMDMatrix1D < MDMatrix
214
+ include_package "cern.colt.matrix.tint.algo"
215
+
216
+ #------------------------------------------------------------------------------------
217
+ #
218
+ #------------------------------------------------------------------------------------
219
+
220
+ def initialize(mdarray, colt_matrix)
221
+ super(mdarray, colt_matrix)
222
+ end
223
+
224
+ end # IntMDMatrix
225
+
226
+ ##########################################################################################
227
+ #
228
+ ##########################################################################################
229
+
230
+ class IntMDMatrix2D < MDMatrix
231
+ include_package "cern.colt.matrix.tint.algo"
232
+
233
+ #------------------------------------------------------------------------------------
234
+ #
235
+ #------------------------------------------------------------------------------------
236
+
237
+ def initialize(mdarray, colt_matrix)
238
+ super(mdarray, colt_matrix)
239
+ end
240
+
241
+ end # IntMDMatrix
242
+
243
+ ##########################################################################################
244
+ #
245
+ ##########################################################################################
246
+
247
+ class IntMDMatrix3D < MDMatrix
248
+ include_package "cern.colt.matrix.tint.algo"
249
+
250
+ #------------------------------------------------------------------------------------
251
+ #
252
+ #------------------------------------------------------------------------------------
253
+
254
+ def initialize(mdarray, colt_matrix)
255
+ super(mdarray, colt_matrix)
256
+ end
257
+
258
+ end # IntMDMatrix