complexfield.py 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. """Implementation of :class:`ComplexField` class. """
  2. from sympy.core.numbers import Float, I
  3. from sympy.polys.domains.characteristiczero import CharacteristicZero
  4. from sympy.polys.domains.field import Field
  5. from sympy.polys.domains.mpelements import MPContext
  6. from sympy.polys.domains.simpledomain import SimpleDomain
  7. from sympy.polys.polyerrors import DomainError, CoercionFailed
  8. from sympy.utilities import public
  9. @public
  10. class ComplexField(Field, CharacteristicZero, SimpleDomain):
  11. """Complex numbers up to the given precision. """
  12. rep = 'CC'
  13. is_ComplexField = is_CC = True
  14. is_Exact = False
  15. is_Numerical = True
  16. has_assoc_Ring = False
  17. has_assoc_Field = True
  18. _default_precision = 53
  19. @property
  20. def has_default_precision(self):
  21. return self.precision == self._default_precision
  22. @property
  23. def precision(self):
  24. return self._context.prec
  25. @property
  26. def dps(self):
  27. return self._context.dps
  28. @property
  29. def tolerance(self):
  30. return self._context.tolerance
  31. def __init__(self, prec=_default_precision, dps=None, tol=None):
  32. context = MPContext(prec, dps, tol, False)
  33. context._parent = self
  34. self._context = context
  35. self.dtype = context.mpc
  36. self.zero = self.dtype(0)
  37. self.one = self.dtype(1)
  38. def __eq__(self, other):
  39. return (isinstance(other, ComplexField)
  40. and self.precision == other.precision
  41. and self.tolerance == other.tolerance)
  42. def __hash__(self):
  43. return hash((self.__class__.__name__, self.dtype, self.precision, self.tolerance))
  44. def to_sympy(self, element):
  45. """Convert ``element`` to SymPy number. """
  46. return Float(element.real, self.dps) + I*Float(element.imag, self.dps)
  47. def from_sympy(self, expr):
  48. """Convert SymPy's number to ``dtype``. """
  49. number = expr.evalf(n=self.dps)
  50. real, imag = number.as_real_imag()
  51. if real.is_Number and imag.is_Number:
  52. return self.dtype(real, imag)
  53. else:
  54. raise CoercionFailed("expected complex number, got %s" % expr)
  55. def from_ZZ(self, element, base):
  56. return self.dtype(element)
  57. def from_QQ(self, element, base):
  58. return self.dtype(int(element.numerator)) / int(element.denominator)
  59. def from_ZZ_python(self, element, base):
  60. return self.dtype(element)
  61. def from_QQ_python(self, element, base):
  62. return self.dtype(element.numerator) / element.denominator
  63. def from_ZZ_gmpy(self, element, base):
  64. return self.dtype(int(element))
  65. def from_QQ_gmpy(self, element, base):
  66. return self.dtype(int(element.numerator)) / int(element.denominator)
  67. def from_GaussianIntegerRing(self, element, base):
  68. return self.dtype(int(element.x), int(element.y))
  69. def from_GaussianRationalField(self, element, base):
  70. x = element.x
  71. y = element.y
  72. return (self.dtype(int(x.numerator)) / int(x.denominator) +
  73. self.dtype(0, int(y.numerator)) / int(y.denominator))
  74. def from_AlgebraicField(self, element, base):
  75. return self.from_sympy(base.to_sympy(element).evalf(self.dps))
  76. def from_RealField(self, element, base):
  77. return self.dtype(element)
  78. def from_ComplexField(self, element, base):
  79. if self == base:
  80. return element
  81. else:
  82. return self.dtype(element)
  83. def get_ring(self):
  84. """Returns a ring associated with ``self``. """
  85. raise DomainError("there is no ring associated with %s" % self)
  86. def get_exact(self):
  87. """Returns an exact domain associated with ``self``. """
  88. raise DomainError("there is no exact domain associated with %s" % self)
  89. def is_negative(self, element):
  90. """Returns ``False`` for any ``ComplexElement``. """
  91. return False
  92. def is_positive(self, element):
  93. """Returns ``False`` for any ``ComplexElement``. """
  94. return False
  95. def is_nonnegative(self, element):
  96. """Returns ``False`` for any ``ComplexElement``. """
  97. return False
  98. def is_nonpositive(self, element):
  99. """Returns ``False`` for any ``ComplexElement``. """
  100. return False
  101. def gcd(self, a, b):
  102. """Returns GCD of ``a`` and ``b``. """
  103. return self.one
  104. def lcm(self, a, b):
  105. """Returns LCM of ``a`` and ``b``. """
  106. return a*b
  107. def almosteq(self, a, b, tolerance=None):
  108. """Check if ``a`` and ``b`` are almost equal. """
  109. return self._context.almosteq(a, b, tolerance)
  110. CC = ComplexField()