8000 [ruby/matrix] Add `Matrix#rotate_entries` [#19] · ruby/ruby@e34f51f · GitHub
[go: up one dir, main page]

Skip to content

Commit e34f51f

Browse files
fwolfstmarcandre
andcommitted
[ruby/matrix] Add Matrix#rotate_entries [#19]
Co-authored-by: Marc-André Lafortune <github@marc-andre.ca>
1 parent 0130e17 commit e34f51f

File tree

2 files changed

+85
-0
lines changed

2 files changed

+85
-0
lines changed

lib/matrix.rb

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1458,6 +1458,35 @@ def rank_e
14581458
rank
14591459
end
14601460

1461+
#
1462+
# Returns a new matrix with rotated elements.
1463+
# The argument specifies the rotation (defaults to `:clockwise`):
1464+
# * :clockwise, 1, -3, etc.: "turn right" - first row becomes last column
1465+
# * :half_turn, 2, -2, etc.: first row becomes last row, elements in reverse order
1466+
# * :counter_clockwise, -1, 3: "turn left" - first row becomes first column
1467+
# (but with elements in reverse order)
1468+
#
1469+
# m = Matrix[ [1, 2], [3, 4] ]
1470+
# r = m.rotate_entries(:clockwise)
1471+
# # => Matrix[[3, 1], [4, 2]]
1472+
#
1473+
def rotate_entries(rotation = :clockwise)
1474+
rotation %= 4 if rotation.respond_to? :to_int
1475+
1476+
case rotation
1477+
when 0
1478+
dup
1479+
when 1, :clockwise
1480+
new_matrix @rows.transpose.each(&:reverse!), row_count
1481+
when 2, :half_turn
1482+
new_matrix @rows.map(&:reverse).reverse!, column_count
1483+
when 3, :counter_clockwise
1484+
new_matrix @rows.transpose.reverse!, row_count
1485+
else
1486+
raise ArgumentError, "expected #{rotation.inspect} to be one of :clockwise, :counter_clockwise, :half_turn or an integer"
1487+
end
1488+
end
1489+
14611490
# Returns a matrix with entries rounded to the given precision
14621491
# (see Float#round)
14631492
#

test/matrix/test_matrix.rb

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -829,4 +829,60 @@ def test_ractor
829829
assert_same obj1, obj2
830830
RUBY
831831
end
832+
833+
def test_rotate_with_symbol
834+
assert_equal(Matrix[[4, 1], [5, 2], [6, 3]], @m1.rotate_entries)
835+
assert_equal(@m1.rotate_entries, @m1.rotate_entries(:clockwise))
836+
assert_equal(Matrix[[4, 1], [5, 2], [6, 3]],
837+
@m1.rotate_entries(:clockwise))
838+
assert_equal(Matrix[[3, 6], [2, 5], [1, 4]],
839+
@m1.rotate_entries(:counter_clockwise))
840+
assert_equal(Matrix[[6, 5, 4], [3, 2, 1]],
841+
@m1.rotate_entries(:half_turn))
842+
assert_equal(Matrix[[6, 5, 4], [3, 2, 1]],
843+
@m1.rotate_entries(:half_turn))
844+
assert_equal(Matrix.empty(0,2),
845+
@e1.rotate_entries(:clockwise))
846+
assert_equal(Matrix.empty(0,2),
847+
@e1.rotate_entries(:counter_clockwise))
848+
assert_equal(Matrix.empty(2,0),
849+
@e1.rotate_entries(:half_turn))
850+
assert_equal(Matrix.empty(0,3),
851+
@e2.rotate_entries(:half_turn))
852+
end
853+
854+
def test_rotate_with_numeric
855+
assert_equal(Matrix[[4, 1], [5, 2], [6, 3]],
856+
@m1.rotate_entries(1))
857+
assert_equal(@m2.rotate_entries(:half_turn),
858+
@m2.rotate_entries(2))
859+
assert_equal(@m2.rotate_entries(:half_turn),
860+
@m1.rotate_entries(2))
861+
assert_equal(@m1.rotate_entries(:counter_clockwise),
862+
@m1.rotate_entries(3))
863+
assert_equal(@m1,
864+
@m1.rotate_entries(4))
865+
assert_equal(@m1,
866+
@m1.rotate_entries(4))
867+
assert_not_same(@m1,
868+
@m1.rotate_entries(4))
869+
assert_equal(@m1.rotate_entries(:clockwise),
870+
@m1.rotate_entries(5))
871+
assert_equal(Matrix.empty(0,2),
872+
@e1.rotate_entries(1))
873+
assert_equal(@e2,
874+
@e2.rotate_entries(2))
875+
assert_equal(@e2.rotate_entries(1),
876+
@e2.rotate_entries(3))
877+
assert_equal(@e2.rotate_entries(:counter_clockwise),
878+
@e2.rotate_entries(-1))
879+
assert_equal(@m1.rotate_entries(:counter_clockwise),
880+
@m1.rotate_entries(-1))
881+
assert_equal(Matrix[[6, 5, 4], [3, 2, 1]],
882+
@m1.rotate_entries(-2))
883+
assert_equal(@m1,
884+
@m1.rotate_entries(-4))
885+
assert_equal(@m1.rotate_entries(-1),
886+
@m1.rotate_entries(-5))
887+
end
832888
end

0 commit comments

Comments
 (0)
0