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.
Files changed (121) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +17 -0
  3. data/.travis.yml +8 -0
  4. data/Gemfile +4 -0
  5. data/LICENSE +25 -0
  6. data/README.md +29 -0
  7. data/Rakefile +1 -0
  8. data/lib/matrix.rb +1 -0
  9. data/lib/rubysl/matrix.rb +2 -0
  10. data/lib/rubysl/matrix/matrix.rb +1537 -0
  11. data/lib/rubysl/matrix/version.rb +5 -0
  12. data/rubysl-matrix.gemspec +27 -0
  13. data/spec/I_spec.rb +5 -0
  14. data/spec/build_spec.rb +75 -0
  15. data/spec/clone_spec.rb +26 -0
  16. data/spec/coerce_spec.rb +11 -0
  17. data/spec/collect_spec.rb +5 -0
  18. data/spec/column_size_spec.rb +14 -0
  19. data/spec/column_spec.rb +36 -0
  20. data/spec/column_vector_spec.rb +36 -0
  21. data/spec/column_vectors_spec.rb +25 -0
  22. data/spec/columns_spec.rb +45 -0
  23. data/spec/conj_spec.rb +7 -0
  24. data/spec/conjugate_spec.rb +7 -0
  25. data/spec/constructor_spec.rb +68 -0
  26. data/spec/det_spec.rb +6 -0
  27. data/spec/determinant_spec.rb +6 -0
  28. data/spec/diagonal_spec.rb +73 -0
  29. data/spec/divide_spec.rb +60 -0
  30. data/spec/each_spec.rb +77 -0
  31. data/spec/each_with_index_spec.rb +84 -0
  32. data/spec/eigenvalue_decomposition/eigenvalue_matrix_spec.rb +10 -0
  33. data/spec/eigenvalue_decomposition/eigenvalues_spec.rb +23 -0
  34. data/spec/eigenvalue_decomposition/eigenvector_matrix_spec.rb +23 -0
  35. data/spec/eigenvalue_decomposition/eigenvectors_spec.rb +25 -0
  36. data/spec/eigenvalue_decomposition/initialize_spec.rb +27 -0
  37. data/spec/eigenvalue_decomposition/to_a_spec.rb +19 -0
  38. data/spec/element_reference_spec.rb +24 -0
  39. data/spec/empty_spec.rb +69 -0
  40. data/spec/eql_spec.rb +12 -0
  41. data/spec/equal_value_spec.rb +10 -0
  42. data/spec/exponent_spec.rb +66 -0
  43. data/spec/find_index_spec.rb +147 -0
  44. data/spec/fixtures/classes.rb +7 -0
  45. data/spec/hash_spec.rb +14 -0
  46. data/spec/hermitian_spec.rb +37 -0
  47. data/spec/identity_spec.rb +5 -0
  48. data/spec/imag_spec.rb +7 -0
  49. data/spec/imaginary_spec.rb +7 -0
  50. data/spec/inspect_spec.rb +30 -0
  51. data/spec/inv_spec.rb +6 -0
  52. data/spec/inverse_from_spec.rb +5 -0
  53. data/spec/inverse_spec.rb +6 -0
  54. data/spec/lower_triangular_spec.rb +25 -0
  55. data/spec/lup_decomposition/determinant_spec.rb +24 -0
  56. data/spec/lup_decomposition/initialize_spec.rb +14 -0
  57. data/spec/lup_decomposition/l_spec.rb +19 -0
  58. data/spec/lup_decomposition/p_spec.rb +19 -0
  59. data/spec/lup_decomposition/solve_spec.rb +54 -0
  60. data/spec/lup_decomposition/to_a_spec.rb +36 -0
  61. data/spec/lup_decomposition/u_spec.rb +19 -0
  62. data/spec/map_spec.rb +5 -0
  63. data/spec/minor_spec.rb +90 -0
  64. data/spec/minus_spec.rb +45 -0
  65. data/spec/multiply_spec.rb +73 -0
  66. data/spec/new_spec.rb +7 -0
  67. data/spec/normal_spec.rb +27 -0
  68. data/spec/orthogonal_spec.rb +27 -0
  69. data/spec/permutation_spec.rb +33 -0
  70. data/spec/plus_spec.rb +45 -0
  71. data/spec/rank_spec.rb +24 -0
  72. data/spec/real_spec.rb +45 -0
  73. data/spec/rect_spec.rb +7 -0
  74. data/spec/rectangular_spec.rb +7 -0
  75. data/spec/regular_spec.rb +34 -0
  76. data/spec/round_spec.rb +24 -0
  77. data/spec/row_size_spec.rb +12 -0
  78. data/spec/row_spec.rb +37 -0
  79. data/spec/row_vector_spec.rb +33 -0
  80. data/spec/row_vectors_spec.rb +25 -0
  81. data/spec/rows_spec.rb +40 -0
  82. data/spec/scalar/Fail_spec.rb +5 -0
  83. data/spec/scalar/Raise_spec.rb +5 -0
  84. data/spec/scalar/divide_spec.rb +5 -0
  85. data/spec/scalar/exponent_spec.rb +5 -0
  86. data/spec/scalar/included_spec.rb +5 -0
  87. data/spec/scalar/initialize_spec.rb +5 -0
  88. data/spec/scalar/minus_spec.rb +5 -0
  89. data/spec/scalar/multiply_spec.rb +5 -0
  90. data/spec/scalar/plus_spec.rb +5 -0
  91. data/spec/scalar_spec.rb +66 -0
  92. data/spec/shared/collect.rb +29 -0
  93. data/spec/shared/conjugate.rb +21 -0
  94. data/spec/shared/determinant.rb +53 -0
  95. data/spec/shared/equal_value.rb +35 -0
  96. data/spec/shared/identity.rb +21 -0
  97. data/spec/shared/imaginary.rb +22 -0
  98. data/spec/shared/inverse.rb +42 -0
  99. data/spec/shared/rectangular.rb +20 -0
  100. data/spec/shared/trace.rb +14 -0
  101. data/spec/shared/transpose.rb +23 -0
  102. data/spec/singular_spec.rb +34 -0
  103. data/spec/spec_helper.rb +35 -0
  104. data/spec/square_spec.rb +29 -0
  105. data/spec/symmetric_spec.rb +30 -0
  106. data/spec/t_spec.rb +5 -0
  107. data/spec/to_a_spec.rb +5 -0
  108. data/spec/to_s_spec.rb +5 -0
  109. data/spec/tr_spec.rb +6 -0
  110. data/spec/trace_spec.rb +6 -0
  111. data/spec/transpose_spec.rb +5 -0
  112. data/spec/unit_spec.rb +5 -0
  113. data/spec/unitary_spec.rb +29 -0
  114. data/spec/upper_triangular_spec.rb +26 -0
  115. data/spec/vector/cross_product_spec.rb +25 -0
  116. data/spec/vector/each2_spec.rb +52 -0
  117. data/spec/vector/eql_spec.rb +17 -0
  118. data/spec/vector/inner_product_spec.rb +25 -0
  119. data/spec/vector/normalize_spec.rb +19 -0
  120. data/spec/zero_spec.rb +55 -0
  121. metadata +341 -0
