mdarray 0.5.4-java → 0.5.5-java

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,363 @@
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
+ class Colt
24
+
25
+ module Property
26
+ include_package "cern.colt.matrix.tdouble.algo"
27
+ include_package "cern.colt.matrix.tfloat.algo"
28
+ include_package "cern.colt.matrix.tlong.algo"
29
+ include_package "cern.colt.matrix.tint.algo"
30
+
31
+ attr_reader :colt_property
32
+ attr_reader :colt_matrix
33
+
34
+ #------------------------------------------------------------------------------------
35
+ # Checks whether the given matrix A is rectangular.
36
+ #------------------------------------------------------------------------------------
37
+
38
+ def check_rectangular
39
+ begin
40
+ @colt_property.checkRectangular(@colt_matrix)
41
+ rescue java.lang.IllegalArgumentException
42
+ raise "rows < columns. Not rectangular matrix"
43
+ end
44
+ end
45
+
46
+ #------------------------------------------------------------------------------------
47
+ # Checks whether the given matrix A is square.
48
+ #------------------------------------------------------------------------------------
49
+
50
+ def check_square
51
+ begin
52
+ @colt_property.checkSquare(@colt_matrix)
53
+ rescue java.lang.IllegalArgumentException
54
+ raise "rows != columns. Not square matrix"
55
+ end
56
+ end
57
+
58
+ #------------------------------------------------------------------------------------
59
+ # Returns the matrix's fraction of non-zero cells; A.cardinality() / A.size().
60
+ #------------------------------------------------------------------------------------
61
+
62
+ def density
63
+ @colt_property.density(@colt_matrix)
64
+ end
65
+
66
+ #------------------------------------------------------------------------------------
67
+ # A matrix A is diagonal if A[i,j] == 0 whenever i != j. Matrix may but need not be
68
+ # square.
69
+ #------------------------------------------------------------------------------------
70
+
71
+ def diagonal?
72
+ @colt_property.diagonal(@colt_matrix)
73
+ end
74
+
75
+ #------------------------------------------------------------------------------------
76
+ # A matrix A is diagonally dominant by column if the absolute value of each diagonal
77
+ # element is larger than the sum of the absolute values of the off-diagonal elements
78
+ # in the corresponding column. returns true if for all i: abs(A[i,i]) >
79
+ # Sum(abs(A[j,i])); j != i. Matrix may but need not be square.
80
+ #
81
+ # Note: Ignores tolerance.
82
+ #------------------------------------------------------------------------------------
83
+
84
+ def diagonally_dominant_by_column?
85
+ @colt_property.diagonallyDominantByColumn(@colt_matrix)
86
+ end
87
+
88
+ #------------------------------------------------------------------------------------
89
+ # A matrix A is diagonally dominant by row if the absolute value of each diagonal
90
+ # element is larger than the sum of the absolute values of the off-diagonal elements in
91
+ # the corresponding row. returns true if for all i: abs(A[i,i]) > Sum(abs(A[i,j]));
92
+ # j != i. Matrix may but need not be square.
93
+ #
94
+ # Note: Ignores tolerance.
95
+ #------------------------------------------------------------------------------------
96
+
97
+ def diagonally_dominant_by_row?
98
+ @colt_property.diagonallyDominantByRow(@colt_matrix)
99
+ end
100
+
101
+ #------------------------------------------------------------------------------------
102
+ # if val is a Numeric value: Returns whether all cells of the given matrix A are
103
+ # equal to the given value.
104
+ # if val is another matrix: Returns whether both given matrices A and B are equal.
105
+ #------------------------------------------------------------------------------------
106
+
107
+ def equals?(val)
108
+
109
+ if (val.is_a? Numeric)
110
+ @colt_property.equals(@colt_matrix, val)
111
+ else
112
+ @colt_property.equals(@colt_matrix, val.colt_matrix)
113
+ end
114
+
115
+ end
116
+
117
+ #------------------------------------------------------------------------------------
118
+ # Modifies the square matrix such that it is diagonally dominant by
119
+ # row and column, hence non-singular, hence invertible.
120
+ #------------------------------------------------------------------------------------
121
+
122
+ def generate_non_singular!
123
+ @colt_property.generateNonSingular(@colt_matrix)
124
+ end
125
+
126
+ #-------------------------------------------------------------------------------------
127
+ # A matrix A is an identity matrix if A[i,i] == 1 and all other cells are zero. Matrix
128
+ # may but need not be square.
129
+ #-------------------------------------------------------------------------------------
130
+
131
+ def identity?
132
+ @colt_property.isIdentity(@colt_matrix)
133
+ end
134
+
135
+ #------------------------------------------------------------------------------------
136
+ # A matrix A is lower bidiagonal if A[i,j]==0 unless i==j || i==j+1.
137
+ #------------------------------------------------------------------------------------
138
+
139
+ def lower_bidiagonal?
140
+ @colt_property.isLowerBidiagonal(@colt_matrix)
141
+ end
142
+
143
+ #------------------------------------------------------------------------------------
144
+ # A matrix A is lower triangular if A[i,j]==0 whenever i < j.
145
+ #------------------------------------------------------------------------------------
146
+
147
+ def lower_triangular?
148
+ @colt_property.isLowerTriangular(@colt_matrix)
149
+ end
150
+
151
+ #------------------------------------------------------------------------------------
152
+ # A matrix A is non-negative if A[i,j] >= 0 holds for all cells.
153
+ #------------------------------------------------------------------------------------
154
+
155
+ def non_negative?
156
+ @colt_property.isNonNegative(@colt_matrix)
157
+ end
158
+
159
+ #------------------------------------------------------------------------------------
160
+ # A square matrix A is orthogonal if A*transpose(A) = I.
161
+ #------------------------------------------------------------------------------------
162
+
163
+ def orthogonal?
164
+ @colt_property.isOrthogonal(@colt_matrix)
165
+ end
166
+
167
+ #------------------------------------------------------------------------------------
168
+ # A matrix A is positive if A[i,j] > 0 holds for all cells.
169
+ #------------------------------------------------------------------------------------
170
+
171
+ def positive?
172
+ @colt_property.isPositive(@colt_matrix)
173
+ end
174
+
175
+ #------------------------------------------------------------------------------------
176
+ # A matrix A is singular if it has no inverse, that is, iff det(A)==0.
177
+ #------------------------------------------------------------------------------------
178
+
179
+ def singular?
180
+ @colt_property.isSingular(@colt_matrix)
181
+ end
182
+
183
+ #------------------------------------------------------------------------------------
184
+ # A square matrix A is skew-symmetric if A = -transpose(A), that is A[i,j] == -A[j,i].
185
+ #------------------------------------------------------------------------------------
186
+
187
+ def skew_symmetric?
188
+ @colt_property.isSkewSymmetric(@colt_matrix)
189
+ end
190
+
191
+ #------------------------------------------------------------------------------------
192
+ #
193
+ #------------------------------------------------------------------------------------
194
+
195
+ def square?
196
+ @colt_property.isSquare(@colt_matrix)
197
+ end
198
+
199
+ #------------------------------------------------------------------------------------
200
+ # A matrix A is strictly lower triangular if A[i,j]==0 whenever i <= j.
201
+ #------------------------------------------------------------------------------------
202
+
203
+ def strictly_lower_triangular?
204
+ @colt_property.isStrictlyLowerTriangular(@colt_matrix)
205
+ end
206
+
207
+ #------------------------------------------------------------------------------------
208
+ # A matrix A is strictly triangular if it is triangular and its diagonal elements all
209
+ # equal 0
210
+ #------------------------------------------------------------------------------------
211
+
212
+ def strictly_triangular?
213
+ @colt_property.isStrictlyTriangular(@colt_matrix)
214
+ end
215
+
216
+ #------------------------------------------------------------------------------------
217
+ # A matrix A is strictly upper triangular if A[i,j]==0 whenever i >= j.
218
+ #------------------------------------------------------------------------------------
219
+
220
+ def strictly_upper_triangular?
221
+ @colt_property.isStrictlyUpperTriangular(@colt_matrix)
222
+ end
223
+
224
+ #------------------------------------------------------------------------------------
225
+ # A matrix A is symmetric if A = tranpose(A), that is A[i,j] == A[j,i].
226
+ #------------------------------------------------------------------------------------
227
+
228
+ def symmetric?
229
+ @colt_property.isSymmetric(@colt_matrix)
230
+ end
231
+
232
+ #------------------------------------------------------------------------------------
233
+ # A matrix A is triangular iff it is either upper or lower triangular.
234
+ #------------------------------------------------------------------------------------
235
+
236
+ def triangular?
237
+ @colt_property.isTriangular(@colt_matrix)
238
+ end
239
+
240
+ #------------------------------------------------------------------------------------
241
+ # A matrix A is tridiagonal if A[i,j]==0 whenever Math.abs(i-j) > 1.
242
+ #------------------------------------------------------------------------------------
243
+
244
+ def tridiagonal?
245
+ @colt_property.isTridiagonal(@colt_matrix)
246
+ end
247
+
248
+ #------------------------------------------------------------------------------------
249
+ # A matrix A is unit triangular if it is triangular and its diagonal elements all
250
+ # equal 1.
251
+ #------------------------------------------------------------------------------------
252
+
253
+ def unit_triangular?
254
+ @colt_property.isUnitTriangular(@colt_matrix)
255
+ end
256
+
257
+ #------------------------------------------------------------------------------------
258
+ # A matrix A is upper bidiagonal if A[i,j]==0 unless i==j || i==j-1.
259
+ #------------------------------------------------------------------------------------
260
+
261
+ def upper_bidiagonal?
262
+ @colt_property.isUpperBidiagonal(@colt_matrix)
263
+ end
264
+
265
+ #------------------------------------------------------------------------------------
266
+ # A matrix A is upper triangular if A[i,j]==0 whenever i > j.
267
+ #------------------------------------------------------------------------------------
268
+
269
+ def upper_triangular?
270
+ @colt_property.isUpperTriangular(@colt_matrix)
271
+ end
272
+
273
+ #------------------------------------------------------------------------------------
274
+ # A matrix A is zero if all its cells are zero.
275
+ #------------------------------------------------------------------------------------
276
+
277
+ def zero?
278
+ @colt_property.isZero(@colt_matrix)
279
+ end
280
+
281
+ #------------------------------------------------------------------------------------
282
+ # The lower bandwidth of a square matrix A is the maximum i-j for which A[i,j] is
283
+ # nonzero and i > j.
284
+ #------------------------------------------------------------------------------------
285
+
286
+ def lower_bandwidth
287
+ @colt_property.lowerBandwidth(@colt_matrix)
288
+ end
289
+
290
+ #------------------------------------------------------------------------------------
291
+ # Returns the semi-bandwidth of the given square matrix A.
292
+ #------------------------------------------------------------------------------------
293
+
294
+ def semi_bandwidth
295
+ @colt_property.semiBandwidth(@colt_matrix)
296
+ end
297
+
298
+ #------------------------------------------------------------------------------------
299
+ # Returns the current tolerance.
300
+ #------------------------------------------------------------------------------------
301
+
302
+ def tolerance
303
+ @colt_property.tolerance()
304
+ end
305
+
306
+ #------------------------------------------------------------------------------------
307
+ # Sets the tolerance to Math.abs(val).
308
+ #------------------------------------------------------------------------------------
309
+
310
+ def tolerance=(val)
311
+ @colt_property.setTolerance(val)
312
+ end
313
+
314
+ #------------------------------------------------------------------------------------
315
+ #
316
+ #------------------------------------------------------------------------------------
317
+
318
+ def tolerance
319
+ @colt_property.tolerance()
320
+ end
321
+
322
+ #------------------------------------------------------------------------------------
323
+ # Returns summary information about the given matrix A.
324
+ #------------------------------------------------------------------------------------
325
+
326
+ def properties
327
+ printf(@colt_property.toString(@colt_matrix))
328
+ end
329
+
330
+ #------------------------------------------------------------------------------------
331
+ # The upper bandwidth of a square matrix A is the maximum j-i for which A[i,j] is
332
+ # nonzero and j > i.
333
+ #------------------------------------------------------------------------------------
334
+
335
+ def upper_bandwidth
336
+ @colt_property.upperBandwidth(@colt_matrix)
337
+ end
338
+
339
+ end
340
+
341
+ end # Colt
342
+
343
+ ##########################################################################################
344
+ #
345
+ ##########################################################################################
346
+
347
+ class MDMatrix
348
+
349
+ include Colt::Property
350
+
351
+ end # MDMatrix
352
+
353
+ ##########################################################################################
354
+ #
355
+ ##########################################################################################
356
+
357
+ class FixPointMDMatrix
358
+
359
+ def singular?
360
+ raise "Cannot calculate singular matrix for fix point matrix"
361
+ end
362
+
363
+ end # IntMDMatrix
@@ -34,6 +34,14 @@ class MDArray
34
34
  #
