Skip to content

Commit 319ec61

Browse files
committed
Another attempt to make mypy happy.
Also, order methods in Compilex in alphbetic order.
1 parent 804d2af commit 319ec61

File tree

1 file changed

+81
-62
lines changed

1 file changed

+81
-62
lines changed

mathics/core/atoms.py

Lines changed: 81 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -817,6 +817,14 @@ def __new__(cls, real, imag):
817817

818818
return self
819819

820+
@cache
821+
def __getnewargs__(self):
822+
return (self.real, self.imag)
823+
824+
@cache
825+
def __neg__(self):
826+
return Complex(-self.real, -self.imag)
827+
820828
def __hash__(self):
821829
return self.hash
822830

@@ -829,27 +837,6 @@ def atom_to_boxes(self, f, evaluation):
829837

830838
return format_element(self, evaluation, f)
831839

832-
def to_sympy(self, **kwargs):
833-
return self.real.to_sympy() + sympy.I * self.imag.to_sympy()
834-
835-
@cache
836-
def to_python(self, *args, **kwargs) -> Union[int, float, complex]:
837-
"""
838-
Returns a Python equivalent value for this complex number.
839-
"""
840-
if self.imag.sameQ(Integer0):
841-
return self.real.to_python(*args, **kwargs)
842-
843-
return complex(
844-
self.real.to_python(*args, **kwargs), self.imag.to_python(*args, **kwargs)
845-
)
846-
847-
@cache
848-
def to_mpmath(self, precision: Optional[int] = None):
849-
return mpmath.mpc(
850-
self.real.to_mpmath(precision), self.imag.to_mpmath(precision)
851-
)
852-
853840
@cache
854841
def default_format(self, evaluation, form) -> str:
855842
return "Complex[%s, %s]" % (
@@ -870,25 +857,6 @@ def element_order(self) -> tuple:
870857
1,
871858
)
872859

873-
@property
874-
def pattern_precedence(self) -> tuple:
875-
"""
876-
Return a precedence value, a tuple, which is used in selecting
877-
which pattern to select when several match.
878-
"""
879-
return super().pattern_precedence
880-
881-
def sameQ(self, rhs) -> bool:
882-
"""Mathics SameQ"""
883-
return (
884-
isinstance(rhs, Complex) and self.real == rhs.real and self.imag == rhs.imag
885-
)
886-
887-
def round(self, d=None) -> "Complex":
888-
real = self.real.round(d)
889-
imag = self.imag.round(d)
890-
return Complex(real, imag)
891-
892860
@property
893861
def is_exact(self) -> bool:
894862
if self.real.is_machine_precision() or self.imag.is_machine_precision():
@@ -920,43 +888,36 @@ def get_float_value(self, permit_complex=False) -> Optional[complex]:
920888
return complex(real, imag)
921889
return None
922890

923-
@cache
924-
def get_precision(self) -> Optional[int]:
925-
"""Returns the default specification for precision in N and other numerical functions.
926-
When `None` is be returned no precision is has been defined and this object's value is
927-
exact.
928-
929-
This function is called by method `is_inexact()`.
930-
"""
931-
real_prec = self.real.get_precision()
932-
imag_prec = self.imag.get_precision()
933-
if imag_prec is None or real_prec is None:
934-
return None
935-
return min(real_prec, imag_prec)
936-
937891
def do_copy(self) -> "Complex":
938892
return Complex(self.real.do_copy(), self.imag.do_copy())
939893

940-
def user_hash(self, update) -> None:
941-
update(b"System`Complex>")
942-
update(self.real)
943-
update(self.imag)
944-
945894
def __eq__(self, other) -> bool:
946895
if isinstance(other, Complex):
947896
return self.real == other.real and self.imag == other.imag
948897
else:
949898
return super().__eq__(other)
950899

900+
@cache
951901
def __getnewargs__(self):
952902
return (self.real, self.imag)
953903

904+
@cache
954905
def __neg__(self):
955906
return Complex(-self.real, -self.imag)
956907

957-
@property
958-
def is_zero(self) -> bool:
959-
return self.real.is_zero and self.imag.is_zero
908+
@cache
909+
def get_precision(self) -> Optional[int]:
910+
"""Returns the default specification for precision in N and other numerical functions.
911+
When `None` is be returned no precision is has been defined and this object's value is
912+
exact.
913+
914+
This function is called by method `is_inexact()`.
915+
"""
916+
real_prec = self.real.get_precision()
917+
imag_prec = self.imag.get_precision()
918+
if imag_prec is None or real_prec is None:
919+
return None
920+
return min(real_prec, imag_prec)
960921

961922
@property
962923
def is_approx_zero(self) -> bool:
@@ -972,6 +933,64 @@ def is_approx_zero(self) -> bool:
972933
)
973934
return real_zero and imag_zero
974935

936+
@property
937+
def is_zero(self) -> bool:
938+
return self.real.is_zero and self.imag.is_zero
939+
940+
@property
941+
def pattern_precedence(self) -> tuple:
942+
"""
943+
Return a precedence value, a tuple, which is used in selecting
944+
which pattern to select when several match.
945+
"""
946+
return super().pattern_precedence
947+
948+
def sameQ(self, rhs) -> bool:
949+
"""Mathics SameQ"""
950+
return (
951+
isinstance(rhs, Complex) and self.real == rhs.real and self.imag == rhs.imag
952+
)
953+
954+
def round(self, d=None) -> "Complex":
955+
real = self.real.round(d)
956+
imag = self.imag.round(d)
957+
return Complex(real, imag)
958+
959+
@cache
960+
def to_mpmath(self, precision: Optional[int] = None):
961+
return mpmath.mpc(
962+
self.real.to_mpmath(precision), self.imag.to_mpmath(precision)
963+
)
964+
965+
@cache
966+
def to_python(self, *args, **kwargs) -> Union[int, float, complex]:
967+
"""
968+
Returns a Python equivalent value for this complex number.
969+
"""
970+
if self.imag.sameQ(Integer0):
971+
return self.real.to_python(*args, **kwargs)
972+
973+
return complex(
974+
self.real.to_python(*args, **kwargs), self.imag.to_python(*args, **kwargs)
975+
)
976+
977+
@cache
978+
def to_sympy(self, **kwargs):
979+
return self.real.to_sympy() + sympy.I * self.imag.to_sympy()
980+
981+
def user_hash(self, update) -> None:
982+
update(b"System`Complex>")
983+
update(self.real)
984+
update(self.imag)
985+
986+
@property
987+
def value(self) -> Union[int, complex, float]:
988+
"""Equivalent value in either Python's native
989+
datatype if that exist. Note the SymPy value
990+
and the Python value might be the same thing.
991+
"""
992+
return self._value
993+
975994

976995
class Rational(Number[sympy.Rational]):
977996
class_head_name = "System`Rational"

0 commit comments

Comments
 (0)