@@ -0,0 +1,45 @@
1
+ require File.expand_path('../fixtures/classes', __FILE__)
2
+ require 'matrix'
3
+
4
+ describe "Matrix#+" do
5
+ before :each do
6
+ @a = Matrix[ [1,2], [3,4] ]
7
+ @b = Matrix[ [4,5], [6,7] ]
8
+ end
9
+
10
+ it "returns the result of adding the corresponding elements of self and other" do
11
+ (@a + @b).should == Matrix[ [5,7], [9,11] ]
12
+ end
13
+
14
+ it "returns an instance of Matrix" do
15
+ (@a + @b).should be_kind_of(Matrix)
16
+ end
17
+
18
+ it "raises a Matrix::ErrDimensionMismatch if the matrices are different sizes" do
19
+ lambda { @a + Matrix[ [1] ] }.should raise_error(Matrix::ErrDimensionMismatch)
20
+ end
21
+
22
+ it "raises a ExceptionForMatrix::ErrOperationNotDefined if other is a Numeric Type" do
23
+ lambda { @a + 2 }.should raise_error(ExceptionForMatrix::ErrOperationNotDefined)
24
+ lambda { @a + 1.2 }.should raise_error(ExceptionForMatrix::ErrOperationNotDefined)
25
+ lambda { @a + bignum_value }.should raise_error(ExceptionForMatrix::ErrOperationNotDefined)
26
+ end
27
+
28
+ ruby_bug "redmine:2365", "1.8.7" do
29
+ it "raises a TypeError if other is of wrong type" do
30
+ lambda { @a + nil }.should raise_error(TypeError)
31
+ lambda { @a + "a" }.should raise_error(TypeError)
32
+ lambda { @a + [ [1, 2] ] }.should raise_error(TypeError)
33
+ lambda { @a + Object.new }.should raise_error(TypeError)
34
+ end
35
+ end
36
+
37
+ ruby_bug "redmine #5307", "1.9.3" do
38
+ describe "for a subclass of Matrix" do
39
+ it "returns an instance of that subclass" do
40
+ m = MatrixSub.ins
41
+ (m+m).should be_an_instance_of(MatrixSub)
42
+ end
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,24 @@
1
+ require 'matrix'
2
+
3
+ describe "Matrix#rank" do
4
+ it "returns the rank of the Matrix" do
5
+ Matrix[ [7,6], [3,9] ].rank.should == 2
6
+ end
7
+
8
+ # The examples below seems to indicate a regression in MRI 1.9.1, as it works
9
+ # in 1.8.7. On 1.9.1 it just hangs.
10
+ ruby_bug "#1020", "1.9.1.500" do
11
+ it "doesn't loop forever" do
12
+ Matrix[ [1,2,3], [4,5,6], [7,8,9] ].rank.should == 2
13
+ Matrix[ [1, 2, 0, 3], [1, -2, 3, 0], [0, 0, 4, 8], [2, 4, 0, 6] ].rank.
14
+ should == 3
15
+ end
16
+ end
17
+
18
+ ruby_bug "revision 24969", "1.8.7" do
19
+ it "works for some easy rectangular matrices" do
20
+ Matrix[[0,0],[0,0],[1,0]].rank.should == 1
21
+ Matrix[[0,1],[0,0],[1,0]].rank.should == 2
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,45 @@
1
+ require File.expand_path('../fixtures/classes', __FILE__)
2
+ require 'matrix'
3
+
4
+ ruby_version_is "1.9" do
5
+ describe "Matrix#real?" do
6
+ it "returns true for matrices with all real entries" do
7
+ Matrix[ [1, 2], [3, 4] ].real?.should be_true
8
+ Matrix[ [1.9, 2], [3, 4] ].real?.should be_true
9
+ end
10
+
11
+ it "returns true for empty matrices" do
12
+ Matrix.empty.real?.should be_true
13
+ end
14
+
15
+ it "returns false if one element is a Complex" do
16
+ Matrix[ [Complex(1,1), 2], [3, 4] ].real?.should be_false
17
+ end
18
+
19
+ conflicts_with :CMath do
20
+ it "returns false if one element is a Complex whose imaginary part is 0" do
21
+ Matrix[ [Complex(1,0), 2], [3, 4] ].real?.should be_false
22
+ end
23
+ end
24
+ end
25
+
26
+ describe "Matrix#real" do
27
+ it "returns a matrix with the real part of the elements of the receiver" do
28
+ Matrix[ [1, 2], [3, 4] ].real.should == Matrix[ [1, 2], [3, 4] ]
29
+ Matrix[ [1.9, Complex(1,1)], [Complex(-0.42, 0), 4] ].real.should == Matrix[ [1.9, 1], [-0.42, 4] ]
30
+ end
31
+
32
+ it "returns empty matrices on the same size if empty" do
33
+ Matrix.empty(0, 3).real.should == Matrix.empty(0, 3)
34
+ Matrix.empty(3, 0).real.should == Matrix.empty(3, 0)
35
+ end
36
+
37
+ ruby_bug "redmine #5307", "1.9.3" do
38
+ describe "for a subclass of Matrix" do
39
+ it "returns an instance of that subclass" do
40
+ MatrixSub.ins.real.should be_an_instance_of(MatrixSub)
41
+ end
42
+ end
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,7 @@
1
+ require File.expand_path('../shared/rectangular', __FILE__)
2
+
3
+ ruby_version_is "1.9" do
4
+ describe "Matrix#rect" do
5
+ it_behaves_like(:matrix_rectangular, :rect)
6
+ end
7
+ end
@@ -0,0 +1,7 @@
1
+ require File.expand_path('../shared/rectangular', __FILE__)
2
+
3
+ ruby_version_is "1.9" do
4
+ describe "Matrix#rectangular" do
5
+ it_behaves_like(:matrix_rectangular, :rectangular)
6
+ end
7
+ end
@@ -0,0 +1,34 @@
1
+ require 'matrix'
2
+
3
+ describe "Matrix#regular?" do
4
+
5
+ ruby_bug "#1020", "1.8.7" do
6
+ it "returns false for singular matrices" do
7
+ m = Matrix[ [1,2,3], [3,4,3], [0,0,0] ]
8
+ m.regular?.should be_false
9
+
10
+ m = Matrix[ [1,2,9], [3,4,9], [1,2,9] ]
11
+ m.regular?.should be_false
12
+ end
13
+
14
+ it "returns true if the Matrix is regular" do
15
+ Matrix[ [0,1], [1,0] ].regular?.should be_true
16
+ end
17
+ end
18
+
19
+ ruby_bug "", "1.8.7" do
20
+ it "returns true for an empty 0x0 matrix" do
21
+ Matrix.empty(0,0).regular?.should be_true
22
+ end
23
+
24
+ it "raises an error for rectangular matrices" do
25
+ lambda {
26
+ Matrix[[1], [2], [3]].regular?
27
+ }.should raise_error(Matrix::ErrDimensionMismatch)
28
+
29
+ lambda {
30
+ Matrix.empty(3,0).regular?
31
+ }.should raise_error(Matrix::ErrDimensionMismatch)
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,24 @@
1
+ require File.expand_path('../fixtures/classes', __FILE__)
2
+ require 'matrix'
3
+
4
+ ruby_version_is "1.9.3" do
5
+ describe "Matrix#round" do
6
+ it "returns a matrix with all entries rounded" do
7
+ Matrix[ [1, 2.34], [5.67, 8] ].round.should == Matrix[ [1, 2], [6, 8] ]
8
+ Matrix[ [1, 2.34], [5.67, 8] ].round(1).should == Matrix[ [1, 2.3], [5.7, 8] ]
9
+ end
10
+
11
+ it "returns empty matrices on the same size if empty" do
12
+ Matrix.empty(0, 3).round.should == Matrix.empty(0, 3)
13
+ Matrix.empty(3, 0).round(42).should == Matrix.empty(3, 0)
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.round.should be_an_instance_of(MatrixSub)
20
+ end
21
+ end
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,12 @@
1
+ require 'matrix'
2
+
3
+ describe "Matrix#row_size" do
4
+ it "returns the number rows" do
5
+ Matrix[ [1,2], [3, 4], [5, 6] ].row_size.should == 3
6
+ end
7
+
8
+ it "returns the number rows even for some empty matrices" do
9
+ Matrix[ [], [], [] ].row_size.should == 3
10
+ end
11
+
12
+ end
@@ -0,0 +1,37 @@
1
+ require 'matrix'
2
+
3
+ describe "Matrix#row" do
4
+ before :all do
5
+ @m = Matrix[ [1, 2], [2, 3], [3, 4] ]
6
+ end
7
+
8
+ it "returns a Vector when called without a block" do
9
+ @m.row(0).should == Vector[1,2]
10
+ end
11
+
12
+ it "yields the elements of the row when called with a block" do
13
+ a = []
14
+ @m.row(0) {|x| a << x}
15
+ a.should == [1,2]
16
+ end
17
+
18
+ it "counts backwards for negative argument" do
19
+ @m.row(-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.row(0) { |x| x }.should equal(@m)
25
+ end
26
+
27
+ it "returns nil when out of bounds" do
28
+ @m.row(3).should == nil
29
+ @m.row(-4).should == nil
30
+ end
31
+
32
+ it "never yields when out of bounds" do
33
+ lambda { @m.row(3){ raise } }.should_not raise_error
34
+ lambda { @m.row(-4){ raise } }.should_not raise_error
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,33 @@
1
+ require File.expand_path('../fixtures/classes', __FILE__)
2
+ require 'matrix'
3
+
4
+ describe "Matrix.row_vector" do
5
+
6
+ it "returns a Matrix" do
7
+ Matrix.row_vector([]).should be_an_instance_of(Matrix)
8
+ end
9
+
10
+ it "returns a single-row Matrix with the specified values" do
11
+ Matrix.row_vector([1,2]).should == Matrix[ [1,2] ]
12
+ end
13
+
14
+ it "returns a 1x0 matrix when called with an empty Array" do
15
+ Matrix.row_vector([]).should == Matrix[ [] ]
16
+ end
17
+
18
+ ruby_version_is ""..."1.9" do
19
+ describe "for a subclass of Matrix" do
20
+ it "returns an instance of Matrix" do
21
+ MatrixSub.row_vector([1]).should be_an_instance_of(Matrix)
22
+ end
23
+ end
24
+ end
25
+
26
+ ruby_version_is "1.9" do
27
+ describe "for a subclass of Matrix" do
28
+ it "returns an instance of that subclass" do
29
+ MatrixSub.row_vector([1]).should be_an_instance_of(MatrixSub)
30
+ end
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,25 @@
1
+ require 'matrix'
2
+
3
+ describe "Matrix#row_vectors" do
4
+
5
+ before(:each) do
6
+ @vectors = Matrix[ [1,2], [3,4] ].row_vectors
7
+ end
8
+
9
+ it "returns an Array" do
10
+ Matrix[ [1,2], [3,4] ].row_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 row as a Vector" do
18
+ @vectors.should == [Vector[1,2], Vector[3,4]]
19
+ end
20
+
21
+ it "returns an empty Array for empty matrices" do
22
+ Matrix[].row_vectors.should == []
23
+ Matrix[ [] ].row_vectors.should == [ Vector[] ]
24
+ end
25
+ end
@@ -0,0 +1,40 @@
1
+ require File.expand_path('../fixtures/classes', __FILE__)
2
+ require 'matrix'
3
+
4
+ describe "Matrix.rows" do
5
+ before :each do
6
+ @a = [1, 2]
7
+ @b = [3, 4]
8
+ @m = Matrix.rows([@a, @b])
9
+ end
10
+
11
+ it "returns a Matrix" do
12
+ @m.should be_kind_of(Matrix)
13
+ end
14
+
15
+ it "creates a matrix from argument rows" do
16
+ @m.row(0).to_a.should == @a
17
+ @m.row(1).to_a.should == @b
18
+ end
19
+
20
+ it "copies the original rows by default" do
21
+ @a << 3
22
+ @b << 6
23
+ @m.row(0).should_not equal(@a)
24
+ @m.row(1).should_not equal(@b)
25
+ end
26
+
27
+ it "references the original rows if copy is false" do
28
+ @m_ref = Matrix.rows([@a, @b], false)
29
+ @a << 3
30
+ @b << 6
31
+ @m_ref.row(0).to_a.should == @a
32
+ @m_ref.row(1).to_a.should == @b
33
+ end
34
+
35
+ describe "for a subclass of Matrix" do
36
+ it "returns an instance of that subclass" do
37
+ MatrixSub.rows([[0, 1], [0, 1]]).should be_an_instance_of(MatrixSub)
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,5 @@
1
+ require 'matrix'
2
+
3
+ describe "Matrix::Scalar#Fail" do
4
+ it "needs to be reviewed for spec completeness"
5
+ end
@@ -0,0 +1,5 @@
1
+ require 'matrix'
2
+
3
+ describe "Matrix::Scalar#Raise" do
4
+ it "needs to be reviewed for spec completeness"
5
+ end
@@ -0,0 +1,5 @@
1
+ require 'matrix'
2
+
3
+ describe "Matrix::Scalar#/" do
4
+ it "needs to be reviewed for spec completeness"
5
+ end
@@ -0,0 +1,5 @@
1
+ require 'matrix'
2
+
3
+ describe "Matrix::Scalar#**" do
4
+ it "needs to be reviewed for spec completeness"
5
+ end
@@ -0,0 +1,5 @@
1
+ require 'matrix'
2
+
3
+ describe "Matrix::Scalar.included" do
4
+ it "needs to be reviewed for spec completeness"
5
+ end
@@ -0,0 +1,5 @@
1
+ require 'matrix'
2
+
3
+ describe "Matrix::Scalar#initialize" do
4
+ it "needs to be reviewed for spec completeness"
5
+ end
@@ -0,0 +1,5 @@
1
+ require 'matrix'
2
+
3
+ describe "Matrix::Scalar#-" do
4
+ it "needs to be reviewed for spec completeness"
5
+ end
@@ -0,0 +1,5 @@
1
+ require 'matrix'
2
+
3
+ describe "Matrix::Scalar#*" do
4
+ it "needs to be reviewed for spec completeness"
5
+ end
@@ -0,0 +1,5 @@
1
+ require 'matrix'
2
+
3
+ describe "Matrix::Scalar#+" do
4
+ it "needs to be reviewed for spec completeness"
5
+ end
@@ -0,0 +1,66 @@
1
+ require 'matrix'
2
+
3
+ describe "Matrix.scalar" do
4
+
5
+ before(:each) do
6
+ @side = 3
7
+ @value = 8
8
+ @a = Matrix.scalar(@side, @value)
9
+ end
10
+
11
+ it "returns a Matrix" do
12
+ @a.should be_kind_of(Matrix)
13
+ end
14
+
15
+ it "returns a n x n matrix" do
16
+ @a.row_size.should == @side
17
+ @a.column_size.should == @side
18
+ end
19
+
20
+ it "initializes diagonal to value" do
21
+ (0...@a.row_size).each do |i|
22
+ @a[i, i].should == @value
23
+ end
24
+ end
25
+
26
+ it "initializes all non-diagonal values to 0" do
27
+ (0...@a.row_size).each do |i|
28
+ (0...@a.column_size).each do |j|
29
+ if i != j
30
+ @a[i, j].should == 0
31
+ end
32
+ end
33
+ end
34
+ end
35
+
36
+ before(:each) do
37
+ @side = 3
38
+ @value = 8
39
+ @a = Matrix.scalar(@side, @value)
40
+ end
41
+
42
+ it "returns a Matrix" do
43
+ @a.should be_kind_of(Matrix)
44
+ end
45
+
46
+ it "returns a square matrix, where the first argument specifies the side of the square" do
47
+ @a.row_size.should == @side
48
+ @a.column_size.should == @side
49
+ end
50
+
51
+ it "puts the second argument in all diagonal values" do
52
+ (0...@a.row_size).each do |i|
53
+ @a[i, i].should == @value
54
+ end
55
+ end
56
+
57
+ it "fills all values not on the main diagonal with 0" do
58
+ (0...@a.row_size).each do |i|
59
+ (0...@a.column_size).each do |j|
60
+ if i != j
61
+ @a[i, j].should == 0
62
+ end
63
+ end
64
+ end
65
+ end
66
+ end