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,474 @@
1
+ # = NMatrix
2
+ #
3
+ # A linear algebra library for scientific computation in Ruby.
4
+ # NMatrix is part of SciRuby.
5
+ #
6
+ # NMatrix was originally inspired by and derived from NArray, by
7
+ # Masahiro Tanaka: http://narray.rubyforge.org
8
+ #
9
+ # == Copyright Information
10
+ #
11
+ # SciRuby is Copyright (c) 2010 - 2014, Ruby Science Foundation
12
+ # NMatrix is Copyright (c) 2012 - 2014, John Woods and the Ruby Science Foundation
13
+ #
14
+ # Please see LICENSE.txt for additional copyright notices.
15
+ #
16
+ # == Contributing
17
+ #
18
+ # By contributing source code to SciRuby, you agree to be bound by
19
+ # our Contributor Agreement:
20
+ #
21
+ # * https://github.com/SciRuby/sciruby/wiki/Contributor-Agreement
22
+ #
23
+ # == shortcuts_spec.rb
24
+ #
25
+ # Specs for the shortcuts used in NMatrix and in NVector.
26
+ #
27
+
28
+ require 'spec_helper'
29
+ require 'pry'
30
+
31
+ describe NMatrix do
32
+ it "zeros() creates a matrix of zeros" do
33
+ m = NMatrix.zeros(3)
34
+ n = NMatrix.new([3, 3], 0)
35
+
36
+ expect(m).to eq n
37
+ end
38
+
39
+ it "ones() creates a matrix of ones" do
40
+ m = NMatrix.ones(3)
41
+ n = NMatrix.new([3, 3], 1)
42
+
43
+ expect(m).to eq n
44
+ end
45
+
46
+ it "eye() creates an identity matrix" do
47
+ m = NMatrix.eye(3)
48
+ identity3 = NMatrix.new([3, 3], [1, 0, 0, 0, 1, 0, 0, 0, 1])
49
+
50
+ expect(m).to eq identity3
51
+ end
52
+
53
+ it "hilbert() creates an hilbert matrix" do
54
+ m = NMatrix.hilbert(8)
55
+ expect(m[4, 0]).to be_within(0.000001).of(0.2)
56
+ expect(m[4, 1]).to be_within(0.000001).of(0.16666666666666666)
57
+ expect(m[4, 2]).to be_within(0.000001).of(0.14285714285714285)
58
+ expect(m[4, 3]).to be_within(0.000001).of(0.125)
59
+
60
+ m = NMatrix.hilbert(3)
61
+ hilbert3 = NMatrix.new([3, 3], [1.0, 0.5, 0.3333333333333333,\
62
+ 0.5, 0.3333333333333333, 0.25, 0.3333333333333333, 0.25, 0.2])
63
+ expect(m).to eq hilbert3
64
+ 0.upto(2) do |i|
65
+ 0.upto(2) do |j|
66
+ expect(m[i, j]).to be_within(0.000001).of(hilbert3[i,j])
67
+ end
68
+ end
69
+ end
70
+
71
+ it "inv_hilbert() creates an inverse hilbert matrix" do
72
+ m = NMatrix.inv_hilbert(6)
73
+ inv_hilbert6 = [3360.0, -88200.0, 564480.0, -1411200.0]
74
+ expect(m[2,0]).to be_within(0.000001).of(inv_hilbert6[0])
75
+ expect(m[2,1]).to be_within(0.000001).of(inv_hilbert6[1])
76
+ expect(m[2,2]).to be_within(0.000001).of(inv_hilbert6[2])
77
+ expect(m[2,3]).to be_within(0.000001).of(inv_hilbert6[3])
78
+
79
+ m = NMatrix.inv_hilbert(3)
80
+ inv_hilbert3 = NMatrix.new([3, 3], [ 9.0, -36.0, 30.0, -36.0, 192.0, -180.0, 30.0, -180.0, 180.0] )
81
+ 0.upto(2) do |i|
82
+ 0.upto(2) do |j|
83
+ expect(m[i, j]).to be_within(0.000001).of(inv_hilbert3[i,j])
84
+ end
85
+ end
86
+ end
87
+
88
+ it "diag() creates a matrix with pre-supplied diagonal" do
89
+ arr = [1,2,3,4]
90
+ m = NMatrix.diag(arr)
91
+ expect(m.is_a?(NMatrix)).to be true
92
+ end
93
+
94
+ it "diagonals() contains the seeded values on the diagonal" do
95
+ arr = [1,2,3,4]
96
+ m = NMatrix.diagonals(arr)
97
+ expect(m[0,0]).to eq(arr[0])
98
+ expect(m[1,1]).to eq(arr[1])
99
+ expect(m[2,2]).to eq(arr[2])
100
+ expect(m[3,3]).to eq(arr[3])
101
+ end
102
+
103
+ ALL_DTYPES.each do |dtype|
104
+ [:dense, :yale, :list].each do |stype|
105
+ context "#block_diagonal #{dtype} #{stype}" do
106
+ it "block_diagonal() creates a block-diagonal NMatrix" do
107
+ pending("not yet implemented for NMatrix-JRuby") if jruby? and dtype == :object
108
+ a = NMatrix.new([2,2], [1,2,
109
+ 3,4])
110
+ b = NMatrix.new([1,1], [123.0])
111
+ c = NMatrix.new([3,3], [1,2,3,
112
+ 1,2,3,
113
+ 1,2,3])
114
+ d = Array[ [1,1,1], [2,2,2], [3,3,3] ]
115
+ e = 12
116
+ m = NMatrix.block_diagonal(a, b, c, d, e, dtype: dtype, stype: stype)
117
+ expect(m).to eq(NMatrix.new([10,10], [1, 2, 0, 0, 0, 0, 0, 0, 0, 0,
118
+ 3, 4, 0, 0, 0, 0, 0, 0, 0, 0,
119
+ 0, 0, 123, 0, 0, 0, 0, 0, 0, 0,
120
+ 0, 0, 0, 1, 2, 3, 0, 0, 0, 0,
121
+ 0, 0, 0, 1, 2, 3, 0, 0, 0, 0,
122
+ 0, 0, 0, 1, 2, 3, 0, 0, 0, 0,
123
+ 0, 0, 0, 0, 0, 0, 1, 1, 1, 0,
124
+ 0, 0, 0, 0, 0, 0, 2, 2, 2, 0,
125
+ 0, 0, 0, 0, 0, 0, 3, 3, 3, 0,
126
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 12], dtype: dtype, stype: stype))
127
+ end
128
+ end
129
+ end
130
+ end
131
+
132
+ context "::random" do
133
+ it "creates a matrix of random numbers" do
134
+ m = NMatrix.random(2)
135
+
136
+ expect(m.stype).to eq(:dense)
137
+ expect(m.dtype).to eq(:float64)
138
+ end
139
+
140
+ it "creates a matrix of random numbers with defined seed value" do
141
+ m1 = NMatrix.random(2,:seed => 62)
142
+ m2 = NMatrix.random(2,:seed => 62)
143
+ m3 = NMatrix.random(2,:seed => 65)
144
+
145
+
146
+ expect(m1).to eq(m2)
147
+ expect(m1).not_to eq(m3)
148
+
149
+ end
150
+
151
+ it "creates a complex matrix of random numbers" do
152
+ m = NMatrix.random(2, :dtype => :complex128)
153
+ end
154
+
155
+ it "correctly accepts :scale parameter" do
156
+ m = NMatrix.random([2,2], dtype: :byte, scale: 255)
157
+ m.each do |v|
158
+ expect(v).to be >= 0
159
+ expect(v).to be < 255
160
+ end
161
+ end
162
+
163
+ it "only accepts an integer or an array as dimension" do
164
+ m = NMatrix.random([2, 2])
165
+
166
+ expect(m.stype).to eq(:dense)
167
+ expect(m.dtype).to eq(:float64)
168
+
169
+ expect { NMatrix.random(2.0) }.to raise_error
170
+ expect { NMatrix.random("not an array or integer") }.to raise_error
171
+ end
172
+ end
173
+
174
+ context "::magic" do
175
+
176
+ ALL_DTYPES.each do |dtype|
177
+ context dtype do
178
+ it "creates a matrix with numbers from 1 to n^n(n squared)" do
179
+ pending("not yet implemented for NMatrix-JRuby") if jruby?
180
+ a = NMatrix.magic(3, dtype: dtype)
181
+ magic3 = NMatrix.new([3,3], [4, 9, 2, 3, 5, 7, 8, 1, 6], dtype: dtype)
182
+ expect(a).to eq magic3
183
+
184
+ b = NMatrix.magic(4, dtype: dtype)
185
+ magic4 = NMatrix.new([4,4], [1, 15, 14, 4, 12, 6, 7, 9, 8, 10, 11, 5, 13, 3, 2, 16], dtype: dtype)
186
+ expect(b).to eq magic4
187
+
188
+ c = NMatrix.magic(6, dtype: dtype)
189
+ magic6 = NMatrix.new([6,6], [31, 9, 2, 22, 27, 20, 3, 32, 7, 21, 23, 25, 35, 1, 6, 26, 19, 24, 4, 36, 29, 13, 18, 11, 30, 5, 34, 12, 14, 16, 8, 28, 33, 17, 10, 15], dtype: dtype)
190
+ expect(c).to eq magic6
191
+ end
192
+ end
193
+ end
194
+
195
+ it "shape of two is not allowed" do
196
+ expect { NMatrix.magic(2) }.to raise_error(ArgumentError)
197
+ end
198
+
199
+ it "Only accepts an integer as dimension" do
200
+ expect { NMatrix.magic(3.0) }.to raise_error(ArgumentError)
201
+ end
202
+ end
203
+
204
+ context "::linspace" do
205
+ it "creates a row vector when given only one shape parameter" do
206
+ pending("not yet implemented for NMatrix-JRuby") if jruby?
207
+ v = NMatrix.linspace(1, 10, 4)
208
+ #Expect a row vector only
209
+ expect(v.shape.length).to eq(1)
210
+
211
+ ans = [1.0,4.0,7.0,10.0]
212
+
213
+ expect(v[0]).to be_within(0.000001).of(ans[0])
214
+ expect(v[1]).to be_within(0.000001).of(ans[1])
215
+ expect(v[2]).to be_within(0.000001).of(ans[2])
216
+ expect(v[3]).to be_within(0.000001).of(ans[3])
217
+ end
218
+
219
+ it "creates a matrix of input shape with each entry linearly spaced in row major order" do
220
+ pending("not yet implemented for NMatrix-JRuby") if jruby?
221
+ v = NMatrix.linspace(1, Math::PI, [2,2])
222
+ expect(v.dtype).to eq(:float64)
223
+
224
+ ans = [1.0, 1.7138642072677612, 2.4277284145355225, 3.1415927410125732]
225
+
226
+ expect(v[0,0]).to be_within(0.000001).of(ans[0])
227
+ expect(v[0,1]).to be_within(0.000001).of(ans[1])
228
+ expect(v[1,0]).to be_within(0.000001).of(ans[2])
229
+ expect(v[1,1]).to be_within(0.000001).of(ans[3])
230
+ end
231
+ end
232
+
233
+ context "::logspace" do
234
+ it "creates a logarithmically spaced vector" do
235
+ pending("not yet implemented for NMatrix-JRuby") if jruby?
236
+ v = NMatrix.logspace(1, 2, 10)
237
+
238
+ expect(v.shape.length).to eq(1)
239
+
240
+ #Unit test taken from Matlab R2015b output of logspace(1,2,10)
241
+ ans = [10.0000, 12.9155, 16.6810, 21.5443, 27.8256, 35.9381, 46.4159, 59.9484, 77.4264, 100.0000]
242
+
243
+ expect(v[0].round(4)).to be_within(0.000001).of(ans[0])
244
+ expect(v[1].round(4)).to be_within(0.000001).of(ans[1])
245
+ expect(v[2].round(4)).to be_within(0.000001).of(ans[2])
246
+ expect(v[3].round(4)).to be_within(0.000001).of(ans[3])
247
+ expect(v[4].round(4)).to be_within(0.000001).of(ans[4])
248
+ expect(v[5].round(4)).to be_within(0.000001).of(ans[5])
249
+ expect(v[6].round(4)).to be_within(0.000001).of(ans[6])
250
+ expect(v[7].round(4)).to be_within(0.000001).of(ans[7])
251
+ expect(v[8].round(4)).to be_within(0.000001).of(ans[8])
252
+ expect(v[9].round(4)).to be_within(0.000001).of(ans[9])
253
+ end
254
+
255
+ it "creates a logarithmically spaced vector bounded by Math::PI if :pi is pre-supplied" do
256
+ pending("not yet implemented for NMatrix-JRuby") if jruby?
257
+ v = NMatrix.logspace(1, :pi, 7)
258
+
259
+ #Unit test taken from Matlab R2015b output of logspace(1,pi,10)
260
+ ans = [10.0000, 8.2450, 6.7980, 5.6050, 4.6213, 3.8103, 3.1416]
261
+
262
+ expect(v[0].round(4)).to be_within(0.000001).of(ans[0])
263
+ expect(v[1].round(4)).to be_within(0.000001).of(ans[1])
264
+ expect(v[2].round(4)).to be_within(0.000001).of(ans[2])
265
+ expect(v[3].round(4)).to be_within(0.000001).of(ans[3])
266
+ expect(v[4].round(4)).to be_within(0.000001).of(ans[4])
267
+ expect(v[5].round(4)).to be_within(0.000001).of(ans[5])
268
+ expect(v[6].round(4)).to be_within(0.000001).of(ans[6])
269
+ end
270
+
271
+ it "creates a matrix of input shape with each entry logarithmically spaced in row major order" do
272
+ pending("not yet implemented for NMatrix-JRuby") if jruby?
273
+ v = NMatrix.logspace(1, 2, [3,2])
274
+
275
+ ans = [10.0, 15.8489, 25.1189, 39.8107, 63.0957, 100.0]
276
+
277
+ expect(v[0,0].round(4)).to be_within(0.000001).of(ans[0])
278
+ expect(v[0,1].round(4)).to be_within(0.000001).of(ans[1])
279
+ expect(v[1,0].round(4)).to be_within(0.000001).of(ans[2])
280
+ expect(v[1,1].round(4)).to be_within(0.000001).of(ans[3])
281
+ expect(v[2,0].round(4)).to be_within(0.000001).of(ans[4])
282
+ expect(v[2,1].round(4)).to be_within(0.000001).of(ans[5])
283
+ end
284
+ end
285
+
286
+ it "seq() creates a matrix of integers, sequentially" do
287
+ m = NMatrix.seq(2) # 2x2 matrix.
288
+ value = 0
289
+
290
+ 2.times do |i|
291
+ 2.times do |j|
292
+ expect(m[i,j]).to eq(value)
293
+ value += 1
294
+ end
295
+ end
296
+ end
297
+
298
+ it "indgen() creates a matrix of integers as well as seq()" do
299
+ m = NMatrix.indgen(2) # 2x2 matrix.
300
+ value = 0
301
+
302
+ 2.times do |i|
303
+ 2.times do |j|
304
+ expect(m[i, j]).to eq(value)
305
+ value += 1
306
+ end
307
+ end
308
+ end
309
+
310
+ it "findgen creates a matrix of floats, sequentially" do
311
+ m = NMatrix.findgen(2) # 2x2 matrix.
312
+ value = 0
313
+
314
+ 2.times do |i|
315
+ 2.times do |j|
316
+ expect(m[i, j]/10).to be_within(Float::EPSILON).of(value.to_f/10)
317
+ value += 1
318
+ end
319
+ end
320
+ end
321
+
322
+ it "bindgen() creates a matrix of bytes" do
323
+ m = NMatrix.bindgen(2) # 2x2 matrix.
324
+ value = 0
325
+
326
+ 2.times do |i|
327
+ 2.times do |j|
328
+ expect(m[i, j]).to eq(value)
329
+ value += 1
330
+ end
331
+ end
332
+ end
333
+
334
+ it "cindgen() creates a matrix of complexes" do
335
+ m = NMatrix.cindgen(2) # 2x2 matrix.
336
+ value = 0
337
+
338
+ 2.times do |i|
339
+ 2.times do |j|
340
+ expect(m[i, j].real).to be_within(Float::EPSILON).of(value)
341
+ expect(m[i, j].imag).to be_within(Float::EPSILON).of(0.0)
342
+ value += 1
343
+ end
344
+ end
345
+ end
346
+
347
+ it "column() returns a NMatrix" do
348
+ m = NMatrix.random(3)
349
+
350
+ expect(m.column(2).is_a?(NMatrix)).to be true
351
+ end
352
+
353
+ it "row() returns a NMatrix" do
354
+ m = NMatrix.random(3)
355
+
356
+ expect(m.row(2).is_a?(NMatrix)).to be true
357
+ end
358
+
359
+ it "diagonals() creates an NMatrix" do
360
+ arr = [1,2,3,4]
361
+ m = NMatrix.diagonals(arr)
362
+ expect(m.is_a?(NMatrix)).to be true
363
+ end
364
+
365
+ it "diagonals() contains the seeded values on the diagonal" do
366
+ arr = [1,2,3,4]
367
+ m = NMatrix.diagonals(arr)
368
+ expect(m[0,0]).to eq(arr[0])
369
+ expect(m[1,1]).to eq(arr[1])
370
+ expect(m[2,2]).to eq(arr[2])
371
+ expect(m[3,3]).to eq(arr[3])
372
+ end
373
+
374
+ context "_like constructors" do
375
+ before :each do
376
+ @nm_1d = NMatrix[5.0,0.0,1.0,2.0,3.0]
377
+ @nm_2d = NMatrix[[0.0,1.0],[2.0,3.0]]
378
+ end
379
+
380
+ it "should create an nmatrix of ones with dimensions and type the same as its argument" do
381
+ pending("not yet implemented for NMatrix-JRuby") if jruby?
382
+ expect(NMatrix.ones_like(@nm_1d)).to eq NMatrix[1.0, 1.0, 1.0, 1.0, 1.0]
383
+ expect(NMatrix.ones_like(@nm_2d)).to eq NMatrix[[1.0, 1.0], [1.0, 1.0]]
384
+ end
385
+
386
+ it "should create an nmatrix of zeros with dimensions and type the same as its argument" do
387
+ expect(NMatrix.zeros_like(@nm_1d)).to eq NMatrix[0.0, 0.0, 0.0, 0.0, 0.0]
388
+ expect(NMatrix.zeros_like(@nm_2d)).to eq NMatrix[[0.0, 0.0], [0.0, 0.0]]
389
+ end
390
+ end
391
+
392
+ end
393
+
394
+ describe "NVector" do
395
+
396
+ it "zeros() creates a vector of zeros" do
397
+ v = NVector.zeros(4)
398
+
399
+ 4.times do |i|
400
+ expect(v[i]).to eq(0)
401
+ end
402
+ end
403
+
404
+ it "ones() creates a vector of ones" do
405
+ pending("not yet implemented for NMatrix-JRuby") if jruby?
406
+ v = NVector.ones(3)
407
+
408
+ 3.times do |i|
409
+ expect(v[i]).to eq(1)
410
+ end
411
+ end
412
+
413
+ it "random() creates a vector of random numbers" do
414
+ pending("not yet implemented for NMatrix-JRuby") if jruby?
415
+ v = NVector.random(4)
416
+ expect(v.dtype).to eq(:float64)
417
+ expect(v.stype).to eq(:dense)
418
+ end
419
+
420
+ it "seq() creates a vector of integers, sequentially" do
421
+ v = NVector.seq(7)
422
+ expect(v).to eq(NMatrix.new([7,1], [0, 1, 2, 3, 4, 5, 6]))
423
+ end
424
+
425
+ it "seq() only accepts integers as dimension" do
426
+ expect { NVector.seq(3) }.to_not raise_error
427
+
428
+ expect { NVector.seq([1, 3]) }.to raise_error
429
+ expect { NVector.seq(:wtf) }.to raise_error
430
+ end
431
+
432
+ it "indgen() creates a vector of integers as well as seq()" do
433
+ v = NVector.indgen(7)
434
+ expect(v).to eq(NMatrix.new([7,1], [0, 1, 2, 3, 4, 5, 6]))
435
+ end
436
+
437
+ it "findgen creates a vector of floats, sequentially" do
438
+ v = NVector.findgen(2)
439
+ expect(v).to eq(NMatrix.new([2,1], [0.0, 1.0]))
440
+ end
441
+
442
+ it "bindgen() creates a vector of bytes, sequentially" do
443
+ v = NVector.bindgen(4)
444
+ expect(v).to eq(NMatrix.new([4,1], [0, 1, 2, 3], dtype: :byte))
445
+ end
446
+
447
+ it "cindgen() creates a vector of complexes, sequentially" do
448
+ pending("not yet implemented for NMatrix-JRuby") if jruby?
449
+ v = NVector.cindgen(2)
450
+ expect(v).to eq(NMatrix.new([2,1], [Complex(0.0, 0.0), Complex(1.0, 0.0)], dtype: :complex64))
451
+ end
452
+
453
+ it "linspace() creates a vector with n values equally spaced between a and b" do
454
+ pending("not yet implemented for NMatrix-JRuby") if jruby?
455
+ v = NVector.linspace(0, 2, 5)
456
+ expect(v).to eq(NMatrix.new([5,1], [0.0, 0.5, 1.0, 1.5, 2.0]))
457
+ end
458
+
459
+ it "logspace() creates a vector with n values logarithmically spaced between decades 10^a and 10^b" do
460
+ pending("not yet implemented for NMatrix-JRuby") if jruby?
461
+ v = NVector.logspace(0, 3, 4)
462
+ expect(v).to eq(NMatrix.new([4,1], [1.0, 10.0, 100.0, 1000.0]))
463
+ end
464
+ end
465
+
466
+ describe "Inline constructor" do
467
+
468
+ it "creates a NMatrix with the given values" do
469
+ m = NMatrix.new([2, 2], [1, 4, 6, 7])
470
+ n = NMatrix[[1, 4], [6, 7]]
471
+
472
+ expect(m).to eq n
473
+ end
474
+ end
@@ -0,0 +1,162 @@
1
+ # = NMatrix
2
+ #
3
+ # A linear algebra library for scientific computation in Ruby.
4
+ # NMatrix is part of SciRuby.
5
+ #
6
+ # NMatrix was originally inspired by and derived from NArray, by
7
+ # Masahiro Tanaka: http://narray.rubyforge.org
8
+ #
9
+ # == Copyright Information
10
+ #
11
+ # SciRuby is Copyright (c) 2010 - 2014, Ruby Science Foundation
12
+ # NMatrix is Copyright (c) 2012 - 2014, John Woods and the Ruby Science Foundation
13
+ #
14
+ # Please see LICENSE.txt for additional copyright notices.
15
+ #
16
+ # == Contributing
17
+ #
18
+ # By contributing source code to SciRuby, you agree to be bound by
19
+ # our Contributor Agreement:
20
+ #
21
+ # * https://github.com/SciRuby/sciruby/wiki/Contributor-Agreement
22
+ #
23
+ # == slice_set_spec.rb
24
+ #
25
+ # Test of slice set operations.
26
+
27
+ require 'spec_helper'
28
+ require 'pry'
29
+
30
+ describe "Set slice operation" do
31
+ include RSpec::Longrun::DSL
32
+
33
+ [:dense, :yale, :list].each do |stype|
34
+ context "for #{stype}" do
35
+ before :each do
36
+ @m = create_matrix(stype)
37
+ end
38
+
39
+ example "set and unset a range of entries with single values" do
40
+
41
+ if stype == :yale
42
+ step "verify correct arrangement of Yale IJA and A arrays" do
43
+ @m.extend NMatrix::YaleFunctions unless jruby?
44
+ if jruby?
45
+ pending("not yet implemented for NMatrix-JRuby")
46
+ else
47
+ expect(@m.yale_ija).to eq([4,6,8,10,1,2,0,2,0,1])
48
+ end
49
+ expect(@m.yale_a).to eq([0,4,8,0, 1,2,3,5,6,7])
50
+ end
51
+ end
52
+
53
+ step "set and reset a single entry" do
54
+ n = @m.clone
55
+ old_val = @m[0,0]
56
+ @m[0,0] = 100
57
+ expect(@m[0,0]).to eq(100)
58
+ @m[0,0] = old_val
59
+ expect(@m).to eq(n)
60
+ end
61
+
62
+ if stype == :yale
63
+ n = @m.clone
64
+ step "set a row of entries" do
65
+ n[0,0..2] = 0
66
+ expect(n[0,0..2].to_flat_array).to eq([0,0,0])
67
+ expect(n[1,0..2].to_flat_array).to eq([3,4,5])
68
+ expect(n[2,0..2].to_flat_array).to eq([6,7,8])
69
+ end
70
+
71
+ step "set a second row of entries" do
72
+ n[2,0..2] = 0
73
+ expect(n[2,0..2].to_flat_array).to eq([0,0,0])
74
+ expect(n[1,0..2].to_flat_array).to eq([3,4,5])
75
+ end
76
+
77
+ step "reset both rows of entries" do
78
+ n[0,0..2] = [0,1,2]
79
+ n[2,0..2] = [6,7,8]
80
+ expect(n).to eq(@m)
81
+ end
82
+ end
83
+
84
+ slice_result_a = NMatrix.new(:dense, 2, 100, @m.dtype).cast(stype)
85
+ slice_result_b = NMatrix.new(:dense, 2, 0, @m.dtype).cast(stype)
86
+ m = @m.clone
87
+
88
+ step "set upper left-hand 2x2 corner to 100" do
89
+ m[0..1,0..1] = 100
90
+
91
+ if stype == :yale
92
+ expect(m.yale_ija).to eq([4, 6, 8, 10, 1, 2, 0, 2, 0, 1])
93
+ expect(m.yale_a).to eq([100, 100, 8, 0, 100, 2, 100, 5, 6, 7])
94
+ end
95
+
96
+ expect(m[0..1,0..1]).to eq(slice_result_a)
97
+ expect(m[2,0..1]).to eq(@m[2,0..1])
98
+ expect(m[0..1,2]).to eq(@m[0..1,2])
99
+ end
100
+
101
+ step "set upper left-hand 2x2 corner to 0" do
102
+ m[0..1,0..1] = 0
103
+ if stype == :yale
104
+ expect([4,5,6,8,2,2,0,1]).to eq(m.yale_ija)
105
+ expect([0,0,8,0,2,5,6,7]).to eq(m.yale_a)
106
+ end
107
+
108
+ expect(m[0..1,0..1]).to eq(slice_result_b)
109
+ end
110
+
111
+ m = @m.clone
112
+ step "set lower left-hand 2x2 corner to 100" do
113
+ m[1..2,0..1] = 100
114
+ expect(m[1..2,0..1]).to eq(slice_result_a)
115
+ expect(m[0,0..1]).to eq(@m[0,0..1])
116
+ expect(m[1..2,2]).to eq(@m[1..2,2])
117
+ end
118
+
119
+ step "set lower left-hand 2x2 corner to 0" do
120
+ m[1..2,0..1] = 0
121
+ expect(m[1..2,0..1]).to eq(slice_result_b)
122
+ end
123
+
124
+ m = @m.clone
125
+ step "set lower right-hand 2x2 corner to 100" do
126
+ m[1..2,1..2] = 100
127
+ expect(m[1..2,1..2]).to eq(slice_result_a)
128
+ expect(m[0,1..2]).to eq(@m[0,1..2])
129
+ expect(m[1..2,0]).to eq(@m[1..2,0])
130
+ end
131
+
132
+ step "set lower right-hand 2x2 corner to 0" do
133
+ m[1..2,1..2] = 0
134
+ expect(m[1..2,1..2]).to eq(slice_result_b)
135
+ end
136
+
137
+ m = @m.clone
138
+ step "set upper right-hand 2x2 corner to 100" do
139
+ m[0..1,1..2] = 100
140
+ expect(m[0..1,1..2]).to eq(slice_result_a)
141
+ expect(m[2,1..2]).to eq(@m[2,1..2])
142
+ expect(m[0..1,0]).to eq(@m[0..1,0])
143
+ end
144
+
145
+ step "set upper right-hand 2x2 corner to 0" do
146
+ m[0..1,1..2] = 0
147
+ expect(m[0..1,1..2]).to eq(slice_result_b)
148
+ end
149
+ end
150
+
151
+ example "set a range of values to a matrix's contents" do
152
+ pending("not yet implemented for int dtype for NMatrix-JRuby") if jruby?
153
+ x = NMatrix.new(4, stype: :yale, dtype: :int16)
154
+ x.extend NMatrix::YaleFunctions if stype == :yale
155
+ x[1..3,1..3] = @m
156
+ expect(x.to_flat_array).to eq([0,0,0,0, 0,0,1,2, 0,3,4,5, 0,6,7,8])
157
+ end
158
+
159
+ end
160
+ end
161
+
162
+ end