Debian Codegraph @

Doc/c-api/complex.rst

raw
1 .. highlight:: c
2
3 .. _complexobjects:
4
5 Complex Number Objects
6 ----------------------
7
8 .. index:: pair: object; complex number
9
10 Python's complex number objects are implemented as two distinct types when
11 viewed from the C API: one is the Python object exposed to Python programs, and
12 the other is a C structure which represents the actual complex number value.
13 The API provides functions for working with both.
14
15
16 Complex Numbers as C Structures
17 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
18
19 Note that the functions which accept these structures as parameters and return
20 them as results do so *by value* rather than dereferencing them through
21 pointers. This is consistent throughout the API.
22
23
24 .. c:type:: Py_complex
25
26 The C structure which corresponds to the value portion of a Python complex
27 number object. Most of the functions for dealing with complex number objects
28 use structures of this type as input or output values, as appropriate.
29
30 .. c:member:: double real
31 double imag
32
33 The structure is defined as::
34
35 typedef struct {
36 double real;
37 double imag;
38 } Py_complex;
39
40
41 .. c:function:: Py_complex _Py_c_sum(Py_complex left, Py_complex right)
42
43 Return the sum of two complex numbers, using the C :c:type:`Py_complex`
44 representation.
45
46
47 .. c:function:: Py_complex _Py_c_diff(Py_complex left, Py_complex right)
48
49 Return the difference between two complex numbers, using the C
50 :c:type:`Py_complex` representation.
51
52
53 .. c:function:: Py_complex _Py_c_neg(Py_complex num)
54
55 Return the negation of the complex number *num*, using the C
56 :c:type:`Py_complex` representation.
57
58
59 .. c:function:: Py_complex _Py_c_prod(Py_complex left, Py_complex right)
60
61 Return the product of two complex numbers, using the C :c:type:`Py_complex`
62 representation.
63
64
65 .. c:function:: Py_complex _Py_c_quot(Py_complex dividend, Py_complex divisor)
66
67 Return the quotient of two complex numbers, using the C :c:type:`Py_complex`
68 representation.
69
70 If *divisor* is null, this method returns zero and sets
71 :c:data:`errno` to :c:macro:`!EDOM`.
72
73
74 .. c:function:: Py_complex _Py_c_pow(Py_complex num, Py_complex exp)
75
76 Return the exponentiation of *num* by *exp*, using the C :c:type:`Py_complex`
77 representation.
78
79 If *num* is null and *exp* is not a positive real number,
80 this method returns zero and sets :c:data:`errno` to :c:macro:`!EDOM`.
81
82
83 Complex Numbers as Python Objects
84 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
85
86
87 .. c:type:: PyComplexObject
88
89 This subtype of :c:type:`PyObject` represents a Python complex number object.
90
91
92 .. c:var:: PyTypeObject PyComplex_Type
93
94 This instance of :c:type:`PyTypeObject` represents the Python complex number
95 type. It is the same object as :class:`complex` in the Python layer.
96
97
98 .. c:function:: int PyComplex_Check(PyObject *p)
99
100 Return true if its argument is a :c:type:`PyComplexObject` or a subtype of
101 :c:type:`PyComplexObject`. This function always succeeds.
102
103
104 .. c:function:: int PyComplex_CheckExact(PyObject *p)
105
106 Return true if its argument is a :c:type:`PyComplexObject`, but not a subtype of
107 :c:type:`PyComplexObject`. This function always succeeds.
108
109
110 .. c:function:: PyObject* PyComplex_FromCComplex(Py_complex v)
111
112 Create a new Python complex number object from a C :c:type:`Py_complex` value.
113 Return ``NULL`` with an exception set on error.
114
115
116 .. c:function:: PyObject* PyComplex_FromDoubles(double real, double imag)
117
118 Return a new :c:type:`PyComplexObject` object from *real* and *imag*.
119 Return ``NULL`` with an exception set on error.
120
121
122 .. c:function:: double PyComplex_RealAsDouble(PyObject *op)
123
124 Return the real part of *op* as a C :c:expr:`double`.
125
126 If *op* is not a Python complex number object but has a
127 :meth:`~object.__complex__` method, this method will first be called to
128 convert *op* to a Python complex number object. If :meth:`!__complex__` is
129 not defined then it falls back to call :c:func:`PyFloat_AsDouble` and
130 returns its result.
131
132 Upon failure, this method returns ``-1.0`` with an exception set, so one
133 should call :c:func:`PyErr_Occurred` to check for errors.
134
135 .. versionchanged:: 3.13
136 Use :meth:`~object.__complex__` if available.
137
138 .. c:function:: double PyComplex_ImagAsDouble(PyObject *op)
139
140 Return the imaginary part of *op* as a C :c:expr:`double`.
141
142 If *op* is not a Python complex number object but has a
143 :meth:`~object.__complex__` method, this method will first be called to
144 convert *op* to a Python complex number object. If :meth:`!__complex__` is
145 not defined then it falls back to call :c:func:`PyFloat_AsDouble` and
146 returns ``0.0`` on success.
147
148 Upon failure, this method returns ``-1.0`` with an exception set, so one
149 should call :c:func:`PyErr_Occurred` to check for errors.
150
151 .. versionchanged:: 3.13
152 Use :meth:`~object.__complex__` if available.
153
154 .. c:function:: Py_complex PyComplex_AsCComplex(PyObject *op)
155
156 Return the :c:type:`Py_complex` value of the complex number *op*.
157
158 If *op* is not a Python complex number object but has a :meth:`~object.__complex__`
159 method, this method will first be called to convert *op* to a Python complex
160 number object. If :meth:`!__complex__` is not defined then it falls back to
161 :meth:`~object.__float__`. If :meth:`!__float__` is not defined then it falls back
162 to :meth:`~object.__index__`.
163
164 Upon failure, this method returns :c:type:`Py_complex`
165 with :c:member:`~Py_complex.real` set to ``-1.0`` and with an exception set, so one
166 should call :c:func:`PyErr_Occurred` to check for errors.
167
168 .. versionchanged:: 3.8
169 Use :meth:`~object.__index__` if available.