spec/00_nmatrix_spec.rb in nmatrix-atlas-0.2.3 vs spec/00_nmatrix_spec.rb in nmatrix-atlas-0.2.4

- old
+ new

@@ -39,14 +39,77 @@ 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 + it "calculates exact determinants on small dense matrices" do expect(NMatrix.new(2, [1,2,3,4], stype: :dense, dtype: :int64).det_exact).to eq(-2) + expect(NMatrix.new(3, [1,2,3,0,5,6,7,8,0], stype: :dense, dtype: :int64) + .det_exact).to eq(-69) end + it "calculates exact determinants on small yale square matrices" do + expect(NMatrix.new(2, [1,2,3,4], stype: :yale, dtype: :int64).det_exact).to eq(-2) + expect(NMatrix.new(3, [1,2,3,0,5,6,7,8,0], stype: :yale, dtype: :int64) + .det_exact).to eq(-69) + end + + it "calculates exact determinants on small list square matrices" do + expect(NMatrix.new(2, [1,2,3,4], stype: :list, dtype: :int64).det_exact).to eq(-2) + expect(NMatrix.new(3, [1,2,3,0,5,6,7,8,0], stype: :list, dtype: :int64) + .det_exact).to eq(-69) + end + + it "calculates inverse exact determinants on small dense matrices" do + pending("not yet implemented for NMatrix-JRuby") if jruby? + a = NMatrix.new(3, [1,2,3,0,1,4,5,6,0], stype: :dense, dtype: :int64) + inversed = a.method(:__inverse_exact__).call(a.clone, 3, 3) + b = NMatrix.new(3, [-24,18,5,20,-15,-4,-5,4,1], stype: :dense, dtype: :int64) + expect(inversed).to eq(b) + + c = NMatrix.new(3, [1,0,3,0,0,1,0,6,0], stype: :dense, dtype: :int64) + inversed = c.method(:__inverse_exact__).call(c.clone, 3, 3) + d = NMatrix.new(3, [1,-3,0,0,0,0,0,1,0], stype: :dense, dtype: :int64) + expect(inversed).to eq(d) + + e = NMatrix.new(2, [3,1,2,1], stype: :dense, dtype: :int64) + inversed = e.method(:__inverse_exact__).call(e.clone, 2, 2) + f = NMatrix.new(2, [1,-1,-2,3], stype: :dense, dtype: :int64) + expect(inversed).to eq(f) + end + + it "calculates inverse exact determinants on small yale matrices" do + pending("not yet implemented for NMatrix-JRuby") if jruby? + a = NMatrix.new(3, [1,2,3,0,1,4,5,6,0], stype: :yale, dtype: :int64) + inversed = a.method(:__inverse_exact__).call(a.clone, 3, 3) + b = NMatrix.new(3, [-24,18,5,20,-15,-4,-5,4,1], stype: :yale, dtype: :int64) + expect(inversed).to eq(b) + + c = NMatrix.new(3, [1,0,3,0,0,1,0,6,0], stype: :yale, dtype: :int64) + inversed = c.method(:__inverse_exact__).call(c.clone, 3, 3) + d = NMatrix.new(3, [1,-3,0,0,0,0,0,1,0], stype: :yale, dtype: :int64) + expect(inversed).to eq(d) + + e = NMatrix.new(2, [3,1,2,1], stype: :yale, dtype: :int64) + inversed = e.method(:__inverse_exact__).call(e.clone, 2, 2) + f = NMatrix.new(2, [1,-1,-2,3], stype: :yale, dtype: :int64) + expect(inversed).to eq(f) + end + + it "calculates inverse exact determinants on small list matrices" do + pending("not yet implemented for NMatrix-JRuby") if jruby? + a = NMatrix.new(3, [1,2,3,0,1,4,5,6,0], stype: :list, dtype: :int64) + inversed = a.method(:__inverse_exact__).call(a.clone, 3, 3) + b = NMatrix.new(3, [-24,18,5,20,-15,-4,-5,4,1], stype: :list, dtype: :int64) + expect(inversed).to eq(b) + + c = NMatrix.new(2, [3,1,2,1], stype: :list, dtype: :int64) + inversed = c.method(:__inverse_exact__).call(c.clone, 2, 2) + d = NMatrix.new(2, [1,-1,-2,3], stype: :list, dtype: :int64) + expect(inversed).to eq(d) + 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 @@ -54,10 +117,11 @@ n = m.cast(:dense, :object) expect(n).to eq(m) end it "allows casting from Ruby objects" do + pending("not yet implemented for NMatrix-JRuby") if jruby? 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 @@ -76,10 +140,11 @@ # 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) + pending("not yet implemented for object dtype for NMatrix-JRuby") if jruby? expect(n[0,0]).to eq(nil) end it "fills dense with individual assignments" do n = NMatrix.new([4,3], dtype: :float64) @@ -144,10 +209,11 @@ expect(m[3,2]).to eq(3.0) end it "dense handles missing initialization value" do n = NMatrix.new(3, dtype: :int8) + pending("not yet implemented for int dtype for NMatrix-JRuby") if jruby? expect(n.stype).to eq(:dense) expect(n.dtype).to eq(:int8) m = NMatrix.new(4, dtype: :float64) expect(m.stype).to eq(:dense) @@ -156,10 +222,12 @@ [: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) + # FIXME + pending("not yet implemented for sparse matrices for NMatrix-JRuby") if jruby? #and storage_type != :dense expect(n.stype).to eq(storage_type) n[0,0] = 0.0 n[0,1] = 0.1 n[1,0] = 1.0 @@ -221,10 +289,11 @@ 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 + pending("not yet implemented for sparse matrices for NMatrix-JRuby") if jruby? and storage_type != :dense 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 @@ -261,10 +330,11 @@ end # dense and list, not yale context "(storage: #{storage_type})" do it "gets default value" do + pending("not yet implemented for sparse matrices for NMatrix-JRuby") if jruby? 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 @@ -319,31 +389,37 @@ end end context "dense" do it "should return the matrix being iterated over when each is called with a block" do + # FIXME + pending("not yet implemented for NMatrix-JRuby") if jruby? 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 + # FIXME + pending("not yet implemented for NMatrix-JRuby") if jruby? 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 + pending("not yet implemented for Complex dtype for NMatrix-JRuby") if jruby? 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 + pending("not yet implemented for Complex dtype for NMatrix-JRuby") if jruby? 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 @@ -403,15 +479,19 @@ end end context "#reshape" do it "should change the shape of a matrix without the contents changing" do + # FIXME + pending("not yet implemented for NMatrix-JRuby") if jruby? 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 + # FIXME + pending("not yet implemented for NMatrix-JRuby") if jruby? 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 @@ -423,17 +503,21 @@ n = NMatrix.seq(4)+1 expect(n.reshape!([8,2]).eql?(n)).to eq(true) # because n itself changes end it "should do the reshape operation in place, changing dimension" do + # FIXME + pending("not yet implemented for NMatrix-JRuby") if jruby? n = NMatrix.seq(4) a = n.reshape!([4,2,2]) expect(n).to eq(NMatrix.seq([4,2,2])) expect(a).to eq(NMatrix.seq([4,2,2])) end it "reshape and reshape! must produce same result" do + # FIXME + pending("not yet implemented for NMatrix-JRuby") if jruby? 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 @@ -479,10 +563,12 @@ 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 + # FIXME + pending("not yet implemented for NMatrix-JRuby") if jruby? m = NMatrix.new([3], [1,2,3], stype: stype) expect(m.dot(m)).to eq (NMatrix.new([1],[14])) end end end @@ -536,30 +622,36 @@ 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 + # FIXME + pending("not yet implemented for NMatrix-JRuby") if jruby? 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 it "should work on matrices with different size along concat dim" do n = N[[1, 2, 3], [4, 5, 6]] m = N[[7], [8]] + # FIXME + pending("not yet implemented for NMatrix-JRuby") if jruby? expect(n.hconcat(m)).to eq N[[1, 2, 3, 7], [4, 5, 6, 8]] expect(m.hconcat(n)).to eq N[[7, 1, 2, 3], [8, 4, 5, 6]] end it "should work on matrices with different size along concat dim" do n = N[[1, 2, 3], [4, 5, 6]] m = N[[7, 8, 9]] + # FIXME + pending("not yet implemented for NMatrix-JRuby") if jruby? expect(n.vconcat(m)).to eq N[[1, 2, 3], [4, 5, 6], [7, 8, 9]] expect(m.vconcat(n)).to eq N[[7, 8, 9], [1, 2, 3], [4, 5, 6]] end end @@ -580,18 +672,20 @@ 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 + pending("not yet implemented for Complex dtype for NMatrix-JRuby") if jruby? 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 + pending("not yet implemented for Complex dtype for NMatrix-JRuby") if jruby? 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 @@ -602,17 +696,19 @@ 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 + pending("not yet implemented for Complex dtype for NMatrix-JRuby") if jruby? 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 + pending("not yet implemented for Complex dtype for NMatrix-JRuby") if jruby? 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 @@ -675,11 +771,11 @@ end end context "#diagonal" do ALL_DTYPES.each do |dtype| - before do + before do @square_matrix = NMatrix.new([3,3], [ 23,11,23, 44, 2, 0, 33, 0, 32 ], dtype: dtype @@ -721,15 +817,18 @@ expect{@sample_matrix.repeat(1, 0)}.to raise_error(ArgumentError) expect{@sample_matrix.repeat(-2, 0)}.to raise_error(ArgumentError) end it "returns repeated matrix" do + pending("Not yet implemented for NMatrix JRuby") if jruby? expect(@sample_matrix.repeat(2, 0)).to eq(NMatrix.new([4, 2], [1, 2, 3, 4, 1, 2, 3, 4])) expect(@sample_matrix.repeat(2, 1)).to eq(NMatrix.new([2, 4], [1, 2, 1, 2, 3, 4, 3, 4])) end it "preserves dtype" do + # FIXME + pending("not yet implemented for NMatrix-JRuby") if jruby? expect(@sample_matrix.repeat(2, 0).dtype).to eq(@sample_matrix.dtype) expect(@sample_matrix.repeat(2, 1).dtype).to eq(@sample_matrix.dtype) end end @@ -740,45 +839,53 @@ @two_dim_array = [[4], [5]] @expected_result = [NMatrix.new([2, 3], [1, 2, 3, 1, 2, 3]), NMatrix.new([2, 3], [4, 4, 4, 5, 5, 5])] @expected_for_ij = [NMatrix.new([3, 2], [1, 1, 2, 2, 3, 3]), NMatrix.new([3, 2], [4, 5, 4, 5, 4, 5])] @expected_for_sparse = [NMatrix.new([1, 3], [1, 2, 3]), NMatrix.new([2, 1], [4, 5])] @expected_for_sparse_ij = [NMatrix.new([3, 1], [1, 2, 3]), NMatrix.new([1, 2], [4, 5])] + # FIXME @expected_3dim = [NMatrix.new([1, 3, 1], [1, 2, 3]).repeat(2, 0).repeat(2, 2), NMatrix.new([2, 1, 1], [4, 5]).repeat(3, 1).repeat(2, 2), - NMatrix.new([1, 1, 2], [6, 7]).repeat(2, 0).repeat(3, 1)] + NMatrix.new([1, 1, 2], [6, 7]).repeat(2, 0).repeat(3, 1)] unless jruby? @expected_3dim_sparse_ij = [NMatrix.new([3, 1, 1], [1, 2, 3]), NMatrix.new([1, 2, 1], [4, 5]), NMatrix.new([1, 1, 2], [6, 7])] end it "checks arrays count" do + pending("Not yet implemented for NMatrix JRuby") if jruby? expect{NMatrix.meshgrid([@x])}.to raise_error(ArgumentError) expect{NMatrix.meshgrid([])}.to raise_error(ArgumentError) end it "flattens input arrays before use" do + pending("Not yet implemented for NMatrix JRuby") if jruby? expect(NMatrix.meshgrid([@two_dim, @two_dim_array])).to eq(NMatrix.meshgrid([@two_dim.to_flat_array, @two_dim_array.flatten])) end it "returns new NMatrixes" do + pending("Not yet implemented for NMatrix JRuby") if jruby? expect(NMatrix.meshgrid([@x, @y])).to eq(@expected_result) end it "has option :sparse" do + pending("Not yet implemented for NMatrix JRuby") if jruby? expect(NMatrix.meshgrid([@x, @y], sparse: true)).to eq(@expected_for_sparse) end it "has option :indexing" do + pending("Not yet implemented for NMatrix JRuby") if jruby? expect(NMatrix.meshgrid([@x, @y], indexing: :ij)).to eq(@expected_for_ij) expect(NMatrix.meshgrid([@x, @y], indexing: :xy)).to eq(@expected_result) expect{NMatrix.meshgrid([@x, @y], indexing: :not_ij_not_xy)}.to raise_error(ArgumentError) end it "works well with both options set" do + pending("Not yet implemented for NMatrix JRuby") if jruby? expect(NMatrix.meshgrid([@x, @y], sparse: true, indexing: :ij)).to eq(@expected_for_sparse_ij) end it "is able to take more than two arrays as arguments and works well with options" do + pending("Not yet implemented for NMatrix JRuby") if jruby? expect(NMatrix.meshgrid([@x, @y, @z])).to eq(@expected_3dim) expect(NMatrix.meshgrid([@x, @y, @z], sparse: true, indexing: :ij)).to eq(@expected_3dim_sparse_ij) end end end