Matrix of polynomial elements

I use NumPy for operations on matrices, to calculate matrix A * of matrix B, trace of matrix, etc. And the elements of my matrices are integers. But I want to know if it is possible to work with polynomial matrices. So, for example, I can work with matrices such as [x,y;a,b], and not [1,1;1,1], and when I calculate the trace, it provides me with the polynomial x + b, not 2. Is there any polynomial class in NumPy that can work with?

+4
source share
1 answer

One option is to use the Sympy Matrices module . SymPy is a symbolic math library for Python that is fully compatible with NumPy, especially for simple matrix manipulation tasks like this.

>>> from sympy import symbols, Matrix
>>> from numpy import trace
>>> x, y, a, b = symbols('x y a b')
>>> M = Matrix(([x, y], [a, b]))
>>> M
Matrix([
[x, y],
[a, b]])
>>> trace(M)
b + x
>>> M.dot(M)
[a*y + x**2, a*b + a*x, b*y + x*y, a*y + b**2]
+2
source

Source: https://habr.com/ru/post/1649398/


All Articles