RotateRight, RotateLeft


RotateRight and RotateLeft can be used on an expression that doesn't have the
head List.  In the two cells below the arguments of an expression are rotated
two places to the right and two places to the left.

ClearAll["Global`*"] ;    RotateRight[foo[a, b, c, d, e, f, g, h], 2]

foo[g, h, a, b, c, d, e, f]

RotateRight[foo[a, b, c, d, e, f, g, h], -2]

foo[c, d, e, f, g, h, a, b]


Normally RotateRight and RotateLeft are used on a simple list and the second
argument (if present) is normally an integer.  However, the second argument
can be a list of integers.  First consider the tensor (m2) below.

m2 = { {{a1, a2, a3}, {b1, b2, b3}, {c1, c2, c3}},  {{d1, d2, d3}, {e1, e2, e3 ... , f2, f3}},  {{g1, g2, g3}, {h1, h2, h3}, {i1, i2, i3}} } ;    MatrixForm[m2]

( ( a1 )   ( b1 )   ( c1 ) )   ...                       i2                      g3                       h3                       i3


First the default case for RotateRight is demonstrated on the above tensor.

RotateRight[m2]//MatrixForm

( ( g1 )   ( h1 )   ( i1 ) )   ...                       f2                      d3                       e3                       f3


In the cell below we see the default case is equivalent to a few values for
the second argument.

RotateRight[m2] === RotateRight[m2, {1}] === RotateRight[m2, {1, 0}] ===       RotateRight[m2, {1, 0, 0}]

True


When the second argument is {0,1} or {0,1,0} the second level is rotated one
place.

RotateRight[m2, {0, 1}]//MatrixForm

( ( c1 )   ( a1 )   ( b1 ) )   ...                       h2                      i3                       g3                       h3


When the second argument is {0,0,1} the third level is rotated one place.

RotateRight[m2, {0, 0, 1}]//MatrixForm

( ( a3 )   ( b3 )   ( c3 ) )   ...                       i1                      g2                       h2                       i2


Created by Mathematica  (May 16, 2004)

Back to Ted’s Tricks index page