blob: 43dfe56c2807a0c7ec289125d59c3530e4488091 [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
66
Georg Brandl60203b42010-10-06 10:11:56 +000067.. c:function:: Py_complex _Py_c_pow(Py_complex num, Py_complex exp)
Georg Brandl54a3faa2008-01-20 09:30:57 +000068
Georg Brandl60203b42010-10-06 10:11:56 +000069 Return the exponentiation of *num* by *exp*, using the C :c:type:`Py_complex`
Georg Brandl54a3faa2008-01-20 09:30:57 +000070 representation.
71
72
73Complex Numbers as Python Objects
74^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
75
76
Georg Brandl60203b42010-10-06 10:11:56 +000077.. c:type:: PyComplexObject
Georg Brandl54a3faa2008-01-20 09:30:57 +000078
Georg Brandl60203b42010-10-06 10:11:56 +000079 This subtype of :c:type:`PyObject` represents a Python complex number object.
Georg Brandl54a3faa2008-01-20 09:30:57 +000080
81
Georg Brandl60203b42010-10-06 10:11:56 +000082.. c:var:: PyTypeObject PyComplex_Type
Georg Brandl54a3faa2008-01-20 09:30:57 +000083
Georg Brandl60203b42010-10-06 10:11:56 +000084 This instance of :c:type:`PyTypeObject` represents the Python complex number
Georg Brandl2aff3352010-10-17 10:59:41 +000085 type. It is the same object as :class:`complex` in the Python layer.
Georg Brandl54a3faa2008-01-20 09:30:57 +000086
87
Georg Brandl60203b42010-10-06 10:11:56 +000088.. c:function:: int PyComplex_Check(PyObject *p)
Georg Brandl54a3faa2008-01-20 09:30:57 +000089
Georg Brandl60203b42010-10-06 10:11:56 +000090 Return true if its argument is a :c:type:`PyComplexObject` or a subtype of
91 :c:type:`PyComplexObject`.
Georg Brandl54a3faa2008-01-20 09:30:57 +000092
93
Georg Brandl60203b42010-10-06 10:11:56 +000094.. c:function:: int PyComplex_CheckExact(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`, but not 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:: PyObject* PyComplex_FromCComplex(Py_complex v)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000101
Georg Brandl60203b42010-10-06 10:11:56 +0000102 Create a new Python complex number object from a C :c:type:`Py_complex` value.
Georg Brandl54a3faa2008-01-20 09:30:57 +0000103
104
Georg Brandl60203b42010-10-06 10:11:56 +0000105.. c:function:: PyObject* PyComplex_FromDoubles(double real, double imag)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000106
Georg Brandl60203b42010-10-06 10:11:56 +0000107 Return a new :c:type:`PyComplexObject` object from *real* and *imag*.
Georg Brandl54a3faa2008-01-20 09:30:57 +0000108
109
Georg Brandl60203b42010-10-06 10:11:56 +0000110.. c:function:: double PyComplex_RealAsDouble(PyObject *op)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000111
Georg Brandl60203b42010-10-06 10:11:56 +0000112 Return the real part of *op* as a C :c:type:`double`.
Georg Brandl54a3faa2008-01-20 09:30:57 +0000113
114
Georg Brandl60203b42010-10-06 10:11:56 +0000115.. c:function:: double PyComplex_ImagAsDouble(PyObject *op)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000116
Georg Brandl60203b42010-10-06 10:11:56 +0000117 Return the imaginary part of *op* as a C :c:type:`double`.
Georg Brandl54a3faa2008-01-20 09:30:57 +0000118
119
Georg Brandl60203b42010-10-06 10:11:56 +0000120.. c:function:: Py_complex PyComplex_AsCComplex(PyObject *op)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000121
Georg Brandl60203b42010-10-06 10:11:56 +0000122 Return the :c:type:`Py_complex` value of the complex number *op*.
Georg Brandl54a3faa2008-01-20 09:30:57 +0000123
124 If *op* is not a Python complex number object but has a :meth:`__complex__`
125 method, this method will first be called to convert *op* to a Python complex
126 number object.