rubysl-matrix 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +17 -0
- data/.travis.yml +8 -0
- data/Gemfile +4 -0
- data/LICENSE +25 -0
- data/README.md +29 -0
- data/Rakefile +1 -0
- data/lib/matrix.rb +1 -0
- data/lib/rubysl/matrix.rb +2 -0
- data/lib/rubysl/matrix/matrix.rb +1537 -0
- data/lib/rubysl/matrix/version.rb +5 -0
- data/rubysl-matrix.gemspec +27 -0
- data/spec/I_spec.rb +5 -0
- data/spec/build_spec.rb +75 -0
- data/spec/clone_spec.rb +26 -0
- data/spec/coerce_spec.rb +11 -0
- data/spec/collect_spec.rb +5 -0
- data/spec/column_size_spec.rb +14 -0
- data/spec/column_spec.rb +36 -0
- data/spec/column_vector_spec.rb +36 -0
- data/spec/column_vectors_spec.rb +25 -0
- data/spec/columns_spec.rb +45 -0
- data/spec/conj_spec.rb +7 -0
- data/spec/conjugate_spec.rb +7 -0
- data/spec/constructor_spec.rb +68 -0
- data/spec/det_spec.rb +6 -0
- data/spec/determinant_spec.rb +6 -0
- data/spec/diagonal_spec.rb +73 -0
- data/spec/divide_spec.rb +60 -0
- data/spec/each_spec.rb +77 -0
- data/spec/each_with_index_spec.rb +84 -0
- data/spec/eigenvalue_decomposition/eigenvalue_matrix_spec.rb +10 -0
- data/spec/eigenvalue_decomposition/eigenvalues_spec.rb +23 -0
- data/spec/eigenvalue_decomposition/eigenvector_matrix_spec.rb +23 -0
- data/spec/eigenvalue_decomposition/eigenvectors_spec.rb +25 -0
- data/spec/eigenvalue_decomposition/initialize_spec.rb +27 -0
- data/spec/eigenvalue_decomposition/to_a_spec.rb +19 -0
- data/spec/element_reference_spec.rb +24 -0
- data/spec/empty_spec.rb +69 -0
- data/spec/eql_spec.rb +12 -0
- data/spec/equal_value_spec.rb +10 -0
- data/spec/exponent_spec.rb +66 -0
- data/spec/find_index_spec.rb +147 -0
- data/spec/fixtures/classes.rb +7 -0
- data/spec/hash_spec.rb +14 -0
- data/spec/hermitian_spec.rb +37 -0
- data/spec/identity_spec.rb +5 -0
- data/spec/imag_spec.rb +7 -0
- data/spec/imaginary_spec.rb +7 -0
- data/spec/inspect_spec.rb +30 -0
- data/spec/inv_spec.rb +6 -0
- data/spec/inverse_from_spec.rb +5 -0
- data/spec/inverse_spec.rb +6 -0
- data/spec/lower_triangular_spec.rb +25 -0
- data/spec/lup_decomposition/determinant_spec.rb +24 -0
- data/spec/lup_decomposition/initialize_spec.rb +14 -0
- data/spec/lup_decomposition/l_spec.rb +19 -0
- data/spec/lup_decomposition/p_spec.rb +19 -0
- data/spec/lup_decomposition/solve_spec.rb +54 -0
- data/spec/lup_decomposition/to_a_spec.rb +36 -0
- data/spec/lup_decomposition/u_spec.rb +19 -0
- data/spec/map_spec.rb +5 -0
- data/spec/minor_spec.rb +90 -0
- data/spec/minus_spec.rb +45 -0
- data/spec/multiply_spec.rb +73 -0
- data/spec/new_spec.rb +7 -0
- data/spec/normal_spec.rb +27 -0
- data/spec/orthogonal_spec.rb +27 -0
- data/spec/permutation_spec.rb +33 -0
- data/spec/plus_spec.rb +45 -0
- data/spec/rank_spec.rb +24 -0
- data/spec/real_spec.rb +45 -0
- data/spec/rect_spec.rb +7 -0
- data/spec/rectangular_spec.rb +7 -0
- data/spec/regular_spec.rb +34 -0
- data/spec/round_spec.rb +24 -0
- data/spec/row_size_spec.rb +12 -0
- data/spec/row_spec.rb +37 -0
- data/spec/row_vector_spec.rb +33 -0
- data/spec/row_vectors_spec.rb +25 -0
- data/spec/rows_spec.rb +40 -0
- data/spec/scalar/Fail_spec.rb +5 -0
- data/spec/scalar/Raise_spec.rb +5 -0
- data/spec/scalar/divide_spec.rb +5 -0
- data/spec/scalar/exponent_spec.rb +5 -0
- data/spec/scalar/included_spec.rb +5 -0
- data/spec/scalar/initialize_spec.rb +5 -0
- data/spec/scalar/minus_spec.rb +5 -0
- data/spec/scalar/multiply_spec.rb +5 -0
- data/spec/scalar/plus_spec.rb +5 -0
- data/spec/scalar_spec.rb +66 -0
- data/spec/shared/collect.rb +29 -0
- data/spec/shared/conjugate.rb +21 -0
- data/spec/shared/determinant.rb +53 -0
- data/spec/shared/equal_value.rb +35 -0
- data/spec/shared/identity.rb +21 -0
- data/spec/shared/imaginary.rb +22 -0
- data/spec/shared/inverse.rb +42 -0
- data/spec/shared/rectangular.rb +20 -0
- data/spec/shared/trace.rb +14 -0
- data/spec/shared/transpose.rb +23 -0
- data/spec/singular_spec.rb +34 -0
- data/spec/spec_helper.rb +35 -0
- data/spec/square_spec.rb +29 -0
- data/spec/symmetric_spec.rb +30 -0
- data/spec/t_spec.rb +5 -0
- data/spec/to_a_spec.rb +5 -0
- data/spec/to_s_spec.rb +5 -0
- data/spec/tr_spec.rb +6 -0
- data/spec/trace_spec.rb +6 -0
- data/spec/transpose_spec.rb +5 -0
- data/spec/unit_spec.rb +5 -0
- data/spec/unitary_spec.rb +29 -0
- data/spec/upper_triangular_spec.rb +26 -0
- data/spec/vector/cross_product_spec.rb +25 -0
- data/spec/vector/each2_spec.rb +52 -0
- data/spec/vector/eql_spec.rb +17 -0
- data/spec/vector/inner_product_spec.rb +25 -0
- data/spec/vector/normalize_spec.rb +19 -0
- data/spec/zero_spec.rb +55 -0
- metadata +341 -0
@@ -0,0 +1,27 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
require './lib/rubysl/matrix/version'
|
3
|
+
|
4
|
+
Gem::Specification.new do |spec|
|
5
|
+
spec.name = "rubysl-matrix"
|
6
|
+
spec.version = RubySL::Matrix::VERSION
|
7
|
+
spec.authors = ["Brian Shirai"]
|
8
|
+
spec.email = ["brixen@gmail.com"]
|
9
|
+
spec.description = %q{Ruby standard library matrix.}
|
10
|
+
spec.summary = %q{Ruby standard library matrix.}
|
11
|
+
spec.homepage = "https://github.com/rubysl/rubysl-matrix"
|
12
|
+
spec.license = "BSD"
|
13
|
+
|
14
|
+
spec.files = `git ls-files`.split($/)
|
15
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
16
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
17
|
+
spec.require_paths = ["lib"]
|
18
|
+
|
19
|
+
spec.required_ruby_version = "~> 1.8.7"
|
20
|
+
|
21
|
+
spec.add_runtime_dependency "rubysl-e2mmap", "~> 1.0"
|
22
|
+
|
23
|
+
spec.add_development_dependency "bundler", "~> 1.3"
|
24
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
25
|
+
spec.add_development_dependency "mspec", "~> 1.5"
|
26
|
+
spec.add_development_dependency "rubysl-prettyprint", "~> 1.0"
|
27
|
+
end
|
data/spec/I_spec.rb
ADDED
data/spec/build_spec.rb
ADDED
@@ -0,0 +1,75 @@
|
|
1
|
+
require File.expand_path('../fixtures/classes', __FILE__)
|
2
|
+
require 'matrix'
|
3
|
+
|
4
|
+
ruby_version_is "1.9" do
|
5
|
+
describe "Matrix.build" do
|
6
|
+
|
7
|
+
it "returns a Matrix object of the given size" do
|
8
|
+
m = Matrix.build(3, 4){1}
|
9
|
+
m.should be_an_instance_of(Matrix)
|
10
|
+
m.row_size.should == 3
|
11
|
+
m.column_size.should == 4
|
12
|
+
end
|
13
|
+
|
14
|
+
it "builds the Matrix using the given block" do
|
15
|
+
Matrix.build(2, 3){|col, row| 10*col - row}.should ==
|
16
|
+
Matrix[[0, -1, -2], [10, 9, 8]]
|
17
|
+
end
|
18
|
+
|
19
|
+
it "iterates through the first row, then the second, ..." do
|
20
|
+
acc = []
|
21
|
+
Matrix.build(2, 3){|*args| acc << args}
|
22
|
+
acc.should == [[0, 0], [0, 1], [0, 2], [1, 0], [1, 1], [1, 2]]
|
23
|
+
end
|
24
|
+
|
25
|
+
it "returns an Enumerator is no block is given" do
|
26
|
+
enum = Matrix.build(2, 1)
|
27
|
+
enum.should be_an_instance_of(enumerator_class)
|
28
|
+
enum.each{1}.should == Matrix[[1], [1]]
|
29
|
+
end
|
30
|
+
|
31
|
+
it "requires integers as parameters" do
|
32
|
+
lambda { Matrix.build("1", "2"){1} }.should raise_error(TypeError)
|
33
|
+
lambda { Matrix.build(nil, nil){1} }.should raise_error(TypeError)
|
34
|
+
lambda { Matrix.build(1..2){1} }.should raise_error(TypeError)
|
35
|
+
end
|
36
|
+
|
37
|
+
it "requires non-negative integers" do
|
38
|
+
lambda { Matrix.build(-1, 1){1} }.should raise_error(ArgumentError)
|
39
|
+
lambda { Matrix.build(+1,-1){1} }.should raise_error(ArgumentError)
|
40
|
+
end
|
41
|
+
|
42
|
+
it "returns empty Matrix if one argument is zero" do
|
43
|
+
m = Matrix.build(0, 3){
|
44
|
+
raise "Should not yield"
|
45
|
+
}
|
46
|
+
m.should be_empty
|
47
|
+
m.column_size.should == 3
|
48
|
+
|
49
|
+
m = Matrix.build(3, 0){
|
50
|
+
raise "Should not yield"
|
51
|
+
}
|
52
|
+
m.should be_empty
|
53
|
+
m.row_size.should == 3
|
54
|
+
end
|
55
|
+
|
56
|
+
it "tries to calls :to_int on arguments" do
|
57
|
+
int = mock('int')
|
58
|
+
int.should_receive(:to_int).twice.and_return(2)
|
59
|
+
Matrix.build(int, int){ 1 }.should == Matrix[ [1,1], [1,1] ]
|
60
|
+
end
|
61
|
+
|
62
|
+
it "builds an nxn Matrix when given only one argument" do
|
63
|
+
m = Matrix.build(3){1}
|
64
|
+
m.row_size.should == 3
|
65
|
+
m.column_size.should == 3
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
69
|
+
describe "for a subclass of Matrix" do
|
70
|
+
it "returns an instance of that subclass" do
|
71
|
+
MatrixSub.build(3){1}.should be_an_instance_of(MatrixSub)
|
72
|
+
end
|
73
|
+
end
|
74
|
+
end
|
75
|
+
|
data/spec/clone_spec.rb
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
require File.expand_path('../fixtures/classes', __FILE__)
|
2
|
+
require 'matrix'
|
3
|
+
|
4
|
+
describe "Matrix#clone" do
|
5
|
+
before(:each) do
|
6
|
+
@a = Matrix[[1, 2], [3, 4], [5, 6]]
|
7
|
+
end
|
8
|
+
|
9
|
+
it "returns a shallow copy of the matrix" do
|
10
|
+
b = @a.clone
|
11
|
+
@a.should_not equal(b)
|
12
|
+
b.should be_kind_of(Matrix)
|
13
|
+
b.should == @a
|
14
|
+
0.upto(@a.row_size - 1) do |i|
|
15
|
+
@a.row(i).should_not equal(b.row(i))
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
ruby_bug "redmine #5307", "1.9.3" do
|
20
|
+
describe "for a subclass of Matrix" do
|
21
|
+
it "returns an instance of that subclass" do
|
22
|
+
MatrixSub.ins.clone.should be_an_instance_of(MatrixSub)
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
data/spec/coerce_spec.rb
ADDED
@@ -0,0 +1,11 @@
|
|
1
|
+
require 'matrix'
|
2
|
+
|
3
|
+
describe "Matrix#coerce" do
|
4
|
+
it "needs to be reviewed for spec completeness"
|
5
|
+
|
6
|
+
ruby_bug "revision 24952", "1.8.7" do
|
7
|
+
it "allows the division of fixnum by a Matrix " do
|
8
|
+
(1/Matrix[[0,1],[-1,0]]).should == Matrix[[0,-1],[1,0]]
|
9
|
+
end
|
10
|
+
end
|
11
|
+
end
|
@@ -0,0 +1,14 @@
|
|
1
|
+
require 'matrix'
|
2
|
+
|
3
|
+
describe "Matrix#column_size" do
|
4
|
+
it "returns the number of columns" do
|
5
|
+
Matrix[ [1,2], [3,4] ].column_size.should == 2
|
6
|
+
end
|
7
|
+
|
8
|
+
ruby_bug "redmine:1532", "1.8.7" do
|
9
|
+
it "returns 0 for empty matrices" do
|
10
|
+
Matrix[ [], [] ].column_size.should == 0
|
11
|
+
Matrix[ ].column_size.should == 0
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
data/spec/column_spec.rb
ADDED
@@ -0,0 +1,36 @@
|
|
1
|
+
require 'matrix'
|
2
|
+
|
3
|
+
describe "Matrix#column" do
|
4
|
+
before :all do
|
5
|
+
@m = Matrix[[1,2,3], [2,3,4]]
|
6
|
+
end
|
7
|
+
|
8
|
+
it "returns a Vector when called without a block" do
|
9
|
+
@m.column(1).should == Vector[2,3]
|
10
|
+
end
|
11
|
+
|
12
|
+
it "yields each element in the column to the block" do
|
13
|
+
a = []
|
14
|
+
@m.column(1) {|n| a << n }
|
15
|
+
a.should == [2,3]
|
16
|
+
end
|
17
|
+
|
18
|
+
it "counts backwards for negative argument" do
|
19
|
+
@m.column(-1).should == Vector[3, 4]
|
20
|
+
end
|
21
|
+
|
22
|
+
ruby_bug "redmine:1532", "1.8.7" do
|
23
|
+
it "returns self when called with a block" do
|
24
|
+
@m.column(0) { |x| x }.should equal(@m)
|
25
|
+
end
|
26
|
+
|
27
|
+
it "returns nil when out of bounds" do
|
28
|
+
@m.column(3).should == nil
|
29
|
+
end
|
30
|
+
|
31
|
+
it "never yields when out of bounds" do
|
32
|
+
lambda { @m.column(3){ raise } }.should_not raise_error
|
33
|
+
lambda { @m.column(-4){ raise } }.should_not raise_error
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
@@ -0,0 +1,36 @@
|
|
1
|
+
require File.expand_path('../fixtures/classes', __FILE__)
|
2
|
+
require 'matrix'
|
3
|
+
|
4
|
+
describe "Matrix.column_vector" do
|
5
|
+
|
6
|
+
it "returns a single column Matrix when called with an Array" do
|
7
|
+
m = Matrix.column_vector([4,5,6])
|
8
|
+
m.should be_an_instance_of(Matrix)
|
9
|
+
m.should == Matrix[ [4],[5],[6] ]
|
10
|
+
end
|
11
|
+
|
12
|
+
ruby_bug "redmine:1532", "1.8.7" do
|
13
|
+
it "returns an empty Matrix when called with an empty Array" do
|
14
|
+
m = Matrix.column_vector([])
|
15
|
+
m.should be_an_instance_of(Matrix)
|
16
|
+
m.row_size.should == 0
|
17
|
+
m.column_size.should == 1
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
ruby_version_is ""..."1.9" do
|
22
|
+
describe "for a subclass of Matrix" do
|
23
|
+
it "returns an instance of Matrix" do
|
24
|
+
MatrixSub.column_vector([4,5,6]).should be_an_instance_of(Matrix)
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
ruby_version_is "1.9" do
|
30
|
+
describe "for a subclass of Matrix" do
|
31
|
+
it "returns an instance of that subclass" do
|
32
|
+
MatrixSub.column_vector([4,5,6]).should be_an_instance_of(MatrixSub)
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
require 'matrix'
|
2
|
+
|
3
|
+
describe "Matrix#column_vectors" do
|
4
|
+
|
5
|
+
before(:each) do
|
6
|
+
@vectors = Matrix[ [1,2], [3,4] ].column_vectors
|
7
|
+
end
|
8
|
+
|
9
|
+
it "returns an Array" do
|
10
|
+
Matrix[ [1,2], [3,4] ].column_vectors.should be_an_instance_of(Array)
|
11
|
+
end
|
12
|
+
|
13
|
+
it "returns an Array of Vectors" do
|
14
|
+
@vectors.all? {|v| v.should be_an_instance_of(Vector)}
|
15
|
+
end
|
16
|
+
|
17
|
+
it "returns each column as a Vector" do
|
18
|
+
@vectors.should == [Vector[1,3], Vector[2,4]]
|
19
|
+
end
|
20
|
+
|
21
|
+
it "returns an empty Array for empty matrices" do
|
22
|
+
Matrix[ [] ].column_vectors.should == []
|
23
|
+
end
|
24
|
+
|
25
|
+
end
|
@@ -0,0 +1,45 @@
|
|
1
|
+
require File.expand_path('../fixtures/classes', __FILE__)
|
2
|
+
require 'matrix'
|
3
|
+
|
4
|
+
describe "Matrix.columns" do
|
5
|
+
before :each do
|
6
|
+
@a = [1, 2]
|
7
|
+
@b = [3, 4]
|
8
|
+
@m = Matrix.columns([@a, @b])
|
9
|
+
end
|
10
|
+
|
11
|
+
it "creates a Matrix from argument columns" do
|
12
|
+
@m.should be_an_instance_of(Matrix)
|
13
|
+
@m.column(0).to_a.should == @a
|
14
|
+
@m.column(1).to_a.should == @b
|
15
|
+
end
|
16
|
+
|
17
|
+
it "accepts Vectors as argument columns" do
|
18
|
+
m = Matrix.columns([Vector[*@a], Vector[*@b]])
|
19
|
+
m.should == @m
|
20
|
+
m.column(0).to_a.should == @a
|
21
|
+
m.column(1).to_a.should == @b
|
22
|
+
end
|
23
|
+
|
24
|
+
ruby_bug "redmine:1532", "1.8.7" do
|
25
|
+
it "handles empty matrices" do
|
26
|
+
e = Matrix.columns([])
|
27
|
+
e.row_size.should == 0
|
28
|
+
e.column_size.should == 0
|
29
|
+
e.should == Matrix[]
|
30
|
+
|
31
|
+
v = Matrix.columns([[],[],[]])
|
32
|
+
v.row_size.should == 0
|
33
|
+
v.column_size.should == 3
|
34
|
+
v.should == Matrix[[], [], []].transpose
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
ruby_bug "redmine #5307", "1.9.3" do
|
39
|
+
describe "for a subclass of Matrix" do
|
40
|
+
it "returns an instance of that subclass" do
|
41
|
+
MatrixSub.columns([[1]]).should be_an_instance_of(MatrixSub)
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
data/spec/conj_spec.rb
ADDED
@@ -0,0 +1,68 @@
|
|
1
|
+
require File.expand_path('../fixtures/classes', __FILE__)
|
2
|
+
require 'matrix'
|
3
|
+
|
4
|
+
describe "Matrix.[]" do
|
5
|
+
|
6
|
+
ruby_bug "redmine:1532", "1.8.7" do
|
7
|
+
it "requires arrays as parameters" do
|
8
|
+
lambda { Matrix[5] }.should raise_error(TypeError)
|
9
|
+
lambda { Matrix[nil] }.should raise_error(TypeError)
|
10
|
+
lambda { Matrix[1..2] }.should raise_error(TypeError)
|
11
|
+
lambda { Matrix[[1, 2], 3] }.should raise_error(TypeError)
|
12
|
+
end
|
13
|
+
|
14
|
+
it "creates an empty Matrix with no arguments" do
|
15
|
+
m = Matrix[]
|
16
|
+
m.column_size.should == 0
|
17
|
+
m.row_size.should == 0
|
18
|
+
end
|
19
|
+
|
20
|
+
it "raises for non-rectangular matrices" do
|
21
|
+
lambda{ Matrix[ [0], [0,1] ] }.should \
|
22
|
+
raise_error(Matrix::ErrDimensionMismatch)
|
23
|
+
lambda{ Matrix[ [0,1], [0,1,2], [0,1] ]}.should \
|
24
|
+
raise_error(Matrix::ErrDimensionMismatch)
|
25
|
+
end
|
26
|
+
|
27
|
+
it "accepts vector arguments" do
|
28
|
+
a = Matrix[Vector[1, 2], Vector[3, 4]]
|
29
|
+
a.should be_an_instance_of(Matrix)
|
30
|
+
a.should == Matrix[ [1, 2], [3, 4] ]
|
31
|
+
end
|
32
|
+
|
33
|
+
it "tries to calls :to_ary on arguments" do
|
34
|
+
array = mock('ary')
|
35
|
+
array.should_receive(:to_ary).and_return([1,2])
|
36
|
+
Matrix[array, [3,4] ].should == Matrix[ [1,2], [3,4] ]
|
37
|
+
end
|
38
|
+
|
39
|
+
end
|
40
|
+
|
41
|
+
it "returns a Matrix object" do
|
42
|
+
Matrix[ [1] ].should be_an_instance_of(Matrix)
|
43
|
+
end
|
44
|
+
|
45
|
+
it "can create an nxn Matrix" do
|
46
|
+
m = Matrix[ [20,30], [40.5, 9] ]
|
47
|
+
m.row_size.should == 2
|
48
|
+
m.column_size.should == 2
|
49
|
+
m.column(0).should == Vector[20, 40.5]
|
50
|
+
m.column(1).should == Vector[30, 9]
|
51
|
+
m.row(0).should == Vector[20, 30]
|
52
|
+
m.row(1).should == Vector[40.5, 9]
|
53
|
+
end
|
54
|
+
|
55
|
+
it "can create a 0xn Matrix" do
|
56
|
+
m = Matrix[ [], [], [] ]
|
57
|
+
m.row_size.should == 3
|
58
|
+
m.column_size.should == 0
|
59
|
+
end
|
60
|
+
|
61
|
+
ruby_bug "redmine #5307", "1.9.3" do
|
62
|
+
describe "for a subclass of Matrix" do
|
63
|
+
it "returns an instance of that subclass" do
|
64
|
+
MatrixSub[ [20,30], [40.5, 9] ].should be_an_instance_of(MatrixSub)
|
65
|
+
end
|
66
|
+
end
|
67
|
+
end
|
68
|
+
end
|
data/spec/det_spec.rb
ADDED
@@ -0,0 +1,73 @@
|
|
1
|
+
require File.expand_path('../fixtures/classes', __FILE__)
|
2
|
+
require 'matrix'
|
3
|
+
|
4
|
+
describe "Matrix.diagonal" do
|
5
|
+
before(:each) do
|
6
|
+
@m = Matrix.diagonal(10, 11, 12, 13, 14)
|
7
|
+
end
|
8
|
+
|
9
|
+
it "returns an object of type Matrix" do
|
10
|
+
@m.should be_kind_of(Matrix)
|
11
|
+
end
|
12
|
+
|
13
|
+
it "returns a square Matrix of the right size" do
|
14
|
+
@m.column_size.should == 5
|
15
|
+
@m.row_size.should == 5
|
16
|
+
end
|
17
|
+
|
18
|
+
it "sets the diagonal to the arguments" do
|
19
|
+
(0..4).each do |i|
|
20
|
+
@m[i, i].should == i + 10
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
it "fills all non-diagonal cells with 0" do
|
25
|
+
(0..4).each do |i|
|
26
|
+
(0..4).each do |j|
|
27
|
+
if i != j
|
28
|
+
@m[i, j].should == 0
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
describe "for a subclass of Matrix" do
|
35
|
+
it "returns an instance of that subclass" do
|
36
|
+
MatrixSub.diagonal(1).should be_an_instance_of(MatrixSub)
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
ruby_version_is "1.9.3" do
|
42
|
+
describe "Matrix.diagonal?" do
|
43
|
+
it "returns true for a diagonal Matrix" do
|
44
|
+
Matrix.diagonal([1, 2, 3]).diagonal?.should be_true
|
45
|
+
end
|
46
|
+
|
47
|
+
it "returns true for a zero square Matrix" do
|
48
|
+
Matrix.zero(3).diagonal?.should be_true
|
49
|
+
end
|
50
|
+
|
51
|
+
it "returns false for a non diagonal square Matrix" do
|
52
|
+
Matrix[[0, 1], [0, 0]].diagonal?.should be_false
|
53
|
+
Matrix[[1, 2, 3], [1, 2, 3], [1, 2, 3]].diagonal?.should be_false
|
54
|
+
end
|
55
|
+
|
56
|
+
it "returns true for an empty 0x0 matrix" do
|
57
|
+
Matrix.empty(0,0).diagonal?.should be_true
|
58
|
+
end
|
59
|
+
|
60
|
+
it "raises an error for rectangular matrices" do
|
61
|
+
[
|
62
|
+
Matrix[[0], [0]],
|
63
|
+
Matrix[[0, 0]],
|
64
|
+
Matrix.empty(0, 2),
|
65
|
+
Matrix.empty(2, 0),
|
66
|
+
].each do |rectangual_matrix|
|
67
|
+
lambda {
|
68
|
+
rectangual_matrix.diagonal?
|
69
|
+
}.should raise_error(Matrix::ErrDimensionMismatch)
|
70
|
+
end
|
71
|
+
end
|
72
|
+
end
|
73
|
+
end
|