1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

Guard ruby/spec with spec/mspec/tool/wrap_with_guard.rb

This commit is contained in:
Hiroshi SHIBATA 2021-05-26 20:36:23 +09:00
parent 350bc29107
commit 835a495608
Notes: git 2021-05-27 14:42:36 +09:00
164 changed files with 4474 additions and 3982 deletions

View file

@ -1,6 +1,9 @@
require_relative '../../spec_helper' require_relative '../../spec_helper'
require_relative 'shared/identity'
describe "Matrix.I" do ruby_version_is ""..."3.1" do
it_behaves_like :matrix_identity, :I require_relative 'shared/identity'
describe "Matrix.I" do
it_behaves_like :matrix_identity, :I
end
end end

View file

@ -1,36 +1,39 @@
require_relative '../../spec_helper' require_relative '../../spec_helper'
require 'matrix'
ruby_version_is "2.6" do ruby_version_is ""..."3.1" do
describe "Matrix#antisymmetric?" do require 'matrix'
it "returns true for an antisymmetric Matrix" do
Matrix[[0, -2, Complex(1, 3)], [2, 0, 5], [-Complex(1, 3), -5, 0]].antisymmetric?.should be_true
end
it "returns true for a 0x0 empty matrix" do ruby_version_is "2.6" do
Matrix.empty.antisymmetric?.should be_true describe "Matrix#antisymmetric?" do
end it "returns true for an antisymmetric Matrix" do
Matrix[[0, -2, Complex(1, 3)], [2, 0, 5], [-Complex(1, 3), -5, 0]].antisymmetric?.should be_true
it "returns false for non-antisymmetric matrices" do
[
Matrix[[1, 2, 3], [4, 5, 6], [7, 8, 9]],
Matrix[[1, -2, 3], [2, 0, 6], [-3, -6, 0]], # wrong diagonal element
Matrix[[0, 2, -3], [2, 0, 6], [-3, 6, 0]] # only signs wrong
].each do |matrix|
matrix.antisymmetric?.should be_false
end end
end
it "raises an error for rectangular matrices" do it "returns true for a 0x0 empty matrix" do
[ Matrix.empty.antisymmetric?.should be_true
Matrix[[0], [0]], end
Matrix[[0, 0]],
Matrix.empty(0, 2), it "returns false for non-antisymmetric matrices" do
Matrix.empty(2, 0), [
].each do |rectangular_matrix| Matrix[[1, 2, 3], [4, 5, 6], [7, 8, 9]],
-> { Matrix[[1, -2, 3], [2, 0, 6], [-3, -6, 0]], # wrong diagonal element
rectangular_matrix.antisymmetric? Matrix[[0, 2, -3], [2, 0, 6], [-3, 6, 0]] # only signs wrong
}.should raise_error(Matrix::ErrDimensionMismatch) ].each do |matrix|
matrix.antisymmetric?.should be_false
end
end
it "raises an error for rectangular matrices" do
[
Matrix[[0], [0]],
Matrix[[0, 0]],
Matrix.empty(0, 2),
Matrix.empty(2, 0),
].each do |rectangular_matrix|
-> {
rectangular_matrix.antisymmetric?
}.should raise_error(Matrix::ErrDimensionMismatch)
end
end end
end end
end end

View file

@ -1,73 +1,76 @@
require_relative '../../spec_helper' require_relative '../../spec_helper'
require_relative 'fixtures/classes'
require 'matrix'
describe "Matrix.build" do ruby_version_is ""..."3.1" do
require_relative 'fixtures/classes'
require 'matrix'
it "returns a Matrix object of the given size" do describe "Matrix.build" do
m = Matrix.build(3, 4){1}
m.should be_an_instance_of(Matrix) it "returns a Matrix object of the given size" do
m.row_size.should == 3 m = Matrix.build(3, 4){1}
m.column_size.should == 4 m.should be_an_instance_of(Matrix)
m.row_size.should == 3
m.column_size.should == 4
end
it "builds the Matrix using the given block" do
Matrix.build(2, 3){|col, row| 10*col - row}.should ==
Matrix[[0, -1, -2], [10, 9, 8]]
end
it "iterates through the first row, then the second, ..." do
acc = []
Matrix.build(2, 3){|*args| acc << args}
acc.should == [[0, 0], [0, 1], [0, 2], [1, 0], [1, 1], [1, 2]]
end
it "returns an Enumerator is no block is given" do
enum = Matrix.build(2, 1)
enum.should be_an_instance_of(Enumerator)
enum.each{1}.should == Matrix[[1], [1]]
end
it "requires integers as parameters" do
-> { Matrix.build("1", "2"){1} }.should raise_error(TypeError)
-> { Matrix.build(nil, nil){1} }.should raise_error(TypeError)
-> { Matrix.build(1..2){1} }.should raise_error(TypeError)
end
it "requires non-negative integers" do
-> { Matrix.build(-1, 1){1} }.should raise_error(ArgumentError)
-> { Matrix.build(+1,-1){1} }.should raise_error(ArgumentError)
end
it "returns empty Matrix if one argument is zero" do
m = Matrix.build(0, 3){
raise "Should not yield"
}
m.should be_empty
m.column_size.should == 3
m = Matrix.build(3, 0){
raise "Should not yield"
}
m.should be_empty
m.row_size.should == 3
end
it "tries to calls :to_int on arguments" do
int = mock('int')
int.should_receive(:to_int).twice.and_return(2)
Matrix.build(int, int){ 1 }.should == Matrix[ [1,1], [1,1] ]
end
it "builds an nxn Matrix when given only one argument" do
m = Matrix.build(3){1}
m.row_size.should == 3
m.column_size.should == 3
end
end end
it "builds the Matrix using the given block" do describe "for a subclass of Matrix" do
Matrix.build(2, 3){|col, row| 10*col - row}.should == it "returns an instance of that subclass" do
Matrix[[0, -1, -2], [10, 9, 8]] MatrixSub.build(3){1}.should be_an_instance_of(MatrixSub)
end end
it "iterates through the first row, then the second, ..." do
acc = []
Matrix.build(2, 3){|*args| acc << args}
acc.should == [[0, 0], [0, 1], [0, 2], [1, 0], [1, 1], [1, 2]]
end
it "returns an Enumerator is no block is given" do
enum = Matrix.build(2, 1)
enum.should be_an_instance_of(Enumerator)
enum.each{1}.should == Matrix[[1], [1]]
end
it "requires integers as parameters" do
-> { Matrix.build("1", "2"){1} }.should raise_error(TypeError)
-> { Matrix.build(nil, nil){1} }.should raise_error(TypeError)
-> { Matrix.build(1..2){1} }.should raise_error(TypeError)
end
it "requires non-negative integers" do
-> { Matrix.build(-1, 1){1} }.should raise_error(ArgumentError)
-> { Matrix.build(+1,-1){1} }.should raise_error(ArgumentError)
end
it "returns empty Matrix if one argument is zero" do
m = Matrix.build(0, 3){
raise "Should not yield"
}
m.should be_empty
m.column_size.should == 3
m = Matrix.build(3, 0){
raise "Should not yield"
}
m.should be_empty
m.row_size.should == 3
end
it "tries to calls :to_int on arguments" do
int = mock('int')
int.should_receive(:to_int).twice.and_return(2)
Matrix.build(int, int){ 1 }.should == Matrix[ [1,1], [1,1] ]
end
it "builds an nxn Matrix when given only one argument" do
m = Matrix.build(3){1}
m.row_size.should == 3
m.column_size.should == 3
end
end
describe "for a subclass of Matrix" do
it "returns an instance of that subclass" do
MatrixSub.build(3){1}.should be_an_instance_of(MatrixSub)
end end
end end

View file

@ -1,25 +1,28 @@
require_relative '../../spec_helper' require_relative '../../spec_helper'
require_relative 'fixtures/classes'
require 'matrix'
describe "Matrix#clone" do ruby_version_is ""..."3.1" do
before :each do require_relative 'fixtures/classes'
@a = Matrix[[1, 2], [3, 4], [5, 6]] require 'matrix'
end
it "returns a shallow copy of the matrix" do describe "Matrix#clone" do
b = @a.clone before :each do
@a.should_not equal(b) @a = Matrix[[1, 2], [3, 4], [5, 6]]
b.should be_kind_of(Matrix)
b.should == @a
0.upto(@a.row_size - 1) do |i|
@a.row(i).should_not equal(b.row(i))
end end
end
describe "for a subclass of Matrix" do it "returns a shallow copy of the matrix" do
it "returns an instance of that subclass" do b = @a.clone
MatrixSub.ins.clone.should be_an_instance_of(MatrixSub) @a.should_not equal(b)
b.should be_kind_of(Matrix)
b.should == @a
0.upto(@a.row_size - 1) do |i|
@a.row(i).should_not equal(b.row(i))
end
end
describe "for a subclass of Matrix" do
it "returns an instance of that subclass" do
MatrixSub.ins.clone.should be_an_instance_of(MatrixSub)
end
end end
end end
end end

View file

@ -1,8 +1,11 @@
require_relative '../../spec_helper' require_relative '../../spec_helper'
require 'matrix'
describe "Matrix#coerce" do ruby_version_is ""..."3.1" do
it "allows the division of integer by a Matrix " do require 'matrix'
(1/Matrix[[0,1],[-1,0]]).should == Matrix[[0,-1],[1,0]]
describe "Matrix#coerce" do
it "allows the division of integer by a Matrix " do
(1/Matrix[[0,1],[-1,0]]).should == Matrix[[0,-1],[1,0]]
end
end end
end end

View file

@ -1,6 +1,9 @@
require_relative '../../spec_helper' require_relative '../../spec_helper'
require_relative 'shared/collect'
describe "Matrix#collect" do ruby_version_is ""..."3.1" do
it_behaves_like :collect, :collect require_relative 'shared/collect'
describe "Matrix#collect" do
it_behaves_like :collect, :collect
end
end end

View file

@ -1,13 +1,16 @@
require_relative '../../spec_helper' require_relative '../../spec_helper'
require 'matrix'
describe "Matrix#column_size" do ruby_version_is ""..."3.1" do
it "returns the number of columns" do require 'matrix'
Matrix[ [1,2], [3,4] ].column_size.should == 2
end
it "returns 0 for empty matrices" do describe "Matrix#column_size" do
Matrix[ [], [] ].column_size.should == 0 it "returns the number of columns" do
Matrix[ ].column_size.should == 0 Matrix[ [1,2], [3,4] ].column_size.should == 2
end
it "returns 0 for empty matrices" do
Matrix[ [], [] ].column_size.should == 0
Matrix[ ].column_size.should == 0
end
end end
end end

View file

@ -1,35 +1,38 @@
require_relative '../../spec_helper' require_relative '../../spec_helper'
require 'matrix'
describe "Matrix#column" do ruby_version_is ""..."3.1" do
before :all do require 'matrix'
@m = Matrix[[1,2,3], [2,3,4]]
end
it "returns a Vector when called without a block" do describe "Matrix#column" do
@m.column(1).should == Vector[2,3] before :all do
end @m = Matrix[[1,2,3], [2,3,4]]
end
it "yields each element in the column to the block" do it "returns a Vector when called without a block" do
a = [] @m.column(1).should == Vector[2,3]
@m.column(1) {|n| a << n } end
a.should == [2,3]
end
it "counts backwards for negative argument" do it "yields each element in the column to the block" do
@m.column(-1).should == Vector[3, 4] a = []
end @m.column(1) {|n| a << n }
a.should == [2,3]
end
it "returns self when called with a block" do it "counts backwards for negative argument" do
@m.column(0) { |x| x }.should equal(@m) @m.column(-1).should == Vector[3, 4]
end end
it "returns nil when out of bounds" do it "returns self when called with a block" do
@m.column(3).should == nil @m.column(0) { |x| x }.should equal(@m)
end end
it "never yields when out of bounds" do it "returns nil when out of bounds" do
-> { @m.column(3){ raise } }.should_not raise_error @m.column(3).should == nil
-> { @m.column(-4){ raise } }.should_not raise_error end
it "never yields when out of bounds" do
-> { @m.column(3){ raise } }.should_not raise_error
-> { @m.column(-4){ raise } }.should_not raise_error
end
end end
end end

View file

@ -1,25 +1,28 @@
require_relative '../../spec_helper' require_relative '../../spec_helper'
require_relative 'fixtures/classes'
require 'matrix'
describe "Matrix.column_vector" do ruby_version_is ""..."3.1" do
require_relative 'fixtures/classes'
require 'matrix'
it "returns a single column Matrix when called with an Array" do describe "Matrix.column_vector" do
m = Matrix.column_vector([4,5,6])
m.should be_an_instance_of(Matrix)
m.should == Matrix[ [4],[5],[6] ]
end
it "returns an empty Matrix when called with an empty Array" do it "returns a single column Matrix when called with an Array" do
m = Matrix.column_vector([]) m = Matrix.column_vector([4,5,6])
m.should be_an_instance_of(Matrix) m.should be_an_instance_of(Matrix)
m.row_size.should == 0 m.should == Matrix[ [4],[5],[6] ]
m.column_size.should == 1 end
end
describe "for a subclass of Matrix" do it "returns an empty Matrix when called with an empty Array" do
it "returns an instance of that subclass" do m = Matrix.column_vector([])
MatrixSub.column_vector([4,5,6]).should be_an_instance_of(MatrixSub) m.should be_an_instance_of(Matrix)
m.row_size.should == 0
m.column_size.should == 1
end
describe "for a subclass of Matrix" do
it "returns an instance of that subclass" do
MatrixSub.column_vector([4,5,6]).should be_an_instance_of(MatrixSub)
end
end end
end end
end end

View file

@ -1,26 +1,29 @@
require_relative '../../spec_helper' require_relative '../../spec_helper'
require 'matrix'
describe "Matrix#column_vectors" do ruby_version_is ""..."3.1" do
require 'matrix'
describe "Matrix#column_vectors" do
before :each do
@vectors = Matrix[ [1,2], [3,4] ].column_vectors
end
it "returns an Array" do
Matrix[ [1,2], [3,4] ].column_vectors.should be_an_instance_of(Array)
end
it "returns an Array of Vectors" do
@vectors.all? {|v| v.should be_an_instance_of(Vector)}
end
it "returns each column as a Vector" do
@vectors.should == [Vector[1,3], Vector[2,4]]
end
it "returns an empty Array for empty matrices" do
Matrix[ [] ].column_vectors.should == []
end
before :each do
@vectors = Matrix[ [1,2], [3,4] ].column_vectors
end end
it "returns an Array" do
Matrix[ [1,2], [3,4] ].column_vectors.should be_an_instance_of(Array)
end
it "returns an Array of Vectors" do
@vectors.all? {|v| v.should be_an_instance_of(Vector)}
end
it "returns each column as a Vector" do
@vectors.should == [Vector[1,3], Vector[2,4]]
end
it "returns an empty Array for empty matrices" do
Matrix[ [] ].column_vectors.should == []
end
end end

View file

@ -1,42 +1,45 @@
require_relative '../../spec_helper' require_relative '../../spec_helper'
require_relative 'fixtures/classes'
require 'matrix'
describe "Matrix.columns" do ruby_version_is ""..."3.1" do
before :each do require_relative 'fixtures/classes'
@a = [1, 2] require 'matrix'
@b = [3, 4]
@m = Matrix.columns([@a, @b])
end
it "creates a Matrix from argument columns" do describe "Matrix.columns" do
@m.should be_an_instance_of(Matrix) before :each do
@m.column(0).to_a.should == @a @a = [1, 2]
@m.column(1).to_a.should == @b @b = [3, 4]
end @m = Matrix.columns([@a, @b])
end
it "accepts Vectors as argument columns" do it "creates a Matrix from argument columns" do
m = Matrix.columns([Vector[*@a], Vector[*@b]]) @m.should be_an_instance_of(Matrix)
m.should == @m @m.column(0).to_a.should == @a
m.column(0).to_a.should == @a @m.column(1).to_a.should == @b
m.column(1).to_a.should == @b end
end
it "handles empty matrices" do it "accepts Vectors as argument columns" do
e = Matrix.columns([]) m = Matrix.columns([Vector[*@a], Vector[*@b]])
e.row_size.should == 0 m.should == @m
e.column_size.should == 0 m.column(0).to_a.should == @a
e.should == Matrix[] m.column(1).to_a.should == @b
end
v = Matrix.columns([[],[],[]]) it "handles empty matrices" do
v.row_size.should == 0 e = Matrix.columns([])
v.column_size.should == 3 e.row_size.should == 0
v.should == Matrix[[], [], []].transpose e.column_size.should == 0
end e.should == Matrix[]
describe "for a subclass of Matrix" do v = Matrix.columns([[],[],[]])
it "returns an instance of that subclass" do v.row_size.should == 0
MatrixSub.columns([[1]]).should be_an_instance_of(MatrixSub) v.column_size.should == 3
v.should == Matrix[[], [], []].transpose
end
describe "for a subclass of Matrix" do
it "returns an instance of that subclass" do
MatrixSub.columns([[1]]).should be_an_instance_of(MatrixSub)
end
end end
end end
end end

View file

@ -1,6 +1,9 @@
require_relative '../../spec_helper' require_relative '../../spec_helper'
require_relative 'shared/conjugate'
describe "Matrix#conj" do ruby_version_is ""..."3.1" do
it_behaves_like :matrix_conjugate, :conj require_relative 'shared/conjugate'
describe "Matrix#conj" do
it_behaves_like :matrix_conjugate, :conj
end
end end

View file

@ -1,6 +1,9 @@
require_relative '../../spec_helper' require_relative '../../spec_helper'
require_relative 'shared/conjugate'
describe "Matrix#conjugate" do ruby_version_is ""..."3.1" do
it_behaves_like :matrix_conjugate, :conjugate require_relative 'shared/conjugate'
describe "Matrix#conjugate" do
it_behaves_like :matrix_conjugate, :conjugate
end
end end

View file

