数组的三个参数pow

pow接受模数的第三个参数,pow(x, y, z)该参数比 更有效的计算x ** y % z。你怎么能用数组做到这一点?我试过的:

>>> import numpy as np
>>> A = np.array(range(10))
>>> pow(A, 23, 13)
TypeError: unsupported operand type(s) for pow(): 'numpy.ndarray', 'int', 'int'

虽然 ndarray 实现了__pow__,直接调用不会做任何事情:

>>> A.__pow__(23, 13)
NotImplemented

在两步中使用求幂和取模会产生不正确的结果(猜测它正在溢出 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

实际数组很大,所以我不能使用 dtype“object”,也不能直接在 Python 中循环。

如何计算 numpy 数组的 3-arg pow?

以上是数组的三个参数pow的全部内容。
THE END
分享
二维码
< <上一篇
下一篇>>