pnmatrix 1.2.4

Sign up to get free protection for your applications and to get access to all the features.
Files changed (111) hide show
  1. checksums.yaml +7 -0
  2. data/ext/nmatrix/binary_format.txt +53 -0
  3. data/ext/nmatrix/data/complex.h +388 -0
  4. data/ext/nmatrix/data/data.cpp +274 -0
  5. data/ext/nmatrix/data/data.h +651 -0
  6. data/ext/nmatrix/data/meta.h +64 -0
  7. data/ext/nmatrix/data/ruby_object.h +386 -0
  8. data/ext/nmatrix/extconf.rb +70 -0
  9. data/ext/nmatrix/math/asum.h +99 -0
  10. data/ext/nmatrix/math/cblas_enums.h +36 -0
  11. data/ext/nmatrix/math/cblas_templates_core.h +507 -0
  12. data/ext/nmatrix/math/gemm.h +241 -0
  13. data/ext/nmatrix/math/gemv.h +178 -0
  14. data/ext/nmatrix/math/getrf.h +255 -0
  15. data/ext/nmatrix/math/getrs.h +121 -0
  16. data/ext/nmatrix/math/imax.h +82 -0
  17. data/ext/nmatrix/math/laswp.h +165 -0
  18. data/ext/nmatrix/math/long_dtype.h +62 -0
  19. data/ext/nmatrix/math/magnitude.h +54 -0
  20. data/ext/nmatrix/math/math.h +751 -0
  21. data/ext/nmatrix/math/nrm2.h +165 -0
  22. data/ext/nmatrix/math/rot.h +117 -0
  23. data/ext/nmatrix/math/rotg.h +106 -0
  24. data/ext/nmatrix/math/scal.h +71 -0
  25. data/ext/nmatrix/math/trsm.h +336 -0
  26. data/ext/nmatrix/math/util.h +162 -0
  27. data/ext/nmatrix/math.cpp +1368 -0
  28. data/ext/nmatrix/nm_memory.h +60 -0
  29. data/ext/nmatrix/nmatrix.cpp +285 -0
  30. data/ext/nmatrix/nmatrix.h +476 -0
  31. data/ext/nmatrix/ruby_constants.cpp +151 -0
  32. data/ext/nmatrix/ruby_constants.h +106 -0
  33. data/ext/nmatrix/ruby_nmatrix.c +3130 -0
  34. data/ext/nmatrix/storage/common.cpp +77 -0
  35. data/ext/nmatrix/storage/common.h +183 -0
  36. data/ext/nmatrix/storage/dense/dense.cpp +1096 -0
  37. data/ext/nmatrix/storage/dense/dense.h +129 -0
  38. data/ext/nmatrix/storage/list/list.cpp +1628 -0
  39. data/ext/nmatrix/storage/list/list.h +138 -0
  40. data/ext/nmatrix/storage/storage.cpp +730 -0
  41. data/ext/nmatrix/storage/storage.h +99 -0
  42. data/ext/nmatrix/storage/yale/class.h +1139 -0
  43. data/ext/nmatrix/storage/yale/iterators/base.h +143 -0
  44. data/ext/nmatrix/storage/yale/iterators/iterator.h +131 -0
  45. data/ext/nmatrix/storage/yale/iterators/row.h +450 -0
  46. data/ext/nmatrix/storage/yale/iterators/row_stored.h +140 -0
  47. data/ext/nmatrix/storage/yale/iterators/row_stored_nd.h +169 -0
  48. data/ext/nmatrix/storage/yale/iterators/stored_diagonal.h +124 -0
  49. data/ext/nmatrix/storage/yale/math/transpose.h +110 -0
  50. data/ext/nmatrix/storage/yale/yale.cpp +2074 -0
  51. data/ext/nmatrix/storage/yale/yale.h +203 -0
  52. data/ext/nmatrix/types.h +55 -0
  53. data/ext/nmatrix/util/io.cpp +279 -0
  54. data/ext/nmatrix/util/io.h +115 -0
  55. data/ext/nmatrix/util/sl_list.cpp +627 -0
  56. data/ext/nmatrix/util/sl_list.h +144 -0
  57. data/ext/nmatrix/util/util.h +78 -0
  58. data/lib/nmatrix/blas.rb +378 -0
  59. data/lib/nmatrix/cruby/math.rb +744 -0
  60. data/lib/nmatrix/enumerate.rb +253 -0
  61. data/lib/nmatrix/homogeneous.rb +241 -0
  62. data/lib/nmatrix/io/fortran_format.rb +138 -0
  63. data/lib/nmatrix/io/harwell_boeing.rb +221 -0
  64. data/lib/nmatrix/io/market.rb +263 -0
  65. data/lib/nmatrix/io/point_cloud.rb +189 -0
  66. data/lib/nmatrix/jruby/decomposition.rb +24 -0
  67. data/lib/nmatrix/jruby/enumerable.rb +13 -0
  68. data/lib/nmatrix/jruby/error.rb +4 -0
  69. data/lib/nmatrix/jruby/math.rb +501 -0
  70. data/lib/nmatrix/jruby/nmatrix_java.rb +840 -0
  71. data/lib/nmatrix/jruby/operators.rb +283 -0
  72. data/lib/nmatrix/jruby/slice.rb +264 -0
  73. data/lib/nmatrix/lapack_core.rb +181 -0
  74. data/lib/nmatrix/lapack_plugin.rb +44 -0
  75. data/lib/nmatrix/math.rb +953 -0
  76. data/lib/nmatrix/mkmf.rb +100 -0
  77. data/lib/nmatrix/monkeys.rb +137 -0
  78. data/lib/nmatrix/nmatrix.rb +1172 -0
  79. data/lib/nmatrix/rspec.rb +75 -0
  80. data/lib/nmatrix/shortcuts.rb +1163 -0
  81. data/lib/nmatrix/version.rb +39 -0
  82. data/lib/nmatrix/yale_functions.rb +118 -0
  83. data/lib/nmatrix.rb +28 -0
  84. data/spec/00_nmatrix_spec.rb +892 -0
  85. data/spec/01_enum_spec.rb +196 -0
  86. data/spec/02_slice_spec.rb +407 -0
  87. data/spec/03_nmatrix_monkeys_spec.rb +80 -0
  88. data/spec/2x2_dense_double.mat +0 -0
  89. data/spec/4x4_sparse.mat +0 -0
  90. data/spec/4x5_dense.mat +0 -0
  91. data/spec/blas_spec.rb +215 -0
  92. data/spec/elementwise_spec.rb +311 -0
  93. data/spec/homogeneous_spec.rb +100 -0
  94. data/spec/io/fortran_format_spec.rb +88 -0
  95. data/spec/io/harwell_boeing_spec.rb +98 -0
  96. data/spec/io/test.rua +9 -0
  97. data/spec/io_spec.rb +159 -0
  98. data/spec/lapack_core_spec.rb +482 -0
  99. data/spec/leakcheck.rb +16 -0
  100. data/spec/math_spec.rb +1363 -0
  101. data/spec/nmatrix_yale_resize_test_associations.yaml +2802 -0
  102. data/spec/nmatrix_yale_spec.rb +286 -0
  103. data/spec/rspec_monkeys.rb +56 -0
  104. data/spec/rspec_spec.rb +35 -0
  105. data/spec/shortcuts_spec.rb +474 -0
  106. data/spec/slice_set_spec.rb +162 -0
  107. data/spec/spec_helper.rb +172 -0
  108. data/spec/stat_spec.rb +214 -0
  109. data/spec/test.pcd +20 -0
  110. data/spec/utm5940.mtx +83844 -0
  111. metadata +295 -0