@ -1,65 +1,68 @@
require_relative '../../spec_helper' require_relative '../../spec_helper'
require_relative 'fixtures/classes'
require 'matrix'
describe "Matrix.[]" do ruby_version_is ""..."3.1" do
require_relative 'fixtures/classes'
require 'matrix'
it "requires arrays as parameters" do describe "Matrix.[]" do
-> { Matrix[5] }.should raise_error(TypeError)
-> { Matrix[nil] }.should raise_error(TypeError)
-> { Matrix[1..2] }.should raise_error(TypeError)
-> { Matrix[[1, 2], 3] }.should raise_error(TypeError)
end
it "creates an empty Matrix with no arguments" do it "requires arrays as parameters" do
m = Matrix[] -> { Matrix[5] }.should raise_error(TypeError)
m.column_size.should == 0 -> { Matrix[nil] }.should raise_error(TypeError)
m.row_size.should == 0 -> { Matrix[1..2] }.should raise_error(TypeError)
end -> { Matrix[[1, 2], 3] }.should raise_error(TypeError)
end
it "raises for non-rectangular matrices" do it "creates an empty Matrix with no arguments" do
->{ Matrix[ [0], [0,1] ] }.should \ m = Matrix[]
raise_error(Matrix::ErrDimensionMismatch) m.column_size.should == 0
->{ Matrix[ [0,1], [0,1,2], [0,1] ]}.should \ m.row_size.should == 0
raise_error(Matrix::ErrDimensionMismatch) end
end
it "accepts vector arguments" do it "raises for non-rectangular matrices" do
a = Matrix[Vector[1, 2], Vector[3, 4]] ->{ Matrix[ [0], [0,1] ] }.should \
a.should be_an_instance_of(Matrix) raise_error(Matrix::ErrDimensionMismatch)
a.should == Matrix[ [1, 2], [3, 4] ] ->{ Matrix[ [0,1], [0,1,2], [0,1] ]}.should \
end raise_error(Matrix::ErrDimensionMismatch)
end
it "tries to calls :to_ary on arguments" do it "accepts vector arguments" do
array = mock('ary') a = Matrix[Vector[1, 2], Vector[3, 4]]
array.should_receive(:to_ary).and_return([1,2]) a.should be_an_instance_of(Matrix)
Matrix[array, [3,4] ].should == Matrix[ [1,2], [3,4] ] a.should == Matrix[ [1, 2], [3, 4] ]
end end
it "tries to calls :to_ary on arguments" do
array = mock('ary')
array.should_receive(:to_ary).and_return([1,2])
Matrix[array, [3,4] ].should == Matrix[ [1,2], [3,4] ]
end
it "returns a Matrix object" do it "returns a Matrix object" do
Matrix[ [1] ].should be_an_instance_of(Matrix) Matrix[ [1] ].should be_an_instance_of(Matrix)
end end
it "can create an nxn Matrix" do it "can create an nxn Matrix" do
m = Matrix[ [20,30], [40.5, 9] ] m = Matrix[ [20,30], [40.5, 9] ]
m.row_size.should == 2 m.row_size.should == 2
m.column_size.should == 2 m.column_size.should == 2
m.column(0).should == Vector[20, 40.5] m.column(0).should == Vector[20, 40.5]
m.column(1).should == Vector[30, 9] m.column(1).should == Vector[30, 9]
m.row(0).should == Vector[20, 30] m.row(0).should == Vector[20, 30]
m.row(1).should == Vector[40.5, 9] m.row(1).should == Vector[40.5, 9]
end end
it "can create a 0xn Matrix" do it "can create a 0xn Matrix" do
m = Matrix[ [], [], [] ] m = Matrix[ [], [], [] ]
m.row_size.should == 3 m.row_size.should == 3
m.column_size.should == 0 m.column_size.should == 0
end end
describe "for a subclass of Matrix" do describe "for a subclass of Matrix" do
it "returns an instance of that subclass" do it "returns an instance of that subclass" do
MatrixSub[ [20,30], [40.5, 9] ].should be_an_instance_of(MatrixSub) MatrixSub[ [20,30], [40.5, 9] ].should be_an_instance_of(MatrixSub)
end
end end
end end
end end

View file

@ -1,7 +1,10 @@
require_relative '../../spec_helper' require_relative '../../spec_helper'
require_relative 'shared/determinant'
require 'matrix'
describe "Matrix#det" do ruby_version_is ""..."3.1" do
it_behaves_like :determinant, :det require_relative 'shared/determinant'
require 'matrix'
describe "Matrix#det" do
it_behaves_like :determinant, :det
end
end end

View file

@ -1,7 +1,10 @@
require_relative '../../spec_helper' require_relative '../../spec_helper'
require_relative 'shared/determinant'
require 'matrix'
describe "Matrix#determinant" do ruby_version_is ""..."3.1" do
it_behaves_like :determinant, :determinant require_relative 'shared/determinant'
require 'matrix'
describe "Matrix#determinant" do
it_behaves_like :determinant, :determinant
end
end end

View file

@ -1,72 +1,75 @@
require_relative '../../spec_helper' require_relative '../../spec_helper'
require_relative 'fixtures/classes'
require 'matrix'
describe "Matrix.diagonal" do ruby_version_is ""..."3.1" do
before :each do require_relative 'fixtures/classes'
@m = Matrix.diagonal(10, 11, 12, 13, 14) require 'matrix'
end
it "returns an object of type Matrix" do describe "Matrix.diagonal" do
@m.should be_kind_of(Matrix) before :each do
end @m = Matrix.diagonal(10, 11, 12, 13, 14)
it "returns a square Matrix of the right size" do
@m.column_size.should == 5
@m.row_size.should == 5
end
it "sets the diagonal to the arguments" do
(0..4).each do |i|
@m[i, i].should == i + 10
end end
end
it "fills all non-diagonal cells with 0" do it "returns an object of type Matrix" do
(0..4).each do |i| @m.should be_kind_of(Matrix)
(0..4).each do |j| end
if i != j
@m[i, j].should == 0 it "returns a square Matrix of the right size" do
@m.column_size.should == 5
@m.row_size.should == 5
end
it "sets the diagonal to the arguments" do
(0..4).each do |i|
@m[i, i].should == i + 10
end
end
it "fills all non-diagonal cells with 0" do
(0..4).each do |i|
(0..4).each do |j|
if i != j
@m[i, j].should == 0
end
end end
end end
end end
describe "for a subclass of Matrix" do
it "returns an instance of that subclass" do
MatrixSub.diagonal(1).should be_an_instance_of(MatrixSub)
end
end
end end
describe "for a subclass of Matrix" do describe "Matrix.diagonal?" do
it "returns an instance of that subclass" do it "returns true for a diagonal Matrix" do
MatrixSub.diagonal(1).should be_an_instance_of(MatrixSub) Matrix.diagonal([1, 2, 3]).diagonal?.should be_true
end end
end
end it "returns true for a zero square Matrix" do
Matrix.zero(3).diagonal?.should be_true
describe "Matrix.diagonal?" do end
it "returns true for a diagonal Matrix" do
Matrix.diagonal([1, 2, 3]).diagonal?.should be_true it "returns false for a non diagonal square Matrix" do
end Matrix[[0, 1], [0, 0]].diagonal?.should be_false
Matrix[[1, 2, 3], [1, 2, 3], [1, 2, 3]].diagonal?.should be_false
it "returns true for a zero square Matrix" do end
Matrix.zero(3).diagonal?.should be_true
end it "returns true for an empty 0x0 matrix" do
Matrix.empty(0,0).diagonal?.should be_true
it "returns false for a non diagonal square Matrix" do end
Matrix[[0, 1], [0, 0]].diagonal?.should be_false
Matrix[[1, 2, 3], [1, 2, 3], [1, 2, 3]].diagonal?.should be_false it "raises an error for rectangular matrices" do
end [
Matrix[[0], [0]],
it "returns true for an empty 0x0 matrix" do Matrix[[0, 0]],
Matrix.empty(0,0).diagonal?.should be_true Matrix.empty(0, 2),
end Matrix.empty(2, 0),
].each do |rectangular_matrix|
it "raises an error for rectangular matrices" do -> {
[ rectangular_matrix.diagonal?
Matrix[[0], [0]], }.should raise_error(Matrix::ErrDimensionMismatch)
Matrix[[0, 0]], end
Matrix.empty(0, 2),
Matrix.empty(2, 0),
].each do |rectangular_matrix|
-> {
rectangular_matrix.diagonal?
}.should raise_error(Matrix::ErrDimensionMismatch)
end end
end end
end end

View file

@ -1,54 +1,57 @@
require_relative '../../spec_helper' require_relative '../../spec_helper'
require_relative 'spec_helper'
require_relative 'fixtures/classes'
require 'matrix'
describe "Matrix#/" do ruby_version_is ""..."3.1" do
before :each do require_relative 'spec_helper'
@a = Matrix[ [1, 2], [3, 4] ] require_relative 'fixtures/classes'
@b = Matrix[ [4, 5], [6, 7] ] require 'matrix'
@c = Matrix[ [1.2, 2.4], [3.6, 4.8] ]
end
it "returns the result of dividing self by another Matrix" do describe "Matrix#/" do
(@a / @b).should be_close_to_matrix([[2.5, -1.5], [1.5, -0.5]]) before :each do
end @a = Matrix[ [1, 2], [3, 4] ]
@b = Matrix[ [4, 5], [6, 7] ]
# Guard against the Mathn library @c = Matrix[ [1.2, 2.4], [3.6, 4.8] ]
guard -> { !defined?(Math.rsqrt) } do
it "returns the result of dividing self by a Fixnum" do
(@a / 2).should == Matrix[ [0, 1], [1, 2] ]
end end
it "returns the result of dividing self by a Bignum" do it "returns the result of dividing self by another Matrix" do
(@a / bignum_value).should == Matrix[ [0, 0], [0, 0] ] (@a / @b).should be_close_to_matrix([[2.5, -1.5], [1.5, -0.5]])
end end
end
it "returns the result of dividing self by a Float" do # Guard against the Mathn library
(@c / 1.2).should == Matrix[ [1, 2], [3, 4] ] guard -> { !defined?(Math.rsqrt) } do
end it "returns the result of dividing self by a Fixnum" do
(@a / 2).should == Matrix[ [0, 1], [1, 2] ]
end
it "raises a Matrix::ErrDimensionMismatch if the matrices are different sizes" do it "returns the result of dividing self by a Bignum" do
-> { @a / Matrix[ [1] ] }.should raise_error(Matrix::ErrDimensionMismatch) (@a / bignum_value).should == Matrix[ [0, 0], [0, 0] ]
end end
it "returns an instance of Matrix" do
(@a / @b).should be_kind_of(Matrix)
end
describe "for a subclass of Matrix" do
it "returns an instance of that subclass" do
m = MatrixSub.ins
(m/m).should be_an_instance_of(MatrixSub)
(m/1).should be_an_instance_of(MatrixSub)
end end
end
it "raises a TypeError if other is of wrong type" do it "returns the result of dividing self by a Float" do
-> { @a / nil }.should raise_error(TypeError) (@c / 1.2).should == Matrix[ [1, 2], [3, 4] ]
-> { @a / "a" }.should raise_error(TypeError) end
-> { @a / [ [1, 2] ] }.should raise_error(TypeError)
-> { @a / Object.new }.should raise_error(TypeError) it "raises a Matrix::ErrDimensionMismatch if the matrices are different sizes" do
-> { @a / Matrix[ [1] ] }.should raise_error(Matrix::ErrDimensionMismatch)
end
it "returns an instance of Matrix" do
(@a / @b).should be_kind_of(Matrix)
end
describe "for a subclass of Matrix" do
it "returns an instance of that subclass" do
m = MatrixSub.ins
(m/m).should be_an_instance_of(MatrixSub)
(m/1).should be_an_instance_of(MatrixSub)
end
end
it "raises a TypeError if other is of wrong type" do
-> { @a / nil }.should raise_error(TypeError)
-> { @a / "a" }.should raise_error(TypeError)
-> { @a / [ [1, 2] ] }.should raise_error(TypeError)
-> { @a / Object.new }.should raise_error(TypeError)
end
end end
end end

View file

@ -1,74 +1,77 @@
require_relative '../../spec_helper' require_relative '../../spec_helper'
require 'matrix'
describe "Matrix#each" do ruby_version_is ""..."3.1" do
before :all do require 'matrix'
@m = Matrix[ [1, 2, 3], [4, 5, 6] ]
@result = (1..6).to_a describe "Matrix#each" do
before :all do
@m = Matrix[ [1, 2, 3], [4, 5, 6] ]
@result = (1..6).to_a
end
it "returns an Enumerator when called without a block" do
enum = @m.each
enum.should be_an_instance_of(Enumerator)
enum.to_a.should == @result
end
it "returns self" do
@m.each{}.should equal(@m)
end
it "yields the elements starting with the those of the first row" do
a = []
@m.each {|x| a << x}
a.should == @result
end
end end
it "returns an Enumerator when called without a block" do describe "Matrix#each with an argument" do
enum = @m.each before :all do
enum.should be_an_instance_of(Enumerator) @m = Matrix[ [1, 2, 3, 4], [5, 6, 7, 8] ]
enum.to_a.should == @result @t = Matrix[ [1, 2], [3, 4], [5, 6], [7, 8] ]
end end
it "returns self" do it "raises an ArgumentError for unrecognized argument" do
@m.each{}.should equal(@m) -> {
end @m.each("all"){}
}.should raise_error(ArgumentError)
-> {
@m.each(nil){}
}.should raise_error(ArgumentError)
-> {
@m.each(:left){}
}.should raise_error(ArgumentError)
end
it "yields the elements starting with the those of the first row" do it "yields the rights elements when passed :diagonal" do
a = [] @m.each(:diagonal).to_a.should == [1, 6]
@m.each {|x| a << x} @t.each(:diagonal).to_a.should == [1, 4]
a.should == @result end
end
end it "yields the rights elements when passed :off_diagonal" do
@m.each(:off_diagonal).to_a.should == [2, 3, 4, 5, 7, 8]
describe "Matrix#each with an argument" do @t.each(:off_diagonal).to_a.should == [2, 3, 5, 6, 7, 8]
before :all do end
@m = Matrix[ [1, 2, 3, 4], [5, 6, 7, 8] ]
@t = Matrix[ [1, 2], [3, 4], [5, 6], [7, 8] ] it "yields the rights elements when passed :lower" do
end @m.each(:lower).to_a.should == [1, 5, 6]
@t.each(:lower).to_a.should == [1, 3, 4, 5, 6, 7, 8]
it "raises an ArgumentError for unrecognized argument" do end
-> {
@m.each("all"){} it "yields the rights elements when passed :strict_lower" do
}.should raise_error(ArgumentError) @m.each(:strict_lower).to_a.should == [5]
-> { @t.each(:strict_lower).to_a.should == [3, 5, 6, 7, 8]
@m.each(nil){} end
}.should raise_error(ArgumentError)
-> { it "yields the rights elements when passed :strict_upper" do
@m.each(:left){} @m.each(:strict_upper).to_a.should == [2, 3, 4, 7, 8]
}.should raise_error(ArgumentError) @t.each(:strict_upper).to_a.should == [2]
end end
it "yields the rights elements when passed :diagonal" do it "yields the rights elements when passed :upper" do
@m.each(:diagonal).to_a.should == [1, 6] @m.each(:upper).to_a.should == [1, 2, 3, 4, 6, 7, 8]
@t.each(:diagonal).to_a.should == [1, 4] @t.each(:upper).to_a.should == [1, 2, 4]
end end
it "yields the rights elements when passed :off_diagonal" do
@m.each(:off_diagonal).to_a.should == [2, 3, 4, 5, 7, 8]
@t.each(:off_diagonal).to_a.should == [2, 3, 5, 6, 7, 8]
end
it "yields the rights elements when passed :lower" do
@m.each(:lower).to_a.should == [1, 5, 6]
@t.each(:lower).to_a.should == [1, 3, 4, 5, 6, 7, 8]
end
it "yields the rights elements when passed :strict_lower" do
@m.each(:strict_lower).to_a.should == [5]
@t.each(:strict_lower).to_a.should == [3, 5, 6, 7, 8]
end
it "yields the rights elements when passed :strict_upper" do
@m.each(:strict_upper).to_a.should == [2, 3, 4, 7, 8]
@t.each(:strict_upper).to_a.should == [2]
end
it "yields the rights elements when passed :upper" do
@m.each(:upper).to_a.should == [1, 2, 3, 4, 6, 7, 8]
@t.each(:upper).to_a.should == [1, 2, 4]
end end
end end

View file

@ -1,81 +1,84 @@
require_relative '../../spec_helper' require_relative '../../spec_helper'
require 'matrix'
describe "Matrix#each_with_index" do ruby_version_is ""..."3.1" do
before :all do require 'matrix'
@m = Matrix[ [1, 2, 3], [4, 5, 6] ]
@result = [ describe "Matrix#each_with_index" do
[1, 0, 0], before :all do
[2, 0, 1], @m = Matrix[ [1, 2, 3], [4, 5, 6] ]
[3, 0, 2], @result = [
[4, 1, 0], [1, 0, 0],
[5, 1, 1], [2, 0, 1],
[6, 1, 2] [3, 0, 2],
] [4, 1, 0],
[5, 1, 1],
[6, 1, 2]
]
end
it "returns an Enumerator when called without a block" do
enum = @m.each_with_index
enum.should be_an_instance_of(Enumerator)
enum.to_a.should == @result
end
it "returns self" do
@m.each_with_index{}.should equal(@m)
end
it "yields the elements starting with the those of the first row" do
a = []
@m.each_with_index {|x, r, c| a << [x, r, c]}
a.should == @result
end
end end
it "returns an Enumerator when called without a block" do describe "Matrix#each_with_index with an argument" do
enum = @m.each_with_index before :all do
enum.should be_an_instance_of(Enumerator) @m = Matrix[ [1, 2, 3, 4], [5, 6, 7, 8] ]
enum.to_a.should == @result @t = Matrix[ [1, 2], [3, 4], [5, 6], [7, 8] ]
end end
it "returns self" do it "raises an ArgumentError for unrecognized argument" do
@m.each_with_index{}.should equal(@m) -> {
end @m.each_with_index("all"){}
}.should raise_error(ArgumentError)
-> {
@m.each_with_index(nil){}
}.should raise_error(ArgumentError)
-> {
@m.each_with_index(:left){}
}.should raise_error(ArgumentError)
end
it "yields the elements starting with the those of the first row" do it "yields the rights elements when passed :diagonal" do
a = [] @m.each_with_index(:diagonal).to_a.should == [[1, 0, 0], [6, 1, 1]]
@m.each_with_index {|x, r, c| a << [x, r, c]} @t.each_with_index(:diagonal).to_a.should == [[1, 0, 0], [4, 1, 1]]
a.should == @result end
end
end it "yields the rights elements when passed :off_diagonal" do
@m.each_with_index(:off_diagonal).to_a.should == [[2, 0, 1], [3, 0, 2], [4, 0, 3], [5, 1, 0], [7, 1, 2], [8, 1, 3]]
describe "Matrix#each_with_index with an argument" do @t.each_with_index(:off_diagonal).to_a.should == [[2, 0, 1], [3, 1, 0], [5, 2, 0], [6, 2, 1], [7, 3, 0], [8, 3, 1]]
before :all do end
@m = Matrix[ [1, 2, 3, 4], [5, 6, 7, 8] ]
@t = Matrix[ [1, 2], [3, 4], [5, 6], [7, 8] ] it "yields the rights elements when passed :lower" do
end @m.each_with_index(:lower).to_a.should == [[1, 0, 0], [5, 1, 0], [6, 1, 1]]
@t.each_with_index(:lower).to_a.should == [[1, 0, 0], [3, 1, 0], [4, 1, 1], [5, 2, 0], [6, 2, 1], [7, 3, 0], [8, 3, 1]]
it "raises an ArgumentError for unrecognized argument" do end
-> {
@m.each_with_index("all"){} it "yields the rights elements when passed :strict_lower" do
}.should raise_error(ArgumentError) @m.each_with_index(:strict_lower).to_a.should == [[5, 1, 0]]
-> { @t.each_with_index(:strict_lower).to_a.should == [[3, 1, 0], [5, 2, 0], [6, 2, 1], [7, 3, 0], [8, 3, 1]]
@m.each_with_index(nil){} end
}.should raise_error(ArgumentError)
-> { it "yields the rights elements when passed :strict_upper" do
@m.each_with_index(:left){} @m.each_with_index(:strict_upper).to_a.should == [[2, 0, 1], [3, 0, 2], [4, 0, 3], [7, 1, 2], [8, 1, 3]]
}.should raise_error(ArgumentError) @t.each_with_index(:strict_upper).to_a.should == [[2, 0, 1]]
end end
it "yields the rights elements when passed :diagonal" do it "yields the rights elements when passed :upper" do
@m.each_with_index(:diagonal).to_a.should == [[1, 0, 0], [6, 1, 1]] @m.each_with_index(:upper).to_a.should == [[1, 0, 0], [2, 0, 1], [3, 0, 2], [4, 0, 3], [6, 1, 1], [7, 1, 2], [8, 1, 3]]
@t.each_with_index(:diagonal).to_a.should == [[1, 0, 0], [4, 1, 1]] @t.each_with_index(:upper).to_a.should == [[1, 0, 0], [2, 0, 1], [4, 1, 1]]
end end
it "yields the rights elements when passed :off_diagonal" do
@m.each_with_index(:off_diagonal).to_a.should == [[2, 0, 1], [3, 0, 2], [4, 0, 3], [5, 1, 0], [7, 1, 2], [8, 1, 3]]
@t.each_with_index(:off_diagonal).to_a.should == [[2, 0, 1], [3, 1, 0], [5, 2, 0], [6, 2, 1], [7, 3, 0], [8, 3, 1]]
end
it "yields the rights elements when passed :lower" do
@m.each_with_index(:lower).to_a.should == [[1, 0, 0], [5, 1, 0], [6, 1, 1]]
@t.each_with_index(:lower).to_a.should == [[1, 0, 0], [3, 1, 0], [4, 1, 1], [5, 2, 0], [6, 2, 1], [7, 3, 0], [8, 3, 1]]
end
it "yields the rights elements when passed :strict_lower" do
@m.each_with_index(:strict_lower).to_a.should == [[5, 1, 0]]
@t.each_with_index(:strict_lower).to_a.should == [[3, 1, 0], [5, 2, 0], [6, 2, 1], [7, 3, 0], [8, 3, 1]]
end
it "yields the rights elements when passed :strict_upper" do
@m.each_with_index(:strict_upper).to_a.should == [[2, 0, 1], [3, 0, 2], [4, 0, 3], [7, 1, 2], [8, 1, 3]]
@t.each_with_index(:strict_upper).to_a.should == [[2, 0, 1]]
end
it "yields the rights elements when passed :upper" do
@m.each_with_index(:upper).to_a.should == [[1, 0, 0], [2, 0, 1], [3, 0, 2], [4, 0, 3], [6, 1, 1], [7, 1, 2], [8, 1, 3]]
@t.each_with_index(:upper).to_a.should == [[1, 0, 0], [2, 0, 1], [4, 1, 1]]
end end
end end

