diff options
author | marcandre <marcandre@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2014-04-06 17:45:54 +0000 |
---|---|---|
committer | marcandre <marcandre@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2014-04-06 17:45:54 +0000 |
commit | b8ba202301e5e4cf392a827c9bb8ebf2265de99f (patch) | |
tree | 0448fc788c02c6012adf8d0a6e0ec07962cd7f3a | |
parent | 1657d516e92002a009b52d992529a04abd33a0ce (diff) | |
download | ruby-b8ba202301e5e4cf392a827c9bb8ebf2265de99f.tar.gz |
* lib/matrix.rb: Add first_minor [fix GH-568]
Patch by gogotanaka
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@45527 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
-rw-r--r-- | NEWS | 5 | ||||
-rw-r--r-- | lib/matrix.rb | 29 | ||||
-rw-r--r-- | test/matrix/test_matrix.rb | 12 |
3 files changed, 46 insertions, 0 deletions
@@ -27,6 +27,11 @@ with all sufficient information, see the ChangeLog file. * Most symbols which are returned by String#to_sym and String#intern are GC-able. +* Matrix + * New methods: + * Matrix#first_minor(row, column) returns the submatrix obtained + by deleting the specified row and column. + === Core classes compatibility issues (excluding feature bug fixes) * IO diff --git a/lib/matrix.rb b/lib/matrix.rb index e0995c394f..7502e6ef8b 100644 --- a/lib/matrix.rb +++ b/lib/matrix.rb @@ -58,6 +58,7 @@ end # * #each_with_index # * #find_index # * #minor(*param) +# * #first_minor(row, column) # # Properties of a matrix: # * #diagonal? @@ -590,6 +591,34 @@ class Matrix new_matrix rows, [column_count - from_col, size_col].min end + # + # Returns the submatrix obtained by deleting the specified row and column. + # + # Matrix.diagonal(9, 5, -3, 4).first_minor(1, 2) + # => 9 0 0 + # 0 0 0 + # 0 0 4 + # + def first_minor(row, column) + raise RuntimeError, "first_minor of empty matrix is not defined" if empty? + + unless 0 <= row && row < row_count + raise ArgumentError, "invalid row (#{row.inspect} for 0..#{row_count - 1})" + end + + unless 0 <= column && column < column_count + raise ArgumentError, "invalid column (#{column.inspect} for 0..#{column_count - 1})" + end + + arrays = to_a + arrays.delete_at(row) + arrays.each do |array| + array.delete_at(column) + end + + new_matrix arrays, column_count - 1 + end + #-- # TESTING -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- #++ diff --git a/test/matrix/test_matrix.rb b/test/matrix/test_matrix.rb index 575b7b0cd6..2412f00822 100644 --- a/test/matrix/test_matrix.rb +++ b/test/matrix/test_matrix.rb @@ -250,6 +250,18 @@ class TestMatrix < Test::Unit::TestCase assert_raise(ArgumentError) { @m1.minor(0) } end + def test_first_minor + assert_equal(Matrix.empty(0, 0), Matrix[[1]].first_minor(0, 0)) + assert_equal(Matrix.empty(0, 2), Matrix[[1, 4, 2]].first_minor(0, 1)) + assert_equal(Matrix[[1, 3]], @m1.first_minor(1, 1)) + assert_equal(Matrix[[4, 6]], @m1.first_minor(0, 1)) + assert_equal(Matrix[[1, 2]], @m1.first_minor(1, 2)) + assert_raise(RuntimeError) { Matrix.empty(0, 0).first_minor(0, 0) } + assert_raise(ArgumentError) { @m1.first_minor(4, 0) } + assert_raise(ArgumentError) { @m1.first_minor(0, -1) } + assert_raise(ArgumentError) { @m1.first_minor(-1, 4) } + end + def test_regular? assert(Matrix[[1, 0], [0, 1]].regular?) assert(Matrix[[1, 0, 0], [0, 1, 0], [0, 0, 1]].regular?) |