spec/shortcuts_spec.rb in nmatrix-0.0.5 vs spec/shortcuts_spec.rb in nmatrix-0.0.6

- old
+ new

@@ -49,10 +49,25 @@ identity3 = NMatrix.new([3, 3], [1, 0, 0, 0, 1, 0, 0, 0, 1]) m.should.eql? identity3 end + it "diag() creates a matrix with pre-supplied diagonal" do + arr = [1,2,3,4] + m = NMatrix.diag(arr) + m.is_a?(NMatrix).should be_true + end + + it "diagonals() contains the seeded values on the diagonal" do + arr = [1,2,3,4] + m = NMatrix.diagonals(arr) + m[0,0].should eq(arr[0]) + m[1,1].should eq(arr[1]) + m[2,2].should eq(arr[2]) + m[3,3].should eq(arr[3]) + end + it "random() creates a matrix of random numbers" do m = NMatrix.random(2) m.stype.should == :dense m.dtype.should == :float64 @@ -199,16 +214,11 @@ v.stype.should == :dense end it "seq() creates a vector of integers, sequentially" do v = NVector.seq(7) - i = 0 - - v.each do |elem| - elem.should == i - i += 1 - end + v.should == NVector.new(7, [0, 1, 2, 3, 4, 5, 6], :int64) end it "seq() only accepts integers as dimension" do expect { NVector.seq(3) }.to_not raise_error @@ -216,55 +226,35 @@ expect { NVector.seq(:wtf) }.to raise_error end it "indgen() creates a vector of integers as well as seq()" do v = NVector.indgen(7) - i = 0 - - v.each do |elem| - elem.should == i - i += 1 - end + v.should == NVector.new(7, [0, 1, 2, 3, 4, 5, 6], :int64) end it "findgen creates a vector of floats, sequentially" do v = NVector.findgen(2) - - 2.times do |i| - (v[i]/10).should be_within(Float::EPSILON).of(i.to_f/10) - end + v.should == NVector.new(2, [0.0, 1.0], :float32) end it "bindgen() creates a vector of bytes, sequentially" do - v = NVector.bindgen(7) - i = 0 - - v.each do |elem| - elem.should == i - i += 1 - end + v = NVector.bindgen(4) + v.should == NVector.new(4, [0, 1, 2, 3], :byte) end it "cindgen() creates a vector of complexes, sequentially" do v = NVector.cindgen(2) - value = 0 - - 2.times do |i| - v[i].real.should be_within(Float::EPSILON).of(value) - v[i].imag.should be_within(Float::EPSILON).of(0.0) - value += 1 - end + v.should == NVector.new(2, [Complex(0.0, 0.0), Complex(1.0, 0.0)], :complex64) end - it "linspace() creates a vector with n values equally spaced between a and b" do v = NVector.linspace(0, 2, 5) - i = 0 + v.should == NVector.new(5, [0, 0.5, 1.0, 1.5, 2.0], :float64) + end - v.each do |elem| - elem.should == i * 0.5 - i += 1 - end + it "logspace() creates a vector with n values logarithmically spaced between decades 10^a and 10^b" do + v = NVector.logspace(0, 3, 4) + v.should == NVector.new(4, [1, 10, 100, 1000], :float64) end end describe "Inline constructor" do