View file

@ -1,9 +1,12 @@
require_relative '../../../spec_helper' require_relative '../../../spec_helper'
require 'matrix'
describe "Matrix::EigenvalueDecomposition#eigenvalue_matrix" do ruby_version_is ""..."3.1" do
it "returns a diagonal matrix with the eigenvalues on the diagonal" do require 'matrix'
Matrix[[14, 16], [-6, -6]].eigensystem.eigenvalue_matrix.should == Matrix[[6, 0],[0, 2]]
Matrix[[1, 1], [-1, 1]].eigensystem.eigenvalue_matrix.should == Matrix[[Complex(1,1), 0],[0, Complex(1,-1)]] describe "Matrix::EigenvalueDecomposition#eigenvalue_matrix" do
it "returns a diagonal matrix with the eigenvalues on the diagonal" do
Matrix[[14, 16], [-6, -6]].eigensystem.eigenvalue_matrix.should == Matrix[[6, 0],[0, 2]]
Matrix[[1, 1], [-1, 1]].eigensystem.eigenvalue_matrix.should == Matrix[[Complex(1,1), 0],[0, Complex(1,-1)]]
end
end end
end end

View file

@ -1,22 +1,25 @@
require_relative '../../../spec_helper' require_relative '../../../spec_helper'
require 'matrix'
describe "Matrix::EigenvalueDecomposition#eigenvalues" do ruby_version_is ""..."3.1" do
it "returns an array of complex eigenvalues for a rotation matrix" do require 'matrix'
Matrix[[ 1, 1],
[-1, 1]].eigensystem.eigenvalues.sort_by{|v| v.imag}.should ==
[ Complex(1, -1), Complex(1, 1)]
end
it "returns an array of real eigenvalues for a symmetric matrix" do describe "Matrix::EigenvalueDecomposition#eigenvalues" do
Matrix[[1, 2], it "returns an array of complex eigenvalues for a rotation matrix" do
[2, 1]].eigensystem.eigenvalues.sort.map!{|x| x.round(10)}.should == Matrix[[ 1, 1],
[ -1, 3 ] [-1, 1]].eigensystem.eigenvalues.sort_by{|v| v.imag}.should ==
end [ Complex(1, -1), Complex(1, 1)]
end
it "returns an array of real eigenvalues for a matrix" do it "returns an array of real eigenvalues for a symmetric matrix" do
Matrix[[14, 16], Matrix[[1, 2],
[-6, -6]].eigensystem.eigenvalues.sort.map!{|x| x.round(10)}.should == [2, 1]].eigensystem.eigenvalues.sort.map!{|x| x.round(10)}.should ==
[ 2, 6 ] [ -1, 3 ]
end
it "returns an array of real eigenvalues for a matrix" do
Matrix[[14, 16],
[-6, -6]].eigensystem.eigenvalues.sort.map!{|x| x.round(10)}.should ==
[ 2, 6 ]
end
end end
end end

View file

@ -1,20 +1,23 @@
require_relative '../../../spec_helper' require_relative '../../../spec_helper'
require 'matrix'
describe "Matrix::EigenvalueDecomposition#eigenvector_matrix" do ruby_version_is ""..."3.1" do
it "returns a complex eigenvector matrix given a rotation matrix" do require 'matrix'
# Fix me: should test for linearity, not for equality
Matrix[[ 1, 1],
[-1, 1]].eigensystem.eigenvector_matrix.should ==
Matrix[[1, 1],
[Complex(0, 1), Complex(0, -1)]]
end
it "returns an real eigenvector matrix for a symmetric matrix" do describe "Matrix::EigenvalueDecomposition#eigenvector_matrix" do
# Fix me: should test for linearity, not for equality it "returns a complex eigenvector matrix given a rotation matrix" do
Matrix[[1, 2], # Fix me: should test for linearity, not for equality
[2, 1]].eigensystem.eigenvector_matrix.should == Matrix[[ 1, 1],
Matrix[[0.7071067811865475, 0.7071067811865475], [-1, 1]].eigensystem.eigenvector_matrix.should ==
[-0.7071067811865475, 0.7071067811865475]] Matrix[[1, 1],
[Complex(0, 1), Complex(0, -1)]]
end
it "returns an real eigenvector matrix for a symmetric matrix" do
# Fix me: should test for linearity, not for equality
Matrix[[1, 2],
[2, 1]].eigensystem.eigenvector_matrix.should ==
Matrix[[0.7071067811865475, 0.7071067811865475],
[-0.7071067811865475, 0.7071067811865475]]
end
end end
end end

View file

@ -1,22 +1,25 @@
require_relative '../../../spec_helper' require_relative '../../../spec_helper'
require 'matrix'
describe "Matrix::EigenvalueDecomposition#eigenvectors" do ruby_version_is ""..."3.1" do
it "returns an array of complex eigenvectors for a rotation matrix" do require 'matrix'
# Fix me: should test for linearity, not for equality
Matrix[[ 1, 1],
[-1, 1]].eigensystem.eigenvectors.should ==
[ Vector[1, Complex(0, 1)],
Vector[1, Complex(0, -1)]
]
end
it "returns an array of real eigenvectors for a symmetric matrix" do describe "Matrix::EigenvalueDecomposition#eigenvectors" do
# Fix me: should test for linearity, not for equality it "returns an array of complex eigenvectors for a rotation matrix" do
Matrix[[1, 2], # Fix me: should test for linearity, not for equality
[2, 1]].eigensystem.eigenvectors.should == Matrix[[ 1, 1],
[ Vector[0.7071067811865475, -0.7071067811865475], [-1, 1]].eigensystem.eigenvectors.should ==
Vector[0.7071067811865475, 0.7071067811865475] [ Vector[1, Complex(0, 1)],
] Vector[1, Complex(0, -1)]
]
end
it "returns an array of real eigenvectors for a symmetric matrix" do
# Fix me: should test for linearity, not for equality
Matrix[[1, 2],
[2, 1]].eigensystem.eigenvectors.should ==
[ Vector[0.7071067811865475, -0.7071067811865475],
Vector[0.7071067811865475, 0.7071067811865475]
]
end
end end
end end

View file

@ -1,24 +1,27 @@
require_relative '../../../spec_helper' require_relative '../../../spec_helper'
require 'matrix'
describe "Matrix::EigenvalueDecomposition#initialize" do ruby_version_is ""..."3.1" do
it "raises an error if argument is not a matrix" do require 'matrix'
-> {
Matrix::EigenvalueDecomposition.new([[]])
}.should raise_error(TypeError)
-> {
Matrix::EigenvalueDecomposition.new(42)
}.should raise_error(TypeError)
end
it "raises an error if matrix is not square" do describe "Matrix::EigenvalueDecomposition#initialize" do
-> { it "raises an error if argument is not a matrix" do
Matrix::EigenvalueDecomposition.new(Matrix[[1, 2]]) -> {
}.should raise_error(Matrix::ErrDimensionMismatch) Matrix::EigenvalueDecomposition.new([[]])
end }.should raise_error(TypeError)
-> {
Matrix::EigenvalueDecomposition.new(42)
}.should raise_error(TypeError)
end
it "never hangs" do it "raises an error if matrix is not square" do
m = Matrix[ [0,0,0,0,0], [0,0,0,0,1], [0,0,0,1,0], [1,1,0,0,1], [1,0,1,0,1] ] -> {
Matrix::EigenvalueDecomposition.new(m).should_not == "infinite loop" Matrix::EigenvalueDecomposition.new(Matrix[[1, 2]])
}.should raise_error(Matrix::ErrDimensionMismatch)
end
it "never hangs" do
m = Matrix[ [0,0,0,0,0], [0,0,0,0,1], [0,0,0,1,0], [1,1,0,0,1], [1,0,1,0,1] ]
Matrix::EigenvalueDecomposition.new(m).should_not == "infinite loop"
end
end end
end end

View file

@ -1,18 +1,21 @@
require_relative '../../../spec_helper' require_relative '../../../spec_helper'
require 'matrix'
describe "Matrix::EigenvalueDecomposition#to_a" do ruby_version_is ""..."3.1" do
before :each do require 'matrix'
@a = Matrix[[14, 16], [-6, -6]]
@e = Matrix::EigenvalueDecomposition.new(@a)
end
it "returns an array of with [V, D, V.inv]" do describe "Matrix::EigenvalueDecomposition#to_a" do
@e.to_a.should == [@e.v, @e.d, @e.v_inv] before :each do
end @a = Matrix[[14, 16], [-6, -6]]
@e = Matrix::EigenvalueDecomposition.new(@a)
end
it "returns a factorization" do it "returns an array of with [V, D, V.inv]" do
v, d, v_inv = @e.to_a @e.to_a.should == [@e.v, @e.d, @e.v_inv]
(v * d * v_inv).map{|e| e.round(10)}.should == @a end
it "returns a factorization" do
v, d, v_inv = @e.to_a
(v * d * v_inv).map{|e| e.round(10)}.should == @a
end
end end
end end

View file

@ -1,23 +1,26 @@
require_relative '../../spec_helper' require_relative '../../spec_helper'
require 'matrix'
describe "Matrix#[]" do ruby_version_is ""..."3.1" do
require 'matrix'
before :all do describe "Matrix#[]" do
@m = Matrix[[0, 1, 2], [3, 4, 5], [6, 7, 8], [9, 10, 11]]
end
it "returns element at (i, j)" do before :all do
(0..3).each do |i| @m = Matrix[[0, 1, 2], [3, 4, 5], [6, 7, 8], [9, 10, 11]]
(0..2).each do |j| end
@m[i, j].should == (i * 3) + j
it "returns element at (i, j)" do
(0..3).each do |i|
(0..2).each do |j|
@m[i, j].should == (i * 3) + j
end
end end
end end
end
it "returns nil for an invalid index pair" do it "returns nil for an invalid index pair" do
@m[8,1].should be_nil @m[8,1].should be_nil
@m[1,8].should be_nil @m[1,8].should be_nil
end end
end
end end

View file

@ -1,68 +1,71 @@
require_relative '../../spec_helper' require_relative '../../spec_helper'
require_relative 'fixtures/classes'
require 'matrix'
describe "Matrix#empty?" do ruby_version_is ""..."3.1" do
it "returns true when the Matrix is empty" do require_relative 'fixtures/classes'
Matrix[ ].empty?.should be_true require 'matrix'
Matrix[ [], [], [] ].empty?.should be_true
Matrix[ [], [], [] ].transpose.empty?.should be_true describe "Matrix#empty?" do
it "returns true when the Matrix is empty" do
Matrix[ ].empty?.should be_true
Matrix[ [], [], [] ].empty?.should be_true
Matrix[ [], [], [] ].transpose.empty?.should be_true
end
it "returns false when the Matrix has elements" do
Matrix[ [1, 2] ].empty?.should be_false
Matrix[ [1], [2] ].empty?.should be_false
end
it "doesn't accept any parameter" do
->{
Matrix[ [1, 2] ].empty?(42)
}.should raise_error(ArgumentError)
end
end end
it "returns false when the Matrix has elements" do describe "Matrix.empty" do
Matrix[ [1, 2] ].empty?.should be_false it "returns an empty matrix of the requested size" do
Matrix[ [1], [2] ].empty?.should be_false m = Matrix.empty(3, 0)
m.row_size.should == 3
m.column_size.should == 0
m = Matrix.empty(0, 3)
m.row_size.should == 0
m.column_size.should == 3
end
it "has arguments defaulting to 0" do
Matrix.empty.should == Matrix.empty(0, 0)
Matrix.empty(42).should == Matrix.empty(42, 0)
end
it "does not accept more than two parameters" do
->{
Matrix.empty(1, 2, 3)
}.should raise_error(ArgumentError)
end
it "raises an error if both dimensions are > 0" do
->{
Matrix.empty(1, 2)
}.should raise_error(ArgumentError)
end
it "raises an error if any dimension is < 0" do
->{
Matrix.empty(-2, 0)
}.should raise_error(ArgumentError)
->{
Matrix.empty(0, -2)
}.should raise_error(ArgumentError)
end
end end
it "doesn't accept any parameter" do describe "for a subclass of Matrix" do
->{ it "returns an instance of that subclass" do
Matrix[ [1, 2] ].empty?(42) MatrixSub.empty(0, 1).should be_an_instance_of(MatrixSub)
}.should raise_error(ArgumentError) end
end
end
describe "Matrix.empty" do
it "returns an empty matrix of the requested size" do
m = Matrix.empty(3, 0)
m.row_size.should == 3
m.column_size.should == 0
m = Matrix.empty(0, 3)
m.row_size.should == 0
m.column_size.should == 3
end
it "has arguments defaulting to 0" do
Matrix.empty.should == Matrix.empty(0, 0)
Matrix.empty(42).should == Matrix.empty(42, 0)
end
it "does not accept more than two parameters" do
->{
Matrix.empty(1, 2, 3)
}.should raise_error(ArgumentError)
end
it "raises an error if both dimensions are > 0" do
->{
Matrix.empty(1, 2)
}.should raise_error(ArgumentError)
end
it "raises an error if any dimension is < 0" do
->{
Matrix.empty(-2, 0)
}.should raise_error(ArgumentError)
->{
Matrix.empty(0, -2)
}.should raise_error(ArgumentError)
end
end
describe "for a subclass of Matrix" do
it "returns an instance of that subclass" do
MatrixSub.empty(0, 1).should be_an_instance_of(MatrixSub)
end end
end end

View file

@ -1,11 +1,14 @@
require_relative '../../spec_helper' require_relative '../../spec_helper'
require_relative 'shared/equal_value'
require 'matrix'
describe "Matrix#eql?" do ruby_version_is ""..."3.1" do
it_behaves_like :equal, :eql? require_relative 'shared/equal_value'
require 'matrix'
it "returns false if some elements are == but not eql?" do describe "Matrix#eql?" do
Matrix[[1, 2],[3, 4]].eql?(Matrix[[1, 2],[3, 4.0]]).should be_false it_behaves_like :equal, :eql?
it "returns false if some elements are == but not eql?" do
Matrix[[1, 2],[3, 4]].eql?(Matrix[[1, 2],[3, 4.0]]).should be_false
end
end end
end end

View file

@ -1,11 +1,14 @@
require_relative '../../spec_helper' require_relative '../../spec_helper'
require_relative 'shared/equal_value'
require 'matrix'
describe "Matrix#==" do ruby_version_is ""..."3.1" do
it_behaves_like :equal, :== require_relative 'shared/equal_value'
require 'matrix'
it "returns true if some elements are == but not eql?" do describe "Matrix#==" do
Matrix[[1, 2],[3, 4]].should == Matrix[[1, 2],[3, 4.0]] it_behaves_like :equal, :==
it "returns true if some elements are == but not eql?" do
Matrix[[1, 2],[3, 4]].should == Matrix[[1, 2],[3, 4.0]]
end
end end
end end

View file

@ -1,64 +1,67 @@
require_relative '../../spec_helper' require_relative '../../spec_helper'
require_relative 'fixtures/classes'
require 'matrix'
describe "Matrix#**" do ruby_version_is ""..."3.1" do
require_relative 'fixtures/classes'
require 'matrix'
describe "given an integer _n_" do describe "Matrix#**" do
it "multiples the Matrix by itself _n_ times" do
m = Matrix[ [7,6], [3,9] ]
(m ** 1).should == m
(m ** 2).should == Matrix[ [67, 96], [48,99] ]
(m ** 2).should == m * m
(m ** 3).should == m * m * m
(m ** 4).should == m * m * m * m
(m ** 5).should == m * m * m * m * m
end
it "raises a ErrDimensionMismatch for non square matrices" do describe "given an integer _n_" do
m = Matrix[ [1, 1], [1, 2], [2, 3]] it "multiples the Matrix by itself _n_ times" do
-> { m ** 3 }.should raise_error(Matrix::ErrDimensionMismatch) m = Matrix[ [7,6], [3,9] ]
-> { m ** 0 }.should raise_error(Matrix::ErrDimensionMismatch) (m ** 1).should == m
end (m ** 2).should == Matrix[ [67, 96], [48,99] ]
(m ** 2).should == m * m
describe "that is < 0" do (m ** 3).should == m * m * m
it "returns the inverse of **(-n)" do (m ** 4).should == m * m * m * m
m = Matrix[ [1, 1], [1, 2] ] (m ** 5).should == m * m * m * m * m
(m ** -2).should == Matrix[ [5, -3], [-3, 2]]
(m ** -4).should == (m.inverse ** 4)
end end
it "raises a ErrNotRegular for irregular matrices" do it "raises a ErrDimensionMismatch for non square matrices" do
m = Matrix[ [1, 1], [1, 1] ] m = Matrix[ [1, 1], [1, 2], [2, 3]]
-> { m ** -2 }.should raise_error(Matrix::ErrNotRegular) -> { m ** 3 }.should raise_error(Matrix::ErrDimensionMismatch)
-> { m ** 0 }.should raise_error(Matrix::ErrDimensionMismatch)
end end
end
ruby_version_is '3.1.0' do # https://bugs.ruby-lang.org/issues/17521 describe "that is < 0" do
describe "that is 0" do it "returns the inverse of **(-n)" do
it "returns the identity for square matrices" do m = Matrix[ [1, 1], [1, 2] ]
(m ** -2).should == Matrix[ [5, -3], [-3, 2]]
(m ** -4).should == (m.inverse ** 4)
end
it "raises a ErrNotRegular for irregular matrices" do
m = Matrix[ [1, 1], [1, 1] ] m = Matrix[ [1, 1], [1, 1] ]
(m ** 0).should == Matrix.identity(2) -> { m ** -2 }.should raise_error(Matrix::ErrNotRegular)
end end
end
it "raises an ErrDimensionMismatch for non-square matrices" do ruby_version_is '3.1.0' do # https://bugs.ruby-lang.org/issues/17521
m = Matrix[ [1, 1] ] describe "that is 0" do
-> { m ** 0 }.should raise_error(Matrix::ErrDimensionMismatch) it "returns the identity for square matrices" do
m = Matrix[ [1, 1], [1, 1] ]
(m ** 0).should == Matrix.identity(2)
end
it "raises an ErrDimensionMismatch for non-square matrices" do
m = Matrix[ [1, 1] ]
-> { m ** 0 }.should raise_error(Matrix::ErrDimensionMismatch)
end
end end
end end
end end
end
it "returns the power for non integer powers" do it "returns the power for non integer powers" do
a = Matrix[[5, 4], [4, 5]] a = Matrix[[5, 4], [4, 5]]
((a ** 0.5) ** 2).round(8).should == a ((a ** 0.5) ** 2).round(8).should == a
a = Matrix[[7, 10], [15, 22]] a = Matrix[[7, 10], [15, 22]]
((a ** 0.25) ** 4).round(8).should == a ((a ** 0.25) ** 4).round(8).should == a
end end
describe "for a subclass of Matrix" do describe "for a subclass of Matrix" do
it "returns an instance of that subclass" do it "returns an instance of that subclass" do
(MatrixSub.ins ** 1).should be_an_instance_of(MatrixSub) (MatrixSub.ins ** 1).should be_an_instance_of(MatrixSub)
end
end end
end end
end end

