nmatrix 0.2.1 → 0.2.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (41) hide show
  1. checksums.yaml +4 -4
  2. data/ext/nmatrix/data/data.cpp +9 -9
  3. data/ext/nmatrix/data/data.h +7 -8
  4. data/ext/nmatrix/data/ruby_object.h +1 -4
  5. data/ext/nmatrix/extconf.rb +9 -127
  6. data/ext/nmatrix/math.cpp +25 -25
  7. data/ext/nmatrix/math/asum.h +10 -31
  8. data/ext/nmatrix/math/cblas_templates_core.h +10 -10
  9. data/ext/nmatrix/math/getrf.h +2 -2
  10. data/ext/nmatrix/math/imax.h +12 -9
  11. data/ext/nmatrix/math/laswp.h +3 -3
  12. data/ext/nmatrix/math/long_dtype.h +16 -3
  13. data/ext/nmatrix/math/magnitude.h +54 -0
  14. data/ext/nmatrix/math/nrm2.h +19 -14
  15. data/ext/nmatrix/math/trsm.h +40 -36
  16. data/ext/nmatrix/math/util.h +14 -0
  17. data/ext/nmatrix/nmatrix.h +39 -1
  18. data/ext/nmatrix/ruby_nmatrix.c +45 -83
  19. data/ext/nmatrix/storage/common.h +9 -3
  20. data/ext/nmatrix/storage/dense/dense.cpp +4 -4
  21. data/ext/nmatrix/storage/list/list.cpp +2 -2
  22. data/ext/nmatrix/storage/yale/class.h +1 -1
  23. data/lib/nmatrix/blas.rb +103 -34
  24. data/lib/nmatrix/io/fortran_format.rb +8 -5
  25. data/lib/nmatrix/io/harwell_boeing.rb +11 -10
  26. data/lib/nmatrix/io/market.rb +9 -6
  27. data/lib/nmatrix/io/mat5_reader.rb +54 -29
  28. data/lib/nmatrix/io/mat_reader.rb +26 -14
  29. data/lib/nmatrix/io/point_cloud.rb +19 -11
  30. data/lib/nmatrix/math.rb +224 -5
  31. data/lib/nmatrix/mkmf.rb +103 -0
  32. data/lib/nmatrix/nmatrix.rb +20 -6
  33. data/lib/nmatrix/shortcuts.rb +415 -0
  34. data/lib/nmatrix/version.rb +1 -1
  35. data/spec/00_nmatrix_spec.rb +50 -1
  36. data/spec/02_slice_spec.rb +21 -21
  37. data/spec/blas_spec.rb +25 -3
  38. data/spec/math_spec.rb +233 -5
  39. data/spec/shortcuts_spec.rb +145 -5
  40. data/spec/spec_helper.rb +24 -1
  41. metadata +20 -4
@@ -50,10 +50,45 @@ describe NMatrix do
50
50
  expect(m).to eq identity3
51
51
  end
52
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
+
53
88
  it "diag() creates a matrix with pre-supplied diagonal" do
54
89
  arr = [1,2,3,4]
55
90
  m = NMatrix.diag(arr)
56
- expect(m.is_a?(NMatrix)).to be_true
91
+ expect(m.is_a?(NMatrix)).to be true
57
92
  end
58
93
 
59
94
  it "diagonals() contains the seeded values on the diagonal" do
@@ -123,6 +158,112 @@ describe NMatrix do
123
158
  expect { NMatrix.random("not an array or integer") }.to raise_error
124
159
  end
125
160
  end
