Hi there,
I am trying to use np.transpose
with the axes argument. It seems that this argument needs to be a tuple and not a list. Hence, I am trying to create dynamically a tuple.
import numba
import numpy as np
# just an example
arr = np.arange(8).reshape(2, 2, 2)
axes = (1, 0, *range(2, arr.ndim))
# this works fine
@numba.njit
def transpose1(a, axes):
return np.transpose(a, axes)
@numba.njit
def transpose2(a):
axes = (1, 0, *range(2, a.ndim))
return np.transpose(a, axes)
@numba.njit
def transpose3(a):
return np.transpose(a, (1, 0))
Using above definitions results in following output
>>> transpose1(arr, axes)
array([[[0, 1],
[4, 5]],
[[2, 3],
[6, 7]]])
# but this not anymore
>>> transpose2(arr)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home/braniii/Documents/Coding/numba/numba/core/dispatcher.py", line 421, in _compile_for_args
error_rewrite(e, 'typing')
File "/home/braniii/Documents/Coding/numba/numba/core/dispatcher.py", line 362, in error_rewrite
raise e.with_traceback(None)
numba.core.errors.TypingError: Failed in nopython mode pipeline (step: nopython frontend)
No implementation of function Function(<built-in function add>) found for signature:
>>> add(Tuple(Literal[int](1), Literal[int](0)), range_state_int64)
There are 18 candidate implementations:
- Of which 16 did not match due to:
Overload of function 'add': File: <numerous>: Line N/A.
With argument(s): '(UniTuple(int64 x 2), range_state_int64)':
No match.
- Of which 2 did not match due to:
Operator Overload in function 'add': File: unknown: Line unknown.
With argument(s): '(UniTuple(int64 x 2), range_state_int64)':
No match for registered cases:
* (int64, int64) -> int64
* (int64, uint64) -> int64
* (uint64, int64) -> int64
* (uint64, uint64) -> uint64
* (float32, float32) -> float32
* (float64, float64) -> float64
* (complex64, complex64) -> complex64
* (complex128, complex128) -> complex128
During: typing of intrinsic-call at <stdin> (3)
File "<stdin>", line 3:
<source missing, REPL/exec in use?>
# using a tuple with only two keys does not work either
# works for
>>> transpose3(arr[:, :, 0])
array([[0, 4],
[2, 6]])
# but fails for ndim != 2
>>> transpose3(arr)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 2, in transpose
File "<__array_function__ internals>", line 5, in transpose
File "/home/braniii/.local/lib/python3.8/site-packages/numpy/core/fromnumeric.py", line 658, in transpose
return _wrapfunc(a, 'transpose', axes)
File "/home/braniii/.local/lib/python3.8/site-packages/numpy/core/fromnumeric.py", line 58, in _wrapfunc
return bound(*args, **kwds)
ValueError: axes don't match array
My problem is, that I need to call np.transpose to implement a needed (from my side) numpy function. So using python mode is not an option. Has someone an idea? Thx.
Daniel