View file

@ -1,146 +1,149 @@
require_relative '../../spec_helper' require_relative '../../spec_helper'
require 'matrix'
describe "Matrix#find_index without any argument" do ruby_version_is ""..."3.1" do
before :all do require 'matrix'
@m = Matrix[ [1, 2, 3, 4], [5, 6, 7, 8] ]
end
it "returns an Enumerator when called without a block" do describe "Matrix#find_index without any argument" do
enum = @m.find_index before :all do
enum.should be_an_instance_of(Enumerator) @m = Matrix[ [1, 2, 3, 4], [5, 6, 7, 8] ]
enum.to_a.should == [1, 2, 3, 4, 5, 6, 7, 8] end
end
it "returns nil if the block is always false" do
@m.find_index{false}.should be_nil
end
it "returns the first index for which the block is true" do
@m.find_index{|x| x >= 3}.should == [0, 2]
end
end
describe "Matrix#find_index with a subselection argument" do
before :all do
@tests = [
[ Matrix[ [1, 2, 3, 4], [5, 6, 7, 8] ], {
diagonal: [1, 6] ,
off_diagonal: [2, 3, 4, 5, 7, 8],
lower: [1, 5, 6] ,
strict_lower: [5] ,
strict_upper: [2, 3, 4, 7, 8] ,
upper: [1, 2, 3, 4, 6, 7, 8] ,
}
],
[ Matrix[ [1, 2], [3, 4], [5, 6], [7, 8] ], {
diagonal: [1, 4] ,
off_diagonal: [2, 3, 5, 6, 7, 8],
lower: [1, 3, 4, 5, 6, 7, 8] ,
strict_lower: [3, 5, 6, 7, 8] ,
strict_upper: [2] ,
upper: [1, 2, 4] ,
}
]]
end
describe "and no generic argument" do
it "returns an Enumerator when called without a block" do it "returns an Enumerator when called without a block" do
@tests.each do |matrix, h| enum = @m.find_index
h.each do |selector, result| enum.should be_an_instance_of(Enumerator)
matrix.find_index(selector).should be_an_instance_of(Enumerator) enum.to_a.should == [1, 2, 3, 4, 5, 6, 7, 8]
end
end
end
it "yields the rights elements" do
@tests.each do |matrix, h|
h.each do |selector, result|
matrix.find_index(selector).to_a.should == result
end
end
end
it "returns the first index for which the block returns true" do
@tests.each do |matrix, h|
h.each do |selector, result|
cnt = result.size.div 2
which = result[cnt]
idx = matrix.find_index(selector){|x| cnt -= 1; x == which}
matrix[*idx].should == which
cnt.should == -1
end
end
end end
it "returns nil if the block is always false" do it "returns nil if the block is always false" do
@tests.each do |matrix, h| @m.find_index{false}.should be_nil
h.each do |selector, result| end
matrix.find_index(selector){ nil }.should == nil
it "returns the first index for which the block is true" do
@m.find_index{|x| x >= 3}.should == [0, 2]
end
end
describe "Matrix#find_index with a subselection argument" do
before :all do
@tests = [
[ Matrix[ [1, 2, 3, 4], [5, 6, 7, 8] ], {
diagonal: [1, 6] ,
off_diagonal: [2, 3, 4, 5, 7, 8],
lower: [1, 5, 6] ,
strict_lower: [5] ,
strict_upper: [2, 3, 4, 7, 8] ,
upper: [1, 2, 3, 4, 6, 7, 8] ,
}
],
[ Matrix[ [1, 2], [3, 4], [5, 6], [7, 8] ], {
diagonal: [1, 4] ,
off_diagonal: [2, 3, 5, 6, 7, 8],
lower: [1, 3, 4, 5, 6, 7, 8] ,
strict_lower: [3, 5, 6, 7, 8] ,
strict_upper: [2] ,
upper: [1, 2, 4] ,
}
]]
end
describe "and no generic argument" do
it "returns an Enumerator when called without a block" do
@tests.each do |matrix, h|
h.each do |selector, result|
matrix.find_index(selector).should be_an_instance_of(Enumerator)
end
end
end
it "yields the rights elements" do
@tests.each do |matrix, h|
h.each do |selector, result|
matrix.find_index(selector).to_a.should == result
end
end
end
it "returns the first index for which the block returns true" do
@tests.each do |matrix, h|
h.each do |selector, result|
cnt = result.size.div 2
which = result[cnt]
idx = matrix.find_index(selector){|x| cnt -= 1; x == which}
matrix[*idx].should == which
cnt.should == -1
end
end
end
it "returns nil if the block is always false" do
@tests.each do |matrix, h|
h.each do |selector, result|
matrix.find_index(selector){ nil }.should == nil
end
end
end
end
describe "and a generic argument" do
it "ignores a block" do
@m.find_index(42, :diagonal){raise "oups"}.should == nil
end
it "returns the index of the requested value" do
@tests.each do |matrix, h|
h.each do |selector, result|
cnt = result.size / 2
which = result[cnt]
idx = matrix.find_index(which, selector)
matrix[*idx].should == which
end
end
end
it "returns nil if the requested value is not found" do
@tests.each do |matrix, h|
h.each do |selector, result|
matrix.find_index(42, selector).should == nil
end
end end
end end
end end
end end
describe "and a generic argument" do describe "Matrix#find_index with only a generic argument" do
before :all do
@m = Matrix[ [1, 2, 3, 4], [1, 2, 3, 4] ]
end
it "returns nil if the value is not found" do
@m.find_index(42).should be_nil
end
it "returns the first index for of the requested value" do
@m.find_index(3).should == [0, 2]
end
it "ignores a block" do it "ignores a block" do
@m.find_index(42, :diagonal){raise "oups"}.should == nil @m.find_index(4){raise "oups"}.should == [0, 3]
end
it "returns the index of the requested value" do
@tests.each do |matrix, h|
h.each do |selector, result|
cnt = result.size / 2
which = result[cnt]
idx = matrix.find_index(which, selector)
matrix[*idx].should == which
end
end
end
it "returns nil if the requested value is not found" do
@tests.each do |matrix, h|
h.each do |selector, result|
matrix.find_index(42, selector).should == nil
end
end
end end
end end
end describe "Matrix#find_index with two arguments" do
it "raises an ArgumentError for an unrecognized last argument" do
describe "Matrix#find_index with only a generic argument" do -> {
before :all do @m.find_index(1, "all"){}
@m = Matrix[ [1, 2, 3, 4], [1, 2, 3, 4] ] }.should raise_error(ArgumentError)
end -> {
@m.find_index(1, nil){}
it "returns nil if the value is not found" do }.should raise_error(ArgumentError)
@m.find_index(42).should be_nil -> {
end @m.find_index(1, :left){}
}.should raise_error(ArgumentError)
it "returns the first index for of the requested value" do -> {
@m.find_index(3).should == [0, 2] @m.find_index(:diagonal, 1){}
end }.should raise_error(ArgumentError)
end
it "ignores a block" do
@m.find_index(4){raise "oups"}.should == [0, 3]
end
end
describe "Matrix#find_index with two arguments" do
it "raises an ArgumentError for an unrecognized last argument" do
-> {
@m.find_index(1, "all"){}
}.should raise_error(ArgumentError)
-> {
@m.find_index(1, nil){}
}.should raise_error(ArgumentError)
-> {
@m.find_index(1, :left){}
}.should raise_error(ArgumentError)
-> {
@m.find_index(:diagonal, 1){}
}.should raise_error(ArgumentError)
end end
end end

View file

@ -1,15 +1,18 @@
require_relative '../../spec_helper' require_relative '../../spec_helper'
require 'matrix'
describe "Matrix#hash" do ruby_version_is ""..."3.1" do
require 'matrix'
describe "Matrix#hash" do
it "returns an Integer" do
Matrix[ [1,2] ].hash.should be_an_instance_of(Integer)
end
it "returns the same value for the same matrix" do
data = [ [40,5], [2,7] ]
Matrix[ *data ].hash.should == Matrix[ *data ].hash
end
it "returns an Integer" do
Matrix[ [1,2] ].hash.should be_an_instance_of(Integer)
end end
it "returns the same value for the same matrix" do
data = [ [40,5], [2,7] ]
Matrix[ *data ].hash.should == Matrix[ *data ].hash
end
end end

View file

@ -1,34 +1,37 @@
require_relative '../../spec_helper' require_relative '../../spec_helper'
require 'matrix'
describe "Matrix.hermitian?" do ruby_version_is ""..."3.1" do
it "returns true for a hermitian Matrix" do require 'matrix'
Matrix[[1, 2, Complex(0, 3)], [2, 4, 5], [Complex(0, -3), 5, 6]].hermitian?.should be_true
end
it "returns true for a 0x0 empty matrix" do describe "Matrix.hermitian?" do
Matrix.empty.hermitian?.should be_true it "returns true for a hermitian Matrix" do
end Matrix[[1, 2, Complex(0, 3)], [2, 4, 5], [Complex(0, -3), 5, 6]].hermitian?.should be_true
end
it "returns false for an asymmetric Matrix" do it "returns true for a 0x0 empty matrix" do
Matrix[[1, 2],[-2, 1]].hermitian?.should be_false Matrix.empty.hermitian?.should be_true
end end
it "raises an error for rectangular matrices" do it "returns false for an asymmetric Matrix" do
[ Matrix[[1, 2],[-2, 1]].hermitian?.should be_false
Matrix[[0], [0]], end
Matrix[[0, 0]],
Matrix.empty(0, 2), it "raises an error for rectangular matrices" do
Matrix.empty(2, 0), [
].each do |rectangular_matrix| Matrix[[0], [0]],
-> { Matrix[[0, 0]],
rectangular_matrix.hermitian? Matrix.empty(0, 2),
}.should raise_error(Matrix::ErrDimensionMismatch) Matrix.empty(2, 0),
].each do |rectangular_matrix|
-> {
rectangular_matrix.hermitian?
}.should raise_error(Matrix::ErrDimensionMismatch)
end
end
it "returns false for a matrix with complex values on the diagonal" do
Matrix[[Complex(1,1)]].hermitian?.should be_false
Matrix[[Complex(1,0)]].hermitian?.should be_true
end end
end end
it "returns false for a matrix with complex values on the diagonal" do
Matrix[[Complex(1,1)]].hermitian?.should be_false
Matrix[[Complex(1,0)]].hermitian?.should be_true
end
end end

View file

@ -1,6 +1,9 @@
require_relative '../../spec_helper' require_relative '../../spec_helper'
require_relative 'shared/identity'
describe "Matrix.identity" do ruby_version_is ""..."3.1" do
it_behaves_like :matrix_identity, :identity require_relative 'shared/identity'
describe "Matrix.identity" do
it_behaves_like :matrix_identity, :identity
end
end end

View file

@ -1,6 +1,9 @@
require_relative '../../spec_helper' require_relative '../../spec_helper'
require_relative 'shared/imaginary'
describe "Matrix#imag" do ruby_version_is ""..."3.1" do
it_behaves_like :matrix_imaginary, :imag require_relative 'shared/imaginary'
describe "Matrix#imag" do
it_behaves_like :matrix_imaginary, :imag
end
end end

View file

@ -1,6 +1,9 @@
require_relative '../../spec_helper' require_relative '../../spec_helper'
require_relative 'shared/imaginary'
describe "Matrix#imaginary" do ruby_version_is ""..."3.1" do
it_behaves_like :matrix_imaginary, :imaginary require_relative 'shared/imaginary'
describe "Matrix#imaginary" do
it_behaves_like :matrix_imaginary, :imaginary
end
end end

View file

@ -1,27 +1,30 @@
require_relative '../../spec_helper' require_relative '../../spec_helper'
require_relative 'fixtures/classes'
require 'matrix'
describe "Matrix#inspect" do ruby_version_is ""..."3.1" do
require_relative 'fixtures/classes'
require 'matrix'
it "returns a stringified representation of the Matrix" do describe "Matrix#inspect" do
Matrix[ [1,2], [2,1] ].inspect.should == "Matrix[[1, 2], [2, 1]]"
end
it "returns 'Matrix.empty(...)' for empty matrices" do it "returns a stringified representation of the Matrix" do
Matrix[ [], [], [] ].inspect.should == "Matrix.empty(3, 0)" Matrix[ [1,2], [2,1] ].inspect.should == "Matrix[[1, 2], [2, 1]]"
Matrix.columns([ [], [], [] ]).inspect.should == "Matrix.empty(0, 3)" end
end
it "calls inspect on its contents" do it "returns 'Matrix.empty(...)' for empty matrices" do
obj = mock("some_value") Matrix[ [], [], [] ].inspect.should == "Matrix.empty(3, 0)"
obj.should_receive(:inspect).and_return("some_value") Matrix.columns([ [], [], [] ]).inspect.should == "Matrix.empty(0, 3)"
Matrix[ [1, 2], [3, obj] ].inspect.should == "Matrix[[1, 2], [3, some_value]]" end
end
describe "for a subclass of Matrix" do it "calls inspect on its contents" do
it "returns a string using the subclass' name" do obj = mock("some_value")
MatrixSub.ins.inspect.should == "MatrixSub[[1, 0], [0, 1]]" obj.should_receive(:inspect).and_return("some_value")
Matrix[ [1, 2], [3, obj] ].inspect.should == "Matrix[[1, 2], [3, some_value]]"
end
describe "for a subclass of Matrix" do
it "returns a string using the subclass' name" do
MatrixSub.ins.inspect.should == "MatrixSub[[1, 0], [0, 1]]"
end
end end
end end
end end

View file

@ -1,7 +1,10 @@
require_relative '../../spec_helper' require_relative '../../spec_helper'
require_relative 'spec_helper'
require_relative 'shared/inverse'
describe "Matrix#inv" do ruby_version_is ""..."3.1" do
it_behaves_like :inverse, :inv require_relative 'spec_helper'
require_relative 'shared/inverse'
describe "Matrix#inv" do
it_behaves_like :inverse, :inv
end
end end

View file

@ -1,6 +1,9 @@
require_relative '../../spec_helper' require_relative '../../spec_helper'
require 'matrix'
describe "Matrix#inverse_from" do ruby_version_is ""..."3.1" do
it "needs to be reviewed for spec completeness" require 'matrix'
describe "Matrix#inverse_from" do
it "needs to be reviewed for spec completeness"
end
end end

View file

@ -1,7 +1,10 @@
require_relative '../../spec_helper' require_relative '../../spec_helper'
require_relative 'spec_helper'
require_relative 'shared/inverse'
describe "Matrix#inverse" do ruby_version_is ""..."3.1" do
it_behaves_like :inverse, :inverse require_relative 'spec_helper'
require_relative 'shared/inverse'
describe "Matrix#inverse" do
it_behaves_like :inverse, :inverse
end
end end

View file

@ -1,24 +1,27 @@
require_relative '../../spec_helper' require_relative '../../spec_helper'
require 'matrix'
describe "Matrix.lower_triangular?" do ruby_version_is ""..."3.1" do
it "returns true for a square lower triangular Matrix" do require 'matrix'
Matrix[[1, 0, 0], [1, 2, 0], [1, 2, 3]].lower_triangular?.should be_true
Matrix.diagonal([1, 2, 3]).lower_triangular?.should be_true
Matrix[[1, 0], [1, 2], [1, 2], [1, 2]].lower_triangular?.should be_true
Matrix[[1, 0, 0, 0], [1, 2, 0, 0]].lower_triangular?.should be_true
end
it "returns true for an empty Matrix" do describe "Matrix.lower_triangular?" do
Matrix.empty(3, 0).lower_triangular?.should be_true it "returns true for a square lower triangular Matrix" do
Matrix.empty(0, 3).lower_triangular?.should be_true Matrix[[1, 0, 0], [1, 2, 0], [1, 2, 3]].lower_triangular?.should be_true
Matrix.empty(0, 0).lower_triangular?.should be_true Matrix.diagonal([1, 2, 3]).lower_triangular?.should be_true
end Matrix[[1, 0], [1, 2], [1, 2], [1, 2]].lower_triangular?.should be_true
Matrix[[1, 0, 0, 0], [1, 2, 0, 0]].lower_triangular?.should be_true
end
it "returns false for a non lower triangular square Matrix" do it "returns true for an empty Matrix" do
Matrix[[0, 1], [0, 0]].lower_triangular?.should be_false Matrix.empty(3, 0).lower_triangular?.should be_true
Matrix[[1, 2, 3], [1, 2, 3], [1, 2, 3]].lower_triangular?.should be_false Matrix.empty(0, 3).lower_triangular?.should be_true
Matrix[[0, 1], [0, 0], [0, 0], [0, 0]].lower_triangular?.should be_false Matrix.empty(0, 0).lower_triangular?.should be_true
Matrix[[0, 0, 0, 1], [0, 0, 0, 0]].lower_triangular?.should be_false end
it "returns false for a non lower triangular square Matrix" do
Matrix[[0, 1], [0, 0]].lower_triangular?.should be_false
Matrix[[1, 2, 3], [1, 2, 3], [1, 2, 3]].lower_triangular?.should be_false
Matrix[[0, 1], [0, 0], [0, 0], [0, 0]].lower_triangular?.should be_false
Matrix[[0, 0, 0, 1], [0, 0, 0, 0]].lower_triangular?.should be_false
end
end end
end end

View file

@ -1,21 +1,24 @@
require_relative '../../../spec_helper' require_relative '../../../spec_helper'
require 'matrix'
describe "Matrix::LUPDecomposition#determinant" do ruby_version_is ""..."3.1" do
it "returns the determinant when the matrix is square" do require 'matrix'
a = Matrix[[7, 8, 9], [14, 46, 51], [28, 82, 163]]
a.lup.determinant.should == 15120 # == a.determinant
end
it "raises an error for rectangular matrices" do describe "Matrix::LUPDecomposition#determinant" do
[ it "returns the determinant when the matrix is square" do
Matrix[[7, 8, 9], [14, 46, 51]], a = Matrix[[7, 8, 9], [14, 46, 51], [28, 82, 163]]
Matrix[[7, 8], [14, 46], [28, 82]], a.lup.determinant.should == 15120 # == a.determinant
].each do |m| end
lup = m.lup
-> { it "raises an error for rectangular matrices" do
lup.determinant [
}.should raise_error(Matrix::ErrDimensionMismatch) Matrix[[7, 8, 9], [14, 46, 51]],
Matrix[[7, 8], [14, 46], [28, 82]],
].each do |m|
lup = m.lup
-> {
lup.determinant
}.should raise_error(Matrix::ErrDimensionMismatch)
end
end end
end end
end end

View file

@ -1,13 +1,16 @@
require_relative '../../../spec_helper' require_relative '../../../spec_helper'
require 'matrix'
describe "Matrix::LUPDecomposition#initialize" do ruby_version_is ""..."3.1" do
it "raises an error if argument is not a matrix" do require 'matrix'
-> {
Matrix::LUPDecomposition.new([[]]) describe "Matrix::LUPDecomposition#initialize" do
}.should raise_error(TypeError) it "raises an error if argument is not a matrix" do
-> { -> {
Matrix::LUPDecomposition.new(42) Matrix::LUPDecomposition.new([[]])
}.should raise_error(TypeError) }.should raise_error(TypeError)
-> {
Matrix::LUPDecomposition.new(42)
}.should raise_error(TypeError)
end
end end
end end

