2
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!

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:

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

>>> [[2 0]
    [1 2]]
ANTZY21
  • 65
  • 6

2 Answers2

1

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

drilow
  • 412
  • 2
  • 10
0

With current SymPy this works:

>>> import sympy
>>> B = sympy.Matrix([[2, 3], [4, 5]])
>>> print(B % 3)
Matrix([[2, 0], [1, 2]])
smichr
  • 16,948
  • 2
  • 27
  • 34