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
data/spec/divide_spec.rb
ADDED
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
require File.expand_path('../spec_helper', __FILE__)
|
|
2
|
+
require File.expand_path('../fixtures/classes', __FILE__)
|
|
3
|
+
require 'matrix'
|
|
4
|
+
|
|
5
|
+
describe "Matrix#/" do
|
|
6
|
+
before :each do
|
|
7
|
+
@a = Matrix[ [1, 2], [3, 4] ]
|
|
8
|
+
@b = Matrix[ [4, 5], [6, 7] ]
|
|
9
|
+
@c = Matrix[ [1.2, 2.4], [3.6, 4.8] ]
|
|
10
|
+
end
|
|
11
|
+
|
|
12
|
+
ruby_bug "?", "1.8.7" do
|
|
13
|
+
it "returns the result of dividing self by another Matrix" do
|
|
14
|
+
(@a / @b).should be_close_to_matrix([[2.5, -1.5], [1.5, -0.5]])
|
|
15
|
+
end
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
conflicts_with :Prime do
|
|
19
|
+
it "returns the result of dividing self by a Fixnum" do
|
|
20
|
+
(@a / 2).should == Matrix[ [0, 1], [1, 2] ]
|
|
21
|
+
end
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
conflicts_with :Prime do
|
|
25
|
+
it "returns the result of dividing self by a Bignum" do
|
|
26
|
+
(@a / bignum_value).should == Matrix[ [0, 0], [0, 0] ]
|
|
27
|
+
end
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
it "returns the result of dividing self by a Float" do
|
|
31
|
+
(@c / 1.2).should == Matrix[ [1, 2], [3, 4] ]
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
it "raises a Matrix::ErrDimensionMismatch if the matrices are different sizes" do
|
|
35
|
+
lambda { @a / Matrix[ [1] ] }.should raise_error(Matrix::ErrDimensionMismatch)
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
it "returns an instance of Matrix" do
|
|
39
|
+
(@a /@b).should be_kind_of(Matrix)
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
ruby_bug "redmine #5307", "1.9.3" do
|
|
43
|
+
describe "for a subclass of Matrix" do
|
|
44
|
+
it "returns an instance of that subclass" do
|
|
45
|
+
m = MatrixSub.ins
|
|
46
|
+
(m/m).should be_an_instance_of(MatrixSub)
|
|
47
|
+
(m/1).should be_an_instance_of(MatrixSub)
|
|
48
|
+
end
|
|
49
|
+
end
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
ruby_bug "redmine:2365", "1.8.7" do
|
|
53
|
+
it "raises a TypeError if other is of wrong type" do
|
|
54
|
+
lambda { @a / nil }.should raise_error(TypeError)
|
|
55
|
+
lambda { @a / "a" }.should raise_error(TypeError)
|
|
56
|
+
lambda { @a / [ [1, 2] ] }.should raise_error(TypeError)
|
|
57
|
+
lambda { @a / Object.new }.should raise_error(TypeError)
|
|
58
|
+
end
|
|
59
|
+
end
|
|
60
|
+
end
|
data/spec/each_spec.rb
ADDED
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
require 'matrix'
|
|
2
|
+
|
|
3
|
+
ruby_version_is "1.9.1" do
|
|
4
|
+
describe "Matrix#each" do
|
|
5
|
+
before :all do
|
|
6
|
+
@m = Matrix[ [1, 2, 3], [4, 5, 6] ]
|
|
7
|
+
@result = (1..6).to_a
|
|
8
|
+
end
|
|
9
|
+
|
|
10
|
+
it "returns an Enumerator when called without a block" do
|
|
11
|
+
enum = @m.each
|
|
12
|
+
enum.should be_an_instance_of(enumerator_class)
|
|
13
|
+
enum.to_a.should == @result
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
it "returns self" do
|
|
17
|
+
@m.each{}.should equal(@m)
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
it "yields the elements starting with the those of the first row" do
|
|
21
|
+
a = []
|
|
22
|
+
@m.each {|x| a << x}
|
|
23
|
+
a.should == @result
|
|
24
|
+
end
|
|
25
|
+
end
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
ruby_version_is "1.9.3" do
|
|
29
|
+
describe "Matrix#each with an argument" do
|
|
30
|
+
before :all do
|
|
31
|
+
@m = Matrix[ [1, 2, 3, 4], [5, 6, 7, 8] ]
|
|
32
|
+
@t = Matrix[ [1, 2], [3, 4], [5, 6], [7, 8] ]
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
it "raises an ArgumentError for unrecognized argument" do
|
|
36
|
+
lambda {
|
|
37
|
+
@m.each("all"){}
|
|
38
|
+
}.should raise_error(ArgumentError)
|
|
39
|
+
lambda {
|
|
40
|
+
@m.each(nil){}
|
|
41
|
+
}.should raise_error(ArgumentError)
|
|
42
|
+
lambda {
|
|
43
|
+
@m.each(:left){}
|
|
44
|
+
}.should raise_error(ArgumentError)
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
it "yields the rights elements when passed :diagonal" do
|
|
48
|
+
@m.each(:diagonal).to_a.should == [1, 6]
|
|
49
|
+
@t.each(:diagonal).to_a.should == [1, 4]
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
it "yields the rights elements when passed :off_diagonal" do
|
|
53
|
+
@m.each(:off_diagonal).to_a.should == [2, 3, 4, 5, 7, 8]
|
|
54
|
+
@t.each(:off_diagonal).to_a.should == [2, 3, 5, 6, 7, 8]
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
it "yields the rights elements when passed :lower" do
|
|
58
|
+
@m.each(:lower).to_a.should == [1, 5, 6]
|
|
59
|
+
@t.each(:lower).to_a.should == [1, 3, 4, 5, 6, 7, 8]
|
|
60
|
+
end
|
|
61
|
+
|
|
62
|
+
it "yields the rights elements when passed :strict_lower" do
|
|
63
|
+
@m.each(:strict_lower).to_a.should == [5]
|
|
64
|
+
@t.each(:strict_lower).to_a.should == [3, 5, 6, 7, 8]
|
|
65
|
+
end
|
|
66
|
+
|
|
67
|
+
it "yields the rights elements when passed :strict_upper" do
|
|
68
|
+
@m.each(:strict_upper).to_a.should == [2, 3, 4, 7, 8]
|
|
69
|
+
@t.each(:strict_upper).to_a.should == [2]
|
|
70
|
+
end
|
|
71
|
+
|
|
72
|
+
it "yields the rights elements when passed :upper" do
|
|
73
|
+
@m.each(:upper).to_a.should == [1, 2, 3, 4, 6, 7, 8]
|
|
74
|
+
@t.each(:upper).to_a.should == [1, 2, 4]
|
|
75
|
+
end
|
|
76
|
+
end
|
|
77
|
+
end
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
require 'matrix'
|
|
2
|
+
|
|
3
|
+
ruby_version_is "1.9.1" do
|
|
4
|
+
describe "Matrix#each_with_index" do
|
|
5
|
+
before :all do
|
|
6
|
+
@m = Matrix[ [1, 2, 3], [4, 5, 6] ]
|
|
7
|
+
@result = [
|
|
8
|
+
[1, 0, 0],
|
|
9
|
+
[2, 0, 1],
|
|
10
|
+
[3, 0, 2],
|
|
11
|
+
[4, 1, 0],
|
|
12
|
+
[5, 1, 1],
|
|
13
|
+
[6, 1, 2]
|
|
14
|
+
]
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
it "returns an Enumerator when called without a block" do
|
|
18
|
+
enum = @m.each_with_index
|
|
19
|
+
enum.should be_an_instance_of(enumerator_class)
|
|
20
|
+
enum.to_a.should == @result
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
it "returns self" do
|
|
24
|
+
@m.each_with_index{}.should equal(@m)
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
it "yields the elements starting with the those of the first row" do
|
|
28
|
+
a = []
|
|
29
|
+
@m.each_with_index {|x, r, c| a << [x, r, c]}
|
|
30
|
+
a.should == @result
|
|
31
|
+
end
|
|
32
|
+
end
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
ruby_version_is "1.9.3" do
|
|
36
|
+
describe "Matrix#each_with_index with an argument" do
|
|
37
|
+
before :all do
|
|
38
|
+
@m = Matrix[ [1, 2, 3, 4], [5, 6, 7, 8] ]
|
|
39
|
+
@t = Matrix[ [1, 2], [3, 4], [5, 6], [7, 8] ]
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
it "raises an ArgumentError for unrecognized argument" do
|
|
43
|
+
lambda {
|
|
44
|
+
@m.each_with_index("all"){}
|
|
45
|
+
}.should raise_error(ArgumentError)
|
|
46
|
+
lambda {
|
|
47
|
+
@m.each_with_index(nil){}
|
|
48
|
+
}.should raise_error(ArgumentError)
|
|
49
|
+
lambda {
|
|
50
|
+
@m.each_with_index(:left){}
|
|
51
|
+
}.should raise_error(ArgumentError)
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
it "yields the rights elements when passed :diagonal" do
|
|
55
|
+
@m.each_with_index(:diagonal).to_a.should == [[1, 0, 0], [6, 1, 1]]
|
|
56
|
+
@t.each_with_index(:diagonal).to_a.should == [[1, 0, 0], [4, 1, 1]]
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
it "yields the rights elements when passed :off_diagonal" do
|
|
60
|
+
@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]]
|
|
61
|
+
@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]]
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
it "yields the rights elements when passed :lower" do
|
|
65
|
+
@m.each_with_index(:lower).to_a.should == [[1, 0, 0], [5, 1, 0], [6, 1, 1]]
|
|
66
|
+
@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]]
|
|
67
|
+
end
|
|
68
|
+
|
|
69
|
+
it "yields the rights elements when passed :strict_lower" do
|
|
70
|
+
@m.each_with_index(:strict_lower).to_a.should == [[5, 1, 0]]
|
|
71
|
+
@t.each_with_index(:strict_lower).to_a.should == [[3, 1, 0], [5, 2, 0], [6, 2, 1], [7, 3, 0], [8, 3, 1]]
|
|
72
|
+
end
|
|
73
|
+
|
|
74
|
+
it "yields the rights elements when passed :strict_upper" do
|
|
75
|
+
@m.each_with_index(:strict_upper).to_a.should == [[2, 0, 1], [3, 0, 2], [4, 0, 3], [7, 1, 2], [8, 1, 3]]
|
|
76
|
+
@t.each_with_index(:strict_upper).to_a.should == [[2, 0, 1]]
|
|
77
|
+
end
|
|
78
|
+
|
|
79
|
+
it "yields the rights elements when passed :upper" do
|
|
80
|
+
@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]]
|
|
81
|
+
@t.each_with_index(:upper).to_a.should == [[1, 0, 0], [2, 0, 1], [4, 1, 1]]
|
|
82
|
+
end
|
|
83
|
+
end
|
|
84
|
+
end
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
require 'matrix'
|
|
2
|
+
|
|
3
|
+
ruby_version_is "1.9.3" do
|
|
4
|
+
describe "Matrix::EigenvalueDecomposition#eigenvalue_matrix" do
|
|
5
|
+
it "returns a diagonal matrix with the eigenvalues on the diagonal" do
|
|
6
|
+
Matrix[[14, 16], [-6, -6]].eigensystem.eigenvalue_matrix.should == Matrix[[6, 0],[0, 2]]
|
|
7
|
+
Matrix[[1, 1], [-1, 1]].eigensystem.eigenvalue_matrix.should == Matrix[[Complex(1,1), 0],[0, Complex(1,-1)]]
|
|
8
|
+
end
|
|
9
|
+
end
|
|
10
|
+
end
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
require 'matrix'
|
|
2
|
+
|
|
3
|
+
ruby_version_is "1.9.3" do
|
|
4
|
+
describe "Matrix::EigenvalueDecomposition#eigenvalues" do
|
|
5
|
+
it "returns an array of complex eigenvalues for a rotation matrix" do
|
|
6
|
+
Matrix[[ 1, 1],
|
|
7
|
+
[-1, 1]].eigensystem.eigenvalues.sort_by{|v| v.imag}.should ==
|
|
8
|
+
[ Complex(1, -1), Complex(1, 1)]
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
it "returns an array of real eigenvalues for a symetric matrix" do
|
|
12
|
+
Matrix[[1, 2],
|
|
13
|
+
[2, 1]].eigensystem.eigenvalues.sort.map!{|x| x.round(10)}.should ==
|
|
14
|
+
[ -1, 3 ]
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
it "returns an array of real eigenvalues for a matrix" do
|
|
18
|
+
Matrix[[14, 16],
|
|
19
|
+
[-6, -6]].eigensystem.eigenvalues.sort.map!{|x| x.round(10)}.should ==
|
|
20
|
+
[ 2, 6 ]
|
|
21
|
+
end
|
|
22
|
+
end
|
|
23
|
+
end
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
require 'matrix'
|
|
2
|
+
|
|
3
|
+
ruby_version_is "1.9.3" do
|
|
4
|
+
describe "Matrix::EigenvalueDecomposition#eigenvector_matrix" do
|
|
5
|
+
ruby_bug '#7208', '1.9.3' do
|
|
6
|
+
it "returns a complex eigenvector matrix given a rotation matrix" do
|
|
7
|
+
# Fix me: should test for linearity, not for equality
|
|
8
|
+
Matrix[[ 1, 1],
|
|
9
|
+
[-1, 1]].eigensystem.eigenvector_matrix.should ==
|
|
10
|
+
Matrix[[1, 1],
|
|
11
|
+
[Complex(0, 1), Complex(0, -1)]]
|
|
12
|
+
end
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
it "returns an real eigenvector matrix for a symetric matrix" do
|
|
16
|
+
# Fix me: should test for linearity, not for equality
|
|
17
|
+
Matrix[[1, 2],
|
|
18
|
+
[2, 1]].eigensystem.eigenvector_matrix.should ==
|
|
19
|
+
Matrix[[0.7071067811865475, 0.7071067811865475],
|
|
20
|
+
[-0.7071067811865475, 0.7071067811865475]]
|
|
21
|
+
end
|
|
22
|
+
end
|
|
23
|
+
end
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
require 'matrix'
|
|
2
|
+
|
|
3
|
+
ruby_version_is "1.9.3" do
|
|
4
|
+
describe "Matrix::EigenvalueDecomposition#eigenvectors" do
|
|
5
|
+
ruby_bug '#7208', '1.9.3' do
|
|
6
|
+
it "returns an array of complex eigenvectors for a rotation matrix" do
|
|
7
|
+
# Fix me: should test for linearity, not for equality
|
|
8
|
+
Matrix[[ 1, 1],
|
|
9
|
+
[-1, 1]].eigensystem.eigenvectors.should ==
|
|
10
|
+
[ Vector[1, Complex(0, 1)],
|
|
11
|
+
Vector[1, Complex(0, -1)]
|
|
12
|
+
]
|
|
13
|
+
end
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
it "returns an array of real eigenvectors for a symetric matrix" do
|
|
17
|
+
# Fix me: should test for linearity, not for equality
|
|
18
|
+
Matrix[[1, 2],
|
|
19
|
+
[2, 1]].eigensystem.eigenvectors.should ==
|
|
20
|
+
[ Vector[0.7071067811865475, -0.7071067811865475],
|
|
21
|
+
Vector[0.7071067811865475, 0.7071067811865475]
|
|
22
|
+
]
|
|
23
|
+
end
|
|
24
|
+
end
|
|
25
|
+
end
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
require 'matrix'
|
|
2
|
+
|
|
3
|
+
ruby_version_is "1.9.3" do
|
|
4
|
+
describe "Matrix::EigenvalueDecomposition#initialize" do
|
|
5
|
+
it "raises an error if argument is not a matrix" do
|
|
6
|
+
lambda {
|
|
7
|
+
Matrix::EigenvalueDecomposition.new([[]])
|
|
8
|
+
}.should raise_error(TypeError)
|
|
9
|
+
lambda {
|
|
10
|
+
Matrix::EigenvalueDecomposition.new(42)
|
|
11
|
+
}.should raise_error(TypeError)
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
it "raises an error if matrix is not square" do
|
|
15
|
+
lambda {
|
|
16
|
+
Matrix::EigenvalueDecomposition.new(Matrix[[1, 2]])
|
|
17
|
+
}.should raise_error(Matrix::ErrDimensionMismatch)
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
ruby_bug "Fixed in Jama 1.0.3", "1.9.3" do
|
|
21
|
+
it "never hangs" do
|
|
22
|
+
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] ]
|
|
23
|
+
Matrix::EigenvalueDecomposition.new(m).should_not == "infinite loop"
|
|
24
|
+
end
|
|
25
|
+
end
|
|
26
|
+
end
|
|
27
|
+
end
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
require 'matrix'
|
|
2
|
+
|
|
3
|
+
ruby_version_is "1.9.3" do
|
|
4
|
+
describe "Matrix::EigenvalueDecomposition#to_a" do
|
|
5
|
+
before :each do
|
|
6
|
+
@a = Matrix[[14, 16], [-6, -6]]
|
|
7
|
+
@e = Matrix::EigenvalueDecomposition.new(@a)
|
|
8
|
+
end
|
|
9
|
+
|
|
10
|
+
it "returns an array of with [V, D, V.inv]" do
|
|
11
|
+
@e.to_a.should == [@e.v, @e.d, @e.v_inv]
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
it "returns a factorization" do
|
|
15
|
+
v, d, v_inv = @e.to_a
|
|
16
|
+
(v * d * v_inv).map{|e| e.round(10)}.should == @a
|
|
17
|
+
end
|
|
18
|
+
end
|
|
19
|
+
end
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
require 'matrix'
|
|
2
|
+
|
|
3
|
+
describe "Matrix#[]" do
|
|
4
|
+
|
|
5
|
+
before(:all) do
|
|
6
|
+
@m = Matrix[[0, 1, 2], [3, 4, 5], [6, 7, 8], [9, 10, 11]]
|
|
7
|
+
end
|
|
8
|
+
|
|
9
|
+
it "returns element at (i, j)" do
|
|
10
|
+
(0..3).each do |i|
|
|
11
|
+
(0..2).each do |j|
|
|
12
|
+
@m[i, j].should == (i * 3) + j
|
|
13
|
+
end
|
|
14
|
+
end
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
ruby_bug "#1518", "1.8.7" do
|
|
18
|
+
it "returns nil for an invalid index pair" do
|
|
19
|
+
@m[8,1].should be_nil
|
|
20
|
+
@m[1,8].should be_nil
|
|
21
|
+
end
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
end
|
data/spec/empty_spec.rb
ADDED
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
require File.expand_path('../fixtures/classes', __FILE__)
|
|
2
|
+
require 'matrix'
|
|
3
|
+
|
|
4
|
+
ruby_version_is "1.8.8" do
|
|
5
|
+
describe "Matrix#empty?" do
|
|
6
|
+
it "returns true when the Matrix is empty" do
|
|
7
|
+
Matrix[ ].empty?.should be_true
|
|
8
|
+
Matrix[ [], [], [] ].empty?.should be_true
|
|
9
|
+
Matrix[ [], [], [] ].transpose.empty?.should be_true
|
|
10
|
+
end
|
|
11
|
+
|
|
12
|
+
it "returns false when the Matrix has elements" do
|
|
13
|
+
Matrix[ [1, 2] ].empty?.should be_false
|
|
14
|
+
Matrix[ [1], [2] ].empty?.should be_false
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
it "doesn't accept any parameter" do
|
|
18
|
+
lambda{
|
|
19
|
+
Matrix[ [1, 2] ].empty?(42)
|
|
20
|
+
}.should raise_error(ArgumentError)
|
|
21
|
+
end
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
describe "Matrix.empty" do
|
|
25
|
+
it "returns an empty matrix of the requested size" do
|
|
26
|
+
m = Matrix.empty(3, 0)
|
|
27
|
+
m.row_size.should == 3
|
|
28
|
+
m.column_size.should == 0
|
|
29
|
+
|
|
30
|
+
m = Matrix.empty(0, 3)
|
|
31
|
+
m.row_size.should == 0
|
|
32
|
+
m.column_size.should == 3
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
it "has arguments defaulting to 0" do
|
|
36
|
+
Matrix.empty.should == Matrix.empty(0, 0)
|
|
37
|
+
Matrix.empty(42).should == Matrix.empty(42, 0)
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
it "does not accept more than two parameters" do
|
|
41
|
+
lambda{
|
|
42
|
+
Matrix.empty(1, 2, 3)
|
|
43
|
+
}.should raise_error(ArgumentError)
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
it "raises an error if both dimensions are > 0" do
|
|
47
|
+
lambda{
|
|
48
|
+
Matrix.empty(1, 2)
|
|
49
|
+
}.should raise_error(ArgumentError)
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
it "raises an error if any dimension is < 0" do
|
|
53
|
+
lambda{
|
|
54
|
+
Matrix.empty(-2, 0)
|
|
55
|
+
}.should raise_error(ArgumentError)
|
|
56
|
+
|
|
57
|
+
lambda{
|
|
58
|
+
Matrix.empty(0, -2)
|
|
59
|
+
}.should raise_error(ArgumentError)
|
|
60
|
+
end
|
|
61
|
+
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
describe "for a subclass of Matrix" do
|
|
65
|
+
it "returns an instance of that subclass" do
|
|
66
|
+
MatrixSub.empty(0, 1).should be_an_instance_of(MatrixSub)
|
|
67
|
+
end
|
|
68
|
+
end
|
|
69
|
+
end
|