View file

@ -1,18 +1,21 @@
require_relative '../../../spec_helper' require_relative '../../../spec_helper'
require 'matrix'
describe "Matrix::LUPDecomposition#l" do ruby_version_is ""..."3.1" do
before :each do require 'matrix'
@a = Matrix[[7, 8, 9], [14, 46, 51], [28, 82, 163]]
@lu = Matrix::LUPDecomposition.new(@a)
@l = @lu.l
end
it "returns the first element of to_a" do describe "Matrix::LUPDecomposition#l" do
@l.should == @lu.to_a[0] before :each do
end @a = Matrix[[7, 8, 9], [14, 46, 51], [28, 82, 163]]
@lu = Matrix::LUPDecomposition.new(@a)
@l = @lu.l
end
it "returns a lower triangular matrix" do it "returns the first element of to_a" do
@l.lower_triangular?.should be_true @l.should == @lu.to_a[0]
end
it "returns a lower triangular matrix" do
@l.lower_triangular?.should be_true
end
end end
end end

View file

@ -1,18 +1,21 @@
require_relative '../../../spec_helper' require_relative '../../../spec_helper'
require 'matrix'
describe "Matrix::LUPDecomposition#p" do ruby_version_is ""..."3.1" do
before :each do require 'matrix'
@a = Matrix[[7, 8, 9], [14, 46, 51], [28, 82, 163]]
@lu = Matrix::LUPDecomposition.new(@a)
@p = @lu.p
end
it "returns the third element of to_a" do describe "Matrix::LUPDecomposition#p" do
@p.should == @lu.to_a[2] before :each do
end @a = Matrix[[7, 8, 9], [14, 46, 51], [28, 82, 163]]
@lu = Matrix::LUPDecomposition.new(@a)
@p = @lu.p
end
it "returns a permutation matrix" do it "returns the third element of to_a" do
@p.permutation?.should be_true @p.should == @lu.to_a[2]
end
it "returns a permutation matrix" do
@p.permutation?.should be_true
end
end end
end end

View file

@ -1,52 +1,55 @@
require_relative '../../../spec_helper' require_relative '../../../spec_helper'
require 'matrix'
describe "Matrix::LUPDecomposition#solve" do ruby_version_is ""..."3.1" do
describe "for rectangular matrices" do require 'matrix'
it "raises an error for singular matrices" do
a = Matrix[[1, 2, 3], [1, 3, 5], [2, 5, 8]]
lu = Matrix::LUPDecomposition.new(a)
-> {
lu.solve(a)
}.should raise_error(Matrix::ErrNotRegular)
end
describe "for non singular matrices" do describe "Matrix::LUPDecomposition#solve" do
before :each do describe "for rectangular matrices" do
@a = Matrix[[7, 8, 9], [14, 46, 51], [28, 82, 163]] it "raises an error for singular matrices" do
@lu = Matrix::LUPDecomposition.new(@a) a = Matrix[[1, 2, 3], [1, 3, 5], [2, 5, 8]]
end lu = Matrix::LUPDecomposition.new(a)
it "returns the appropriate empty matrix when given an empty matrix" do
@lu.solve(Matrix.empty(3,0)).should == Matrix.empty(3,0)
empty = Matrix::LUPDecomposition.new(Matrix.empty(0, 0))
empty.solve(Matrix.empty(0,3)).should == Matrix.empty(0,3)
end
it "returns the right matrix when given a matrix of the appropriate size" do
solution = Matrix[[1, 2, 3, 4], [0, 1, 2, 3], [-1, -2, -3, -4]]
values = Matrix[[-2, 4, 10, 16], [-37, -28, -19, -10], [-135, -188, -241, -294]] # == @a * solution
@lu.solve(values).should == solution
end
it "raises an error when given a matrix of the wrong size" do
values = Matrix[[1, 2, 3, 4], [0, 1, 2, 3]]
-> { -> {
@lu.solve(values) lu.solve(a)
}.should raise_error(Matrix::ErrDimensionMismatch) }.should raise_error(Matrix::ErrNotRegular)
end end
it "returns the right vector when given a vector of the appropriate size" do describe "for non singular matrices" do
solution = Vector[1, 2, -1] before :each do
values = Vector[14, 55, 29] # == @a * solution @a = Matrix[[7, 8, 9], [14, 46, 51], [28, 82, 163]]
@lu.solve(values).should == solution @lu = Matrix::LUPDecomposition.new(@a)
end end
it "raises an error when given a vector of the wrong size" do it "returns the appropriate empty matrix when given an empty matrix" do
values = Vector[14, 55] @lu.solve(Matrix.empty(3,0)).should == Matrix.empty(3,0)
-> { empty = Matrix::LUPDecomposition.new(Matrix.empty(0, 0))
@lu.solve(values) empty.solve(Matrix.empty(0,3)).should == Matrix.empty(0,3)
}.should raise_error(Matrix::ErrDimensionMismatch) end
it "returns the right matrix when given a matrix of the appropriate size" do
solution = Matrix[[1, 2, 3, 4], [0, 1, 2, 3], [-1, -2, -3, -4]]
values = Matrix[[-2, 4, 10, 16], [-37, -28, -19, -10], [-135, -188, -241, -294]] # == @a * solution
@lu.solve(values).should == solution
end
it "raises an error when given a matrix of the wrong size" do
values = Matrix[[1, 2, 3, 4], [0, 1, 2, 3]]
-> {
@lu.solve(values)
}.should raise_error(Matrix::ErrDimensionMismatch)
end
it "returns the right vector when given a vector of the appropriate size" do
solution = Vector[1, 2, -1]
values = Vector[14, 55, 29] # == @a * solution
@lu.solve(values).should == solution
end
it "raises an error when given a vector of the wrong size" do
values = Vector[14, 55]
-> {
@lu.solve(values)
}.should raise_error(Matrix::ErrDimensionMismatch)
end
end end
end end
end end

View file

@ -1,33 +1,36 @@
require_relative '../../../spec_helper' require_relative '../../../spec_helper'
require 'matrix'
describe "Matrix::LUPDecomposition#to_a" do ruby_version_is ""..."3.1" do
before :each do require 'matrix'
@a = Matrix[[7, 8, 9], [14, 46, 51], [28, 82, 163]]
@lu = Matrix::LUPDecomposition.new(@a)
@to_a = @lu.to_a
@l, @u, @p = @to_a
end
it "returns an array of three matrices" do describe "Matrix::LUPDecomposition#to_a" do
@to_a.should be_kind_of(Array) before :each do
@to_a.length.should == 3 @a = Matrix[[7, 8, 9], [14, 46, 51], [28, 82, 163]]
@to_a.each{|m| m.should be_kind_of(Matrix)} @lu = Matrix::LUPDecomposition.new(@a)
end @to_a = @lu.to_a
@l, @u, @p = @to_a
it "returns [l, u, p] such that l*u == a*p" do
(@l * @u).should == (@p * @a)
end
it "returns the right values for rectangular matrices" do
[
Matrix[[7, 8, 9], [14, 46, 51]],
Matrix[[4, 11], [5, 8], [3, 4]],
].each do |a|
l, u, p = Matrix::LUPDecomposition.new(a).to_a
(l * u).should == (p * a)
end end
end
it "has other properties implied by the specs of #l, #u and #p" it "returns an array of three matrices" do
@to_a.should be_kind_of(Array)
@to_a.length.should == 3
@to_a.each{|m| m.should be_kind_of(Matrix)}
end
it "returns [l, u, p] such that l*u == a*p" do
(@l * @u).should == (@p * @a)
end
it "returns the right values for rectangular matrices" do
[
Matrix[[7, 8, 9], [14, 46, 51]],
Matrix[[4, 11], [5, 8], [3, 4]],
].each do |a|
l, u, p = Matrix::LUPDecomposition.new(a).to_a
(l * u).should == (p * a)
end
end
it "has other properties implied by the specs of #l, #u and #p"
end
end end

View file

@ -1,18 +1,21 @@
require_relative '../../../spec_helper' require_relative '../../../spec_helper'
require 'matrix'
describe "Matrix::LUPDecomposition#u" do ruby_version_is ""..."3.1" do
before :each do require 'matrix'
@a = Matrix[[7, 8, 9], [14, 46, 51], [28, 82, 163]]
@lu = Matrix::LUPDecomposition.new(@a)
@u = @lu.u
end
it "returns the second element of to_a" do describe "Matrix::LUPDecomposition#u" do
@u.should == @lu.to_a[1] before :each do
end @a = Matrix[[7, 8, 9], [14, 46, 51], [28, 82, 163]]
@lu = Matrix::LUPDecomposition.new(@a)
@u = @lu.u
end
it "returns an upper triangular matrix" do it "returns the second element of to_a" do
@u.upper_triangular?.should be_true @u.should == @lu.to_a[1]
end
it "returns an upper triangular matrix" do
@u.upper_triangular?.should be_true
end
end end
end end

View file

@ -1,6 +1,9 @@
require_relative '../../spec_helper' require_relative '../../spec_helper'
require_relative 'shared/collect'
describe "Matrix#map" do ruby_version_is ""..."3.1" do
it_behaves_like :collect, :map require_relative 'shared/collect'
describe "Matrix#map" do
it_behaves_like :collect, :map
end
end end

View file

@ -1,85 +1,88 @@
require_relative '../../spec_helper' require_relative '../../spec_helper'
require_relative 'fixtures/classes'
require 'matrix'
describe "Matrix#minor" do ruby_version_is ""..."3.1" do
before :each do require_relative 'fixtures/classes'
@matrix = Matrix[ [1,2], [3,4], [5,6] ] require 'matrix'
end
describe "with start_row, nrows, start_col, ncols" do describe "Matrix#minor" do
it "returns the given portion of the Matrix" do before :each do
@matrix.minor(0,1,0,2).should == Matrix[ [1, 2] ] @matrix = Matrix[ [1,2], [3,4], [5,6] ]
@matrix.minor(1,2,1,1).should == Matrix[ [4], [6] ]
end end
it "returns an empty Matrix if nrows or ncols is 0" do describe "with start_row, nrows, start_col, ncols" do
@matrix.minor(0,0,0,0).should == Matrix[] it "returns the given portion of the Matrix" do
@matrix.minor(1,0,1,0).should == Matrix[] @matrix.minor(0,1,0,2).should == Matrix[ [1, 2] ]
@matrix.minor(1,0,1,1).should == Matrix.columns([[]]) @matrix.minor(1,2,1,1).should == Matrix[ [4], [6] ]
@matrix.minor(1,1,1,0).should == Matrix[[]] end
it "returns an empty Matrix if nrows or ncols is 0" do
@matrix.minor(0,0,0,0).should == Matrix[]
@matrix.minor(1,0,1,0).should == Matrix[]
@matrix.minor(1,0,1,1).should == Matrix.columns([[]])
@matrix.minor(1,1,1,0).should == Matrix[[]]
end
it "returns nil for out-of-bounds start_row/col" do
r = @matrix.row_size + 1
c = @matrix.column_size + 1
@matrix.minor(r,0,0,10).should == nil
@matrix.minor(0,10,c,9).should == nil
@matrix.minor(-r,0,0,10).should == nil
@matrix.minor(0,10,-c,9).should == nil
end
it "returns nil for negative nrows or ncols" do
@matrix.minor(0,1,0,-1).should == nil
@matrix.minor(0,-1,0,1).should == nil
end
it "start counting backwards for start_row or start_col below zero" do
@matrix.minor(0, 1, -1, 1).should == @matrix.minor(0, 1, 1, 1)
@matrix.minor(-1, 1, 0, 1).should == @matrix.minor(2, 1, 0, 1)
end
it "returns empty matrices for extreme start_row/col" do
@matrix.minor(3,10,1,10).should == Matrix.columns([[]])
@matrix.minor(1,10,2,10).should == Matrix[[], []]
@matrix.minor(3,0,0,10).should == Matrix.columns([[], []])
end
it "ignores big nrows or ncols" do
@matrix.minor(0,1,0,20).should == Matrix[ [1, 2] ]
@matrix.minor(1,20,1,1).should == Matrix[ [4], [6] ]
end
end end
it "returns nil for out-of-bounds start_row/col" do describe "with col_range, row_range" do
r = @matrix.row_size + 1 it "returns the given portion of the Matrix" do
c = @matrix.column_size + 1 @matrix.minor(0..0, 0..1).should == Matrix[ [1, 2] ]
@matrix.minor(r,0,0,10).should == nil @matrix.minor(1..2, 1..2).should == Matrix[ [4], [6] ]
@matrix.minor(0,10,c,9).should == nil @matrix.minor(1...3, 1...3).should == Matrix[ [4], [6] ]
@matrix.minor(-r,0,0,10).should == nil end
@matrix.minor(0,10,-c,9).should == nil
it "returns nil if col_range or row_range is out of range" do
r = @matrix.row_size + 1
c = @matrix.column_size + 1
@matrix.minor(r..6, c..6).should == nil
@matrix.minor(0..1, c..6).should == nil
@matrix.minor(r..6, 0..1).should == nil
@matrix.minor(-r..6, -c..6).should == nil
@matrix.minor(0..1, -c..6).should == nil
@matrix.minor(-r..6, 0..1).should == nil
end
it "start counting backwards for col_range or row_range below zero" do
@matrix.minor(0..1, -2..-1).should == @matrix.minor(0..1, 0..1)
@matrix.minor(0..1, -2..1).should == @matrix.minor(0..1, 0..1)
@matrix.minor(-2..-1, 0..1).should == @matrix.minor(1..2, 0..1)
@matrix.minor(-2..2, 0..1).should == @matrix.minor(1..2, 0..1)
end
end end
it "returns nil for negative nrows or ncols" do describe "for a subclass of Matrix" do
@matrix.minor(0,1,0,-1).should == nil it "returns an instance of that subclass" do
@matrix.minor(0,-1,0,1).should == nil MatrixSub.ins.minor(0, 1, 0, 1).should be_an_instance_of(MatrixSub)
end end
it "start counting backwards for start_row or start_col below zero" do
@matrix.minor(0, 1, -1, 1).should == @matrix.minor(0, 1, 1, 1)
@matrix.minor(-1, 1, 0, 1).should == @matrix.minor(2, 1, 0, 1)
end
it "returns empty matrices for extreme start_row/col" do
@matrix.minor(3,10,1,10).should == Matrix.columns([[]])
@matrix.minor(1,10,2,10).should == Matrix[[], []]
@matrix.minor(3,0,0,10).should == Matrix.columns([[], []])
end
it "ignores big nrows or ncols" do
@matrix.minor(0,1,0,20).should == Matrix[ [1, 2] ]
@matrix.minor(1,20,1,1).should == Matrix[ [4], [6] ]
end
end
describe "with col_range, row_range" do
it "returns the given portion of the Matrix" do
@matrix.minor(0..0, 0..1).should == Matrix[ [1, 2] ]
@matrix.minor(1..2, 1..2).should == Matrix[ [4], [6] ]
@matrix.minor(1...3, 1...3).should == Matrix[ [4], [6] ]
end
it "returns nil if col_range or row_range is out of range" do
r = @matrix.row_size + 1
c = @matrix.column_size + 1
@matrix.minor(r..6, c..6).should == nil
@matrix.minor(0..1, c..6).should == nil
@matrix.minor(r..6, 0..1).should == nil
@matrix.minor(-r..6, -c..6).should == nil
@matrix.minor(0..1, -c..6).should == nil
@matrix.minor(-r..6, 0..1).should == nil
end
it "start counting backwards for col_range or row_range below zero" do
@matrix.minor(0..1, -2..-1).should == @matrix.minor(0..1, 0..1)
@matrix.minor(0..1, -2..1).should == @matrix.minor(0..1, 0..1)
@matrix.minor(-2..-1, 0..1).should == @matrix.minor(1..2, 0..1)
@matrix.minor(-2..2, 0..1).should == @matrix.minor(1..2, 0..1)
end
end
describe "for a subclass of Matrix" do
it "returns an instance of that subclass" do
MatrixSub.ins.minor(0, 1, 0, 1).should be_an_instance_of(MatrixSub)
end end
end end
end end

View file

@ -1,42 +1,45 @@
require_relative '../../spec_helper' require_relative '../../spec_helper'
require_relative 'fixtures/classes'
require 'matrix'
describe "Matrix#-" do ruby_version_is ""..."3.1" do
before :each do require_relative 'fixtures/classes'
@a = Matrix[ [1, 2], [3, 4] ] require 'matrix'
@b = Matrix[ [4, 5], [6, 7] ]
end
it "returns the result of subtracting the corresponding elements of other from self" do describe "Matrix#-" do
(@a - @b).should == Matrix[ [-3,-3], [-3,-3] ] before :each do
end @a = Matrix[ [1, 2], [3, 4] ]
@b = Matrix[ [4, 5], [6, 7] ]
end
it "returns an instance of Matrix" do it "returns the result of subtracting the corresponding elements of other from self" do
(@a - @b).should be_kind_of(Matrix) (@a - @b).should == Matrix[ [-3,-3], [-3,-3] ]
end end
it "raises a Matrix::ErrDimensionMismatch if the matrices are different sizes" do it "returns an instance of Matrix" do
-> { @a - Matrix[ [1] ] }.should raise_error(Matrix::ErrDimensionMismatch) (@a - @b).should be_kind_of(Matrix)
end end
it "raises a ExceptionForMatrix::ErrOperationNotDefined if other is a Numeric Type" do it "raises a Matrix::ErrDimensionMismatch if the matrices are different sizes" do
-> { @a - 2 }.should raise_error(Matrix::ErrOperationNotDefined) -> { @a - Matrix[ [1] ] }.should raise_error(Matrix::ErrDimensionMismatch)
-> { @a - 1.2 }.should raise_error(Matrix::ErrOperationNotDefined) end
-> { @a - bignum_value }.should raise_error(Matrix::ErrOperationNotDefined)
end
it "raises a TypeError if other is of wrong type" do it "raises a ExceptionForMatrix::ErrOperationNotDefined if other is a Numeric Type" do
-> { @a - nil }.should raise_error(TypeError) -> { @a - 2 }.should raise_error(Matrix::ErrOperationNotDefined)
-> { @a - "a" }.should raise_error(TypeError) -> { @a - 1.2 }.should raise_error(Matrix::ErrOperationNotDefined)
-> { @a - [ [1, 2] ] }.should raise_error(TypeError) -> { @a - bignum_value }.should raise_error(Matrix::ErrOperationNotDefined)
-> { @a - Object.new }.should raise_error(TypeError) end
end
describe "for a subclass of Matrix" do it "raises a TypeError if other is of wrong type" do
it "returns an instance of that subclass" do -> { @a - nil }.should raise_error(TypeError)
m = MatrixSub.ins -> { @a - "a" }.should raise_error(TypeError)
(m-m).should be_an_instance_of(MatrixSub) -> { @a - [ [1, 2] ] }.should raise_error(TypeError)
-> { @a - Object.new }.should raise_error(TypeError)
end
describe "for a subclass of Matrix" do
it "returns an instance of that subclass" do
m = MatrixSub.ins
(m-m).should be_an_instance_of(MatrixSub)
end
end end
end end
end end

View file