161
+
162
+ context "::magic" do
163
+
164
+ ALL_DTYPES.each do |dtype|
165
+ context dtype do
166
+ it "creates a matrix with numbers from 1 to n^n(n squared)" do
167
+ a = NMatrix.magic(3, dtype: dtype)
168
+ magic3 = NMatrix.new([3,3], [4, 9, 2, 3, 5, 7, 8, 1, 6], dtype: dtype)
169
+ expect(a).to eq magic3
170
+
171
+ b = NMatrix.magic(4, dtype: dtype)
172
+ magic4 = NMatrix.new([4,4], [1, 15, 14, 4, 12, 6, 7, 9, 8, 10, 11, 5, 13, 3, 2, 16], dtype: dtype)
173
+ expect(b).to eq magic4
174
+
175
+ c = NMatrix.magic(6, dtype: dtype)
176
+ 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)
177
+ expect(c).to eq magic6
178
+ end
179
+ end
180
+ end
181
+
182
+ it "shape of two is not allowed" do
183
+ expect { NMatrix.magic(2) }.to raise_error(ArgumentError)
184
+ end
185
+
186
+ it "Only accepts an integer as dimension" do
187
+ expect { NMatrix.magic(3.0) }.to raise_error(ArgumentError)
188
+ end
189
+ end
190
+
191
+ context "::linspace" do
192
+ it "creates a row vector when given only one shape parameter" do
193
+ v = NMatrix.linspace(1, 10, 4)
194
+ #Expect a row vector only
195
+ expect(v.shape.length).to eq(1)
196
+
197
+ ans = [1.0,4.0,7.0,10.0]
198
+
199
+ expect(v[0]).to be_within(0.000001).of(ans[0])
200
+ expect(v[1]).to be_within(0.000001).of(ans[1])
201
+ expect(v[2]).to be_within(0.000001).of(ans[2])
202
+ expect(v[3]).to be_within(0.000001).of(ans[3])
203
+ end
204
+
205
+ it "creates a matrix of input shape with each entry linearly spaced in row major order" do
206
+ v = NMatrix.linspace(1, Math::PI, [2,2])
207
+ expect(v.dtype).to eq(:float64)
208
+
209
+ ans = [1.0, 1.7138642072677612, 2.4277284145355225, 3.1415927410125732]
210
+
211
+ expect(v[0,0]).to be_within(0.000001).of(ans[0])
212
+ expect(v[0,1]).to be_within(0.000001).of(ans[1])
213
+ expect(v[1,0]).to be_within(0.000001).of(ans[2])
214
+ expect(v[1,1]).to be_within(0.000001).of(ans[3])
215
+ end
216
+ end
217
+
218
+ context "::logspace" do
219
+ it "creates a logarithmically spaced vector" do
220
+ v = NMatrix.logspace(1, 2, 10)
221
+
222
+ expect(v.shape.length).to eq(1)
223
+
224
+ #Unit test taken from Matlab R2015b output of logspace(1,2,10)
225
+ ans = [10.0000, 12.9155, 16.6810, 21.5443, 27.8256, 35.9381, 46.4159, 59.9484, 77.4264, 100.0000]
226
+
227
+ expect(v[0].round(4)).to be_within(0.000001).of(ans[0])
228
+ expect(v[1].round(4)).to be_within(0.000001).of(ans[1])
229
+ expect(v[2].round(4)).to be_within(0.000001).of(ans[2])
230
+ expect(v[3].round(4)).to be_within(0.000001).of(ans[3])
231
+ expect(v[4].round(4)).to be_within(0.000001).of(ans[4])
232
+ expect(v[5].round(4)).to be_within(0.000001).of(ans[5])
233
+ expect(v[6].round(4)).to be_within(0.000001).of(ans[6])
234
+ expect(v[7].round(4)).to be_within(0.000001).of(ans[7])
235
+ expect(v[8].round(4)).to be_within(0.000001).of(ans[8])
236
+ expect(v[9].round(4)).to be_within(0.000001).of(ans[9])
237
+ end
238
+
239
+ it "creates a logarithmically spaced vector bounded by Math::PI if :pi is pre-supplied" do
240
+ v = NMatrix.logspace(1, :pi, 7)
241
+
242
+ #Unit test taken from Matlab R2015b output of logspace(1,pi,10)
243
+ ans = [10.0000, 8.2450, 6.7980, 5.6050, 4.6213, 3.8103, 3.1416]
244
+
245
+ expect(v[0].round(4)).to be_within(0.000001).of(ans[0])
246
+ expect(v[1].round(4)).to be_within(0.000001).of(ans[1])
247
+ expect(v[2].round(4)).to be_within(0.000001).of(ans[2])
248
+ expect(v[3].round(4)).to be_within(0.000001).of(ans[3])
249
+ expect(v[4].round(4)).to be_within(0.000001).of(ans[4])
250
+ expect(v[5].round(4)).to be_within(0.000001).of(ans[5])
251
+ expect(v[6].round(4)).to be_within(0.000001).of(ans[6])
252
+ end
253
+
254
+ it "creates a matrix of input shape with each entry logarithmically spaced in row major order" do
255
+ v = NMatrix.logspace(1, 2, [3,2])
256
+
257
+ ans = [10.0, 15.8489, 25.1189, 39.8107, 63.0957, 100.0]
258
+
259
+ expect(v[0,0].round(4)).to be_within(0.000001).of(ans[0])
260
+ expect(v[0,1].round(4)).to be_within(0.000001).of(ans[1])
261
+ expect(v[1,0].round(4)).to be_within(0.000001).of(ans[2])
262
+ expect(v[1,1].round(4)).to be_within(0.000001).of(ans[3])
263
+ expect(v[2,0].round(4)).to be_within(0.000001).of(ans[4])
264
+ expect(v[2,1].round(4)).to be_within(0.000001).of(ans[5])
265
+ end
266
+ end
126
267
 
