Chandler Squires
Chandler Squires

Reputation: 407

Solving system of polynomial matrix equations in sympy

I would like to use sympy to solve for a vector that satisfies a set of quadratic and linear equations, such as x.T @ A @ x = x.T @ B @ x = 1, and x.T @ C = 0. Here is my attempt:

import numpy as np
from sympy.polys.polymatrix import PolyMatrix
from sympy import symbols
from sympy.solvers import solve_poly_system

x = PolyMatrix([symbols(f'x{k}') for k in range(2)])
A = np.eye(2)
B = np.array([1, 0]).reshape([2, 1])
one = PolyMatrix([1])

solve_poly_system([x.T @ A @ x - one, x.T @ B], [symbols(f'x{k}') for k in range(2)])

However, this generates the following error:

PolynomialError: non-commutative expressions are not supported

Any suggestions?

Upvotes: 1

Views: 197

Answers (1)

Oscar Benjamin
Oscar Benjamin

Reputation: 14480

Just use solve instead of solve_poly_system:

>>> solve([x.T @ A @ x - one, x.T @ B], [symbols(f'x{k}') for k in range(2)])
[(0.0, -1.0), (0.0, 1.0)]

Upvotes: 1

Related Questions