@@ -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
@@ -1546,6 +1565,14 @@ cdef class Number(Expr):
1546
1565
def is_complex (Basic self ):
1547
1566
return deref(symengine.rcp_static_cast_Number(self .thisptr)).is_complex()
1548
1567
1568
+ @property
1569
+ def real (self ):
1570
+ return self
1571
+
1572
+ @property
1573
+ def imag (self ):
1574
+ return S.Zero
1575
+
1549
1576
1550
1577
class Rational (Number ):
1551
1578
@@ -1602,6 +1629,7 @@ class Rational(Number):
1602
1629
def func (self ):
1603
1630
return self .__class__
1604
1631
1632
+
1605
1633
class Integer (Rational ):
1606
1634
1607
1635
def __new__ (cls , i ):
@@ -1687,14 +1715,8 @@ class Integer(Rational):
1687
1715
import sage.all as sage
1688
1716
return sage.Integer(str (self ))
1689
1717
1690
- def __int__ (self ):
1691
- return int (str (self ))
1692
-
1693
- def __long__ (self ):
1694
- return long (str (self ))
1695
-
1696
- def __float__ (self ):
1697
- return float (str (self ))
1718
+ def __int__ (Basic self ):
1719
+ return symengine.mp_get_si(deref(symengine.rcp_static_cast_Integer(self .thisptr)).as_integer_class())
1698
1720
1699
1721
@property
1700
1722
def p (self ):
@@ -1779,27 +1801,40 @@ class RealDouble(Float):
1779
1801
1780
1802
def _sage_ (Basic self ):
1781
1803
import sage.all as sage
1782
- cdef double i = deref(symengine.rcp_static_cast_RealDouble(self .thisptr)).as_double()
1783
- return sage.RealDoubleField()(i)
1804
+ return sage.RealDoubleField()(float (self ))
1784
1805
1785
- def __float__ (self ):
1786
- return float (str (self ))
1806
+ def __float__ (Basic self ):
1807
+ return deref(symengine.rcp_static_cast_RealDouble(self .thisptr)).as_double()
1808
+
1809
+ def __complex__ (self ):
1810
+ return complex (float (self ))
1787
1811
1788
1812
1789
- cdef class ComplexDouble(Number):
1813
+ cdef class ComplexBase(Number):
1814
+
1815
+ def real_part (Basic self ):
1816
+ return c2py(< rcp_const_basic> deref(symengine.rcp_static_cast_ComplexBase(self .thisptr)).real_part())
1817
+
1818
+ def imaginary_part (Basic self ):
1819
+ return c2py(< rcp_const_basic> deref(symengine.rcp_static_cast_ComplexBase(self .thisptr)).imaginary_part())
1820
+
1821
+ @property
1822
+ def real (self ):
1823
+ return self .real_part()
1824
+
1825
+ @property
1826
+ def imag (self ):
1827
+ return self .imaginary_part()
1828
+
1829
+
1830
+ cdef class ComplexDouble(ComplexBase):
1790
1831
1791
1832
def __cinit__ (self , i = None ):
1792
1833
if i is None :
1793
1834
return
1794
1835
cdef double complex i_ = i
1795
1836
self .thisptr = symengine.make_rcp_ComplexDouble(i_)
1796
1837
1797
- def real_part (Basic self ):
1798
- return c2py(< rcp_const_basic> deref(symengine.rcp_static_cast_ComplexDouble(self .thisptr)).real_part())
1799
-
1800
- def imaginary_part (Basic self ):
1801
- return c2py(< rcp_const_basic> deref(symengine.rcp_static_cast_ComplexDouble(self .thisptr)).imaginary_part())
1802
-
1803
1838
def _sympy_ (self ):
1804
1839
import sympy
1805
1840
return self .real_part()._sympy_() + sympy.I * self .imaginary_part()._sympy_()
@@ -1808,6 +1843,9 @@ cdef class ComplexDouble(Number):
1808
1843
import sage.all as sage
1809
1844
return self .real_part()._sage_() + sage.I * self .imaginary_part()._sage_()
1810
1845
1846
+ def __complex__ (Basic self ):
1847
+ return deref(symengine.rcp_static_cast_ComplexDouble(self .thisptr)).as_complex_double()
1848
+
1811
1849
1812
1850
class RealMPFR (Float ):
1813
1851
@@ -1836,14 +1874,11 @@ class RealMPFR(Float):
1836
1874
except ImportError :
1837
1875
import sage.all as sage
1838
1876
return sage.RealField(int (self .get_prec()))(str (self ))
1839
-
1840
- def __float__ (self ):
1841
- return float (str (self ))
1842
1877
ELSE :
1843
1878
pass
1844
1879
1845
1880
1846
- cdef class ComplexMPC(Number ):
1881
+ cdef class ComplexMPC(ComplexBase ):
1847
1882
IF HAVE_SYMENGINE_MPC:
1848
1883
def __cinit__ (self , i = None , j = 0 , long prec = 53 , unsigned base = 10 ):
1849
1884
if i is None :
@@ -1852,12 +1887,6 @@ cdef class ComplexMPC(Number):
1852
1887
cdef symengine.mpc_class m = symengine.mpc_class(i_, prec, base)
1853
1888
self .thisptr = < rcp_const_basic> symengine.complex_mpc(symengine.std_move_mpc(m))
1854
1889
1855
- def real_part (self ):
1856
- return c2py(< rcp_const_basic> deref(symengine.rcp_static_cast_ComplexMPC(self .thisptr)).real_part())
1857
-
1858
- def imaginary_part (self ):
1859
- return c2py(< rcp_const_basic> deref(symengine.rcp_static_cast_ComplexMPC(self .thisptr)).imaginary_part())
1860
-
1861
1890
def _sympy_ (self ):
1862
1891
import sympy
1863
1892
return self .real_part()._sympy_() + sympy.I * self .imaginary_part()._sympy_()
@@ -1873,13 +1902,7 @@ cdef class ComplexMPC(Number):
1873
1902
pass
1874
1903
1875
1904
1876
- cdef class Complex(Number):
1877
-
1878
- def real_part (self ):
1879
- return c2py(< rcp_const_basic> deref(symengine.rcp_static_cast_Complex(self .thisptr)).real_part())
1880
-
1881
- def imaginary_part (self ):
1882
- return c2py(< rcp_const_basic> deref(symengine.rcp_static_cast_Complex(self .thisptr)).imaginary_part())
1905
+ cdef class Complex(ComplexBase):
1883
1906
1884
1907
def _sympy_ (self ):
1885
1908
import sympy
0 commit comments