127
268
  it "seq() creates a matrix of integers, sequentially" do
128
269
  m = NMatrix.seq(2) # 2x2 matrix.
@@ -136,7 +277,6 @@ describe NMatrix do
136
277
  end
137
278
  end
138
279
 
139
-
140
280
  it "indgen() creates a matrix of integers as well as seq()" do
141
281
  m = NMatrix.indgen(2) # 2x2 matrix.
142
282
  value = 0
@@ -189,19 +329,19 @@ describe NMatrix do
189
329
  it "column() returns a NMatrix" do
190
330
  m = NMatrix.random(3)
191
331
 
192
- expect(m.column(2).is_a?(NMatrix)).to be_true
332
+ expect(m.column(2).is_a?(NMatrix)).to be true
193
333
  end
194
334
 
195
335
  it "row() returns a NMatrix" do
196
336
  m = NMatrix.random(3)
197
337
 
198
- expect(m.row(2).is_a?(NMatrix)).to be_true
338
+ expect(m.row(2).is_a?(NMatrix)).to be true
199
339
  end
200
340
 
201
341
  it "diagonals() creates an NMatrix" do
202
342
  arr = [1,2,3,4]
203
343
  m = NMatrix.diagonals(arr)
204
- expect(m.is_a?(NMatrix)).to be_true
344
+ expect(m.is_a?(NMatrix)).to be true
205
345
  end
206
346
 
207
347
  it "diagonals() contains the seeded values on the diagonal" do
@@ -32,7 +32,7 @@ require "./lib/nmatrix/rspec"
32
32
 
33
33
  ALL_DTYPES = [:byte,:int8,:int16,:int32,:int64, :float32,:float64, :object,
34
34
  :complex64, :complex128]
35
-
35
+
36
36
  NON_INTEGER_DTYPES = [:float32, :float64, :complex64, :complex128,
37
37
  :object]
38
38
 
