Skip to content

Commit 515dd96

Browse files
fwolfstmarcandre
andcommitted
Add Matrix#rotate_entries [#19]
Co-authored-by: Marc-André Lafortune <github@marc-andre.ca>
1 parent 7ebc1a0 commit 515dd96

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
@@ -827,4 +827,60 @@ def test_ractor
827827
end.take
828828
assert_same obj1, obj2
829829
end if defined?(Ractor)
830+
831+
def test_rotate_with_symbol
832+
assert_equal(Matrix[[4, 1], [5, 2], [6, 3]], @m1.rotate_entries)
833+
assert_equal(@m1.rotate_entries, @m1.rotate_entries(:clockwise))
834+
assert_equal(Matrix[[4, 1], [5, 2], [6, 3]],
835+
@m1.rotate_entries(:clockwise))
836+
assert_equal(Matrix[[3, 6], [2, 5], [1, 4]],
837+
@m1.rotate_entries(:counter_clockwise))
838+
assert_equal(Matrix[[6, 5, 4], [3, 2, 1]],
839+
@m1.rotate_entries(:half_turn))
840+
assert_equal(Matrix[[6, 5, 4], [3, 2, 1]],
841+
@m1.rotate_entries(:half_turn))
842+
assert_equal(Matrix.empty(0,2),
843+
@e1.rotate_entries(:clockwise))
844+
assert_equal(Matrix.empty(0,2),
845+
@e1.rotate_entries(:counter_clockwise))
846+
assert_equal(Matrix.empty(2,0),
847+
@e1.rotate_entries(:half_turn))
848+
assert_equal(Matrix.empty(0,3),
849+
@e2.rotate_entries(:half_turn))
850+
end
851+
852+
def test_rotate_with_numeric
853+
assert_equal(Matrix[[4, 1], [5, 2], [6, 3]],
854+
@m1.rotate_entries(1))
855+
assert_equal(@m2.rotate_entries(:half_turn),
856+
@m2.rotate_entries(2))
857+
assert_equal(@m2.rotate_entries(:half_turn),
858+
@m1.rotate_entries(2))
859+
assert_equal(@m1.rotate_entries(:counter_clockwise),
860+
@m1.rotate_entries(3))
861+
assert_equal(@m1,
862+
@m1.rotate_entries(4))
863+
assert_equal(@m1,
864+
@m1.rotate_entries(4))
865+
assert_not_same(@m1,
866+
@m1.rotate_entries(4))
867+
assert_equal(@m1.rotate_entries(:clockwise),
868+
@m1.rotate_entries(5))
869+
assert_equal(Matrix.empty(0,2),
870+
@e1.rotate_entries(1))
871+
assert_equal(@e2,
872+
@e2.rotate_entries(2))
873+
assert_equal(@e2.rotate_entries(1),
874+
@e2.rotate_entries(3))
875+
assert_equal(@e2.rotate_entries(:counter_clockwise),
876+
@e2.rotate_entries(-1))
877+
assert_equal(@m1.rotate_entries(:counter_clockwise),
878+
@m1.rotate_entries(-1))
879+
assert_equal(Matrix[[6, 5, 4], [3, 2, 1]],
880+
@m1.rotate_entries(-2))
881+
assert_equal(@m1,
882+
@m1.rotate_entries(-4))
883+
assert_equal(@m1.rotate_entries(-1),
884+
@m1.rotate_entries(-5))
885+
end
830886
end

0 commit comments

Comments
 (0)