File: 00_nmatrix_spec.rb

package info (click to toggle)
ruby-nmatrix 0.1.0~rc3-2
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 4,156 kB
  • sloc: cpp: 8,479; ruby: 4,755; ansic: 3,744; sh: 31; makefile: 4
file content (558 lines) | stat: -rw-r--r-- 18,180 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
# = NMatrix
#
# A linear algebra library for scientific computation in Ruby.
# NMatrix is part of SciRuby.
#
# NMatrix was originally inspired by and derived from NArray, by
# Masahiro Tanaka: http://narray.rubyforge.org
#
# == Copyright Information
#
# SciRuby is Copyright (c) 2010 - 2014, Ruby Science Foundation
# NMatrix is Copyright (c) 2012 - 2014, John Woods and the Ruby Science Foundation
#
# Please see LICENSE.txt for additional copyright notices.
#
# == Contributing
#
# By contributing source code to SciRuby, you agree to be bound by
# our Contributor Agreement:
#
# * https://github.com/SciRuby/sciruby/wiki/Contributor-Agreement
#
# == 00_nmatrix_spec.rb
#
# Basic tests for NMatrix. These should load first, as they're
# essential to NMatrix operation.
#

require File.dirname(__FILE__) + "/spec_helper.rb"

describe NMatrix do
  #after :each do
  #  GC.start
  #end

  it "creates a matrix with the new constructor" do
    n = NMatrix.new([2,2], [0,1,2,3], dtype: :int64)
    expect(n.shape).to eq([2,2])
    expect(n.entries).to eq([0,1,2,3])
    expect(n.dtype).to eq(:int64)
  end

  it "adequately requires information to access a single entry of a dense matrix" do
    n = NMatrix.new(:dense, 4, [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15], :float64)
    expect(n[0,0]).to eq(0)
    expect { n[0] }.to raise_error(ArgumentError)
  end

  it "calculates exact determinants on small square matrices" do
    expect(NMatrix.new(2, [1,2,3,4], stype: :dense, dtype: :int64).det_exact).to eq(-2)
  end

  it "calculates determinants" do
    expect(NMatrix.new(3, [-2,2,3,-1,1,3,2,0,-1], stype: :dense, dtype: :int64).det).to eq(6)
  end

  it "allows casting to Ruby objects" do
    m = NMatrix.new([3,3], [0,0,1,0,2,0,3,4,5], dtype: :int64, stype: :dense)
    n = m.cast(:dense, :object)
    expect(n).to eq(m)
  end

  it "allows casting from Ruby objects" do
    m = NMatrix.new(:dense, [3,3], [0,0,1,0,2,0,3,4,5], :object)
    n = m.cast(:dense, :int64)
    expect(m).to eq(n)
  end

  it "allows stype casting of a dim 2 matrix between dense, sparse, and list (different dtypes)" do
    m = NMatrix.new(:dense, [3,3], [0,0,1,0,2,0,3,4,5], :int64).
      cast(:yale, :int32).
      cast(:dense, :float64).
      cast(:list, :object).
      cast(:dense, :int16).
      cast(:list, :int32).
      cast(:yale, :int64) #.
    #cast(:list, :int32).
    #cast(:dense, :int16)
    #m.should.equal?(original)
    # For some reason this causes some weird garbage collector problems when we uncomment these. The above lines won't
    # work at all in IRB, but work fine when run in a regular Ruby session.
  end

  it "fills dense Ruby object matrix with nil" do
    n = NMatrix.new([4,3], dtype: :object)
    expect(n[0,0]).to eq(nil)
  end

  it "fills dense with individual assignments" do
    n = NMatrix.new([4,3], dtype: :float64)
    n[0,0] = 14.0
    n[0,1] = 9.0
    n[0,2] = 3.0
    n[1,0] = 2.0
    n[1,1] = 11.0
    n[1,2] = 15.0
    n[2,0] = 0.0
    n[2,1] = 12.0
    n[2,2] = 17.0
    n[3,0] = 5.0
    n[3,1] = 2.0
    n[3,2] = 3.0

    expect(n[0,0]).to eq(14.0)
    expect(n[0,1]).to eq(9.0)
    expect(n[0,2]).to eq(3.0)
    expect(n[1,0]).to eq(2.0)
    expect(n[1,1]).to eq(11.0)
    expect(n[1,2]).to eq(15.0)
    expect(n[2,0]).to eq(0.0)
    expect(n[2,1]).to eq(12.0)
    expect(n[2,2]).to eq(17.0)
    expect(n[3,0]).to eq(5.0)
    expect(n[3,1]).to eq(2.0)
    expect(n[3,2]).to eq(3.0)
  end

  it "fills dense with a single mass assignment" do
    n = NMatrix.new([4,3], [14.0, 9.0, 3.0, 2.0, 11.0, 15.0, 0.0, 12.0, 17.0, 5.0, 2.0, 3.0])

    expect(n[0,0]).to eq(14.0)
    expect(n[0,1]).to eq(9.0)
    expect(n[0,2]).to eq(3.0)
    expect(n[1,0]).to eq(2.0)
    expect(n[1,1]).to eq(11.0)
    expect(n[1,2]).to eq(15.0)
    expect(n[2,0]).to eq(0.0)
    expect(n[2,1]).to eq(12.0)
    expect(n[2,2]).to eq(17.0)
    expect(n[3,0]).to eq(5.0)
    expect(n[3,1]).to eq(2.0)
    expect(n[3,2]).to eq(3.0)
  end

  it "fills dense with a single mass assignment, with dtype specified" do
    m = NMatrix.new([4,3], [14.0, 9.0, 3.0, 2.0, 11.0, 15.0, 0.0, 12.0, 17.0, 5.0, 2.0, 3.0], dtype: :float32)

    expect(m[0,0]).to eq(14.0)
    expect(m[0,1]).to eq(9.0)
    expect(m[0,2]).to eq(3.0)
    expect(m[1,0]).to eq(2.0)
    expect(m[1,1]).to eq(11.0)
    expect(m[1,2]).to eq(15.0)
    expect(m[2,0]).to eq(0.0)
    expect(m[2,1]).to eq(12.0)
    expect(m[2,2]).to eq(17.0)
    expect(m[3,0]).to eq(5.0)
    expect(m[3,1]).to eq(2.0)
    expect(m[3,2]).to eq(3.0)
  end

  it "dense handles missing initialization value" do
    n = NMatrix.new(3, dtype: :int8)
    expect(n.stype).to eq(:dense)
    expect(n.dtype).to eq(:int8)

    m = NMatrix.new(4, dtype: :float64)
    expect(m.stype).to eq(:dense)
    expect(m.dtype).to eq(:float64)
  end

  [:dense, :list, :yale].each do |storage_type|
    context storage_type do
    it "can be duplicated" do
        n = NMatrix.new([2,3], 1.1, stype: storage_type, dtype: :float64)
        expect(n.stype).to eq(storage_type)

        n[0,0] = 0.0
        n[0,1] = 0.1
        n[1,0] = 1.0

        m = n.dup
        expect(m.shape).to eq(n.shape)
        expect(m.dim).to eq(n.dim)
        expect(m.object_id).not_to eq(n.object_id)
        expect(m.stype).to eq(storage_type)
        expect(m[0,0]).to eq(n[0,0])
        m[0,0] = 3.0
        expect(m[0,0]).not_to eq(n[0,0])
      end

      it "enforces shape boundaries" do
        expect { NMatrix.new([1,10], 0, dtype: :int8, stype: storage_type, default: 0)[1,0]  }.to raise_error(RangeError)
        expect { NMatrix.new([1,10], 0, dtype: :int8, stype: storage_type, default: 0)[0,10] }.to raise_error(RangeError)
      end

      it "sets and gets" do
        n = NMatrix.new(2, 0, stype: storage_type, dtype: :int8)
        n[0,1] = 1
        expect(n[0,0]).to eq(0)
        expect(n[1,0]).to eq(0)
        expect(n[0,1]).to eq(1)
        expect(n[1,1]).to eq(0)
      end

      it "sets and gets references" do
        n = NMatrix.new(2, stype: storage_type, dtype: :int8, default: 0)
        expect(n[0,1] = 1).to eq(1)
        expect(n[0,1]).to eq(1)
      end

      # Tests Ruby object versus any C dtype (in this case we use :int64)
      [:object, :int64].each do |dtype|
        c = dtype == :object ? "Ruby object" : "non-Ruby object"
        context c do
          it "allows iteration of matrices" do
            n = nil
            if storage_type == :dense
              n = NMatrix.new(:dense, [3,3], [1,2,3,4,5,6,7,8,9], dtype)
            else
              n = NMatrix.new([3,4], 0, stype: storage_type, dtype: dtype)
              n[0,0] = 1
              n[0,1] = 2
              n[2,3] = 4
              n[2,0] = 3
            end

            ary = []
            n.each do |x|
              ary << x
            end

            if storage_type == :dense
              expect(ary).to eq([1,2,3,4,5,6,7,8,9])
            else
              expect(ary).to eq([1,2,0,0,0,0,0,0,3,0,0,4])
            end
          end

          it "allows storage-based iteration of matrices" do
            STDERR.puts storage_type.inspect
            STDERR.puts dtype.inspect
            n = NMatrix.new([3,3], 0, stype: storage_type, dtype: dtype)
            n[0,0] = 1
            n[0,1] = 2
            n[2,0] = 5 if storage_type == :yale
            n[2,1] = 4
            n[2,2] = 3

            values = []
            is = []
            js = []

            n.each_stored_with_indices do |v,i,j|
              values << v
              is << i
              js << j
            end

            if storage_type == :yale
              expect(is).to     eq([0,1,2,0,2,2])
              expect(js).to     eq([0,1,2,1,0,1])
              expect(values).to eq([1,0,3,2,5,4])
            elsif storage_type == :list
              expect(values).to eq([1,2,4,3])
              expect(is).to     eq([0,0,2,2])
              expect(js).to     eq([0,1,1,2])
            elsif storage_type == :dense
              expect(values).to eq([1,2,0,0,0,0,0,4,3])
              expect(is).to     eq([0,0,0,1,1,1,2,2,2])
              expect(js).to     eq([0,1,2,0,1,2,0,1,2])
            end
          end
        end
      end
    end

    # dense and list, not yale
    context "(storage: #{storage_type})" do
      it "gets default value" do
        expect(NMatrix.new(3, 0, stype: storage_type)[1,1]).to eq(0)
        expect(NMatrix.new(3, 0.1, stype: storage_type)[1,1]).to eq(0.1)
        expect(NMatrix.new(3, 1, stype: storage_type)[1,1]).to eq(1)

      end
      it "returns shape and dim" do
        expect(NMatrix.new([3,2,8], 0, stype: storage_type).shape).to eq([3,2,8])
        expect(NMatrix.new([3,2,8], 0, stype: storage_type).dim).to eq(3)
      end

      it "returns number of rows and columns" do
        expect(NMatrix.new([7, 4], 3, stype: storage_type).rows).to eq(7)
        expect(NMatrix.new([7, 4], 3, stype: storage_type).cols).to eq(4)
      end
    end unless storage_type == :yale
  end


  it "handles dense construction" do
    expect(NMatrix.new(3,0)[1,1]).to eq(0)
    expect(lambda { NMatrix.new(3,dtype: :int8)[1,1] }).to_not raise_error
  end

  it "converts from list to yale properly" do
    m = NMatrix.new(3, 0, stype: :list)
    m[0,2] = 333
    m[2,2] = 777
    n = m.cast(:yale, :int32)
    #puts n.capacity
    #n.extend NMatrix::YaleFunctions
    #puts n.yale_ija.inspect
    #puts n.yale_a.inspect

    expect(n[0,0]).to eq(0)
    expect(n[0,1]).to eq(0)
    expect(n[0,2]).to eq(333)
    expect(n[1,0]).to eq(0)
    expect(n[1,1]).to eq(0)
    expect(n[1,2]).to eq(0)
    expect(n[2,0]).to eq(0)
    expect(n[2,1]).to eq(0)
    expect(n[2,2]).to eq(777)
  end

  it "should return an enumerator when each is called without a block" do
    a = NMatrix.new(2, 1)
    b = NMatrix.new(2, [-1,0,1,0])
    enums = [a.each, b.each]

    begin
      atans = []
      atans << Math.atan2(*enums.map(&:next)) while true
    rescue StopIteration
    end
  end

  context "dense" do
    it "should return the matrix being iterated over when each is called with a block" do
      a = NMatrix.new(2, 1)
      val = (a.each { })
      expect(val).to eq(a)
    end

    it "should return the matrix being iterated over when each_stored_with_indices is called with a block" do
      a = NMatrix.new(2,1)
      val = (a.each_stored_with_indices { })
      expect(val).to eq(a)
    end
  end

  [:list, :yale].each do |storage_type|
    context storage_type do
      it "should return the matrix being iterated over when each_stored_with_indices is called with a block" do
        n = NMatrix.new([2,3], 1.1, stype: storage_type, dtype: :float64, default: 0)
        val = (n.each_stored_with_indices { })
        expect(val).to eq(n)
      end

      it "should return an enumerator when each_stored_with_indices is called without a block" do
        n = NMatrix.new([2,3], 1.1, stype: storage_type, dtype: :float64, default: 0)
        val = n.each_stored_with_indices
        expect(val).to be_a Enumerator
      end
    end
  end

  it "should iterate through element 256 without a segfault" do
    t = NVector.random(256)
    t.each { |x| x + 0 }
  end