@ -1,68 +1,71 @@
require_relative '../../spec_helper' require_relative '../../spec_helper'
require_relative 'fixtures/classes'
require 'matrix'
describe "Matrix#*" do ruby_version_is ""..."3.1" do
before :each do require_relative 'fixtures/classes'
@a = Matrix[ [1, 2], [3, 4] ] require 'matrix'
@b = Matrix[ [4, 5], [6, 7] ]
end
it "returns the result of multiplying the corresponding elements of self and a Matrix" do describe "Matrix#*" do
(@a * @b).should == Matrix[ [16,19], [36,43] ] before :each do
end @a = Matrix[ [1, 2], [3, 4] ]
@b = Matrix[ [4, 5], [6, 7] ]
end
it "returns the result of multiplying the corresponding elements of self and a Vector" do it "returns the result of multiplying the corresponding elements of self and a Matrix" do
(@a * Vector[1,2]).should == Vector[5, 11] (@a * @b).should == Matrix[ [16,19], [36,43] ]
end end
it "returns the result of multiplying the elements of self and a Fixnum" do it "returns the result of multiplying the corresponding elements of self and a Vector" do
(@a * 2).should == Matrix[ [2, 4], [6, 8] ] (@a * Vector[1,2]).should == Vector[5, 11]
end end
it "returns the result of multiplying the elements of self and a Bignum" do it "returns the result of multiplying the elements of self and a Fixnum" do
(@a * bignum_value).should == Matrix[ (@a * 2).should == Matrix[ [2, 4], [6, 8] ]
[9223372036854775808, 18446744073709551616], end
[27670116110564327424, 36893488147419103232]
]
end
it "returns the result of multiplying the elements of self and a Float" do it "returns the result of multiplying the elements of self and a Bignum" do
(@a * 2.0).should == Matrix[ [2.0, 4.0], [6.0, 8.0] ] (@a * bignum_value).should == Matrix[
end [9223372036854775808, 18446744073709551616],
[27670116110564327424, 36893488147419103232]
]
end
it "raises a Matrix::ErrDimensionMismatch if the matrices are different sizes" do it "returns the result of multiplying the elements of self and a Float" do
-> { @a * Matrix[ [1] ] }.should raise_error(Matrix::ErrDimensionMismatch) (@a * 2.0).should == Matrix[ [2.0, 4.0], [6.0, 8.0] ]
end end
it "returns a zero matrix if (nx0) * (0xn)" do it "raises a Matrix::ErrDimensionMismatch if the matrices are different sizes" do
(Matrix[[],[],[]] * Matrix.columns([[],[],[]])).should == Matrix.zero(3) -> { @a * Matrix[ [1] ] }.should raise_error(Matrix::ErrDimensionMismatch)
end end
it "returns an empty matrix if (0xn) * (nx0)" do it "returns a zero matrix if (nx0) * (0xn)" do
(Matrix.columns([[],[],[]]) * Matrix[[],[],[]]).should == Matrix[] (Matrix[[],[],[]] * Matrix.columns([[],[],[]])).should == Matrix.zero(3)
end end
it "returns a mx0 matrix if (mxn) * (nx0)" do it "returns an empty matrix if (0xn) * (nx0)" do
(Matrix[[1,2],[3,4],[5,6]] * Matrix[[],[]]).should == Matrix[[],[],[]] (Matrix.columns([[],[],[]]) * Matrix[[],[],[]]).should == Matrix[]
end end
it "returns a 0xm matrix if (0xm) * (mxn)" do it "returns a mx0 matrix if (mxn) * (nx0)" do
(Matrix.columns([[], [], []]) * Matrix[[1,2],[3,4],[5,6]]).should == Matrix.columns([[],[]]) (Matrix[[1,2],[3,4],[5,6]] * Matrix[[],[]]).should == Matrix[[],[],[]]
end end
it "raises a TypeError if other is of wrong type" do it "returns a 0xm matrix if (0xm) * (mxn)" do
-> { @a * nil }.should raise_error(TypeError) (Matrix.columns([[], [], []]) * Matrix[[1,2],[3,4],[5,6]]).should == Matrix.columns([[],[]])
-> { @a * "a" }.should raise_error(TypeError) end
-> { @a * [ [1, 2] ] }.should raise_error(TypeError)
-> { @a * Object.new }.should raise_error(TypeError)
end
describe "for a subclass of Matrix" do it "raises a TypeError if other is of wrong type" do
it "returns an instance of that subclass" do -> { @a * nil }.should raise_error(TypeError)
m = MatrixSub.ins -> { @a * "a" }.should raise_error(TypeError)
(m*m).should be_an_instance_of(MatrixSub) -> { @a * [ [1, 2] ] }.should raise_error(TypeError)
(m*1).should be_an_instance_of(MatrixSub) -> { @a * Object.new }.should raise_error(TypeError)
end
describe "for a subclass of Matrix" do
it "returns an instance of that subclass" do
m = MatrixSub.ins
(m*m).should be_an_instance_of(MatrixSub)
(m*1).should be_an_instance_of(MatrixSub)
end
end end
end end
end end

View file

@ -1,8 +1,11 @@
require_relative '../../spec_helper' require_relative '../../spec_helper'
require 'matrix'
describe "Matrix.new" do ruby_version_is ""..."3.1" do
it "is private" do require 'matrix'
Matrix.should have_private_method(:new)
describe "Matrix.new" do
it "is private" do
Matrix.should have_private_method(:new)
end
end end
end end

View file

@ -1,26 +1,29 @@
require_relative '../../spec_helper' require_relative '../../spec_helper'
require 'matrix'
describe "Matrix.normal?" do ruby_version_is ""..."3.1" do
# it "returns false for non normal matrices" do require 'matrix'
# Matrix[[0, 1], [1, 2]].should_not.normal?
# end
it "returns true for normal matrices" do describe "Matrix.normal?" do
Matrix[[1, 1, 0], [0, 1, 1], [1, 0, 1]].should.normal? # it "returns false for non normal matrices" do
Matrix[[0, Complex(0, 2)], [Complex(0, -2), 0]].should.normal? # Matrix[[0, 1], [1, 2]].should_not.normal?
end # end
it "raises an error for rectangular matrices" do it "returns true for normal matrices" do
[ Matrix[[1, 1, 0], [0, 1, 1], [1, 0, 1]].should.normal?
Matrix[[0], [0]], Matrix[[0, Complex(0, 2)], [Complex(0, -2), 0]].should.normal?
Matrix[[0, 0]], end
Matrix.empty(0, 2),
Matrix.empty(2, 0), it "raises an error for rectangular matrices" do
].each do |rectangular_matrix| [
-> { Matrix[[0], [0]],
rectangular_matrix.normal? Matrix[[0, 0]],
}.should raise_error(Matrix::ErrDimensionMismatch) Matrix.empty(0, 2),
Matrix.empty(2, 0),
].each do |rectangular_matrix|
-> {
rectangular_matrix.normal?
}.should raise_error(Matrix::ErrDimensionMismatch)
end
end end
end end
end end

View file

@ -1,26 +1,29 @@
require_relative '../../spec_helper' require_relative '../../spec_helper'
require 'matrix'
describe "Matrix.orthogonal?" do ruby_version_is ""..."3.1" do
it "returns false for non orthogonal matrices" do require 'matrix'
Matrix[[0, 1], [1, 2]].should_not.orthogonal?
Matrix[[1, 1, 0], [0, 1, 1], [1, 0, 1]].should_not.orthogonal?
end
it "returns true for orthogonal matrices" do describe "Matrix.orthogonal?" do
Matrix[[0, 1], [1, 0]].should.orthogonal? it "returns false for non orthogonal matrices" do
end Matrix[[0, 1], [1, 2]].should_not.orthogonal?
Matrix[[1, 1, 0], [0, 1, 1], [1, 0, 1]].should_not.orthogonal?
end
it "raises an error for rectangular matrices" do it "returns true for orthogonal matrices" do
[ Matrix[[0, 1], [1, 0]].should.orthogonal?
Matrix[[0], [0]], end
Matrix[[0, 0]],
Matrix.empty(0, 2), it "raises an error for rectangular matrices" do
Matrix.empty(2, 0), [
].each do |rectangular_matrix| Matrix[[0], [0]],
-> { Matrix[[0, 0]],
rectangular_matrix.orthogonal? Matrix.empty(0, 2),
}.should raise_error(Matrix::ErrDimensionMismatch) Matrix.empty(2, 0),
].each do |rectangular_matrix|
-> {
rectangular_matrix.orthogonal?
}.should raise_error(Matrix::ErrDimensionMismatch)
end
end end
end end
end end

View file

@ -1,32 +1,35 @@
require_relative '../../spec_helper' require_relative '../../spec_helper'
require 'matrix'
describe "Matrix#permutation?" do ruby_version_is ""..."3.1" do
it "returns true for a permutation Matrix" do require 'matrix'
Matrix[[0, 1, 0], [0, 0, 1], [1, 0, 0]].permutation?.should be_true
end
it "returns false for a non permutation square Matrix" do describe "Matrix#permutation?" do
Matrix[[0, 1], [0, 0]].permutation?.should be_false it "returns true for a permutation Matrix" do
Matrix[[-1, 0], [0, -1]].permutation?.should be_false Matrix[[0, 1, 0], [0, 0, 1], [1, 0, 0]].permutation?.should be_true
Matrix[[1, 0], [1, 0]].permutation?.should be_false end
Matrix[[1, 0], [1, 1]].permutation?.should be_false
end
it "returns true for an empty 0x0 matrix" do it "returns false for a non permutation square Matrix" do
Matrix.empty(0,0).permutation?.should be_true Matrix[[0, 1], [0, 0]].permutation?.should be_false
end Matrix[[-1, 0], [0, -1]].permutation?.should be_false
Matrix[[1, 0], [1, 0]].permutation?.should be_false
Matrix[[1, 0], [1, 1]].permutation?.should be_false
end
it "raises an error for rectangular matrices" do it "returns true for an empty 0x0 matrix" do
[ Matrix.empty(0,0).permutation?.should be_true
Matrix[[0], [0]], end
Matrix[[0, 0]],
Matrix.empty(0, 2), it "raises an error for rectangular matrices" do
Matrix.empty(2, 0), [
].each do |rectangular_matrix| Matrix[[0], [0]],
-> { Matrix[[0, 0]],
rectangular_matrix.permutation? Matrix.empty(0, 2),
}.should raise_error(Matrix::ErrDimensionMismatch) Matrix.empty(2, 0),
].each do |rectangular_matrix|
-> {
rectangular_matrix.permutation?
}.should raise_error(Matrix::ErrDimensionMismatch)
end
end end
end end
end end

View file

@ -1,42 +1,45 @@
require_relative '../../spec_helper' require_relative '../../spec_helper'
require_relative 'fixtures/classes'
require 'matrix'
describe "Matrix#+" do ruby_version_is ""..."3.1" do
before :each do require_relative 'fixtures/classes'
@a = Matrix[ [1,2], [3,4] ] require 'matrix'
@b = Matrix[ [4,5], [6,7] ]
end
it "returns the result of adding the corresponding elements of self and other" do describe "Matrix#+" do
(@a + @b).should == Matrix[ [5,7], [9,11] ] before :each do
end @a = Matrix[ [1,2], [3,4] ]
@b = Matrix[ [4,5], [6,7] ]
end
it "returns an instance of Matrix" do it "returns the result of adding the corresponding elements of self and other" do
(@a + @b).should be_kind_of(Matrix) (@a + @b).should == Matrix[ [5,7], [9,11] ]
end end
it "raises a Matrix::ErrDimensionMismatch if the matrices are different sizes" do it "returns an instance of Matrix" do
-> { @a + Matrix[ [1] ] }.should raise_error(Matrix::ErrDimensionMismatch) (@a + @b).should be_kind_of(Matrix)
end end
it "raises a ExceptionForMatrix::ErrOperationNotDefined if other is a Numeric Type" do it "raises a Matrix::ErrDimensionMismatch if the matrices are different sizes" do
-> { @a + 2 }.should raise_error(ExceptionForMatrix::ErrOperationNotDefined) -> { @a + Matrix[ [1] ] }.should raise_error(Matrix::ErrDimensionMismatch)
-> { @a + 1.2 }.should raise_error(ExceptionForMatrix::ErrOperationNotDefined) end
-> { @a + bignum_value }.should raise_error(ExceptionForMatrix::ErrOperationNotDefined)
end
it "raises a TypeError if other is of wrong type" do it "raises a ExceptionForMatrix::ErrOperationNotDefined if other is a Numeric Type" do
-> { @a + nil }.should raise_error(TypeError) -> { @a + 2 }.should raise_error(ExceptionForMatrix::ErrOperationNotDefined)
-> { @a + "a" }.should raise_error(TypeError) -> { @a + 1.2 }.should raise_error(ExceptionForMatrix::ErrOperationNotDefined)
-> { @a + [ [1, 2] ] }.should raise_error(TypeError) -> { @a + bignum_value }.should raise_error(ExceptionForMatrix::ErrOperationNotDefined)
-> { @a + Object.new }.should raise_error(TypeError) end
end
describe "for a subclass of Matrix" do it "raises a TypeError if other is of wrong type" do
it "returns an instance of that subclass" do -> { @a + nil }.should raise_error(TypeError)
m = MatrixSub.ins -> { @a + "a" }.should raise_error(TypeError)
(m+m).should be_an_instance_of(MatrixSub) -> { @a + [ [1, 2] ] }.should raise_error(TypeError)
-> { @a + Object.new }.should raise_error(TypeError)
end
describe "for a subclass of Matrix" do
it "returns an instance of that subclass" do
m = MatrixSub.ins
(m+m).should be_an_instance_of(MatrixSub)
end
end end
end end
end end

View file

@ -1,19 +1,22 @@
require_relative '../../spec_helper' require_relative '../../spec_helper'
require 'matrix'
describe "Matrix#rank" do ruby_version_is ""..."3.1" do
it "returns the rank of the Matrix" do require 'matrix'
Matrix[ [7,6], [3,9] ].rank.should == 2
end
it "doesn't loop forever" do describe "Matrix#rank" do
Matrix[ [1,2,3], [4,5,6], [7,8,9] ].rank.should == 2 it "returns the rank of the Matrix" do
Matrix[ [1, 2, 0, 3], [1, -2, 3, 0], [0, 0, 4, 8], [2, 4, 0, 6] ].rank. Matrix[ [7,6], [3,9] ].rank.should == 2
should == 3 end
end
it "works for some easy rectangular matrices" do it "doesn't loop forever" do
Matrix[[0,0],[0,0],[1,0]].rank.should == 1 Matrix[ [1,2,3], [4,5,6], [7,8,9] ].rank.should == 2
Matrix[[0,1],[0,0],[1,0]].rank.should == 2 Matrix[ [1, 2, 0, 3], [1, -2, 3, 0], [0, 0, 4, 8], [2, 4, 0, 6] ].rank.
should == 3
end
it "works for some easy rectangular matrices" do
Matrix[[0,0],[0,0],[1,0]].rank.should == 1
Matrix[[0,1],[0,0],[1,0]].rank.should == 2
end
end end
end end

View file

@ -1,43 +1,46 @@
require_relative '../../spec_helper' require_relative '../../spec_helper'
require_relative 'fixtures/classes'
require 'matrix'
describe "Matrix#real?" do ruby_version_is ""..."3.1" do
it "returns true for matrices with all real entries" do require_relative 'fixtures/classes'
Matrix[ [1, 2], [3, 4] ].real?.should be_true require 'matrix'
Matrix[ [1.9, 2], [3, 4] ].real?.should be_true
describe "Matrix#real?" do
it "returns true for matrices with all real entries" do
Matrix[ [1, 2], [3, 4] ].real?.should be_true
Matrix[ [1.9, 2], [3, 4] ].real?.should be_true
end
it "returns true for empty matrices" do
Matrix.empty.real?.should be_true
end
it "returns false if one element is a Complex" do
Matrix[ [Complex(1,1), 2], [3, 4] ].real?.should be_false
end
# Guard against the Mathn library
guard -> { !defined?(Math.rsqrt) } do
it "returns false if one element is a Complex whose imaginary part is 0" do
Matrix[ [Complex(1,0), 2], [3, 4] ].real?.should be_false
end
end
end end
it "returns true for empty matrices" do describe "Matrix#real" do
Matrix.empty.real?.should be_true it "returns a matrix with the real part of the elements of the receiver" do
end Matrix[ [1, 2], [3, 4] ].real.should == Matrix[ [1, 2], [3, 4] ]
Matrix[ [1.9, Complex(1,1)], [Complex(-0.42, 0), 4] ].real.should == Matrix[ [1.9, 1], [-0.42, 4] ]
end
it "returns false if one element is a Complex" do it "returns empty matrices on the same size if empty" do
Matrix[ [Complex(1,1), 2], [3, 4] ].real?.should be_false Matrix.empty(0, 3).real.should == Matrix.empty(0, 3)
end Matrix.empty(3, 0).real.should == Matrix.empty(3, 0)
end
# Guard against the Mathn library describe "for a subclass of Matrix" do
guard -> { !defined?(Math.rsqrt) } do it "returns an instance of that subclass" do
it "returns false if one element is a Complex whose imaginary part is 0" do MatrixSub.ins.real.should be_an_instance_of(MatrixSub)
Matrix[ [Complex(1,0), 2], [3, 4] ].real?.should be_false end
end
end
end
describe "Matrix#real" do
it "returns a matrix with the real part of the elements of the receiver" do
Matrix[ [1, 2], [3, 4] ].real.should == Matrix[ [1, 2], [3, 4] ]
Matrix[ [1.9, Complex(1,1)], [Complex(-0.42, 0), 4] ].real.should == Matrix[ [1.9, 1], [-0.42, 4] ]
end
it "returns empty matrices on the same size if empty" do
Matrix.empty(0, 3).real.should == Matrix.empty(0, 3)
Matrix.empty(3, 0).real.should == Matrix.empty(3, 0)
end
describe "for a subclass of Matrix" do
it "returns an instance of that subclass" do
MatrixSub.ins.real.should be_an_instance_of(MatrixSub)
end end
end end
end end

View file

@ -1,6 +1,9 @@
require_relative '../../spec_helper' require_relative '../../spec_helper'
require_relative 'shared/rectangular'
describe "Matrix#rect" do ruby_version_is ""..."3.1" do
it_behaves_like :matrix_rectangular, :rect require_relative 'shared/rectangular'
describe "Matrix#rect" do
it_behaves_like :matrix_rectangular, :rect
end
end end

View file

@ -1,6 +1,9 @@
require_relative '../../spec_helper' require_relative '../../spec_helper'
require_relative 'shared/rectangular'
describe "Matrix#rectangular" do ruby_version_is ""..."3.1" do
it_behaves_like :matrix_rectangular, :rectangular require_relative 'shared/rectangular'
describe "Matrix#rectangular" do
it_behaves_like :matrix_rectangular, :rectangular
end
end end

View file

@ -1,31 +1,34 @@
require_relative '../../spec_helper' require_relative '../../spec_helper'
require 'matrix'
describe "Matrix#regular?" do ruby_version_is ""..."3.1" do
require 'matrix'
it "returns false for singular matrices" do describe "Matrix#regular?" do
m = Matrix[ [1,2,3], [3,4,3], [0,0,0] ]
m.regular?.should be_false
m = Matrix[ [1,2,9], [3,4,9], [1,2,9] ] it "returns false for singular matrices" do
m.regular?.should be_false m = Matrix[ [1,2,3], [3,4,3], [0,0,0] ]
end m.regular?.should be_false
it "returns true if the Matrix is regular" do m = Matrix[ [1,2,9], [3,4,9], [1,2,9] ]
Matrix[ [0,1], [1,0] ].regular?.should be_true m.regular?.should be_false
end end
it "returns true for an empty 0x0 matrix" do it "returns true if the Matrix is regular" do
Matrix.empty(0,0).regular?.should be_true Matrix[ [0,1], [1,0] ].regular?.should be_true
end end
it "raises an error for rectangular matrices" do it "returns true for an empty 0x0 matrix" do
-> { Matrix.empty(0,0).regular?.should be_true
Matrix[[1], [2], [3]].regular? end
}.should raise_error(Matrix::ErrDimensionMismatch)
-> { it "raises an error for rectangular matrices" do
Matrix.empty(3,0).regular? -> {
}.should raise_error(Matrix::ErrDimensionMismatch) Matrix[[1], [2], [3]].regular?
}.should raise_error(Matrix::ErrDimensionMismatch)
-> {
Matrix.empty(3,0).regular?
}.should raise_error(Matrix::ErrDimensionMismatch)
end
end end
end end

