blob: fc63b57a85521ce534caf0b78f470667ec69cf14 [file] [log] [blame]
Georg Brandl54a3faa2008-01-20 09:30:57 +00001.. highlightlang:: c
2
3.. _complexobjects:
4
5Complex Number Objects
6----------------------
7
8.. index:: object: complex number
9
10Python's complex number objects are implemented as two distinct types when
11viewed from the C API: one is the Python object exposed to Python programs, and
12the other is a C structure which represents the actual complex number value.
13The API provides functions for working with both.
14
15
16Complex Numbers as C Structures
17^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
18
19Note that the functions which accept these structures as parameters and return
20them as results do so *by value* rather than dereferencing them through
21pointers. This is consistent throughout the API.
22
23
Georg Brandl60203b42010-10-06 10:11:56 +000024.. c:type:: Py_complex
Georg Brandl54a3faa2008-01-20 09:30:57 +000025
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. It is
29 defined as::
30
31 typedef struct {
32 double real;
33 double imag;
34 } Py_complex;
35
36
Georg Brandl60203b42010-10-06 10:11:56 +000037.. c:function:: Py_complex _Py_c_sum(Py_complex left, Py_complex right)
Georg Brandl54a3faa2008-01-20 09:30:57 +000038
Georg Brandl60203b42010-10-06 10:11:56 +000039 Return the sum of two complex numbers, using the C :c:type:`Py_complex`
Georg Brandl54a3faa2008-01-20 09:30:57 +000040 representation.
41
42
Georg Brandl60203b42010-10-06 10:11:56 +000043.. c:function:: Py_complex _Py_c_diff(Py_complex left, Py_complex right)
Georg Brandl54a3faa2008-01-20 09:30:57 +000044
45 Return the difference between two complex numbers, using the C
Georg Brandl60203b42010-10-06 10:11:56 +000046 :c:type:`Py_complex` representation.
Georg Brandl54a3faa2008-01-20 09:30:57 +000047
48
Georg Brandl60203b42010-10-06 10:11:56 +000049.. c:function:: Py_complex _Py_c_neg(Py_complex complex)
Georg Brandl54a3faa2008-01-20 09:30:57 +000050
51 Return the negation of the complex number *complex*, using the C
Georg Brandl60203b42010-10-06 10:11:56 +000052 :c:type:`Py_complex` representation.
Georg Brandl54a3faa2008-01-20 09:30:57 +000053
54
Georg Brandl60203b42010-10-06 10:11:56 +000055.. c:function:: Py_complex _Py_c_prod(Py_complex left, Py_complex right)
Georg Brandl54a3faa2008-01-20 09:30:57 +000056
Georg Brandl60203b42010-10-06 10:11:56 +000057 Return the product of two complex numbers, using the C :c:type:`Py_complex`
Georg Brandl54a3faa2008-01-20 09:30:57 +000058 representation.
59
60
Georg Brandl60203b42010-10-06 10:11:56 +000061.. c:function:: Py_complex _Py_c_quot(Py_complex dividend, Py_complex divisor)
Georg Brandl54a3faa2008-01-20 09:30:57 +000062
Georg Brandl60203b42010-10-06 10:11:56 +000063 Return the quotient of two complex numbers, using the C :c:type:`Py_complex`
Georg Brandl54a3faa2008-01-20 09:30:57 +000064 representation.
65
Antoine Pitrou07b1c872011-12-18 01:25:27 +010066 If *divisor* is null, this method returns zero and sets
67 :c:data:`errno` to :c:data:`EDOM`.
68
Georg Brandl54a3faa2008-01-20 09:30:57 +000069
Georg Brandl60203b42010-10-06 10:11:56 +000070.. c:function:: Py_complex _Py_c_pow(Py_complex num, Py_complex exp)
Georg Brandl54a3faa2008-01-20 09:30:57 +000071
Georg Brandl60203b42010-10-06 10:11:56 +000072 Return the exponentiation of *num* by *exp*, using the C :c:type:`Py_complex`
Georg Brandl54a3faa2008-01-20 09:30:57 +000073 representation.
74
Victor Stinnerb99bb202011-12-18 02:56:18 +010075 If *num* is null and *exp* is not a positive real number,
Antoine Pitrou07b1c872011-12-18 01:25:27 +010076 this method returns zero and sets :c:data:`errno` to :c:data:`EDOM`.
77
Georg Brandl54a3faa2008-01-20 09:30:57 +000078
79Complex Numbers as Python Objects
80^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
81
82
Georg Brandl60203b42010-10-06 10:11:56 +000083.. c:type:: PyComplexObject
Georg Brandl54a3faa2008-01-20 09:30:57 +000084
Georg Brandl60203b42010-10-06 10:11:56 +000085 This subtype of :c:type:`PyObject` represents a Python complex number object.
Georg Brandl54a3faa2008-01-20 09:30:57 +000086
87
Georg Brandl60203b42010-10-06 10:11:56 +000088.. c:var:: PyTypeObject PyComplex_Type
Georg Brandl54a3faa2008-01-20 09:30:57 +000089
Georg Brandl60203b42010-10-06 10:11:56 +000090 This instance of :c:type:`PyTypeObject` represents the Python complex number
Georg Brandl2aff3352010-10-17 10:59:41 +000091 type. It is the same object as :class:`complex` in the Python layer.
Georg Brandl54a3faa2008-01-20 09:30:57 +000092
93
Georg Brandl60203b42010-10-06 10:11:56 +000094.. c:function:: int PyComplex_Check(PyObject *p)
Georg Brandl54a3faa2008-01-20 09:30:57 +000095
Georg Brandl60203b42010-10-06 10:11:56 +000096 Return true if its argument is a :c:type:`PyComplexObject` or a subtype of
97 :c:type:`PyComplexObject`.
Georg Brandl54a3faa2008-01-20 09:30:57 +000098
99
Georg Brandl60203b42010-10-06 10:11:56 +0000100.. c:function:: int PyComplex_CheckExact(PyObject *p)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000101
Georg Brandl60203b42010-10-06 10:11:56 +0000102 Return true if its argument is a :c:type:`PyComplexObject`, but not a subtype of
103 :c:type:`PyComplexObject`.
Georg Brandl54a3faa2008-01-20 09:30:57 +0000104
105
Georg Brandl60203b42010-10-06 10:11:56 +0000106.. c:function:: PyObject* PyComplex_FromCComplex(Py_complex v)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000107
Georg Brandl60203b42010-10-06 10:11:56 +0000108 Create a new Python complex number object from a C :c:type:`Py_complex` value.
Georg Brandl54a3faa2008-01-20 09:30:57 +0000109
110
Georg Brandl60203b42010-10-06 10:11:56 +0000111.. c:function:: PyObject* PyComplex_FromDoubles(double real, double imag)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000112
Georg Brandl60203b42010-10-06 10:11:56 +0000113 Return a new :c:type:`PyComplexObject` object from *real* and *imag*.
Georg Brandl54a3faa2008-01-20 09:30:57 +0000114
115
Georg Brandl60203b42010-10-06 10:11:56 +0000116.. c:function:: double PyComplex_RealAsDouble(PyObject *op)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000117
Georg Brandl60203b42010-10-06 10:11:56 +0000118 Return the real part of *op* as a C :c:type:`double`.
Georg Brandl54a3faa2008-01-20 09:30:57 +0000119
120
Georg Brandl60203b42010-10-06 10:11:56 +0000121.. c:function:: double PyComplex_ImagAsDouble(PyObject *op)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000122
Georg Brandl60203b42010-10-06 10:11:56 +0000123 Return the imaginary part of *op* as a C :c:type:`double`.
Georg Brandl54a3faa2008-01-20 09:30:57 +0000124
125
Georg Brandl60203b42010-10-06 10:11:56 +0000126.. c:function:: Py_complex PyComplex_AsCComplex(PyObject *op)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000127
Georg Brandl60203b42010-10-06 10:11:56 +0000128 Return the :c:type:`Py_complex` value of the complex number *op*.
Georg Brandl54a3faa2008-01-20 09:30:57 +0000129
130 If *op* is not a Python complex number object but has a :meth:`__complex__`
131 method, this method will first be called to convert *op* to a Python complex
Antoine Pitrou07b1c872011-12-18 01:25:27 +0100132 number object. Upon failure, this method returns ``-1.0`` as a real value.