companion.py 1.7 KB

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