@@ -48,6 +48,29 @@ COMPLEX_MATRIX32A_ARRAY = MATRIX32A_ARRAY.zip(MATRIX32A_ARRAY.reverse).collect {
48
48
  GETRF_EXAMPLE_ARRAY = [-1,0,10,4,9,2,3,5,7,8,1,6]
49
49
  GETRF_SOLUTION_ARRAY = [9.0, 2.0, 3.0, 5.0, 7.0/9, 58.0/9, -4.0/3, 19.0/9, -1.0/9, 1.0/29, 301.0/29, 130.0/29]
50
50
 
51
+ TAU_SOLUTION_ARRAY = [1.8571428571428572,1.9938461538461538, 0.0]
52
+
53
+ GEQRF_SOLUTION_ARRAY =[ -14.0, -21.0, 14.000000000000002,
54
+ 0.23076923076923078, -175.00000000000003, 70.00000000000001,
55
+ -0.15384615384615385, 0.055555555555555546, -35.0]
56
+
57
+ R_SOLUTION_ARRAY = [-159.2388143638353, -41.00131005172065, -56.75123892439876, -90.75048729628048,
58
+ 0.0, 25.137473501580676, 2.073591725046292, 9.790607357775713,
59
+ 0.0, 0.0, -20.83259700334131, -17.592414929551445]
60
+
61
+ Q_SOLUTION_ARRAY_1 = [-0.632455532033676, -0.5209522876558295, -0.3984263084135902, -0.41214704991068,
62
+ -0.42783756578748666, -0.20837937347171134, 0.876505919951498, 0.07259770177184455,
63
+ -0.48364246567281094, 0.8265854747306287,-0.015758658987033422, -0.2873988222474053,
64
+ -0.42783756578748666, 0.044081783789183565, -0.26971376257215296, 0.8615487797670971]
65
+
66
+ Q_SOLUTION_ARRAY_2 = [-0.8571428571428572, 0.3942857142857143, 0.33142857142857146,
67
+ -0.4285714285714286, -0.9028571428571428, -0.03428571428571425,
68
+ 0.28571428571428575, -0.1714285714285714, 0.9428571428571428]
69
+
70
+ Q_SOLUTION_ARRAY_3 = [-0.7724247413634004, -0.026670393594597247, -0.6345460653374136,
71
+ -0.5777485870360393, -0.38541856437557026, 0.7194853024298236,
72
+ -0.26375478973384403, 0.9223563413020934, 0.28229805268947933]
73
+
51
74
  def create_matrix(stype) #:nodoc:
52
75
  m = NMatrix.new([3,3], 0, dtype: :int32, stype: stype, default: 0)
53
76
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: nmatrix
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.1
4
+ version: 0.2.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - John Woods
@@ -10,7 +10,7 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2016-01-18 00:00:00.000000000 Z
13
+ date: 2016-07-25 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: packable
@@ -46,6 +46,20 @@ dependencies:
46
46
  - - "~>"
47
47
  - !ruby/object:Gem::Version
48
48
  version: '1.6'
49
+ - !ruby/object:Gem::Dependency
50
+ name: json
51
+ requirement: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - "~>"
54
+ - !ruby/object:Gem::Version
55
+ version: 2.0.1
56
+ type: :development
57
+ prerelease: false
58
+ version_requirements: !ruby/object:Gem::Requirement
59
+ requirements:
60
+ - - "~>"
61
+ - !ruby/object:Gem::Version
62
+ version: 2.0.1
49
63
  - !ruby/object:Gem::Dependency
50
64
  name: pry
51
65
  requirement: !ruby/object:Gem::Requirement
@@ -163,6 +177,7 @@ files:
163
177
  - ext/nmatrix/math/imax.h
164
178
  - ext/nmatrix/math/laswp.h
165
179
  - ext/nmatrix/math/long_dtype.h
180
+ - ext/nmatrix/math/magnitude.h
166
181
  - ext/nmatrix/math/math.h
167
182
  - ext/nmatrix/math/nrm2.h
168
183
  - ext/nmatrix/math/rot.h
@@ -214,6 +229,7 @@ files:
214
229
  - lib/nmatrix/lapack_core.rb
215
230
  - lib/nmatrix/lapack_plugin.rb
216
231
  - lib/nmatrix/math.rb
232
+ - lib/nmatrix/mkmf.rb
217
233
  - lib/nmatrix/monkeys.rb
218
234
  - lib/nmatrix/nmatrix.rb
219
235
  - lib/nmatrix/rspec.rb
@@ -249,7 +265,7 @@ files:
249
265
  - spec/utm5940.mtx
250
266
  homepage: http://sciruby.com
251
267
  licenses:
252
- - BSD 3-clause
268
+ - BSD-3-Clause
253
269
  metadata: {}
254
270
  post_install_message: |
255
271
  ***********************************************************
@@ -289,7 +305,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
289
305
  version: '0'
290
306
  requirements: []
291
307
  rubyforge_project:
292
- rubygems_version: 2.4.5
308
+ rubygems_version: 2.5.1
293
309
  signing_key:
294
310
  specification_version: 4
295
311
  summary: NMatrix is a linear algebra library for Ruby