rubysl-matrix 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- 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,29 @@
|
|
|
1
|
+
require 'matrix'
|
|
2
|
+
|
|
3
|
+
describe :collect, :shared => true do
|
|
4
|
+
before :all do
|
|
5
|
+
@m = Matrix[ [1, 2], [1, 2] ]
|
|
6
|
+
end
|
|
7
|
+
|
|
8
|
+
it "returns an instance of Matrix" do
|
|
9
|
+
@m.send(@method){|n| n * 2 }.should be_kind_of(Matrix)
|
|
10
|
+
end
|
|
11
|
+
|
|
12
|
+
it "returns a Matrix where each element is the result of the block" do
|
|
13
|
+
@m.send(@method) { |n| n * 2 }.should == Matrix[ [2, 4], [2, 4] ]
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
ruby_bug "#1531", "1.8.7" do
|
|
17
|
+
it "returns an enumerator if no block is given" do
|
|
18
|
+
@m.send(@method).should be_an_instance_of(enumerator_class)
|
|
19
|
+
end
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
ruby_bug "redmine #5307", "1.9.3" do
|
|
23
|
+
describe "for a subclass of Matrix" do
|
|
24
|
+
it "returns an instance of that subclass" do
|
|
25
|
+
MatrixSub.ins.send(@method){1}.should be_an_instance_of(MatrixSub)
|
|
26
|
+
end
|
|
27
|
+
end
|
|
28
|
+
end
|
|
29
|
+
end
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
require 'matrix'
|
|
2
|
+
|
|
3
|
+
describe :matrix_conjugate, :shared => true do
|
|
4
|
+
it "returns a matrix with all entries 'conjugated'" do
|
|
5
|
+
Matrix[ [1, 2], [3, 4] ].send(@method).should == Matrix[ [1, 2], [3, 4] ]
|
|
6
|
+
Matrix[ [1.9, Complex(1,1)], [3, 4] ].send(@method).should == Matrix[ [1.9, Complex(1,-1)], [3, 4] ]
|
|
7
|
+
end
|
|
8
|
+
|
|
9
|
+
it "returns empty matrices on the same size if empty" do
|
|
10
|
+
Matrix.empty(0, 3).send(@method).should == Matrix.empty(0, 3)
|
|
11
|
+
Matrix.empty(3, 0).send(@method).should == Matrix.empty(3, 0)
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
ruby_bug "redmine #5307", "1.9.3" do
|
|
15
|
+
describe "for a subclass of Matrix" do
|
|
16
|
+
it "returns an instance of that subclass" do
|
|
17
|
+
MatrixSub.ins.send(@method).should be_an_instance_of(MatrixSub)
|
|
18
|
+
end
|
|
19
|
+
end
|
|
20
|
+
end
|
|
21
|
+
end
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
require 'matrix'
|
|
2
|
+
|
|
3
|
+
describe :determinant, :shared => true do
|
|
4
|
+
# Ruby versions less than 1.9.0, AFAICT, return the wrong determinant in
|
|
5
|
+
# most non-trivial cases. As the rdoc for Matrix suggests, "require 'mathn'"
|
|
6
|
+
# seems to fix this, but as Matrix doesn't require that library itself, the
|
|
7
|
+
# bug remains. I've reported http://redmine.ruby-lang.org/issues/show/1516 ,
|
|
8
|
+
# which is ostensibly about the documentation for #determinant not
|
|
9
|
+
# reflecting this bug, but raises the question of why 1.8.7 doesn't handle
|
|
10
|
+
# this properly.
|
|
11
|
+
ruby_bug "#1516", "1.8.7" do
|
|
12
|
+
it "returns the determinant of a square Matrix" do
|
|
13
|
+
m = Matrix[ [7,6], [3,9] ]
|
|
14
|
+
m.send(@method).should == 45
|
|
15
|
+
|
|
16
|
+
m = Matrix[ [9, 8], [6,5] ]
|
|
17
|
+
m.send(@method).should == -3
|
|
18
|
+
|
|
19
|
+
m = Matrix[ [9,8,3], [4,20,5], [1,1,1] ]
|
|
20
|
+
m.send(@method).should == 95
|
|
21
|
+
end
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
it "returns the determinant of a single-element Matrix" do
|
|
25
|
+
m = Matrix[ [2] ]
|
|
26
|
+
m.send(@method).should == 2
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
ruby_bug "redmine:1532", "1.8.7" do
|
|
30
|
+
it "returns 1 for an empty Matrix" do
|
|
31
|
+
m = Matrix[ ]
|
|
32
|
+
m.send(@method).should == 1
|
|
33
|
+
end
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
ruby_bug "#1531", "1.8.7" do
|
|
37
|
+
it "returns the determinant even for Matrices containing 0 as first entry" do
|
|
38
|
+
Matrix[[0,1],[1,0]].send(@method).should == -1
|
|
39
|
+
end
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
ruby_bug "#2770", "1.8.7" do
|
|
43
|
+
it "raises an error for rectangular matrices" do
|
|
44
|
+
lambda {
|
|
45
|
+
Matrix[[1], [2], [3]].send(@method)
|
|
46
|
+
}.should raise_error(Matrix::ErrDimensionMismatch)
|
|
47
|
+
|
|
48
|
+
lambda {
|
|
49
|
+
Matrix.empty(3,0).send(@method)
|
|
50
|
+
}.should raise_error(Matrix::ErrDimensionMismatch)
|
|
51
|
+
end
|
|
52
|
+
end
|
|
53
|
+
end
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
require File.expand_path('../../fixtures/classes', __FILE__)
|
|
2
|
+
require 'matrix'
|
|
3
|
+
|
|
4
|
+
describe :equal, :shared => true do
|
|
5
|
+
before do
|
|
6
|
+
@matrix = Matrix[ [1, 2, 3, 4, 5], [2, 3, 4, 5, 6] ]
|
|
7
|
+
end
|
|
8
|
+
|
|
9
|
+
it "returns true for self" do
|
|
10
|
+
@matrix.send(@method, @matrix).should be_true
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
it "returns true for equal matrices" do
|
|
14
|
+
@matrix.send(@method, Matrix[ [1, 2, 3, 4, 5], [2, 3, 4, 5, 6] ]).should be_true
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
it "returns false for different matrices" do
|
|
18
|
+
@matrix.send(@method, Matrix[ [42, 2, 3, 4, 5], [2, 3, 4, 5, 6] ]).should be_false
|
|
19
|
+
@matrix.send(@method, Matrix[ [1, 2, 3, 4, 5, 6], [2, 3, 4, 5, 6, 7] ]).should be_false
|
|
20
|
+
@matrix.send(@method, Matrix[ [1, 2, 3], [2, 3, 4] ]).should be_false
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
ruby_version_is "1.8.8" do
|
|
24
|
+
it "returns false for different empty matrices" do
|
|
25
|
+
Matrix.empty(42, 0).send(@method, Matrix.empty(6, 0)).should be_false
|
|
26
|
+
Matrix.empty(0, 42).send(@method, Matrix.empty(0, 6)).should be_false
|
|
27
|
+
Matrix.empty(0, 0).send(@method, Matrix.empty(6, 0)).should be_false
|
|
28
|
+
Matrix.empty(0, 0).send(@method, Matrix.empty(0, 6)).should be_false
|
|
29
|
+
end
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
it "doesn't distinguish on subclasses" do
|
|
33
|
+
MatrixSub.ins.send(@method, Matrix.I(2)).should be_true
|
|
34
|
+
end
|
|
35
|
+
end
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
require File.expand_path('../../fixtures/classes', __FILE__)
|
|
2
|
+
require 'matrix'
|
|
3
|
+
|
|
4
|
+
describe :matrix_identity, :shared => true do
|
|
5
|
+
it "returns a Matrix" do
|
|
6
|
+
Matrix.send(@method, 2).should be_kind_of(Matrix)
|
|
7
|
+
end
|
|
8
|
+
|
|
9
|
+
it "returns a n x n identity matrix" do
|
|
10
|
+
Matrix.send(@method, 3).should == Matrix.scalar(3, 1)
|
|
11
|
+
Matrix.send(@method, 100).should == Matrix.scalar(100, 1)
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
ruby_bug "redmine #5307", "1.9.3" do
|
|
15
|
+
describe "for a subclass of Matrix" do
|
|
16
|
+
it "returns an instance of that subclass" do
|
|
17
|
+
MatrixSub.send(@method, 2).should be_an_instance_of(MatrixSub)
|
|
18
|
+
end
|
|
19
|
+
end
|
|
20
|
+
end
|
|
21
|
+
end
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
require File.expand_path('../../fixtures/classes', __FILE__)
|
|
2
|
+
require 'matrix'
|
|
3
|
+
|
|
4
|
+
describe :matrix_imaginary, :shared => true do
|
|
5
|
+
it "returns a matrix with the imaginary part of the elements of the receiver" do
|
|
6
|
+
Matrix[ [1, 2], [3, 4] ].send(@method).should == Matrix[ [0, 0], [0, 0] ]
|
|
7
|
+
Matrix[ [1.9, Complex(1,1)], [Complex(-2,0.42), 4] ].send(@method).should == Matrix[ [0, 1], [0.42, 0] ]
|
|
8
|
+
end
|
|
9
|
+
|
|
10
|
+
it "returns empty matrices on the same size if empty" do
|
|
11
|
+
Matrix.empty(0, 3).send(@method).should == Matrix.empty(0, 3)
|
|
12
|
+
Matrix.empty(3, 0).send(@method).should == Matrix.empty(3, 0)
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
ruby_bug "redmine #5307", "1.9.3" do
|
|
16
|
+
describe "for a subclass of Matrix" do
|
|
17
|
+
it "returns an instance of that subclass" do
|
|
18
|
+
MatrixSub.ins.send(@method).should be_an_instance_of(MatrixSub)
|
|
19
|
+
end
|
|
20
|
+
end
|
|
21
|
+
end
|
|
22
|
+
end
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
require File.expand_path('../../fixtures/classes', __FILE__)
|
|
2
|
+
require 'matrix'
|
|
3
|
+
|
|
4
|
+
describe :inverse, :shared => true do
|
|
5
|
+
|
|
6
|
+
it "returns a Matrix" do
|
|
7
|
+
Matrix[ [1,2], [2,1] ].send(@method).should be_an_instance_of(Matrix)
|
|
8
|
+
end
|
|
9
|
+
|
|
10
|
+
it "returns the inverse of the Matrix" do
|
|
11
|
+
Matrix[
|
|
12
|
+
[1, 3, 3], [1, 4, 3], [1, 3, 4]
|
|
13
|
+
].send(@method).should ==
|
|
14
|
+
Matrix[
|
|
15
|
+
[7, -3, -3], [-1, 1, 0], [-1, 0, 1]
|
|
16
|
+
]
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
ruby_bug "?", "1.8.7" do
|
|
20
|
+
it "returns the inverse of the Matrix (other case)" do
|
|
21
|
+
Matrix[
|
|
22
|
+
[1, 2, 3], [0, 1, 4], [5, 6, 0]
|
|
23
|
+
].send(@method).should be_close_to_matrix([
|
|
24
|
+
[-24, 18, 5], [20, -15, -4], [-5, 4, 1]
|
|
25
|
+
])
|
|
26
|
+
end
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
it "raises a ErrDimensionMismatch if the Matrix is not square" do
|
|
30
|
+
lambda{
|
|
31
|
+
Matrix[ [1,2,3], [1,2,3] ].send(@method)
|
|
32
|
+
}.should raise_error(Matrix::ErrDimensionMismatch)
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
ruby_bug "redmine #5307", "1.9.3" do
|
|
36
|
+
describe "for a subclass of Matrix" do
|
|
37
|
+
it "returns an instance of that subclass" do
|
|
38
|
+
MatrixSub.ins.send(@method).should be_an_instance_of(MatrixSub)
|
|
39
|
+
end
|
|
40
|
+
end
|
|
41
|
+
end
|
|
42
|
+
end
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
require File.expand_path('../../fixtures/classes', __FILE__)
|
|
2
|
+
require 'matrix'
|
|
3
|
+
|
|
4
|
+
describe :matrix_rectangular, :shared => true do
|
|
5
|
+
it "returns [receiver.real, receiver.imag]" do
|
|
6
|
+
m = Matrix[ [1.2, Complex(1,2)], [Complex(-2,0.42), 4] ]
|
|
7
|
+
m.send(@method).should == [m.real, m.imag]
|
|
8
|
+
|
|
9
|
+
m = Matrix.empty(3, 0)
|
|
10
|
+
m.send(@method).should == [m.real, m.imag]
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
ruby_bug "redmine #5307", "1.9.3" do
|
|
14
|
+
describe "for a subclass of Matrix" do
|
|
15
|
+
it "returns instances of that subclass" do
|
|
16
|
+
MatrixSub.ins.send(@method).each{|m| m.should be_an_instance_of(MatrixSub) }
|
|
17
|
+
end
|
|
18
|
+
end
|
|
19
|
+
end
|
|
20
|
+
end
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
require 'matrix'
|
|
2
|
+
|
|
3
|
+
describe :trace, :shared => true do
|
|
4
|
+
it "returns the sum of diagonal elements in a square Matrix" do
|
|
5
|
+
Matrix[[7,6], [3,9]].trace.should == 16
|
|
6
|
+
end
|
|
7
|
+
|
|
8
|
+
ruby_bug "redmine:1532", "1.8.7" do
|
|
9
|
+
it "returns the sum of diagonal elements in a rectangular Matrix" do
|
|
10
|
+
lambda{ Matrix[[1,2,3], [4,5,6]].trace}.should raise_error(Matrix::ErrDimensionMismatch)
|
|
11
|
+
end
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
end
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
require File.expand_path('../../fixtures/classes', __FILE__)
|
|
2
|
+
require 'matrix'
|
|
3
|
+
|
|
4
|
+
describe :matrix_transpose, :shared => true do
|
|
5
|
+
it "returns a transposed matrix" do
|
|
6
|
+
Matrix[[1, 2], [3, 4], [5, 6]].send(@method).should == Matrix[[1, 3, 5], [2, 4, 6]]
|
|
7
|
+
end
|
|
8
|
+
|
|
9
|
+
ruby_bug "redmine:1532", "1.8.7" do
|
|
10
|
+
it "can transpose empty matrices" do
|
|
11
|
+
m = Matrix[[], [], []]
|
|
12
|
+
m.send(@method).send(@method).should == m
|
|
13
|
+
end
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
ruby_bug "redmine #5307", "1.9.3" do
|
|
17
|
+
describe "for a subclass of Matrix" do
|
|
18
|
+
it "returns an instance of that subclass" do
|
|
19
|
+
MatrixSub.ins.send(@method).should be_an_instance_of(MatrixSub)
|
|
20
|
+
end
|
|
21
|
+
end
|
|
22
|
+
end
|
|
23
|
+
end
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
require 'matrix'
|
|
2
|
+
|
|
3
|
+
describe "Matrix#singular?" do
|
|
4
|
+
ruby_bug "#1020", "1.8.7" do
|
|
5
|
+
it "returns true for singular matrices" do
|
|
6
|
+
m = Matrix[ [1,2,3], [3,4,3], [0,0,0] ]
|
|
7
|
+
m.singular?.should be_true
|
|
8
|
+
|
|
9
|
+
m = Matrix[ [1,2,9], [3,4,9], [1,2,9] ]
|
|
10
|
+
m.singular?.should be_true
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
it "returns false if the Matrix is regular" do
|
|
14
|
+
Matrix[ [0,1], [1,0] ].singular?.should be_false
|
|
15
|
+
end
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
ruby_bug "", "1.8.7" do
|
|
19
|
+
it "returns false for an empty 0x0 matrix" do
|
|
20
|
+
Matrix.empty(0,0).singular?.should be_false
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
it "raises an error for rectangular matrices" do
|
|
24
|
+
lambda {
|
|
25
|
+
Matrix[[1], [2], [3]].singular?
|
|
26
|
+
}.should raise_error(Matrix::ErrDimensionMismatch)
|
|
27
|
+
|
|
28
|
+
lambda {
|
|
29
|
+
Matrix.empty(3,0).singular?
|
|
30
|
+
}.should raise_error(Matrix::ErrDimensionMismatch)
|
|
31
|
+
end
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
end
|
data/spec/spec_helper.rb
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
class BeCloseToMatrixMatcher
|
|
2
|
+
def initialize(expected, tolerance = TOLERANCE)
|
|
3
|
+
SpecExpectation.matcher! rescue "Used with the balance_should_and_match branch of mspec"
|
|
4
|
+
@expected = Matrix[*expected]
|
|
5
|
+
@tolerance = tolerance
|
|
6
|
+
end
|
|
7
|
+
|
|
8
|
+
def matches?(actual)
|
|
9
|
+
@actual = actual
|
|
10
|
+
return false unless @actual.is_a? Matrix
|
|
11
|
+
return false unless @actual.row_size == @expected.row_size
|
|
12
|
+
@actual.row_size.times do |i|
|
|
13
|
+
a, e = @actual.row(i), @expected.row(i)
|
|
14
|
+
return false unless a.size == e.size
|
|
15
|
+
a.size.times do |j|
|
|
16
|
+
return false unless (a[j] - e[j]).abs < @tolerance
|
|
17
|
+
end
|
|
18
|
+
end
|
|
19
|
+
true
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
def failure_message
|
|
23
|
+
["Expected #{@expected}", "to be within +/- #{@tolerance} of #{@actual}"]
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
def negative_failure_message
|
|
27
|
+
["Expected #{@expected}", "not to be within +/- #{@tolerance} of #{@actual}"]
|
|
28
|
+
end
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
class Object
|
|
32
|
+
def be_close_to_matrix(expected, tolerance = TOLERANCE)
|
|
33
|
+
BeCloseToMatrixMatcher.new(expected, tolerance)
|
|
34
|
+
end
|
|
35
|
+
end
|
data/spec/square_spec.rb
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
require 'matrix'
|
|
2
|
+
|
|
3
|
+
describe "Matrix#square?" do
|
|
4
|
+
|
|
5
|
+
it "returns true when the Matrix is square" do
|
|
6
|
+
Matrix[ [1,2], [2,4] ].square?.should be_true
|
|
7
|
+
Matrix[ [100,3,5], [9.5, 4.9, 8], [2,0,77] ].square?.should be_true
|
|
8
|
+
end
|
|
9
|
+
|
|
10
|
+
it "returns true when the Matrix has only one element" do
|
|
11
|
+
Matrix[ [9] ].square?.should be_true
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
it "returns false when the Matrix is rectangular" do
|
|
15
|
+
Matrix[ [1, 2] ].square?.should be_false
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
it "returns false when the Matrix is rectangular" do
|
|
19
|
+
Matrix[ [1], [2] ].square?.should be_false
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
ruby_bug "redmine:1532", "1.8.7" do
|
|
23
|
+
it "returns handles empty matrices" do
|
|
24
|
+
Matrix[].square?.should be_true
|
|
25
|
+
Matrix[[]].square?.should be_false
|
|
26
|
+
Matrix.columns([[]]).square?.should be_false
|
|
27
|
+
end
|
|
28
|
+
end
|
|
29
|
+
end
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
require 'matrix'
|
|
2
|
+
|
|
3
|
+
ruby_version_is "1.9.3" do
|
|
4
|
+
describe "Matrix.symmetric?" do
|
|
5
|
+
it "returns true for a symmetric Matrix" do
|
|
6
|
+
Matrix[[1, 2, Complex(0, 3)], [2, 4, 5], [Complex(0, 3), 5, 6]].symmetric?.should be_true
|
|
7
|
+
end
|
|
8
|
+
|
|
9
|
+
it "returns true for a 0x0 empty matrix" do
|
|
10
|
+
Matrix.empty.symmetric?.should be_true
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
it "returns false for an assymetric Matrix" do
|
|
14
|
+
Matrix[[1, 2],[-2, 1]].symmetric?.should be_false
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
it "raises an error for rectangular matrices" do
|
|
18
|
+
[
|
|
19
|
+
Matrix[[0], [0]],
|
|
20
|
+
Matrix[[0, 0]],
|
|
21
|
+
Matrix.empty(0, 2),
|
|
22
|
+
Matrix.empty(2, 0),
|
|
23
|
+
].each do |rectangual_matrix|
|
|
24
|
+
lambda {
|
|
25
|
+
rectangual_matrix.symmetric?
|
|
26
|
+
}.should raise_error(Matrix::ErrDimensionMismatch)
|
|
27
|
+
end
|
|
28
|
+
end
|
|
29
|
+
end
|
|
30
|
+
end
|
data/spec/t_spec.rb
ADDED
data/spec/to_a_spec.rb
ADDED