35
35
  #------------------------------------------------------------------------------------
36
36
 
37
+ def flip(dim)
38
+ MDArray.build_from_nc_array(@type, @nc_array.flip(dim))
39
+ end
40
+
41
+ #------------------------------------------------------------------------------------
42
+ #
43
+ #------------------------------------------------------------------------------------
44
+
37
45
  def reshape(shape, copy = false)
38
46
 
39
47
  new_shape = shape.to_java :int
@@ -58,6 +66,7 @@ class MDArray
58
66
  # when we reshape an array we need to re-initialize its index and local_iterator
59
67
  @local_index = Counter.new(self)
60
68
  @local_iterator = nil
69
+ @self
61
70
  end
62
71
 
63
72
  #------------------------------------------------------------------------------------
@@ -130,6 +139,8 @@ class MDArray
130
139
  return arr.get()
131
140
  end
132
141
 
142
+ # Build the new array as a section from the given one. Last argument to build is
143
+ # "true" indicating this is a section.
133
144
  section = MDArray.build_from_nc_array(@type, arr, true)
134
145
  copy_print_parameters(section)
135
146
  return section
@@ -163,7 +174,41 @@ class MDArray
163
174
  arr = @nc_array.sectionNoReduce(jorigin, jshape, jstride)
164
175
  end
