Intel® Fortran Compiler Classic and Intel® Fortran Compiler Developer Guide and Reference

ID 767251
Date 3/22/2024
Public

A newer version of this document is available. Customers should click here to go to the newest version.

Document Table of Contents

CSHIFT

Transformational Intrinsic Function (Generic): Performs a circular shift on a rank-one array, or performs circular shifts on all the complete rank-one sections (vectors) along a given dimension of an array of rank two or greater.

Elements shifted off one end are inserted at the other end. Different sections can be shifted by different amounts and in different directions.

result = CSHIFT (array,shift [,dim])

array

(Input) Array whose elements are to be shifted. It can be of any data type.

shift

(Input) The number of positions shifted. Must be a scalar integer or an array with a rank that is one less than array, and shape (d1, d2, ..., ddim-1, ddim+1, ..., dn), where (d1, d2, ..., dn) is the shape of array.

dim

(Input; optional) Optional dimension along which to perform the shift. Must be a scalar integer with a value in the range 1 to n, where n is the rank of array. If dim is omitted, it is assumed to be 1.

Results

The result is an array with the same type and kind parameters, and shape as array.

If array has rank one, element i of the result is array(1 + MODULO (i + shift- 1, SIZE ( array))). (The same shift is applied to each element.)

If array has rank greater than one, each section (s1,s2, ..., sdim-1, :, sdim+1, ..., sn) of the result is shifted as follows:

  • By the value of shift, if shift is scalar

  • According to the corresponding value in shift(s1, s2,..., sdim-1, sdim+1,..., sn), if shift is an array

The value of shift determines the amount and direction of the circular shift. A positive shift value causes a shift to the left (in rows) or up (in columns). A negative shift value causes a shift to the right (in rows) or down (in columns). A zero shift value causes no shift.

Example

V is the array (1, 2, 3, 4, 5, 6).

CSHIFT (V, SHIFT=2) shifts the elements in V circularly to the left by 2 positions, producing the value (3, 4, 5, 6, 1, 2). 1 and 2 are shifted off the beginning and inserted at the end.

CSHIFT (V, SHIFT= -2) shifts the elements in V circularly to the right by 2 positions, producing the value (5, 6, 1, 2, 3, 4). 5 and 6 are shifted off the end and inserted at the beginning.

M is the array

  [ 1  2  3 ]
  [ 4  5  6 ]
  [ 7  8  9 ].

CSHIFT (M, SHIFT = 1, DIM = 2) produces the result

  [ 2  3  1 ]
  [ 5  6  4 ]
  [ 8  9  7 ].

Each element in rows 1, 2, and 3 is shifted to the left by 1 position. The elements shifted off the beginning are inserted at the end.

CSHIFT (M, SHIFT = -1, DIM = 1) produces the result

  [ 7  8  9 ]
  [ 1  2  3 ]
  [ 4  5  6 ].

Each element in columns 1, 2, and 3 is shifted down by 1 position. The elements shifted off the end are inserted at the beginning.

CSHIFT (M, SHIFT = (/1, -1, 0/), DIM = 2) produces the result

  [ 2  3  1 ]
  [ 6  4  5 ]
  [ 7  8  9 ].

Each element in row 1 is shifted to the left by 1 position; each element in row 2 is shifted to the right by 1 position; no element in row 3 is shifted at all.

The following shows another example:

INTEGER array (3, 3), AR1(3, 3), AR2 (3, 3)
DATA array /1, 4, 7, 2, 5, 8, 3, 6, 9/
!
! array is   1 2 3
!            4 5 6
!            7 8 9
!AR1 = CSHIFT(array, 1, DIM = 1) ! shifts all columns
                                 ! by 1 yielding
                                 !         4 5 6
                                 !         7 8 9
                                 !         1 2 3
                                 !
AR2=CSHIFT(array,shift=(/-1, 1, 0/),DIM=2) ! shifts
                                 ! each row separately
                                 ! by the amount in
                                 ! shift yielding
                                 !         3 1 2
                                 !         5 6 4
                                 !         7 8 9

See Also