summaryrefslogtreecommitdiff
path: root/lib/matrix.rb
diff options
context:
space:
mode:
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 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 -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
#++