Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
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), but sometimes it's necessary to work with column-major order (Fortran-order), especially in scientific computing applications.
Creating Arrays in Different Orders
Let's start by creating a simple 2D NumPy array and examining its memory layout ?
import numpy as np
# Create array in default C-order
arr_c = np.array([[1, 2, 3],
[4, 5, 6]])
print("C-order array:")
print(arr_c)
print("Flags:", arr_c.flags['C_CONTIGUOUS'], arr_c.flags['F_CONTIGUOUS'])
C-order array: [[1 2 3] [4 5 6]] Flags: True False
Creating Arrays in Fortran Order
To create an array directly in Fortran order, use the order='F' parameter ?
import numpy as np
# Create array in Fortran order
arr_f = np.array([[1, 2, 3],
[4, 5, 6]], order='F')
print("Fortran-order array:")
print(arr_f)
print("Flags:", arr_f.flags['C_CONTIGUOUS'], arr_f.flags['F_CONTIGUOUS'])
Fortran-order array: [[1 2 3] [4 5 6]] Flags: False True
Converting Existing Arrays to Fortran Order
Using asfortranarray()
The most direct way to convert an array to Fortran order ?
import numpy as np
# Original C-order array
arr_c = np.array([[1, 2, 3],
[4, 5, 6],
[7, 8, 9]])
# Convert to Fortran order
arr_f = np.asfortranarray(arr_c)
print("Original array:")
print(arr_c)
print("Fortran-order array:")
print(arr_f)
print("Memory layout changed:", arr_f.flags['F_CONTIGUOUS'])
Original array: [[1 2 3] [4 5 6] [7 8 9]] Fortran-order array: [[1 2 3] [4 5 6] [7 8 9]] Memory layout changed: True
Using flatten() and reshape()
Another approach using flatten with Fortran order and reshape ?
import numpy as np
arr = np.array([[1, 2, 3],
[4, 5, 6],
[7, 8, 9]])
# Flatten in Fortran order and reshape
flattened_f = arr.flatten(order='F')
reshaped_f = flattened_f.reshape(arr.shape, order='F')
print("Original array:")
print(arr)
print("Flattened in Fortran order:", flattened_f)
print("Reshaped in Fortran order:")
print(reshaped_f)
print("Is Fortran contiguous:", reshaped_f.flags['F_CONTIGUOUS'])
Original array: [[1 2 3] [4 5 6] [7 8 9]] Flattened in Fortran order: [1 4 7 2 5 8 3 6 9] Reshaped in Fortran order: [[1 2 3] [4 5 6] [7 8 9]] Is Fortran contiguous: True
Checking Memory Layout
Use the flags attribute to examine array memory layout ?
import numpy as np
arr_c = np.array([[1, 2, 3], [4, 5, 6]])
arr_f = np.asfortranarray(arr_c)
print("C-order flags:")
print(f" C_CONTIGUOUS: {arr_c.flags['C_CONTIGUOUS']}")
print(f" F_CONTIGUOUS: {arr_c.flags['F_CONTIGUOUS']}")
print("\nFortran-order flags:")
print(f" C_CONTIGUOUS: {arr_f.flags['C_CONTIGUOUS']}")
print(f" F_CONTIGUOUS: {arr_f.flags['F_CONTIGUOUS']}")
C-order flags: C_CONTIGUOUS: True F_CONTIGUOUS: False Fortran-order flags: C_CONTIGUOUS: False F_CONTIGUOUS: True
Comparison of Methods
| Method | Creates Copy? | Best For |
|---|---|---|
np.array(order='F') |
N/A (creation) | Creating new arrays |
np.asfortranarray() |
Only if needed | Converting existing arrays |
flatten('F') + reshape |
Yes | Understanding memory layout |
Conclusion
Use np.asfortranarray() to convert existing arrays to Fortran order efficiently. Use order='F' when creating new arrays that need column-major layout for better performance in scientific computing applications.
