summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorFelix Wolfsteller <felix.wolfsteller@gmail.com>2020-12-21 18:28:55 +0100
committerMarc-Andre Lafortune <github@marc-andre.ca>2021-01-21 13:22:25 -0500
commite34f51fe609d6be48b655695dd8c15e17694242e (patch)
treebb623e8537aae815ad22cf73340062ff33372415 /lib
parent0130e17a410d60a10e7041ce98748b8de6946971 (diff)
[ruby/matrix] Add `Matrix#rotate_entries` [#19]
Co-authored-by: Marc-André Lafortune <github@marc-andre.ca>
Diffstat (limited to 'lib')
-rw-r--r--lib/matrix.rb29
1 files changed, 29 insertions, 0 deletions
diff --git a/lib/matrix.rb b/lib/matrix.rb
index 8c636f61e8..0a17678972 100644
--- a/lib/matrix.rb
+++ b/lib/matrix.rb
@@ -1458,6 +1458,35 @@ class Matrix
rank
end
+ #
+ # Returns a new matrix with rotated elements.
+ # The argument specifies the rotation (defaults to `:clockwise`):
+ # * :clockwise, 1, -3, etc.: "turn right" - first row becomes last column
+ # * :half_turn, 2, -2, etc.: first row becomes last row, elements in reverse order
+ # * :counter_clockwise, -1, 3: "turn left" - first row becomes first column
+ # (but with elements in reverse order)
+ #
+ # m = Matrix[ [1, 2], [3, 4] ]
+ # r = m.rotate_entries(:clockwise)
+ # # => Matrix[[3, 1], [4, 2]]
+ #
+ def rotate_entries(rotation = :clockwise)
+ rotation %= 4 if rotation.respond_to? :to_int
+
+ case rotation
+ when 0
+ dup
+ when 1, :clockwise
+ new_matrix @rows.transpose.each(&:reverse!), row_count
+ when 2, :half_turn
+ new_matrix @rows.map(&:reverse).reverse!, column_count
+ when 3, :counter_clockwise
+ new_matrix @rows.transpose.reverse!, row_count
+ else
+ raise ArgumentError, "expected #{rotation.inspect} to be one of :clockwise, :counter_clockwise, :half_turn or an integer"
+ end
+ end
+
# Returns a matrix with entries rounded to the given precision
# (see Float#round)
#