Display NumPy array in Fortran order


In this article, we will explore how to display a NumPy array in Fortran order. By default, NumPy arrays are stored in row-major order (C-order), which means that the elements of the array are stored in consecutive memory locations row-wise. However, sometimes it is necessary to store and display arrays in column-major order (Fortran-order), especially in scientific computing applications.

In NumPy, an array can be created in Fortran order using the order parameter of the np.array() function. Fortran order is also known as column-major order, which means that the elements of the array are stored column by column in memory.

Here's an example of how to create a NumPy array in Fortran order −

Prerequisites

Before we begin, make sure you have NumPy installed on your machine. You can install it by running the following command in your terminal −

pip install numpy
Getting Started

To begin, let's create a simple 2D Numpy array in row-major order −

import numpy as np a = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) Print(a)

Output

array([[1, 4, 7],
   [2, 5, 8],
   [3, 6, 9]])

By default, the array a is stored in C-order, meaning that the elements are stored row-wise. To check the order of the array, we can use the flags attribute of the Numpy array −

print(a.flags)

This will print a dictionary-like object that contains various flags related to the array, including the C_CONTIGUOUS and F_CONTIGUOUS flags. By default, the C_CONTIGUOUS flag is set to True, indicating that the array is stored in C-order.

Displaying in Fortran Order

To display the array a in Fortran order, we can use the np.ndarray.flatten method to create a 1D array, and then use the np.reshape method to reshape the array in Fortran order. Here's the code to display the array in Fortran order −

b = np.reshape(a.flatten(order='F'), a.shape)
print(b)

The a.flatten(order='F') method flattens the array a in Fortran order, and the np.reshape method reshapes the flattened array back to its original shape in Fortran order. The resulting array b will have the same elements as a, but will be stored and displayed in column-major order.

Alternatively, we can use the np.transpose method to swap the rows and columns of the array −

b = np.transpose(a)
print(b)

Example

# Program to transpose a matrix using a nested loop

X = [[12,7],
   [4 ,5],
   [3 ,8]]
result = [[0,0,0],
   [0,0,0]]
# iterate through rows
for i in range(len(X)):
   # iterate through columns
   for j in range(len(X[0])):
      result[j][i] = X[i][j]
for r in result:
   print(r)

Output

[12, 4, 3]
[7, 5, 8]

The np.transpose method returns a new array with the axes transposed, effectively swapping the rows and columns of the original array. This method also works for higher-dimensional arrays, where it swaps the corresponding pairs of axes.

Conclusion

In this article, we have explored how to display a Numpy array in Fortran order. We have seen how to check the order of a Numpy array using the flags attribute, and how to display an array in Fortran order using the np.ndarray.flatten and np.reshape methods, or the np.transpose method. Storing and displaying arrays in Fortran order is sometimes necessary for compatibility with legacy code, or to take advantage of performance optimizations for column-major access patterns.

Updated on: 31-Jul-2023

225 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements