1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
|
# mode: run
# tag: complex
import cython
def complex_attributes():
"""
>>> complex_attributes()
(1.0, 2.0)
"""
c: complex = 1+2j
return (c.real, c.imag)
def complex_coercion():
"""
>>> complex_coercion()
(1.0, 2.0, 1.0, 2.0)
"""
py_c: complex = 1+2j
c_c: cython.doublecomplex = py_c
py: object = c_c
return (c_c.real, c_c.imag, py.real, py.imag)
def complex_arg(c: complex):
"""
>>> complex_arg(1+2j)
(1.0, 2.0)
"""
return (c.real, c.imag)
def complex_conjugate_nonsimple_float():
"""
>>> complex_conjugate_nonsimple_float()
1.0
"""
x = float(1.0).conjugate()
return x
@cython.cfunc
def float_result() -> cython.double:
return 1.0
def complex_conjugate_nonsimple():
"""
>>> complex_conjugate_nonsimple()
1.0
"""
x = float_result().conjugate()
return x
def complex_builtin_function(a, b):
"""
>>> two = complex_builtin_function(1.0, 0.5)
>>> two == (2+0j) or two
True
"""
return complex(a) / complex(b)
|