File: builtin_pycomplex.pyx

package info (click to toggle)
cython 3.1.6%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 19,932 kB
  • sloc: python: 92,172; ansic: 19,275; cpp: 1,407; xml: 1,031; javascript: 511; makefile: 373; sh: 223; sed: 11
file content (83 lines) | stat: -rw-r--r-- 1,482 bytes parent folder | download
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# mode: run
# tag: pycomplex

from cpython.complex cimport complex


def complex_attributes():
    """
    >>> complex_attributes()
    (1.0, 2.0)
    """
    cdef complex c = 1+2j
    return (c.real, c.imag)


def complex_attributes_assign():
    """
    >>> complex_attributes_assign()
    (10.0, 20.0)
    """
    cdef complex c = 1+2j
    c.cval.real, c.cval.imag = 10, 20
    return (c.real, c.imag)


def complex_cstruct_assign():
    """
    >>> complex_cstruct_assign()
    (10.0, 20.0)
    """
    cdef complex c = 1+2j
    cval = &c.cval
    cval.real, cval.imag = 10, 20
    return (c.real, c.imag)


def complex_coercion():
    """
    >>> complex_coercion()
    (1.0, 2.0, 1.0, 2.0)
    """
    cdef complex py_c = 1+2j
    cdef double complex c_c = py_c
    cdef object py = c_c
    return (c_c.real, c_c.imag, py.real, py.imag)


def complex_arg(complex c):
    """
    >>> 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


cdef double float_result():
    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)