summaryrefslogtreecommitdiff
path: root/lib/matrix.rb
diff options
context:
space:
mode:
authormarcandre <marcandre@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2014-04-06 17:45:54 +0000
committermarcandre <marcandre@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2014-04-06 17:45:54 +0000
commitb8ba202301e5e4cf392a827c9bb8ebf2265de99f (patch)
tree0448fc788c02c6012adf8d0a6e0ec07962cd7f3a /lib/matrix.rb
parent1657d516e92002a009b52d992529a04abd33a0ce (diff)
* 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
Diffstat (limited to 'lib/matrix.rb')
-rw-r--r--lib/matrix.rb29
1 files changed, 29 insertions, 0 deletions
diff --git a/lib/matrix.rb b/lib/matrix.rb
index e0995c3..7502e6e 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 -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
#++