@@ -0,0 +1,840 @@
1
+ require 'java'
2
+ require_relative '../../../ext/nmatrix_java/vendor/commons-math3-3.6.1.jar'
3
+ require_relative '../../../ext/nmatrix_java/target/nmatrix.jar'
4
+
5
+ java_import 'org.apache.commons.math3.linear.ArrayRealVector'
6
+ java_import 'org.apache.commons.math3.linear.RealMatrix'
7
+ java_import 'org.apache.commons.math3.linear.MatrixUtils'
8
+ java_import 'org.apache.commons.math3.linear.DecompositionSolver'
9
+ java_import 'org.apache.commons.math3.linear.LUDecomposition'
10
+ java_import 'org.apache.commons.math3.linear.QRDecomposition'
11
+ java_import 'org.apache.commons.math3.linear.CholeskyDecomposition'
12
+ java_import 'MatrixGenerator'
13
+ java_import 'ArrayGenerator'
14
+ java_import 'MathHelper'
15
+ java_import 'ArrayComparator'
16
+
17
+ class NMatrix
18
+ include_package 'org.apache.commons.math3.analysis.function'
19
+ attr_accessor :shape, :dim, :dtype, :stype, :s
20
+
21
+ def initialize(*args)
22
+ if args[-1] == :copy
23
+ @shape = [2,2]
24
+ @s = [0,0,0,0]
25
+ @dim = shape.is_a?(Array) ? shape.length : 2
26
+ else
27
+ if (args.length <= 3)
28
+ @shape = args[0]
29
+ if args[1].is_a?(Array)
30
+ elements = args[1]
31
+ if args.length > 2
32
+ hash = args[2]
33
+ # puts hash
34
+ @dtype = hash[:dtype]
35
+ @stype = hash[:stype]
36
+ else
37
+ @dtype = :float64
38
+ @stype = :dense
39
+ end
40
+ else
41
+ # elements = Java::double[shape[0]*shape[1]].new{ Java::Double.NaN }
42
+ if args.length > 1
43
+ if args[1].is_a?(Symbol)
44
+ hash = args[1]
45
+ @dtype = hash[:dtype]
46
+ @stype = hash[:stype]
47
+ elements = Array.new(shape*shape) unless shape.is_a? Array
48
+ else
49
+ elements = Array.new(shape*shape) unless shape.is_a? Array
50
+ end
51
+ end
52
+ end
53
+ else
54
+
55
+ offset = 0
56
+ if (!args[0].is_a?(Symbol) && !args[0].is_a?(String))
57
+ @stype = :dense
58
+ else
59
+ offset = 1
60
+ @stype = :dense
61
+ @dtype = args[-1]
62
+ end
63
+
64
+ @shape = args[offset]
65
+ elements = args[offset+1]
66
+
67
+ end
68
+
69
+
70
+ @shape = [shape,shape] unless shape.is_a?(Array)
71
+ # @dtype = interpret_dtype(argc-1-offset, argv+offset+1, stype);
72
+ # @dtype = args[:dtype] if args[:dtype]
73
+ @dtype_sym = nil
74
+ @stype_sym = nil
75
+ @default_val_num = nil
76
+ @capacity_num = nil
77
+ @size = (0...@shape.size).inject(1) { |x,i| x * @shape[i] }
78
+
79
+ j=0
80
+
81
+ if (elements.is_a?(ArrayRealVector))
82
+ @s = elements
83
+ # elsif elements.java_class.to_s == "[D"
84
+ # @s = ArrayRealVector.new(elements)
85
+ else
86
+ storage = Array.new(size)
87
+ elements = [elements,elements] unless elements.is_a?(Array)
88
+ if size > elements.length
89
+ (0...size).each do |i|
90
+ j=0 unless j!=elements.length
91
+ storage[i] = elements[j]
92
+ j+=1
93
+ end
94
+ else
95
+ storage = elements
96
+ end
97
+ if @dtype == :object
98
+ @s = storage
99
+ else
100
+ @s = ArrayRealVector.new(storage.to_java Java::double)
101
+ end
102
+ end
103
+
104
+ @dim = @shape.is_a?(Array) ? @shape.length : 2
105
+
106
+ end
107
+ end
108
+
109
+ # Needs to be properly implemented
110
+ def self.min_dtype(alpha)
111
+ :int8
112
+ end
113
+
114
+ def self.upcast(alpha, beta)
115
+ false
116
+ end
117
+
118
+ def clone
119
+ result = create_dummy_nmatrix
120
+ # ArrayRealVector#clone is disable, hence use copy
121
+ # that returns a deep copy of the object.
122
+ result.s = @s.copy
123
+ return result
124
+ end
125
+
126
+ def entries
127
+ return @s.toArray.to_a
128
+ end
129
+
130
+ def twoDMat
131
+ return MatrixUtils.createRealMatrix MatrixGenerator.getMatrixDouble(self.s.toArray, @shape[0], @shape[1])
132
+ end
133
+
134
+ def dtype
135
+ return @dtype
136
+ end
137
+
138
+ #FIXME
139
+ def self.guess_dtype arg
140
+ :float32
141
+ end
142
+
143
+ def stype
144
+ @stype = :dense
145
+ end
146
+
147
+ def cast_full *args
148
+ if args.is_a? Hash
149
+ self.dtype = args[:dtype]
150
+ else
151
+ self.dtype = args[1]
152
+ end
153
+ return self
154
+ end
155
+
156
+ def default_value
157
+ return nil
158
+ end
159
+
160
+ def __list_default_value__
161
+ #not implemented currently
162
+ end
163
+
164
+ def __yale_default_value__
165
+ #not implemented currently
166
+ end
167
+
168
+ def [] *args
169
+ return xslice(args)
170
+ end
171
+
172
+ def slice(*args)
173
+ return xslice(args)
174
+ end
175
+
176
+ def []=(*args)
177
+ to_return = nil
178
+ if args.length > @dim+1
179
+ raise(ArgumentError, "wrong number of arguments (#{args.length} for #{effective_dim(dim+1)})" )
180
+ else
181
+ slice = get_slice(@dim, args, @shape)
182
+ dense_storage_set(slice, args[-1])
183
+ to_return = args[-1]
184
+ end
185
+ return to_return
186
+ end
187
+
188
+ def is_ref?
189
+
190
+ end
191
+
192
+ # def dim
193
+ # shape.is_a?(Array) ? shape.length : 2
194
+ # end
195
+
196
+ alias :dimensions :dim
197
+
198
+ def effective_dim(s)
199
+ d = 0
200
+ (0...@dim).each do |i|
201
+ d+=1 unless @shape[i] == 1
202
+ end
203
+ return d
204
+ end
205
+
206
+ alias :effective_dimensions :effective_dim
207
+
208
+
209
+
210
+ protected
211
+
212
+ def create_dummy_nmatrix
213
+ nmatrix = NMatrix.new(:copy)
214
+ nmatrix.shape = self.shape
215
+ nmatrix.dim = self.dim
216
+ nmatrix.dtype = self.dtype
217
+ nmatrix.stype = self.stype
218
+ return nmatrix
219
+ end
220
+
221
+ def __list_to_hash__
222
+
223
+ end
224
+
225
+ public
226
+
227
+ def shape
228
+ @shape
229
+ end
230
+
231
+ def supershape s
232
+ if (s[:src] == @s)
233
+ return shape
234
+ # easy case (not a slice)
235
+ else
236
+ @s = s[:src]
237
+ end
238
+
239
+ new_shape = Array.new(dim)
240
+ (0...dim).each do |index|
241
+ new_shape[index] = shape[index]
242
+ end
243
+
244
+ return new_shape
245
+ end
246
+
247
+ def offset
248
+ # ArrayRealVector takes care of the offset value when indexing a Vector.
249
+ # Hence, return 0.
250
+ 0
251
+ end
252
+
253
+ def det_exact
254
+ # if (:stype != :dense)
255
+ # raise Exception.new("can only calculate exact determinant for dense matrices")
256
+ # return nil
257
+ # end
258
+ raise(DataTypeError, "cannot call det_exact on unsigned type") if(self.dtype == :byte)
259
+ if (@dim != 2 || @shape[0] != @shape[1])
260
+ raise(ShapeError, "matrices must be square to have a determinant defined")
261
+ return nil
262
+ end
263
+ to_return = nil
264
+ if (dtype == :object)
265
+ # to_return = *reinterpret_cast<VALUE*>(result);
266
+ else
267
+ to_return = LUDecomposition.new(self.twoDMat).getDeterminant()
268
+ end
269
+
270
+ return to_return.round(3)
271
+ end
272
+
273
+ def det_exact2
274
+ if (@dim != 2 || @shape[0] != @shape[1])
275
+ raise(ShapeError, "matrices must be square to have a determinant defined")
276
+ return nil
277
+ end
278
+ to_return = nil
279
+ if (dtype == :object)
280
+ # to_return = *reinterpret_cast<VALUE*>(result);
281
+ else
282
+ to_return = LUDecomposition.new(self.twoDMat).getDeterminant()
283
+ end
284
+
285
+ return to_return.round(3)
286
+ end
287
+
288
+ def complex_conjugate!
289
+
290
+ end
291
+
292
+
293
+ protected
294
+
295
+ def count_max_elements
296
+ return size
297
+ end
298
+
299
+ def reshape_bang arg
300
+ if(@stype == :dense)
301
+ shape_ary = arg
302
+ size = count_max_elements
303
+ new_size = 1
304
+ shape = interpret_shape(shape_ary, dim)
305
+
306
+ (0...dim).each do |index|
307
+ new_size *= shape[index]
308
+ end
309
+
310
+ if (size == new_size)
311
+ self.shape = shape
312
+ self.dim = dim
313
+ return self
314
+ else
315
+ raise(ArgumentError, "reshape cannot resize; size of new and old matrices must match")
316
+ end
317
+ else
318
+ raise(NotImplementedError, "reshape in place only for dense stype")
319
+ end
320
+ end
321
+
322
+ def interpret_shape(shape_ary, dim)
323
+ shape = []
324
+
325
+ if shape_ary.is_a?(Array)
326
+ dim = shape_ary.length
327
+
328
+ (0...dim).each do |index|
329
+ shape[index] = shape_ary[index].to_i
330
+ end
331
+
332
+ elsif shape_ary.is_a?(FIXNUM)
333
+ dim = 2
334
+ shape = Array.new(dim)
335
+ shape[0] = shape_ary.to_i
336
+ shape[1] = shape_ary.to_i
337
+ else
338
+ raise(ArgumentError, "Expected an array of numbers or a single Fixnum for matrix shape")
339
+ end
340
+
341
+ return shape
342
+ end
343
+
344
+
345
+ public
346
+
347
+ def each_with_indices
348
+ nmatrix = create_dummy_nmatrix
349
+ stride = get_stride(self)
350
+ offset = 0
351
+ #Create indices and initialize them to zero
352
+ coords = Array.new(dim){ 0 }
353
+
354
+ shape_copy = Array.new(dim)
355
+ (0...size).each do |k|
356
+ dense_storage_coords(nmatrix, k, coords, stride, offset)
357
+ slice_index = dense_storage_pos(coords,stride)
358
+ ary = Array.new
359
+ if (@dtype == :object)
360
+ ary << self.s[slice_index]
361
+ else
362
+ ary << self.s.toArray.to_a[slice_index]
363
+ end
364
+ (0...dim).each do |p|
365
+ ary << coords[p]
366
+ end
367
+
368
+ # yield the array which now consists of the value and the indices
369
+ yield(ary)
370
+ end if block_given?
371
+
372
+ return nmatrix
373
+ end
374
+
375
+
376
+ def each_stored_with_indices
377
+ nmatrix = create_dummy_nmatrix
378
+ stride = get_stride(self)
379
+ offset = 0
380
+ #Create indices and initialize them to zero
381
+ coords = Array.new(dim){ 0 }
382
+
383
+ shape_copy = Array.new(dim)
384
+
385
+ (0...size).each do |k|
386
+ dense_storage_coords(nmatrix, k, coords, stride, offset)
387
+ slice_index = dense_storage_pos(coords,stride)
388
+ ary = Array.new
389
+ if (@dtype == :object)
390
+ ary << self.s[slice_index]
391
+ else
392
+ ary << self.s.toArray.to_a[slice_index]
393
+ end
394
+ (0...dim).each do |p|
395
+ ary << coords[p]
396
+ end
397
+ # yield the array which now consists of the value and the indices
398
+ yield(ary)
399
+ end if block_given?
400
+
401
+ return nmatrix
402
+ end
403
+
404
+ def map_stored
405
+
406
+ end
407
+
408
+ def each_ordered_stored_with_indices
409
+
410
+ end
411
+
412
+
413
+ protected
414
+
415
+ def __dense_each__
416
+ nmatrix = create_dummy_nmatrix
417
+ stride = get_stride(self)
418
+ offset = 0
419
+ #Create indices and initialize them to zero
420
+ coords = Array.new(dim){ 0 }
421
+
422
+ shape_copy = Array.new(dim)
423
+ (0...size).each do |k|
424
+ if (@dtype == :object)
425
+ dense_storage_coords(nmatrix, k, coords, stride, offset)
426
+ slice_index = dense_storage_pos(coords,stride)
427
+ yield self.s[slice_index]
428
+ else
429
+ dense_storage_coords(nmatrix, k, coords, stride, offset)
430
+ slice_index = dense_storage_pos(coords,stride)
431
+ yield self.s.toArray.to_a[slice_index]
432
+ end
433
+ end if block_given?
434
+ if (@dtype == :object)
435
+ return @s.to_enum
436
+ else
437
+ return @s.toArray().to_a.to_enum
438
+ end
439
+ end
440
+
441
+ def __dense_map__
442
+ nmatrix = create_dummy_nmatrix
443
+ stride = get_stride(self)
444
+ offset = 0
445
+ coords = Array.new(dim){ 0 }
446
+ shape_copy = Array.new(dim)
447
+
448
+ s= Java::double[size].new
449
+ (0...size).each do |k|
450
+ dense_storage_coords(nmatrix, k, coords, stride, offset)
451
+ slice_index = dense_storage_pos(coords,stride)
452
+
453
+ y = yield @s.getEntry(slice_index)
454
+ @s.setEntry(slice_index, y)
455
+ end
456
+ nmatrix.s = ArrayRealVector.new s
457
+
458
+ return nmatrix
459
+ end
460
+
461
+ def __dense_map_pair__
462
+
463
+ end
464
+
465
+ def __list_map_merged_stored__
466
+
467
+ end
468
+
469
+ def __list_map_stored__
470
+
471
+ end
472
+
473
+ def __yale_map_merged_stored__
474
+
475
+ end
476
+
477
+ def __yale_map_stored__
478
+
479
+ end
480
+
481
+ def __yale_stored_diagonal_each_with_indices__
482
+
483
+ end
484
+
485
+ def __yale_stored_nondiagonal_each_with_indices__
486
+
487
+ end
488
+
489
+
490
+ public
491
+
492
+ def ==(otherNmatrix)
493
+ result = false
494
+ if (otherNmatrix.is_a?(NMatrix))
495
+ #check dimension
496
+ if (@dim != otherNmatrix.dim)
497
+ raise(ShapeError, "cannot compare matrices with different dimension")
498
+ end
499
+ #check shape
500
+ (0...dim).each do |i|
501
+ if (@shape[i] != otherNmatrix.shape[i])
502
+ raise(ShapeError, "cannot compare matrices with different shapes");
503
+ end
504
+ end
505
+
506
+ #check the entries
507
+ if dtype == :object
508
+ result = @s == otherNmatrix.s
509
+ else
510
+ result = ArrayComparator.equals(@s.toArray, otherNmatrix.s.toArray)
511
+ end
512
+ end
513
+ result
514
+ end
515
+
516
+ def =~ (other)
517
+ lha = @s.toArray.to_a
518
+ rha = other.s.toArray.to_a
519
+ resultArray = Array.new(lha.length)
520
+ if (other.is_a?(NMatrix))
521
+ #check dimension
522
+ if (@dim != other.dim)
523
+ raise(ShapeError, "cannot compare matrices with different dimension")
524
+ return nil
525
+ end
526
+ #check shape
527
+ (0...dim).each do |i|
528
+ if (@shape[i] != other.shape[i])
529
+ raise(ShapeError, "cannot compare matrices with different shapes");
530
+ return nil
531
+ end
532
+ end
533
+ #check the entries
534
+ (0...lha.length).each do |i|
535
+ resultArray[i] = lha[i] == rha[i] ? true : false
536
+ end
537
+ result = NMatrix.new(:copy)
538
+ result.shape = @shape
539
+ result.dtype = :object
540
+ result.s = resultArray
541
+ end
542
+ result
543
+ end
544
+
545
+ def !~ (other)
546
+ lha = @s.toArray.to_a
547
+ rha = other.s.toArray.to_a
548
+ resultArray = Array.new(lha.length)
549
+ if (other.is_a?(NMatrix))
550
+ #check dimension
551
+ if (@dim != other.dim)
552
+ raise(ShapeError, "cannot compare matrices with different dimension")
553
+ return nil
554
+ end
555
+ #check shape
556
+ (0...dim).each do |i|
557
+ if (@shape[i] != other.shape[i])
558
+ raise(ShapeError, "cannot compare matrices with different shapes");
559
+ return nil
560
+ end
561
+ end
562
+ #check the entries
563
+ (0...lha.length).each do |i|
564
+ resultArray[i] = lha[i] != rha[i] ? true : false
565
+ end
566
+ result = NMatrix.new(:copy)
567
+ result.shape = @shape
568
+ result.dtype = :object
569
+ result.s = resultArray
570
+ end
571
+ result
572
+ end
573
+
574
+ def <= (other)
575
+ lha = @s.toArray.to_a
576
+ rha = other.s.toArray.to_a
577
+ resultArray = Array.new(lha.length)
578
+ if (other.is_a?(NMatrix))
579
+ #check dimension
580
+ if (@dim != other.dim)
581
+ raise(ShapeError, "cannot compare matrices with different dimension")
582
+ return nil
583
+ end
584
+ #check shape
585
+ (0...dim).each do |i|
586
+ if (@shape[i] != other.shape[i])
587
+ raise(ShapeError, "cannot compare matrices with different shapes");
588
+ return nil
589
+ end
590
+ end
591
+ #check the entries
592
+ (0...lha.length).each do |i|
593
+ resultArray[i] = lha[i] <= rha[i] ? true : false
594
+ end
595
+ result = NMatrix.new(:copy)
596
+ result.shape = @shape
597
+ result.dtype = :object
598
+ result.s = resultArray
599
+ end
600
+ result
601
+ end
602
+
603
+ def >= (other)
604
+ lha = @s.toArray.to_a
605
+ rha = other.s.toArray.to_a
606
+ resultArray = Array.new(lha.length)
607
+ if (other.is_a?(NMatrix))
608
+ #check dimension
609
+ if (@dim != other.dim)
610
+ raise(ShapeError, "cannot compare matrices with different dimension")
611
+ return nil
612
+ end
613
+ #check shape
614
+ (0...dim).each do |i|
615
+ if (@shape[i] != other.shape[i])
616
+ raise(ShapeError, "cannot compare matrices with different shapes");
617
+ return nil
618
+ end
619
+ end
620
+ #check the entries
621
+ (0...lha.length).each do |i|
622
+ resultArray[i] = lha[i] >= rha[i] ? true : false
623
+ end
624
+ result = NMatrix.new(:copy)
625
+ result.shape = @shape
626
+ result.dtype = :object
627
+ result.s = resultArray
628
+ end
629
+ result
630
+ end
631
+
632
+ def < (other)
633
+ lha = @s.toArray.to_a
634
+ rha = other.s.toArray.to_a
635
+ resultArray = Array.new(lha.length)
636
+ if (other.is_a?(NMatrix))
637
+ #check dimension
638
+ if (@dim != other.dim)
639
+ raise(ShapeError, "cannot compare matrices with different dimension")
640
+ return nil
641
+ end
642
+ #check shape
643
+ (0...dim).each do |i|
644
+ if (@shape[i] != other.shape[i])
645
+ raise(ShapeError, "cannot compare matrices with different shapes");
646
+ return nil
647
+ end
648
+ end
649
+ #check the entries
650
+ (0...lha.length).each do |i|
651
+ resultArray[i] = lha[i] < rha[i] ? true : false
652
+ end
653
+ result = NMatrix.new(:copy)
654
+ result.shape = @shape
655
+ result.dtype = :object
656
+ result.s = resultArray
657
+ end
658
+ result
659
+ end
660
+
661
+ def > (other)
662
+ lha = @s.toArray.to_a
663
+ rha = other.s.toArray.to_a
664
+ resultArray = Array.new(lha.length)
665
+ if (other.is_a?(NMatrix))
666
+ #check dimension
667
+ if (@dim != other.dim)
668
+ raise(ShapeError, "cannot compare matrices with different dimension")
669
+ return nil
670
+ end
671
+ #check shape
672
+ (0...dim).each do |i|
673
+ if (@shape[i] != other.shape[i])
674
+ raise(ShapeError, "cannot compare matrices with different shapes");
675
+ return nil
676
+ end
677
+ end
678
+ #check the entries
679
+ (0...lha.length).each do |i|
680
+ resultArray[i] = lha[i] > rha[i] ? true : false
681
+ end
682
+ result = NMatrix.new(:copy)
683
+ result.shape = @shape
684
+ result.dtype = :object
685
+ result.s = resultArray
686
+ end
687
+ result
688
+ end
689
+
690
+ # /////////////////////////
691
+ # // Matrix Math Methods //
692
+ # /////////////////////////
693
+
694
+ def dot(other)
695
+ result = nil
696
+ if (other.is_a?(NMatrix))
697
+ #check dimension
698
+ if (@shape.length!=2 || other.shape.length!=2)
699
+ raise(NotImplementedError, "please convert array to nx1 or 1xn NMatrix first")
700
+ return nil
701
+ end
702
+ #check shape
703
+ if (@shape[1] != other.shape[0])
704
+ raise(ArgumentError, "incompatible dimensions")
705
+ return nil
706
+ end
707
+
708
+ # if(@stype != other.stype)
709
+ # raise(NotImplementedError, "matrices must have same stype")
710
+ # end
711
+
712
+ result = create_dummy_nmatrix
713
+ result.shape = [@shape[0],other.shape[1]]
714
+ twoDMat = self.twoDMat.multiply(other.twoDMat)
715
+ result.s = ArrayRealVector.new(ArrayGenerator.getArrayDouble(twoDMat.getData, @shape[0],other.shape[1]))
716
+ else
717
+ raise(ArgumentError, "cannot have dot product with a scalar");
718
+ end
719
+ return result;
720
+ end
721
+
722
+ def symmetric?
723
+ return is_symmetric(false)
724
+ end
725
+
726
+ def is_symmetric(hermitian)
727
+ is_symmetric = true
728
+
729
+ if (@shape[0] == @shape[1] and @dim == 2)
730
+ if @stype == :dense
731
+ if (hermitian)
732
+ #Currently, we are not dealing with complex matrices.
733
+ eps = 0
734
+ is_symmetric = MatrixUtils.isSymmetric(self.twoDMat, eps)
735
+ else
736
+ eps = 0
737
+ is_symmetric = MatrixUtils.isSymmetric(self.twoDMat, eps)
738
+ end
739
+
740
+ else
741
+ #TODO: Implement, at the very least, yale_is_symmetric. Model it after yale/transp.template.c.
742
+ # raise Exception.new("symmetric? and hermitian? only implemented for dense currently")
743
+ end
744
+ end
745
+ return is_symmetric ? true : false
746
+ end
747
+
748
+ def hermitian?
749
+ return is_symmetric(true)
750
+ end
751
+
752
+ def capacity
753
+
754
+ end
755
+
756
+ # // protected methods
757
+
758
+ protected
759
+
760
+ def __inverse__(matrix, bool =true)
761
+ # if (:stype != :dense)
762
+ # raise Exception.new("needs exact determinant implementation for this matrix stype")
763
+ # return nil
764
+ # end
765
+
766
+ if (@dim != 2 || @shape[0] != @shape[1])
767
+ raise Exception.new("matrices must be square to have an inverse defined")
768
+ return nil
769
+ end
770
+ to_return = nil
771
+ if (dtype == :RUBYOBJ)
772
+ # to_return = *reinterpret_cast<VALUE*>(result);
773
+ else
774
+ to_return = create_dummy_nmatrix
775
+ twoDMat = MatrixUtils.inverse(matrix.twoDMat)
776
+ to_return.s = ArrayRealVector.new(ArrayGenerator.getArrayDouble(twoDMat.getData, @shape[0], @shape[1]))
777
+ end
778
+
779
+ return to_return
780
+ end
781
+
782
+ def __inverse__!
783
+ # if (:stype != :dense)
784
+ # raise Exception.new("needs exact determinant implementation for this matrix stype")
785
+ # return nil
786
+ # end
787
+
788
+ if (@dim != 2 || @shape[0] != @shape[1])
789
+ raise Exception.new("matrices must be square to have an inverse defined")
790
+ return nil
791
+ end
792
+ to_return = nil
793
+ if (dtype == :RUBYOBJ)
794
+ # to_return = *reinterpret_cast<VALUE*>(result);
795
+ else
796
+ twoDMat = MatrixUtils.inverse(self.twoDMat)
797
+ @s = ArrayRealVector.new(ArrayGenerator.getArrayDouble(twoDMat.getData, @shape[0], @shape[1]))
798
+ end
799
+
800
+ return self
801
+ end
802
+
803
+ def __inverse_exact__
804
+ # if (:stype != :dense)
805
+ # raise Exception.new("needs exact determinant implementation for this matrix stype")
806
+ # return nil
807
+ # end
808
+
809
+ if (@dim != 2 || @shape[0] != @shape[1])
810
+ raise Exception.new("matrices must be square to have an inverse defined")
811
+ return nil
812
+ end
813
+ to_return = nil
814
+ if (dtype == :RUBYOBJ)
815
+ # to_return = *reinterpret_cast<VALUE*>(result);
816
+ else
817
+ to_return = create_dummy_nmatrix
818
+ twoDMat = MatrixUtils.inverse(self.twoDMat)
819
+ to_return.s = ArrayRealVector.new(ArrayGenerator.getArrayDouble(twoDMat.getData, @shape[0], @shape[1]))
820
+ end
821
+
822
+ return to_return
823
+
824
+ end
825
+
826
+ private
827
+
828
+ # // private methods
829
+
830
+ def __hessenberg__(param)
831
+ raise(NotImplementedError, "Hessenberg Transformer not implemented for NMatrix-JRuby")
832
+ end
833
+ end
834
+
835
+ # load jruby implementation of operators.
836
+ require_relative './slice.rb'
837
+ require_relative './operators.rb'
838
+ require_relative './decomposition.rb'
839
+ require_relative './error.rb'
840
+ require_relative './enumerable.rb'