The following code produces different outputs:
import numpy as np
from numba import njit
@njit
def resh_numba(a):
res = a.transpose(1, 0, 2)
res = res.copy().reshape(2, 6)
return res
x = np.arange(12).reshape(2, 2, 3)
print("numpy")
x_numpy = x.transpose(1, 0, 2).reshape(2, 6)
print(x_numpy)
print("numba:")
x_numba = resh_numba(x)
print(x_numba)
Output:
numpy
[[ 0 1 2 6 7 8]
[ 3 4 5 9 10 11]]
numba:
[[ 0 4 8 2 6 10]
[ 1 5 9 3 7 11]]
What is the reason for this? I'm suspecting some order='C'
vs order='F'
happening somewhere, but I expected both numpy and numba to use order='C'
by default everywhere.
Copyright Notice:Content Author:「P. Camilleri」,Reproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/53595093/np-transpose-and-np-reshape-combination-gives-different-results-in-pure-nump