1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

* 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
This commit is contained in:
marcandre 2014-04-06 17:45:54 +00:00
parent 1657d516e9
commit b8ba202301
3 changed files with 46 additions and 0 deletions

5
NEWS
View file

@ -27,6 +27,11 @@ with all sufficient information, see the ChangeLog file.
* Most symbols which are returned by String#to_sym and * Most symbols which are returned by String#to_sym and
String#intern are GC-able. 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) === Core classes compatibility issues (excluding feature bug fixes)
* IO * IO

View file

@ -58,6 +58,7 @@ end
# * #each_with_index # * #each_with_index
# * #find_index # * #find_index
# * #minor(*param) # * #minor(*param)
# * #first_minor(row, column)
# #
# Properties of a matrix: # Properties of a matrix:
# * #diagonal? # * #diagonal?
@ -590,6 +591,34 @@ class Matrix
new_matrix rows, [column_count - from_col, size_col].min new_matrix rows, [column_count - from_col, size_col].min
end 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 -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- # TESTING -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
#++ #++

View file

@ -250,6 +250,18 @@ class TestMatrix < Test::Unit::TestCase
assert_raise(ArgumentError) { @m1.minor(0) } assert_raise(ArgumentError) { @m1.minor(0) }
end 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? def test_regular?
assert(Matrix[[1, 0], [0, 1]].regular?) assert(Matrix[[1, 0], [0, 1]].regular?)
assert(Matrix[[1, 0, 0], [0, 1, 0], [0, 0, 1]].regular?) assert(Matrix[[1, 0, 0], [0, 1, 0], [0, 0, 1]].regular?)