C-style transformation of non-contiguous to contiguous layout

non-contiguous array of shape (4, 3)
Let us consider a 2-D array, x = numpy.random.rand(4,3). The shape of the array is therefore 4 × 3 array ( numpy.shape(x) = (4,3)). Also any element in the array is associated with its respective, index (n0, n1).

Then, for a C-style non-contiguous layout with shape, (d0, d1, …, dN−1), the value associated with (n0, n1, …, nN−1) is located in the contiguous layout with index = nC.

If,

ni ≜ value of ith index in the non-contiguous layout.
eg. for (2, 1) entry in x is (n0 = 2, n1 = 1)

For C-style non-contiguous layout with shape (d0, d1, …, dN−1) the value in (n0, n1, …, nN−1) is located in the contiguous layout with index nC
expression for index in contiguous layout mapped from index in C-style non-contiguous layout
such that, for dk, dk + 1, …, dm−1, dm if k > m
products of d sub j is 1 if start index is greater than final index

Examples illustrating transformation to contiguous layout in C-style

Eg.1: Location of element in x with index (2, 1)

For a C-style non-contiguous layout x with shape, (d0 = 4, d1 = 3) i.e. (d0, d2−1) ∴ N = 2, the value in (n0 = 2, n1 = 1) of the non-contiguous layout is located in the contiguous layout with index nC given by,

index in contiguous layout for (2, 1) index in C-style non-contiguous layout is equal to 7
index in C-style non-contiguous layout into respective mapped index in contiguous layout


Eg.2: Location of element in x with index (3, 1)

For a C-style non-contiguous layout x with shape, (d0 = 4, d1 = 3) the value in (n0 = 3, n1 = 1) of the non-contiguous layout is located in the contiguous layout with index nCgiven by,

index in contiguous layout for (3, 1) index in C-style non-contiguous layout is equal to 10

Notice that to move from the element with index (2, 1) in the array x to the element at (3, 1), three elements within the array must be jumped.

Stride in an array

The number of elements that must be jumped to get to the next element (eg. next arrow) is given by the stride.

Stride in N-dimensional C-style array

expression of index in contiguous layout in terms of stride
where
Stride is
stride is equal to product of d sub i's