165
176
 
166
- # this is an array section, set it to true
177
+ # Build the new array as a section from the given one. Last argument to build is
178
+ # "true" indicating this is a section.
179
+ section = MDArray.build_from_nc_array(@type, arr, true)
180
+ copy_print_parameters(section)
181
+ return section
182
+
183
+ end
184
+
185
+ #---------------------------------------------------------------------------------------
186
+ # Gets a region from this array. Region is the same as section but using a different
187
+ # interface.
188
+ # parameters that can be given
189
+ # shape
190
+ # origin
191
+ # size
192
+ # stride
193
+ # range
194
+ # section
195
+ # spec
196
+ #---------------------------------------------------------------------------------------
197
+
198
+ def region(*args)
199
+
200
+ opts = Map.options(args)
201
+ reduce = opts.getopt(:reduce)
202
+ sec = MDArray::Section.build(*args)
203
+
204
+ if (reduce)
205
+ arr = @nc_array.section(sec.netcdf_elmt.getRanges())
206
+ else
207
+ arr = @nc_array.sectionNoReduce(sec.netcdf_elmt.getRanges())
208
+ end
209
+
210
+ # Build the new array as a section from the given one. Last argument to build is
211
+ # "true" indicating this is a section.
167
212
  section = MDArray.build_from_nc_array(@type, arr, true)