end


describe 'NMatrix' do
  context "#upper_triangle" do
    it "should create a copy with the lower corner set to zero" do
      n = NMatrix.seq(4)+1
      expect(n.upper_triangle).to eq(NMatrix.new(4, [1,2,3,4,0,6,7,8,0,0,11,12,0,0,0,16]))
      expect(n.upper_triangle(2)).to eq(NMatrix.new(4, [1,2,3,4,5,6,7,8,9,10,11,12,0,14,15,16]))
    end
  end

  context "#lower_triangle" do
    it "should create a copy with the lower corner set to zero" do
      n = NMatrix.seq(4)+1
      expect(n.lower_triangle).to eq(NMatrix.new(4, [1,0,0,0,5,6,0,0,9,10,11,0,13,14,15,16]))
      expect(n.lower_triangle(2)).to eq(NMatrix.new(4, [1,2,3,0,5,6,7,8,9,10,11,12,13,14,15,16]))
    end
  end

  context "#upper_triangle!" do
    it "should create a copy with the lower corner set to zero" do
      n = NMatrix.seq(4)+1
      expect(n.upper_triangle!).to eq(NMatrix.new(4, [1,2,3,4,0,6,7,8,0,0,11,12,0,0,0,16]))
      n = NMatrix.seq(4)+1
      expect(n.upper_triangle!(2)).to eq(NMatrix.new(4, [1,2,3,4,5,6,7,8,9,10,11,12,0,14,15,16]))
    end
  end

  context "#lower_triangle!" do
    it "should create a copy with the lower corner set to zero" do
      n = NMatrix.seq(4)+1
      expect(n.lower_triangle!).to eq(NMatrix.new(4, [1,0,0,0,5,6,0,0,9,10,11,0,13,14,15,16]))
      n = NMatrix.seq(4)+1
      expect(n.lower_triangle!(2)).to eq(NMatrix.new(4, [1,2,3,0,5,6,7,8,9,10,11,12,13,14,15,16]))
    end
  end

  context "#reshape" do
    it "should change the shape of a matrix without the contents changing" do
      n = NMatrix.seq(4)+1
      expect(n.reshape([8,2]).to_flat_array).to eq(n.to_flat_array)
    end

    it "should permit a change of dimensionality" do
      n = NMatrix.seq(4)+1
      expect(n.reshape([8,1,2]).to_flat_array).to eq(n.to_flat_array)
    end

    it "should prevent a resize" do
      n = NMatrix.seq(4)+1
      expect { n.reshape([5,2]) }.to raise_error(ArgumentError)
    end

    it "should do the reshape operation in place" do
      n = NMatrix.seq(4)+1
      expect(n.reshape!([8,2]).eql?(n)).to eq(true) # because n itself changes
    end

    it "reshape and reshape! must produce same result" do
      n = NMatrix.seq(4)+1
      a = NMatrix.seq(4)+1
      expect(n.reshape!([8,2])==a.reshape(8,2)).to eq(true) # because n itself changes
    end

    it "should prevent a resize in place" do
      n = NMatrix.seq(4)+1
      expect { n.reshape([5,2]) }.to raise_error(ArgumentError)
    end
  end

  context "#transpose" do
    [:dense, :list, :yale].each do |stype|
      context(stype) do
        it "should transpose a #{stype} matrix (2-dimensional)" do
          n = NMatrix.seq(4, stype: stype)
          expect(n.transpose.to_a.flatten).to eq([0,4,8,12,1,5,9,13,2,6,10,14,3,7,11,15])
        end
      end
    end

    [:dense, :list].each do |stype|
      context(stype) do
        it "should transpose a #{stype} matrix (3-dimensional)" do
          n = NMatrix.new([4,4,1], [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15], stype: stype)
          expect(n.transpose([2,1,0]).to_flat_array).to eq([0,4,8,12,1,5,9,13,2,6,10,14,3,7,11,15])
          expect(n.transpose([1,0,2]).to_flat_array).to eq([0,4,8,12,1,5,9,13,2,6,10,14,3,7,11,15])
          expect(n.transpose([0,2,1]).to_flat_array).to eq(n.to_flat_array) # for dense, make this reshape!
        end
      end

      it "should just copy a 1-dimensional #{stype} matrix" do
        n = NMatrix.new([3], [1,2,3], stype: stype)
        expect(n.transpose).to eq n
        expect(n.transpose).not_to be n
      end
    end
  end

  context "#dot_product" do
    [:dense].each do |stype| # list storage transpose not yet implemented
      context(stype) do # yale support only 2-dim matrix
        it "should work like vector product on a #{stype} (1-dimensional)" do
          m = NMatrix.new([3], [1,2,3], stype: stype)
          expect(m.dot(m)).to eq (NMatrix.new([1],[14]))
        end
      end
    end
  end

  context "#==" do
    [:dense, :list, :yale].each do |left|
      [:dense, :list, :yale].each do |right|
        next if left == right
        context ("#{left}?#{right}") do
          it "should compare two matrices of differing stypes" do
            n = NMatrix.new([3,4], [0,0,1,2,0,0,3,4,0,0,0,0,5,6,7,0], stype: left)
            m = NMatrix.new([3,4], [0,0,1,2,0,0,3,4,0,0,0,0,5,6,7,0], stype: right)
            expect(n).to eq(m)
          end
        end
      end
    end
  end

  context "#concat" do
    it "should default to horizontal concatenation" do
      n = NMatrix.new([1,3], [1,2,3])
      expect(n.concat(n)).to eq(NMatrix.new([1,6], [1,2,3,1,2,3]))
    end

    it "should permit vertical concatenation" do
      n = NMatrix.new([1,3], [1,2,3])
      expect(n.vconcat(n)).to eq(NMatrix.new([2,3], [1,2,3]))
    end

    it "should permit depth concatenation on tensors" do
      n = NMatrix.new([1,3,1], [1,2,3])
      expect(n.dconcat(n)).to eq(NMatrix.new([1,3,2], [1,1,2,2,3,3]))
    end
  end

  context "#[]" do
    it "should return values based on indices" do
      n = NMatrix.new([2,5], [1,2,3,4,5,6,7,8,9,0])
      expect(n[1,0]).to eq 6
      expect(n[1,0..3]).to eq NMatrix.new([1,4],[6,7,8,9])
    end

    it "should work for negative indices" do
      n = NMatrix.new([1,5], [1,2,3,4,5])
      expect(n[-1]).to eq(5)
      expect(n[0,0..-2]).to eq(NMatrix.new([1,4],[1,2,3,4]))
    end
  end

  context "#complex_conjugate!" do
    [:dense, :yale, :list].each do |stype|
      context(stype) do
        it "should work in-place for complex dtypes" do
          pending("not yet implemented for list stype") if stype == :list
          n = NMatrix.new([2,3], [Complex(2,3)], stype: stype, dtype: :complex128)
          n.complex_conjugate!
          expect(n).to eq(NMatrix.new([2,3], [Complex(2,-3)], stype: stype, dtype: :complex128))
        end

        [:object, :int64].each do |dtype|
          it "should work in-place for non-complex dtypes" do
            pending("not yet implemented for list stype") if stype == :list
            n = NMatrix.new([2,3], 1, stype: stype, dtype: dtype)
            n.complex_conjugate!
            expect(n).to eq(NMatrix.new([2,3], [1], stype: stype, dtype: dtype))
          end
        end
      end
    end
  end

  context "#complex_conjugate" do
    [:dense, :yale, :list].each do |stype|
      context(stype) do
        it "should work out-of-place for complex dtypes" do
          pending("not yet implemented for list stype") if stype == :list
          n = NMatrix.new([2,3], [Complex(2,3)], stype: stype, dtype: :complex128)
          expect(n.complex_conjugate).to eq(NMatrix.new([2,3], [Complex(2,-3)], stype: stype, dtype: :complex128))
        end

        [:object, :int64].each do |dtype|
          it "should work out-of-place for non-complex dtypes" do
            pending("not yet implemented for list stype") if stype == :list
            n = NMatrix.new([2,3], 1, stype: stype, dtype: dtype)
            expect(n.complex_conjugate).to eq(NMatrix.new([2,3], [1], stype: stype, dtype: dtype))
          end
        end
      end
    end
  end
end