| 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 |
Set :c:data:`errno` to :c:macro:`!ERANGE` on overflows. |
| 83 |
|
| 84 |
|
| 85 |
Complex Numbers as Python Objects |
| 86 |
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ |
| 87 |
|
| 88 |
|
| 89 |
.. c:type:: PyComplexObject |
| 90 |
|
| 91 |
This subtype of :c:type:`PyObject` represents a Python complex number object. |
| 92 |
|
| 93 |
|
| 94 |
.. c:var:: PyTypeObject PyComplex_Type |
| 95 |
|
| 96 |
This instance of :c:type:`PyTypeObject` represents the Python complex number |
| 97 |
type. It is the same object as :class:`complex` in the Python layer. |
| 98 |
|
| 99 |
|
| 100 |
.. c:function:: int PyComplex_Check(PyObject *p) |
| 101 |
|
| 102 |
Return true if its argument is a :c:type:`PyComplexObject` or a subtype of |
| 103 |
:c:type:`PyComplexObject`. This function always succeeds. |
| 104 |
|
| 105 |
|
| 106 |
.. c:function:: int PyComplex_CheckExact(PyObject *p) |
| 107 |
|
| 108 |
Return true if its argument is a :c:type:`PyComplexObject`, but not a subtype of |
| 109 |
:c:type:`PyComplexObject`. This function always succeeds. |
| 110 |
|
| 111 |
|
| 112 |
.. c:function:: PyObject* PyComplex_FromCComplex(Py_complex v) |
| 113 |
|
| 114 |
Create a new Python complex number object from a C :c:type:`Py_complex` value. |
| 115 |
Return ``NULL`` with an exception set on error. |
| 116 |
|
| 117 |
|
| 118 |
.. c:function:: PyObject* PyComplex_FromDoubles(double real, double imag) |
| 119 |
|
| 120 |
Return a new :c:type:`PyComplexObject` object from *real* and *imag*. |
| 121 |
Return ``NULL`` with an exception set on error. |
| 122 |
|
| 123 |
|
| 124 |
.. c:function:: double PyComplex_RealAsDouble(PyObject *op) |
| 125 |
|
| 126 |
Return the real part of *op* as a C :c:expr:`double`. |
| 127 |
|
| 128 |
If *op* is not a Python complex number object but has a |
| 129 |
:meth:`~object.__complex__` method, this method will first be called to |
| 130 |
convert *op* to a Python complex number object. If :meth:`!__complex__` is |
| 131 |
not defined then it falls back to call :c:func:`PyFloat_AsDouble` and |
| 132 |
returns its result. |
| 133 |
|
| 134 |
Upon failure, this method returns ``-1.0`` with an exception set, so one |
| 135 |
should call :c:func:`PyErr_Occurred` to check for errors. |
| 136 |
|
| 137 |
.. versionchanged:: 3.13 |
| 138 |
Use :meth:`~object.__complex__` if available. |
| 139 |
|
| 140 |
.. c:function:: double PyComplex_ImagAsDouble(PyObject *op) |
| 141 |
|
| 142 |
Return the imaginary part of *op* as a C :c:expr:`double`. |
| 143 |
|
| 144 |
If *op* is not a Python complex number object but has a |
| 145 |
:meth:`~object.__complex__` method, this method will first be called to |
| 146 |
convert *op* to a Python complex number object. If :meth:`!__complex__` is |
| 147 |
not defined then it falls back to call :c:func:`PyFloat_AsDouble` and |
| 148 |
returns ``0.0`` on success. |
| 149 |
|
| 150 |
Upon failure, this method returns ``-1.0`` with an exception set, so one |
| 151 |
should call :c:func:`PyErr_Occurred` to check for errors. |
| 152 |
|
| 153 |
.. versionchanged:: 3.13 |
| 154 |
Use :meth:`~object.__complex__` if available. |
| 155 |
|
| 156 |
.. c:function:: Py_complex PyComplex_AsCComplex(PyObject *op) |
| 157 |
|
| 158 |
Return the :c:type:`Py_complex` value of the complex number *op*. |
| 159 |
|
| 160 |
If *op* is not a Python complex number object but has a :meth:`~object.__complex__` |
| 161 |
method, this method will first be called to convert *op* to a Python complex |
| 162 |
number object. If :meth:`!__complex__` is not defined then it falls back to |
| 163 |
:meth:`~object.__float__`. If :meth:`!__float__` is not defined then it falls back |
| 164 |
to :meth:`~object.__index__`. |
| 165 |
|
| 166 |
Upon failure, this method returns :c:type:`Py_complex` |
| 167 |
with :c:member:`~Py_complex.real` set to ``-1.0`` and with an exception set, so one |
| 168 |
should call :c:func:`PyErr_Occurred` to check for errors. |
| 169 |
|
| 170 |
.. versionchanged:: 3.8 |
| 171 |
Use :meth:`~object.__index__` if available. |