exceptions.py 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. """Special exception classes for numberfields. """
  2. class ClosureFailure(Exception):
  3. r"""
  4. Signals that a :py:class:`ModuleElement` which we tried to represent in a
  5. certain :py:class:`Module` cannot in fact be represented there.
  6. Examples
  7. ========
  8. >>> from sympy.polys import Poly, cyclotomic_poly, ZZ
  9. >>> from sympy.polys.matrices import DomainMatrix
  10. >>> from sympy.polys.numberfields.modules import PowerBasis, to_col, ClosureFailure
  11. >>> from sympy.testing.pytest import raises
  12. >>> T = Poly(cyclotomic_poly(5))
  13. >>> A = PowerBasis(T)
  14. >>> B = A.submodule_from_matrix(2 * DomainMatrix.eye(4, ZZ))
  15. Because we are in a cyclotomic field, the power basis ``A`` is an integral
  16. basis, and the submodule ``B`` is just the ideal $(2)$. Therefore ``B`` can
  17. represent an element having all even coefficients over the power basis:
  18. >>> a1 = A(to_col([2, 4, 6, 8]))
  19. >>> print(B.represent(a1))
  20. DomainMatrix([[1], [2], [3], [4]], (4, 1), ZZ)
  21. but ``B`` cannot represent an element with an odd coefficient:
  22. >>> a2 = A(to_col([1, 2, 2, 2]))
  23. >>> print(raises(ClosureFailure, lambda: B.represent(a2)))
  24. <ExceptionInfo ClosureFailure('Element in QQ-span but not ZZ-span of this basis.')>
  25. """
  26. pass
  27. class StructureError(Exception):
  28. r"""
  29. Represents cases in which an algebraic structure was expected to have a
  30. certain property, or be of a certain type, but was not.
  31. """
  32. pass
  33. class MissingUnityError(StructureError):
  34. r"""Structure should contain a unity element but does not."""
  35. pass
  36. __all__ = [
  37. 'ClosureFailure', 'StructureError', 'MissingUnityError',
  38. ]