pow accepts a third argument for modulo pow(x, y, z) that is more efficient computation than x ** y % z. How can you do that with arrays? What I've tried:
>>> import numpy as np
>>> A = np.array(range(10))
>>> pow(A, 23, 13)
TypeError: unsupported operand type(s) for pow(): 'numpy.ndarray', 'int', 'int'
Though ndarray implements __pow__, invoking directly doesn't do anything:
>>> A.__pow__(23, 13)
NotImplemented
Using the exponentiation and modulo in two step gives incorrect results (guess it is overflowing the dtype)
>>> print(*(A ** 23 % 13)) # wrong result!
0 1 7 9 10 8 11 12 0 6
>>> print(*[pow(int(a), 23, 13) for a in A]) # correct result
0 1 7 9 10 8 11 2 5 3
The actual array is large so I can not use dtype "object" nor have the looping directly in Python.
How to compute 3-arg pow for numpy arrays?