Sympy Matrix M(模数n)......怎么样?

时间:2022-08-06 04:38:38
import sympy

I am trying to find a matrix after taking each of its values (mod n). I know numpy arrays work fine with this but i have to use sympy unfortunately. Does anyone know of an inbuilt sympy function that does this or any other way round it? Thank you!

我试图在获取每个值(mod n)后找到一个矩阵。我知道numpy数组可以正常使用但不幸的是我必须使用sympy。有没有人知道一个内置的sympy功能可以做到这一点还是其他任何方式?谢谢!

B = sympy.Matrix([[2, 3], [4, 5]])
print(B % 3)

This is my error

这是我的错误

TypeError: unsupported operand type(s) for %: 'MutableDenseMatrix' and 'int'

with numpy this is the correct output:

有numpy这是正确的输出:

B = np.array([[2, 3], [4, 5]])
print(B % 3)

>>> [[2 0]
    [1 2]]

1 个解决方案

#1


1  

You can use applyfunc to apply a function to every element: B.applyfunc(lambda x : x % 3)

您可以使用applyfunc将函数应用于每个元素:B.applyfunc(lambda x:x%3)

#1


1  

You can use applyfunc to apply a function to every element: B.applyfunc(lambda x : x % 3)

您可以使用applyfunc将函数应用于每个元素:B.applyfunc(lambda x:x%3)