Numpy 是否具有内置的元素矩阵模幂实现
Does Numpy have an inbuilt elementwise matrix modular exponentiation implementation
numpy 是否具有矩阵模幂的内置实现?
(正如 user2357112 所指出的,我实际上是在寻找元素明智的模块化缩减)
对常规数字进行模幂运算的一种方法是使用平方求幂 (https://en.wikipedia.org/wiki/Exponentiation_by_squaring),并在每一步进行模约简。我想知道是否有类似的矩阵乘法内置解决方案。我知道我可以编写代码来轻松模拟这一点,但我想知道是否有内置解决方案。
NumPy 当前未内置模幂运算(GitHub issue). The easiest/laziest way to achieve it is frompyfunc:
modexp = np.frompyfunc(pow, 3, 1)
print(modexp(np.array([[1, 2], [3, 4]]), 2, 3).astype(int))
打印
[[1 1]
[0 1]]
这当然比原生 NumPy 慢,我们得到一个 dtype=object 的数组(因此添加了 astype(int)
)。
numpy 是否具有矩阵模幂的内置实现?
(正如 user2357112 所指出的,我实际上是在寻找元素明智的模块化缩减)
对常规数字进行模幂运算的一种方法是使用平方求幂 (https://en.wikipedia.org/wiki/Exponentiation_by_squaring),并在每一步进行模约简。我想知道是否有类似的矩阵乘法内置解决方案。我知道我可以编写代码来轻松模拟这一点,但我想知道是否有内置解决方案。
NumPy 当前未内置模幂运算(GitHub issue). The easiest/laziest way to achieve it is frompyfunc:
modexp = np.frompyfunc(pow, 3, 1)
print(modexp(np.array([[1, 2], [3, 4]]), 2, 3).astype(int))
打印
[[1 1]
[0 1]]
这当然比原生 NumPy 慢,我们得到一个 dtype=object 的数组(因此添加了 astype(int)
)。