@@ -1066,6 +1066,25 @@ cdef class Basic(object):
1066
1066
s.append(c2py(< rcp_const_basic> (Y[i]))._sympy_())
1067
1067
return s
1068
1068
1069
+ def __float__ (self ):
1070
+ f = self .n(real = True )
1071
+ if not isinstance (f, RealDouble):
1072
+ raise TypeError (" Can't convert expression to float" )
1073
+ return float (f)
1074
+
1075
+ def __int__ (self ):
1076
+ return int (float (self ))
1077
+
1078
+ def __long__ (self ):
1079
+ return long (float (self ))
1080
+
1081
+ def __complex__ (self ):
1082
+ f = self .n(real = False )
1083
+ if not isinstance (f, (ComplexDouble, RealDouble)):
1084
+ raise TypeError (" Can't convert expression to float" )
1085
+ return complex (f)
1086
+
1087
+
1069
1088
def series (ex , x = None , x0 = 0 , n = 6 , as_deg_coef_pair = False ):
1070
1089
# TODO: check for x0 an infinity, see sympy/core/expr.py
1071
1090
# TODO: nonzero x0
@@ -1610,17 +1629,6 @@ class Rational(Number):
1610
1629
def func (self ):
1611
1630
return self .__class__
1612
1631
1613
- def __int__ (Basic self ):
1614
- return int (float (self ))
1615
-
1616
- def __long__ (self ):
1617
- return int (self )
1618
-
1619
- def __float__ (Basic self ):
1620
- return symengine.mp_get_d(deref(symengine.rcp_static_cast_Rational(self .thisptr)).as_rational_class())
1621
-
1622
- def __complex__ (self ):
1623
- return complex (float (self ))
1624
1632
1625
1633
class Integer (Rational ):
1626
1634
@@ -1710,9 +1718,6 @@ class Integer(Rational):
1710
1718
def __int__ (Basic self ):
1711
1719
return symengine.mp_get_si(deref(symengine.rcp_static_cast_Integer(self .thisptr)).as_integer_class())
1712
1720
1713
- def __float__ (Basic self ):
1714
- return symengine.mp_get_d(deref(symengine.rcp_static_cast_Integer(self .thisptr)).as_integer_class())
1715
-
1716
1721
@property
1717
1722
def p (self ):
1718
1723
return int (self )
@@ -1801,27 +1806,12 @@ class RealDouble(Float):
1801
1806
def __float__ (Basic self ):
1802
1807
return deref(symengine.rcp_static_cast_RealDouble(self .thisptr)).as_double()
1803
1808
1804
- def __int__ (self ):
1805
- return int (float (self ))
1806
-
1807
- def __long__ (self ):
1808
- return long (float (self ))
1809
-
1810
1809
def __complex__ (self ):
1811
1810
return complex (float (self ))
1812
1811
1813
1812
1814
1813
cdef class ComplexBase(Number):
1815
1814
1816
- def __int__ (self ):
1817
- return int (complex (self ))
1818
-
1819
- def __long__ (self ):
1820
- return long (complex (self ))
1821
-
1822
- def __float__ (self ):
1823
- return float (complex (self ))
1824
-
1825
1815
def real_part (Basic self ):
1826
1816
return c2py(< rcp_const_basic> deref(symengine.rcp_static_cast_ComplexBase(self .thisptr)).real_part())
1827
1817
@@ -1884,9 +1874,6 @@ class RealMPFR(Float):
1884
1874
except ImportError :
1885
1875
import sage.all as sage
1886
1876
return sage.RealField(int (self .get_prec()))(str (self ))
1887
-
1888
- def __float__ (self ):
1889
- return float (self .n(real = True ))
1890
1877
ELSE :
1891
1878
pass
1892
1879
@@ -1911,9 +1898,6 @@ cdef class ComplexMPC(ComplexBase):
1911
1898
except ImportError :
1912
1899
import sage.all as sage
1913
1900
return sage.MPComplexField(int (self .get_prec()))(str (self .real_part()), str (self .imaginary_part()))
1914
-
1915
- def __complex__ (self ):
1916
- return complex (self .n(real = False ))
1917
1901
ELSE :
1918
1902
pass
1919
1903
@@ -1928,9 +1912,6 @@ cdef class Complex(ComplexBase):
1928
1912
import sage.all as sage
1929
1913
return self .real_part()._sage_() + sage.I * self .imaginary_part()._sage_()
1930
1914
1931
- def __complex__ (self ):
1932
- return complex (self .n(real = False ))
1933
-
1934
1915
1935
1916
cdef class Infinity(Number):
1936
1917
0 commit comments