View file

@ -1,21 +1,24 @@
require_relative '../../spec_helper' require_relative '../../spec_helper'
require_relative 'fixtures/classes'
require 'matrix'
describe "Matrix#round" do ruby_version_is ""..."3.1" do
it "returns a matrix with all entries rounded" do require_relative 'fixtures/classes'
Matrix[ [1, 2.34], [5.67, 8] ].round.should == Matrix[ [1, 2], [6, 8] ] require 'matrix'
Matrix[ [1, 2.34], [5.67, 8] ].round(1).should == Matrix[ [1, 2.3], [5.7, 8] ]
end
it "returns empty matrices on the same size if empty" do describe "Matrix#round" do
Matrix.empty(0, 3).round.should == Matrix.empty(0, 3) it "returns a matrix with all entries rounded" do
Matrix.empty(3, 0).round(42).should == Matrix.empty(3, 0) Matrix[ [1, 2.34], [5.67, 8] ].round.should == Matrix[ [1, 2], [6, 8] ]
end Matrix[ [1, 2.34], [5.67, 8] ].round(1).should == Matrix[ [1, 2.3], [5.7, 8] ]
end
describe "for a subclass of Matrix" do it "returns empty matrices on the same size if empty" do
it "returns an instance of that subclass" do Matrix.empty(0, 3).round.should == Matrix.empty(0, 3)
MatrixSub.ins.round.should be_an_instance_of(MatrixSub) Matrix.empty(3, 0).round(42).should == Matrix.empty(3, 0)
end
describe "for a subclass of Matrix" do
it "returns an instance of that subclass" do
MatrixSub.ins.round.should be_an_instance_of(MatrixSub)
end
end end
end end
end end

View file

@ -1,13 +1,16 @@
require_relative '../../spec_helper' require_relative '../../spec_helper'
require 'matrix'
describe "Matrix#row_size" do ruby_version_is ""..."3.1" do
it "returns the number rows" do require 'matrix'
Matrix[ [1,2], [3, 4], [5, 6] ].row_size.should == 3
describe "Matrix#row_size" do
it "returns the number rows" do
Matrix[ [1,2], [3, 4], [5, 6] ].row_size.should == 3
end
it "returns the number rows even for some empty matrices" do
Matrix[ [], [], [] ].row_size.should == 3
end
end end
it "returns the number rows even for some empty matrices" do
Matrix[ [], [], [] ].row_size.should == 3
end
end end

View file

@ -1,36 +1,39 @@
require_relative '../../spec_helper' require_relative '../../spec_helper'
require 'matrix'
describe "Matrix#row" do ruby_version_is ""..."3.1" do
before :all do require 'matrix'
@m = Matrix[ [1, 2], [2, 3], [3, 4] ]
end
it "returns a Vector when called without a block" do describe "Matrix#row" do
@m.row(0).should == Vector[1,2] before :all do
end @m = Matrix[ [1, 2], [2, 3], [3, 4] ]
end
it "yields the elements of the row when called with a block" do it "returns a Vector when called without a block" do
a = [] @m.row(0).should == Vector[1,2]
@m.row(0) {|x| a << x} end
a.should == [1,2]
end
it "counts backwards for negative argument" do it "yields the elements of the row when called with a block" do
@m.row(-1).should == Vector[3, 4] a = []
end @m.row(0) {|x| a << x}
a.should == [1,2]
end
it "returns self when called with a block" do it "counts backwards for negative argument" do
@m.row(0) { |x| x }.should equal(@m) @m.row(-1).should == Vector[3, 4]
end end
it "returns nil when out of bounds" do it "returns self when called with a block" do
@m.row(3).should == nil @m.row(0) { |x| x }.should equal(@m)
@m.row(-4).should == nil end
end
it "never yields when out of bounds" do it "returns nil when out of bounds" do
-> { @m.row(3){ raise } }.should_not raise_error @m.row(3).should == nil
-> { @m.row(-4){ raise } }.should_not raise_error @m.row(-4).should == nil
end
it "never yields when out of bounds" do
-> { @m.row(3){ raise } }.should_not raise_error
-> { @m.row(-4){ raise } }.should_not raise_error
end
end end
end end

View file

@ -1,24 +1,27 @@
require_relative '../../spec_helper' require_relative '../../spec_helper'
require_relative 'fixtures/classes'
require 'matrix'
describe "Matrix.row_vector" do ruby_version_is ""..."3.1" do
require_relative 'fixtures/classes'
require 'matrix'
it "returns a Matrix" do describe "Matrix.row_vector" do
Matrix.row_vector([]).should be_an_instance_of(Matrix)
end
it "returns a single-row Matrix with the specified values" do it "returns a Matrix" do
Matrix.row_vector([1,2]).should == Matrix[ [1,2] ] Matrix.row_vector([]).should be_an_instance_of(Matrix)
end end
it "returns a 1x0 matrix when called with an empty Array" do it "returns a single-row Matrix with the specified values" do
Matrix.row_vector([]).should == Matrix[ [] ] Matrix.row_vector([1,2]).should == Matrix[ [1,2] ]
end end
describe "for a subclass of Matrix" do it "returns a 1x0 matrix when called with an empty Array" do
it "returns an instance of that subclass" do Matrix.row_vector([]).should == Matrix[ [] ]
MatrixSub.row_vector([1]).should be_an_instance_of(MatrixSub) end
describe "for a subclass of Matrix" do
it "returns an instance of that subclass" do
MatrixSub.row_vector([1]).should be_an_instance_of(MatrixSub)
end
end end
end end
end end

View file

@ -1,26 +1,29 @@
require_relative '../../spec_helper' require_relative '../../spec_helper'
require 'matrix'
describe "Matrix#row_vectors" do ruby_version_is ""..."3.1" do
require 'matrix'
before :each do describe "Matrix#row_vectors" do
@vectors = Matrix[ [1,2], [3,4] ].row_vectors
end
it "returns an Array" do before :each do
Matrix[ [1,2], [3,4] ].row_vectors.should be_an_instance_of(Array) @vectors = Matrix[ [1,2], [3,4] ].row_vectors
end end
it "returns an Array of Vectors" do it "returns an Array" do
@vectors.all? {|v| v.should be_an_instance_of(Vector)} Matrix[ [1,2], [3,4] ].row_vectors.should be_an_instance_of(Array)
end end
it "returns each row as a Vector" do it "returns an Array of Vectors" do
@vectors.should == [Vector[1,2], Vector[3,4]] @vectors.all? {|v| v.should be_an_instance_of(Vector)}
end end
it "returns an empty Array for empty matrices" do it "returns each row as a Vector" do
Matrix[].row_vectors.should == [] @vectors.should == [Vector[1,2], Vector[3,4]]
Matrix[ [] ].row_vectors.should == [ Vector[] ] end
it "returns an empty Array for empty matrices" do
Matrix[].row_vectors.should == []
Matrix[ [] ].row_vectors.should == [ Vector[] ]
end
end end
end end

View file

@ -1,41 +1,44 @@
require_relative '../../spec_helper' require_relative '../../spec_helper'
require_relative 'fixtures/classes'
require 'matrix'
describe "Matrix.rows" do ruby_version_is ""..."3.1" do
before :each do require_relative 'fixtures/classes'
@a = [1, 2] require 'matrix'
@b = [3, 4]
@m = Matrix.rows([@a, @b])
end
it "returns a Matrix" do describe "Matrix.rows" do
@m.should be_kind_of(Matrix) before :each do
end @a = [1, 2]
@b = [3, 4]
@m = Matrix.rows([@a, @b])
end
it "creates a matrix from argument rows" do it "returns a Matrix" do
@m.row(0).to_a.should == @a @m.should be_kind_of(Matrix)
@m.row(1).to_a.should == @b end
end
it "copies the original rows by default" do it "creates a matrix from argument rows" do
@a << 3 @m.row(0).to_a.should == @a
@b << 6 @m.row(1).to_a.should == @b
@m.row(0).should_not equal(@a) end
@m.row(1).should_not equal(@b)
end
it "references the original rows if copy is false" do it "copies the original rows by default" do
@m_ref = Matrix.rows([@a, @b], false) @a << 3
@a << 3 @b << 6
@b << 6 @m.row(0).should_not equal(@a)
@m_ref.row(0).to_a.should == @a @m.row(1).should_not equal(@b)
@m_ref.row(1).to_a.should == @b end
end
describe "for a subclass of Matrix" do it "references the original rows if copy is false" do
it "returns an instance of that subclass" do @m_ref = Matrix.rows([@a, @b], false)
MatrixSub.rows([[0, 1], [0, 1]]).should be_an_instance_of(MatrixSub) @a << 3
@b << 6
@m_ref.row(0).to_a.should == @a
@m_ref.row(1).to_a.should == @b
end
describe "for a subclass of Matrix" do
it "returns an instance of that subclass" do
MatrixSub.rows([[0, 1], [0, 1]]).should be_an_instance_of(MatrixSub)
end
end end
end end
end end

View file

@ -1,6 +1,9 @@
require_relative '../../../spec_helper' require_relative '../../../spec_helper'
require 'matrix'
describe "Matrix::Scalar#Fail" do ruby_version_is ""..."3.1" do
it "needs to be reviewed for spec completeness" require 'matrix'
describe "Matrix::Scalar#Fail" do
it "needs to be reviewed for spec completeness"
end
end end

View file

@ -1,6 +1,9 @@
require_relative '../../../spec_helper' require_relative '../../../spec_helper'
require 'matrix'
describe "Matrix::Scalar#Raise" do ruby_version_is ""..."3.1" do
it "needs to be reviewed for spec completeness" require 'matrix'
describe "Matrix::Scalar#Raise" do
it "needs to be reviewed for spec completeness"
end
end end

View file

@ -1,6 +1,9 @@
require_relative '../../../spec_helper' require_relative '../../../spec_helper'
require 'matrix'
describe "Matrix::Scalar#/" do ruby_version_is ""..."3.1" do
it "needs to be reviewed for spec completeness" require 'matrix'
describe "Matrix::Scalar#/" do
it "needs to be reviewed for spec completeness"
end
end end

View file

@ -1,6 +1,9 @@
require_relative '../../../spec_helper' require_relative '../../../spec_helper'
require 'matrix'
describe "Matrix::Scalar#**" do ruby_version_is ""..."3.1" do
it "needs to be reviewed for spec completeness" require 'matrix'
describe "Matrix::Scalar#**" do
it "needs to be reviewed for spec completeness"
end
end end

View file

@ -1,6 +1,9 @@
require_relative '../../../spec_helper' require_relative '../../../spec_helper'
require 'matrix'
describe "Matrix::Scalar.included" do ruby_version_is ""..."3.1" do
it "needs to be reviewed for spec completeness" require 'matrix'
describe "Matrix::Scalar.included" do
it "needs to be reviewed for spec completeness"
end
end end

View file

@ -1,6 +1,9 @@
require_relative '../../../spec_helper' require_relative '../../../spec_helper'
require 'matrix'
describe "Matrix::Scalar#initialize" do ruby_version_is ""..."3.1" do
it "needs to be reviewed for spec completeness" require 'matrix'
describe "Matrix::Scalar#initialize" do
it "needs to be reviewed for spec completeness"
end
end end

View file

@ -1,6 +1,9 @@
require_relative '../../../spec_helper' require_relative '../../../spec_helper'
require 'matrix'
describe "Matrix::Scalar#-" do ruby_version_is ""..."3.1" do
it "needs to be reviewed for spec completeness" require 'matrix'
describe "Matrix::Scalar#-" do
it "needs to be reviewed for spec completeness"
end
end end

View file

@ -1,6 +1,9 @@
require_relative '../../../spec_helper' require_relative '../../../spec_helper'
require 'matrix'
describe "Matrix::Scalar#*" do ruby_version_is ""..."3.1" do
it "needs to be reviewed for spec completeness" require 'matrix'
describe "Matrix::Scalar#*" do
it "needs to be reviewed for spec completeness"
end
end end

View file

@ -1,6 +1,9 @@
require_relative '../../../spec_helper' require_relative '../../../spec_helper'
require 'matrix'
describe "Matrix::Scalar#+" do ruby_version_is ""..."3.1" do
it "needs to be reviewed for spec completeness" require 'matrix'
describe "Matrix::Scalar#+" do
it "needs to be reviewed for spec completeness"
end
end end

View file

@ -1,65 +1,68 @@
require_relative '../../spec_helper' require_relative '../../spec_helper'
require 'matrix'
describe "Matrix.scalar" do ruby_version_is ""..."3.1" do
require 'matrix'
before :each do describe "Matrix.scalar" do
@side = 3
@value = 8
@a = Matrix.scalar(@side, @value)
end
it "returns a Matrix" do before :each do
@a.should be_kind_of(Matrix) @side = 3
end @value = 8
@a = Matrix.scalar(@side, @value)
it "returns a n x n matrix" do
@a.row_size.should == @side
@a.column_size.should == @side
end
it "initializes diagonal to value" do
(0...@a.row_size).each do |i|
@a[i, i].should == @value
end end
end
it "initializes all non-diagonal values to 0" do it "returns a Matrix" do
(0...@a.row_size).each do |i| @a.should be_kind_of(Matrix)
(0...@a.column_size).each do |j| end
if i != j
@a[i, j].should == 0 it "returns a n x n matrix" do
@a.row_size.should == @side
@a.column_size.should == @side
end
it "initializes diagonal to value" do
(0...@a.row_size).each do |i|
@a[i, i].should == @value
end
end
it "initializes all non-diagonal values to 0" do
(0...@a.row_size).each do |i|
(0...@a.column_size).each do |j|
if i != j
@a[i, j].should == 0
end
end end
end end
end end
end
before :each do before :each do
@side = 3 @side = 3
@value = 8 @value = 8
@a = Matrix.scalar(@side, @value) @a = Matrix.scalar(@side, @value)
end
it "returns a Matrix" do
@a.should be_kind_of(Matrix)
end
it "returns a square matrix, where the first argument specifies the side of the square" do
@a.row_size.should == @side
@a.column_size.should == @side
end
it "puts the second argument in all diagonal values" do
(0...@a.row_size).each do |i|
@a[i, i].should == @value
end end
end
it "fills all values not on the main diagonal with 0" do it "returns a Matrix" do
(0...@a.row_size).each do |i| @a.should be_kind_of(Matrix)
(0...@a.column_size).each do |j| end
if i != j
@a[i, j].should == 0 it "returns a square matrix, where the first argument specifies the side of the square" do
@a.row_size.should == @side
@a.column_size.should == @side
end
it "puts the second argument in all diagonal values" do
(0...@a.row_size).each do |i|
@a[i, i].should == @value
end
end
it "fills all values not on the main diagonal with 0" do
(0...@a.row_size).each do |i|
(0...@a.column_size).each do |j|
if i != j
@a[i, j].should == 0
end
end end
end end
end end

View file

@ -1,31 +1,34 @@
require_relative '../../spec_helper' require_relative '../../spec_helper'
require 'matrix'
describe "Matrix#singular?" do ruby_version_is ""..."3.1" do
it "returns true for singular matrices" do require 'matrix'
m = Matrix[ [1,2,3], [3,4,3], [0,0,0] ]
m.singular?.should be_true describe "Matrix#singular?" do
it "returns true for singular matrices" do
m = Matrix[ [1,2,3], [3,4,3], [0,0,0] ]
m.singular?.should be_true
m = Matrix[ [1,2,9], [3,4,9], [1,2,9] ]
m.singular?.should be_true
end
it "returns false if the Matrix is regular" do
Matrix[ [0,1], [1,0] ].singular?.should be_false
end
it "returns false for an empty 0x0 matrix" do
Matrix.empty(0,0).singular?.should be_false
end
it "raises an error for rectangular matrices" do
-> {
Matrix[[1], [2], [3]].singular?
}.should raise_error(Matrix::ErrDimensionMismatch)
-> {
Matrix.empty(3,0).singular?
}.should raise_error(Matrix::ErrDimensionMismatch)
end
m = Matrix[ [1,2,9], [3,4,9], [1,2,9] ]
m.singular?.should be_true
end end
it "returns false if the Matrix is regular" do
Matrix[ [0,1], [1,0] ].singular?.should be_false
end
it "returns false for an empty 0x0 matrix" do
Matrix.empty(0,0).singular?.should be_false
end
it "raises an error for rectangular matrices" do
-> {
Matrix[[1], [2], [3]].singular?
}.should raise_error(Matrix::ErrDimensionMismatch)
-> {
Matrix.empty(3,0).singular?
}.should raise_error(Matrix::ErrDimensionMismatch)
end
end end

View file

@ -1,28 +1,31 @@
require_relative '../../spec_helper' require_relative '../../spec_helper'
require 'matrix'
describe "Matrix#square?" do ruby_version_is ""..."3.1" do
require 'matrix'
it "returns true when the Matrix is square" do describe "Matrix#square?" do
Matrix[ [1,2], [2,4] ].square?.should be_true
Matrix[ [100,3,5], [9.5, 4.9, 8], [2,0,77] ].square?.should be_true
end
it "returns true when the Matrix has only one element" do it "returns true when the Matrix is square" do
Matrix[ [9] ].square?.should be_true Matrix[ [1,2], [2,4] ].square?.should be_true
end Matrix[ [100,3,5], [9.5, 4.9, 8], [2,0,77] ].square?.should be_true
end
it "returns false when the Matrix is rectangular" do it "returns true when the Matrix has only one element" do
Matrix[ [1, 2] ].square?.should be_false Matrix[ [9] ].square?.should be_true
end end
it "returns false when the Matrix is rectangular" do it "returns false when the Matrix is rectangular" do
Matrix[ [1], [2] ].square?.should be_false Matrix[ [1, 2] ].square?.should be_false
end end
it "returns handles empty matrices" do it "returns false when the Matrix is rectangular" do
Matrix[].square?.should be_true Matrix[ [1], [2] ].square?.should be_false
Matrix[[]].square?.should be_false end
Matrix.columns([[]]).square?.should be_false
it "returns handles empty matrices" do
Matrix[].square?.should be_true
Matrix[[]].square?.should be_false
Matrix.columns([[]]).square?.should be_false
end
end end
end end

View file

@ -1,29 +1,32 @@
require_relative '../../spec_helper' require_relative '../../spec_helper'
require 'matrix'
describe "Matrix.symmetric?" do ruby_version_is ""..."3.1" do
it "returns true for a symmetric Matrix" do require 'matrix'
Matrix[[1, 2, Complex(0, 3)], [2, 4, 5], [Complex(0, 3), 5, 6]].symmetric?.should be_true
end
it "returns true for a 0x0 empty matrix" do describe "Matrix.symmetric?" do
Matrix.empty.symmetric?.should be_true it "returns true for a symmetric Matrix" do
end Matrix[[1, 2, Complex(0, 3)], [2, 4, 5], [Complex(0, 3), 5, 6]].symmetric?.should be_true
end
it "returns false for an asymmetric Matrix" do it "returns true for a 0x0 empty matrix" do
Matrix[[1, 2],[-2, 1]].symmetric?.should be_false Matrix.empty.symmetric?.should be_true
end end
it "raises an error for rectangular matrices" do it "returns false for an asymmetric Matrix" do
[ Matrix[[1, 2],[-2, 1]].symmetric?.should be_false
Matrix[[0], [0]], end
Matrix[[0, 0]],
Matrix.empty(0, 2), it "raises an error for rectangular matrices" do
Matrix.empty(2, 0), [
].each do |rectangular_matrix| Matrix[[0], [0]],
-> { Matrix[[0, 0]],
rectangular_matrix.symmetric? Matrix.empty(0, 2),
}.should raise_error(Matrix::ErrDimensionMismatch) Matrix.empty(2, 0),
].each do |rectangular_matrix|
-> {
rectangular_matrix.symmetric?
}.should raise_error(Matrix::ErrDimensionMismatch)
end
end end
end end
end end