168
213
  copy_print_parameters(section)
169
214
  return section
@@ -0,0 +1,29 @@
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, without fee and without a signed
6
+ # licensing agreement, is hereby granted, provided that the above copyright notice, this
7
+ # paragraph and the following two paragraphs appear in all copies, modifications, and
8
+ # distributions.
9
+ #
10
+ # IN NO EVENT SHALL RODRIGO BOTAFOGO BE LIABLE TO ANY PARTY FOR DIRECT, INDIRECT, SPECIAL,
11
+ # INCIDENTAL, OR CONSEQUENTIAL DAMAGES, INCLUDING LOST PROFITS, ARISING OUT OF THE USE OF
12
+ # THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF RODRIGO BOTAFOGO HAS BEEN ADVISED OF THE
13
+ # POSSIBILITY OF SUCH DAMAGE.
14
+ #
15
+ # RODRIGO BOTAFOGO SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
16
+ # THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE
17
+ # SOFTWARE AND ACCOMPANYING DOCUMENTATION, IF ANY, PROVIDED HEREUNDER IS PROVIDED "AS IS".
18
+ # RODRIGO BOTAFOGO HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS,
19
+ # OR MODIFICATIONS.
20
+ ##########################################################################################
21
+
22
+ require_relative 'test_creation'
23
+ require_relative 'test_matrix2d_floatingalgebra'
24
+ require_relative 'test_matrix2d_fixpointalgebra'
25
+ require_relative 'test_matrix1d_floatingalgebra'
26
+ #require_relative 'test_matrix1d_fixpointalgebra'
27
+ require_relative 'test_operations'
28
+ require_relative 'test_properties'
29
+