polyerrors.py 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. """Definitions of common exceptions for `polys` module. """
  2. from sympy.utilities import public
  3. @public
  4. class BasePolynomialError(Exception):
  5. """Base class for polynomial related exceptions. """
  6. def new(self, *args):
  7. raise NotImplementedError("abstract base class")
  8. @public
  9. class ExactQuotientFailed(BasePolynomialError):
  10. def __init__(self, f, g, dom=None):
  11. self.f, self.g, self.dom = f, g, dom
  12. def __str__(self): # pragma: no cover
  13. from sympy.printing.str import sstr
  14. if self.dom is None:
  15. return "%s does not divide %s" % (sstr(self.g), sstr(self.f))
  16. else:
  17. return "%s does not divide %s in %s" % (sstr(self.g), sstr(self.f), sstr(self.dom))
  18. def new(self, f, g):
  19. return self.__class__(f, g, self.dom)
  20. @public
  21. class PolynomialDivisionFailed(BasePolynomialError):
  22. def __init__(self, f, g, domain):
  23. self.f = f
  24. self.g = g
  25. self.domain = domain
  26. def __str__(self):
  27. if self.domain.is_EX:
  28. msg = "You may want to use a different simplification algorithm. Note " \
  29. "that in general it's not possible to guarantee to detect zero " \
  30. "in this domain."
  31. elif not self.domain.is_Exact:
  32. msg = "Your working precision or tolerance of computations may be set " \
  33. "improperly. Adjust those parameters of the coefficient domain " \
  34. "and try again."
  35. else:
  36. msg = "Zero detection is guaranteed in this coefficient domain. This " \
  37. "may indicate a bug in SymPy or the domain is user defined and " \
  38. "doesn't implement zero detection properly."
  39. return "couldn't reduce degree in a polynomial division algorithm when " \
  40. "dividing %s by %s. This can happen when it's not possible to " \
  41. "detect zero in the coefficient domain. The domain of computation " \
  42. "is %s. %s" % (self.f, self.g, self.domain, msg)
  43. @public
  44. class OperationNotSupported(BasePolynomialError):
  45. def __init__(self, poly, func):
  46. self.poly = poly
  47. self.func = func
  48. def __str__(self): # pragma: no cover
  49. return "`%s` operation not supported by %s representation" % (self.func, self.poly.rep.__class__.__name__)
  50. @public
  51. class HeuristicGCDFailed(BasePolynomialError):
  52. pass
  53. class ModularGCDFailed(BasePolynomialError):
  54. pass
  55. @public
  56. class HomomorphismFailed(BasePolynomialError):
  57. pass
  58. @public
  59. class IsomorphismFailed(BasePolynomialError):
  60. pass
  61. @public
  62. class ExtraneousFactors(BasePolynomialError):
  63. pass
  64. @public
  65. class EvaluationFailed(BasePolynomialError):
  66. pass
  67. @public
  68. class RefinementFailed(BasePolynomialError):
  69. pass
  70. @public
  71. class CoercionFailed(BasePolynomialError):
  72. pass
  73. @public
  74. class NotInvertible(BasePolynomialError):
  75. pass
  76. @public
  77. class NotReversible(BasePolynomialError):
  78. pass
  79. @public
  80. class NotAlgebraic(BasePolynomialError):
  81. pass
  82. @public
  83. class DomainError(BasePolynomialError):
  84. pass
  85. @public
  86. class PolynomialError(BasePolynomialError):
  87. pass
  88. @public
  89. class UnificationFailed(BasePolynomialError):
  90. pass
  91. @public
  92. class GeneratorsError(BasePolynomialError):
  93. pass
  94. @public
  95. class GeneratorsNeeded(GeneratorsError):
  96. pass
  97. @public
  98. class ComputationFailed(BasePolynomialError):
  99. def __init__(self, func, nargs, exc):
  100. self.func = func
  101. self.nargs = nargs
  102. self.exc = exc
  103. def __str__(self):
  104. return "%s(%s) failed without generators" % (self.func, ', '.join(map(str, self.exc.exprs[:self.nargs])))
  105. @public
  106. class UnivariatePolynomialError(PolynomialError):
  107. pass
  108. @public
  109. class MultivariatePolynomialError(PolynomialError):
  110. pass
  111. @public
  112. class PolificationFailed(PolynomialError):
  113. def __init__(self, opt, origs, exprs, seq=False):
  114. if not seq:
  115. self.orig = origs
  116. self.expr = exprs
  117. self.origs = [origs]
  118. self.exprs = [exprs]
  119. else:
  120. self.origs = origs
  121. self.exprs = exprs
  122. self.opt = opt
  123. self.seq = seq
  124. def __str__(self): # pragma: no cover
  125. if not self.seq:
  126. return "Cannot construct a polynomial from %s" % str(self.orig)
  127. else:
  128. return "Cannot construct polynomials from %s" % ', '.join(map(str, self.origs))
  129. @public
  130. class OptionError(BasePolynomialError):
  131. pass
  132. @public
  133. class FlagError(OptionError):
  134. pass