View file

@ -1,6 +1,9 @@
require_relative '../../spec_helper' require_relative '../../spec_helper'
require_relative 'shared/transpose'
describe "Matrix#transpose" do ruby_version_is ""..."3.1" do
it_behaves_like :matrix_transpose, :t require_relative 'shared/transpose'
describe "Matrix#transpose" do
it_behaves_like :matrix_transpose, :t
end
end end

View file

@ -1,11 +1,14 @@
require_relative '../../spec_helper' require_relative '../../spec_helper'
require 'matrix'
describe "Matrix#to_a" do ruby_version_is ""..."3.1" do
it "returns the array of arrays that describe the rows of the matrix" do require 'matrix'
Matrix[].to_a.should == []
Matrix[[]].to_a.should == [[]] describe "Matrix#to_a" do
Matrix[[1]].to_a.should == [[1]] it "returns the array of arrays that describe the rows of the matrix" do
Matrix[[1, 2], [3, 4]].to_a.should == [[1, 2],[3, 4]] Matrix[].to_a.should == []
Matrix[[]].to_a.should == [[]]
Matrix[[1]].to_a.should == [[1]]
Matrix[[1, 2], [3, 4]].to_a.should == [[1, 2],[3, 4]]
end
end end
end end

View file

@ -1,6 +1,9 @@
require_relative '../../spec_helper' require_relative '../../spec_helper'
require 'matrix'
describe "Matrix#to_s" do ruby_version_is ""..."3.1" do
it "needs to be reviewed for spec completeness" require 'matrix'
describe "Matrix#to_s" do
it "needs to be reviewed for spec completeness"
end
end end

View file

@ -1,7 +1,10 @@
require_relative '../../spec_helper' require_relative '../../spec_helper'
require_relative 'shared/trace'
require 'matrix'
describe "Matrix#tr" do ruby_version_is ""..."3.1" do
it_behaves_like :trace, :tr require_relative 'shared/trace'
require 'matrix'
describe "Matrix#tr" do
it_behaves_like :trace, :tr
end
end end

View file

@ -1,7 +1,10 @@
require_relative '../../spec_helper' require_relative '../../spec_helper'
require_relative 'shared/trace'
require 'matrix'
describe "Matrix#trace" do ruby_version_is ""..."3.1" do
it_behaves_like :trace, :trace require_relative 'shared/trace'
require 'matrix'
describe "Matrix#trace" do
it_behaves_like :trace, :trace
end
end end

View file

@ -1,6 +1,9 @@
require_relative '../../spec_helper' require_relative '../../spec_helper'
require_relative 'shared/transpose'
describe "Matrix#transpose" do ruby_version_is ""..."3.1" do
it_behaves_like :matrix_transpose, :transpose require_relative 'shared/transpose'
describe "Matrix#transpose" do
it_behaves_like :matrix_transpose, :transpose
end
end end

View file

@ -1,6 +1,9 @@
require_relative '../../spec_helper' require_relative '../../spec_helper'
require_relative 'shared/identity'
describe "Matrix.unit" do ruby_version_is ""..."3.1" do
it_behaves_like :matrix_identity, :unit require_relative 'shared/identity'
describe "Matrix.unit" do
it_behaves_like :matrix_identity, :unit
end
end end

View file

@ -1,33 +1,36 @@
require_relative '../../spec_helper' require_relative '../../spec_helper'
require 'matrix'
describe "Matrix.unitary?" do ruby_version_is ""..."3.1" do
it "returns false for non unitary matrices" do require 'matrix'
Matrix[[0, 1], [1, 2]].should_not.unitary?
Matrix[[0, Complex(0, 2)], [Complex(0, 2), 0]].should_not.unitary?
Matrix[[1, 1, 0], [0, 1, 1], [1, 0, 1]].should_not.unitary?
end
it "returns true for unitary matrices" do describe "Matrix.unitary?" do
Matrix[[0, Complex(0, 1)], [Complex(0, 1), 0]].should.unitary? it "returns false for non unitary matrices" do
end Matrix[[0, 1], [1, 2]].should_not.unitary?
Matrix[[0, Complex(0, 2)], [Complex(0, 2), 0]].should_not.unitary?
version_is((Matrix::const_defined?(:VERSION) ? Matrix::VERSION : "0.1.0"), "0.3.0") do Matrix[[1, 1, 0], [0, 1, 1], [1, 0, 1]].should_not.unitary?
it "returns true for unitary matrices with a Complex and a negative #imag" do
Matrix[[0, Complex(0, 1)], [Complex(0, -1), 0]].should.unitary?
end end
end
it "raises an error for rectangular matrices" do it "returns true for unitary matrices" do
[ Matrix[[0, Complex(0, 1)], [Complex(0, 1), 0]].should.unitary?
Matrix[[0], [0]], end
Matrix[[0, 0]],
Matrix.empty(0, 2), version_is((Matrix::const_defined?(:VERSION) ? Matrix::VERSION : "0.1.0"), "0.3.0") do
Matrix.empty(2, 0), it "returns true for unitary matrices with a Complex and a negative #imag" do
].each do |rectangular_matrix| Matrix[[0, Complex(0, 1)], [Complex(0, -1), 0]].should.unitary?
-> { end
rectangular_matrix.unitary? end
}.should raise_error(Matrix::ErrDimensionMismatch)
it "raises an error for rectangular matrices" do
[
Matrix[[0], [0]],
Matrix[[0, 0]],
Matrix.empty(0, 2),
Matrix.empty(2, 0),
].each do |rectangular_matrix|
-> {
rectangular_matrix.unitary?
}.should raise_error(Matrix::ErrDimensionMismatch)
end
end end
end end
end end

View file

@ -1,24 +1,27 @@
require_relative '../../spec_helper' require_relative '../../spec_helper'
require 'matrix'
describe "Matrix.upper_triangular?" do ruby_version_is ""..."3.1" do
it "returns true for an upper triangular Matrix" do require 'matrix'
Matrix[[1, 2, 3], [0, 2, 3], [0, 0, 3]].upper_triangular?.should be_true
Matrix.diagonal([1, 2, 3]).upper_triangular?.should be_true
Matrix[[1, 2], [0, 2], [0, 0], [0, 0]].upper_triangular?.should be_true
Matrix[[1, 2, 3, 4], [0, 2, 3, 4]].upper_triangular?.should be_true
end
it "returns false for a non upper triangular square Matrix" do describe "Matrix.upper_triangular?" do
Matrix[[0, 0], [1, 0]].upper_triangular?.should be_false it "returns true for an upper triangular Matrix" do
Matrix[[1, 2, 3], [1, 2, 3], [1, 2, 3]].upper_triangular?.should be_false Matrix[[1, 2, 3], [0, 2, 3], [0, 0, 3]].upper_triangular?.should be_true
Matrix[[0, 0], [0, 0], [0, 0], [0, 1]].upper_triangular?.should be_false Matrix.diagonal([1, 2, 3]).upper_triangular?.should be_true
Matrix[[0, 0, 0, 0], [1, 0, 0, 0]].upper_triangular?.should be_false Matrix[[1, 2], [0, 2], [0, 0], [0, 0]].upper_triangular?.should be_true
end Matrix[[1, 2, 3, 4], [0, 2, 3, 4]].upper_triangular?.should be_true
end
it "returns true for an empty matrix" do it "returns false for a non upper triangular square Matrix" do
Matrix.empty(3,0).upper_triangular?.should be_true Matrix[[0, 0], [1, 0]].upper_triangular?.should be_false
Matrix.empty(0,3).upper_triangular?.should be_true Matrix[[1, 2, 3], [1, 2, 3], [1, 2, 3]].upper_triangular?.should be_false
Matrix.empty(0,0).upper_triangular?.should be_true Matrix[[0, 0], [0, 0], [0, 0], [0, 1]].upper_triangular?.should be_false
Matrix[[0, 0, 0, 0], [1, 0, 0, 0]].upper_triangular?.should be_false
end
it "returns true for an empty matrix" do
Matrix.empty(3,0).upper_triangular?.should be_true
Matrix.empty(0,3).upper_triangular?.should be_true
Matrix.empty(0,0).upper_triangular?.should be_true
end
end end
end end

View file

@ -1,14 +1,17 @@
require_relative '../../../spec_helper' require_relative '../../../spec_helper'
require 'matrix'
describe "Vector#cross_product" do ruby_version_is ""..."3.1" do
it "returns the cross product of a vector" do require 'matrix'
Vector[1, 2, 3].cross_product(Vector[0, -4, 5]).should == Vector[22, -5, -4]
end
it "raises an error unless both vectors have dimension 3" do describe "Vector#cross_product" do
-> { it "returns the cross product of a vector" do
Vector[1, 2, 3].cross_product(Vector[0, -4]) Vector[1, 2, 3].cross_product(Vector[0, -4, 5]).should == Vector[22, -5, -4]
}.should raise_error(Vector::ErrDimensionMismatch) end
it "raises an error unless both vectors have dimension 3" do
-> {
Vector[1, 2, 3].cross_product(Vector[0, -4])
}.should raise_error(Vector::ErrDimensionMismatch)
end
end end
end end

View file

@ -1,49 +1,52 @@
require_relative '../../../spec_helper' require_relative '../../../spec_helper'
require 'matrix'
describe "Vector.each2" do ruby_version_is ""..."3.1" do
before :all do require 'matrix'
@v = Vector[1, 2, 3]
@v2 = Vector[4, 5, 6]
end
it "requires one argument" do describe "Vector.each2" do
-> { @v.each2(@v2, @v2){} }.should raise_error(ArgumentError) before :all do
-> { @v.each2(){} }.should raise_error(ArgumentError) @v = Vector[1, 2, 3]
end @v2 = Vector[4, 5, 6]
describe "given one argument" do
it "accepts an Array argument" do
a = []
@v.each2([7, 8, 9]){|x, y| a << x << y}
a.should == [1, 7, 2, 8, 3, 9]
end end
it "raises a DimensionMismatch error if the Vector size is different" do it "requires one argument" do
-> { @v.each2(Vector[1,2]){} }.should raise_error(Vector::ErrDimensionMismatch) -> { @v.each2(@v2, @v2){} }.should raise_error(ArgumentError)
-> { @v.each2(Vector[1,2,3,4]){} }.should raise_error(Vector::ErrDimensionMismatch) -> { @v.each2(){} }.should raise_error(ArgumentError)
end end
it "yields arguments in sequence" do describe "given one argument" do
a = [] it "accepts an Array argument" do
@v.each2(@v2){|first, second| a << [first, second]} a = []
a.should == [[1, 4], [2, 5], [3, 6]] @v.each2([7, 8, 9]){|x, y| a << x << y}
end a.should == [1, 7, 2, 8, 3, 9]
end
it "yield arguments in pairs" do it "raises a DimensionMismatch error if the Vector size is different" do
a = [] -> { @v.each2(Vector[1,2]){} }.should raise_error(Vector::ErrDimensionMismatch)
@v.each2(@v2){|*pair| a << pair} -> { @v.each2(Vector[1,2,3,4]){} }.should raise_error(Vector::ErrDimensionMismatch)
a.should == [[1, 4], [2, 5], [3, 6]] end
end
it "returns self when given a block" do it "yields arguments in sequence" do
@v.each2(@v2){}.should equal(@v) a = []
end @v.each2(@v2){|first, second| a << [first, second]}
a.should == [[1, 4], [2, 5], [3, 6]]
end
it "returns an enumerator if no block given" do it "yield arguments in pairs" do
enum = @v.each2(@v2) a = []
enum.should be_an_instance_of(Enumerator) @v.each2(@v2){|*pair| a << pair}
enum.to_a.should == [[1, 4], [2, 5], [3, 6]] a.should == [[1, 4], [2, 5], [3, 6]]
end
it "returns self when given a block" do
@v.each2(@v2){}.should equal(@v)
end
it "returns an enumerator if no block given" do
enum = @v.each2(@v2)
enum.should be_an_instance_of(Enumerator)
enum.to_a.should == [[1, 4], [2, 5], [3, 6]]
end
end end
end end
end end

View file

@ -1,16 +1,19 @@
require_relative '../../../spec_helper' require_relative '../../../spec_helper'
require 'matrix'
describe "Vector#eql?" do ruby_version_is ""..."3.1" do
before do require 'matrix'
@vector = Vector[1, 2, 3, 4, 5]
end
it "returns true for self" do describe "Vector#eql?" do
@vector.eql?(@vector).should be_true before do
end @vector = Vector[1, 2, 3, 4, 5]
end
it "returns false when there are a pair corresponding elements which are not equal in the sense of Kernel#eql?" do it "returns true for self" do
@vector.eql?(Vector[1, 2, 3, 4, 5.0]).should be_false @vector.eql?(@vector).should be_true
end
it "returns false when there are a pair corresponding elements which are not equal in the sense of Kernel#eql?" do
@vector.eql?(Vector[1, 2, 3, 4, 5.0]).should be_false
end
end end
end end

View file

@ -1,22 +1,25 @@
require_relative '../../../spec_helper' require_relative '../../../spec_helper'
require 'matrix'
describe "Vector#inner_product" do ruby_version_is ""..."3.1" do
it "returns the inner product of a vector" do require 'matrix'
Vector[1, 2, 3].inner_product(Vector[0, -4, 5]).should == 7
end
it "returns 0 for empty vectors" do describe "Vector#inner_product" do
Vector[].inner_product(Vector[]).should == 0 it "returns the inner product of a vector" do
end Vector[1, 2, 3].inner_product(Vector[0, -4, 5]).should == 7
end
it "raises an error for mismatched vectors" do it "returns 0 for empty vectors" do
-> { Vector[].inner_product(Vector[]).should == 0
Vector[1, 2, 3].inner_product(Vector[0, -4]) end
}.should raise_error(Vector::ErrDimensionMismatch)
end
it "uses the conjugate of its argument" do it "raises an error for mismatched vectors" do
Vector[Complex(1,2)].inner_product(Vector[Complex(3,4)]).should == Complex(11, 2) -> {
Vector[1, 2, 3].inner_product(Vector[0, -4])
}.should raise_error(Vector::ErrDimensionMismatch)
end
it "uses the conjugate of its argument" do
Vector[Complex(1,2)].inner_product(Vector[Complex(3,4)]).should == Complex(11, 2)
end
end end
end end

View file

@ -1,18 +1,21 @@
require_relative '../../../spec_helper' require_relative '../../../spec_helper'
require 'matrix'
describe "Vector#normalize" do ruby_version_is ""..."3.1" do
it "returns a normalized copy of the vector" do require 'matrix'
x = 0.2672612419124244
Vector[1, 2, 3].normalize.should == Vector[x, x * 2, x * 3]
end
it "raises an error for zero vectors" do describe "Vector#normalize" do
-> { it "returns a normalized copy of the vector" do
Vector[].normalize x = 0.2672612419124244
}.should raise_error(Vector::ZeroVectorError) Vector[1, 2, 3].normalize.should == Vector[x, x * 2, x * 3]
-> { end
Vector[0, 0, 0].normalize
}.should raise_error(Vector::ZeroVectorError) it "raises an error for zero vectors" do
-> {
Vector[].normalize
}.should raise_error(Vector::ZeroVectorError)
-> {
Vector[0, 0, 0].normalize
}.should raise_error(Vector::ZeroVectorError)
end
end end
end end

View file

@ -1,52 +1,55 @@
require_relative '../../spec_helper' require_relative '../../spec_helper'
require_relative 'fixtures/classes'
require 'matrix'
describe "Matrix.zero" do ruby_version_is ""..."3.1" do
it "returns an object of type Matrix" do require_relative 'fixtures/classes'
Matrix.zero(3).should be_kind_of(Matrix) require 'matrix'
end
it "creates a n x n matrix" do describe "Matrix.zero" do
m3 = Matrix.zero(3) it "returns an object of type Matrix" do
m3.row_size.should == 3 Matrix.zero(3).should be_kind_of(Matrix)
m3.column_size.should == 3 end
m8 = Matrix.zero(8) it "creates a n x n matrix" do
m8.row_size.should == 8 m3 = Matrix.zero(3)
m8.column_size.should == 8 m3.row_size.should == 3
end m3.column_size.should == 3
it "initializes all cells to 0" do m8 = Matrix.zero(8)
size = 10 m8.row_size.should == 8
m = Matrix.zero(size) m8.column_size.should == 8
end
(0...size).each do |i| it "initializes all cells to 0" do
(0...size).each do |j| size = 10
m[i, j].should == 0 m = Matrix.zero(size)
(0...size).each do |i|
(0...size).each do |j|
m[i, j].should == 0
end
end
end
describe "for a subclass of Matrix" do
it "returns an instance of that subclass" do
MatrixSub.zero(3).should be_an_instance_of(MatrixSub)
end end
end end
end end
describe "for a subclass of Matrix" do describe "Matrix.zero?" do
it "returns an instance of that subclass" do it "returns true for empty matrices" do
MatrixSub.zero(3).should be_an_instance_of(MatrixSub) Matrix.empty.should.zero?
Matrix.empty(3,0).should.zero?
Matrix.empty(0,3).should.zero?
end
it "returns true for matrices with zero entries" do
Matrix.zero(2,3).should.zero?
end
it "returns false for matrices with non zero entries" do
Matrix[[1]].should_not.zero?
end end
end end
end end
describe "Matrix.zero?" do
it "returns true for empty matrices" do
Matrix.empty.should.zero?
Matrix.empty(3,0).should.zero?
Matrix.empty(0,3).should.zero?
end
it "returns true for matrices with zero entries" do
Matrix.zero(2,3).should.zero?
end
it "returns false for matrices with non zero entries" do
Matrix[[1]].should_not.zero?
end
end

View file

@ -1,8 +1,11 @@
require_relative '../../spec_helper' require_relative '../../spec_helper'
require 'net/ftp'
describe "Net::FTPError" do ruby_version_is ""..."3.1" do
it "is an Exception" do require 'net/ftp'
Net::FTPError.should < Exception
describe "Net::FTPError" do
it "is an Exception" do
Net::FTPError.should < Exception
end
end end
end end

View file

@ -1,12 +1,15 @@
require_relative '../../spec_helper' require_relative '../../spec_helper'
require 'net/ftp'
describe "Net::FTPPermError" do ruby_version_is ""..."3.1" do
it "is an Exception" do require 'net/ftp'
Net::FTPPermError.should < Exception
end
it "is a subclass of Net::FTPError" do describe "Net::FTPPermError" do
Net::FTPPermError.should < Net::FTPError it "is an Exception" do
Net::FTPPermError.should < Exception
end
it "is a subclass of Net::FTPError" do
Net::FTPPermError.should < Net::FTPError
end
end end
end end

View file

@ -1,12 +1,15 @@
require_relative '../../spec_helper' require_relative '../../spec_helper'
require 'net/ftp'
describe "Net::FTPProtoError" do ruby_version_is ""..."3.1" do
it "is an Exception" do require 'net/ftp'
Net::FTPProtoError.should < Exception
end
it "is a subclass of Net::FTPError" do describe "Net::FTPProtoError" do
Net::FTPPermError.should < Net::FTPError it "is an Exception" do
Net::FTPProtoError.should < Exception
end
it "is a subclass of Net::FTPError" do
Net::FTPPermError.should < Net::FTPError
end
end end
end end

Some files were not shown because too many files have changed in this diff Show more