Spaces:
Sleeping
Sleeping
File size: 1,705 Bytes
6a86ad5 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
from sympy.core.singleton import S
from sympy.core.sympify import _sympify
from sympy.polys.polytools import Poly
from .matexpr import MatrixExpr
class CompanionMatrix(MatrixExpr):
"""A symbolic companion matrix of a polynomial.
Examples
========
>>> from sympy import Poly, Symbol, symbols
>>> from sympy.matrices.expressions import CompanionMatrix
>>> x = Symbol('x')
>>> c0, c1, c2, c3, c4 = symbols('c0:5')
>>> p = Poly(c0 + c1*x + c2*x**2 + c3*x**3 + c4*x**4 + x**5, x)
>>> CompanionMatrix(p)
CompanionMatrix(Poly(x**5 + c4*x**4 + c3*x**3 + c2*x**2 + c1*x + c0,
x, domain='ZZ[c0,c1,c2,c3,c4]'))
"""
def __new__(cls, poly):
poly = _sympify(poly)
if not isinstance(poly, Poly):
raise ValueError("{} must be a Poly instance.".format(poly))
if not poly.is_monic:
raise ValueError("{} must be a monic polynomial.".format(poly))
if not poly.is_univariate:
raise ValueError(
"{} must be a univariate polynomial.".format(poly))
if not poly.degree() >= 1:
raise ValueError(
"{} must have degree not less than 1.".format(poly))
return super().__new__(cls, poly)
@property
def shape(self):
poly = self.args[0]
size = poly.degree()
return size, size
def _entry(self, i, j):
if j == self.cols - 1:
return -self.args[0].all_coeffs()[-1 - i]
elif i == j + 1:
return S.One
return S.Zero
def as_explicit(self):
from sympy.matrices.immutable import ImmutableDenseMatrix
return ImmutableDenseMatrix.companion(self.args[0])
|