@@ -1546,6 +1546,14 @@ cdef class Number(Expr):
1546
1546
def is_complex (Basic self ):
1547
1547
return deref(symengine.rcp_static_cast_Number(self .thisptr)).is_complex()
1548
1548
1549
+ @property
1550
+ def real (self ):
1551
+ return self
1552
+
1553
+ @property
1554
+ def imag (self ):
1555
+ return S.Zero
1556
+
1549
1557
1550
1558
class Rational (Number ):
1551
1559
@@ -1602,6 +1610,18 @@ class Rational(Number):
1602
1610
def func (self ):
1603
1611
return self .__class__
1604
1612
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
+
1605
1625
class Integer (Rational ):
1606
1626
1607
1627
def __new__ (cls , i ):
@@ -1687,14 +1707,11 @@ class Integer(Rational):
1687
1707
import sage.all as sage
1688
1708
return sage.Integer(str (self ))
1689
1709
1690
- def __int__ (self ):
1691
- return int ( str ( self ))
1710
+ def __int__ (Basic self ):
1711
+ return symengine.mp_get_si(deref(symengine.rcp_static_cast_Integer( self .thisptr)).as_integer_class( ))
1692
1712
1693
- def __long__ (self ):
1694
- return long (str (self ))
1695
-
1696
- def __float__ (self ):
1697
- return float (str (self ))
1713
+ def __float__ (Basic self ):
1714
+ return symengine.mp_get_d(deref(symengine.rcp_static_cast_Integer(self .thisptr)).as_integer_class())
1698
1715
1699
1716
@property
1700
1717
def p (self ):
@@ -1779,27 +1796,55 @@ class RealDouble(Float):
1779
1796
1780
1797
def _sage_ (Basic self ):
1781
1798
import sage.all as sage
1782
- cdef double i = deref(symengine.rcp_static_cast_RealDouble(self .thisptr)).as_double()
1783
- return sage.RealDoubleField()(i)
1799
+ return sage.RealDoubleField()(float (self ))
1800
+
1801
+ def __float__ (Basic self ):
1802
+ return deref(symengine.rcp_static_cast_RealDouble(self .thisptr)).as_double()
1803
+
1804
+ def __int__ (self ):
1805
+ return int (float (self ))
1806
+
1807
+ def __long__ (self ):
1808
+ return long (float (self ))
1809
+
1810
+ def __complex__ (self ):
1811
+ return complex (float (self ))
1812
+
1813
+
1814
+ cdef class ComplexBase(Number):
1815
+
1816
+ def __int__ (self ):
1817
+ return int (complex (self ))
1818
+
1819
+ def __long__ (self ):
1820
+ return long (complex (self ))
1784
1821
1785
1822
def __float__ (self ):
1786
- return float (str (self ))
1823
+ return float (complex (self ))
1824
+
1825
+ def real_part (Basic self ):
1826
+ return c2py(< rcp_const_basic> deref(symengine.rcp_static_cast_ComplexBase(self .thisptr)).real_part())
1827
+
1828
+ def imaginary_part (Basic self ):
1829
+ return c2py(< rcp_const_basic> deref(symengine.rcp_static_cast_ComplexBase(self .thisptr)).imaginary_part())
1830
+
1831
+ @property
1832
+ def real (self ):
1833
+ return self .real_part()
1834
+
1835
+ @property
1836
+ def imag (self ):
1837
+ return self .imaginary_part()
1787
1838
1788
1839
1789
- cdef class ComplexDouble(Number ):
1840
+ cdef class ComplexDouble(ComplexBase ):
1790
1841
1791
1842
def __cinit__ (self , i = None ):
1792
1843
if i is None :
1793
1844
return
1794
1845
cdef double complex i_ = i
1795
1846
self .thisptr = symengine.make_rcp_ComplexDouble(i_)
1796
1847
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
1848
def _sympy_ (self ):
1804
1849
import sympy
1805
1850
return self .real_part()._sympy_() + sympy.I * self .imaginary_part()._sympy_()
@@ -1808,6 +1853,9 @@ cdef class ComplexDouble(Number):
1808
1853
import sage.all as sage
1809
1854
return self .real_part()._sage_() + sage.I * self .imaginary_part()._sage_()
1810
1855
1856
+ def __complex__ (Basic self ):
1857
+ return deref(symengine.rcp_static_cast_ComplexDouble(self .thisptr)).as_complex_double()
1858
+
1811
1859
1812
1860
class RealMPFR (Float ):
1813
1861
@@ -1838,12 +1886,12 @@ class RealMPFR(Float):
1838
1886
return sage.RealField(int (self .get_prec()))(str (self ))
1839
1887
1840
1888
def __float__ (self ):
1841
- return float (str ( self ))
1889
+ return float (self .n( real = True ))
1842
1890
ELSE :
1843
1891
pass
1844
1892
1845
1893
1846
- cdef class ComplexMPC(Number ):
1894
+ cdef class ComplexMPC(ComplexBase ):
1847
1895
IF HAVE_SYMENGINE_MPC:
1848
1896
def __cinit__ (self , i = None , j = 0 , long prec = 53 , unsigned base = 10 ):
1849
1897
if i is None :
@@ -1852,12 +1900,6 @@ cdef class ComplexMPC(Number):
1852
1900
cdef symengine.mpc_class m = symengine.mpc_class(i_, prec, base)
1853
1901
self .thisptr = < rcp_const_basic> symengine.complex_mpc(symengine.std_move_mpc(m))
1854
1902
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
1903
def _sympy_ (self ):
1862
1904
import sympy
1863
1905
return self .real_part()._sympy_() + sympy.I * self .imaginary_part()._sympy_()
@@ -1869,17 +1911,14 @@ cdef class ComplexMPC(Number):
1869
1911
except ImportError :
1870
1912
import sage.all as sage
1871
1913
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 ))
1872
1917
ELSE :
1873
1918
pass
1874
1919
1875
1920
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())
1921
+ cdef class Complex(ComplexBase):
1883
1922
1884
1923
def _sympy_ (self ):
1885
1924
import sympy
@@ -1889,6 +1928,9 @@ cdef class Complex(Number):
1889
1928
import sage.all as sage
1890
1929
return self .real_part()._sage_() + sage.I * self .imaginary_part()._sage_()
1891
1930
1931
+ def __complex__ (self ):
1932
+ return complex (self .n(real = False ))
1933
+
1892
1934
1893
1935
cdef class Infinity(Number):
1894
1936
0 commit comments