You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

132 lines
3.9 KiB

  1. .. highlightlang:: c
  2. .. _complexobjects:
  3. Complex Number Objects
  4. ----------------------
  5. .. index:: object: complex number
  6. Python's complex number objects are implemented as two distinct types when
  7. viewed from the C API: one is the Python object exposed to Python programs, and
  8. the other is a C structure which represents the actual complex number value.
  9. The API provides functions for working with both.
  10. Complex Numbers as C Structures
  11. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  12. Note that the functions which accept these structures as parameters and return
  13. them as results do so *by value* rather than dereferencing them through
  14. pointers. This is consistent throughout the API.
  15. .. c:type:: Py_complex
  16. The C structure which corresponds to the value portion of a Python complex
  17. number object. Most of the functions for dealing with complex number objects
  18. use structures of this type as input or output values, as appropriate. It is
  19. defined as::
  20. typedef struct {
  21. double real;
  22. double imag;
  23. } Py_complex;
  24. .. c:function:: Py_complex _Py_c_sum(Py_complex left, Py_complex right)
  25. Return the sum of two complex numbers, using the C :c:type:`Py_complex`
  26. representation.
  27. .. c:function:: Py_complex _Py_c_diff(Py_complex left, Py_complex right)
  28. Return the difference between two complex numbers, using the C
  29. :c:type:`Py_complex` representation.
  30. .. c:function:: Py_complex _Py_c_neg(Py_complex complex)
  31. Return the negation of the complex number *complex*, using the C
  32. :c:type:`Py_complex` representation.
  33. .. c:function:: Py_complex _Py_c_prod(Py_complex left, Py_complex right)
  34. Return the product of two complex numbers, using the C :c:type:`Py_complex`
  35. representation.
  36. .. c:function:: Py_complex _Py_c_quot(Py_complex dividend, Py_complex divisor)
  37. Return the quotient of two complex numbers, using the C :c:type:`Py_complex`
  38. representation.
  39. If *divisor* is null, this method returns zero and sets
  40. :c:data:`errno` to :c:data:`EDOM`.
  41. .. c:function:: Py_complex _Py_c_pow(Py_complex num, Py_complex exp)
  42. Return the exponentiation of *num* by *exp*, using the C :c:type:`Py_complex`
  43. representation.
  44. If *num* is null and *exp* is not a positive real number,
  45. this method returns zero and sets :c:data:`errno` to :c:data:`EDOM`.
  46. Complex Numbers as Python Objects
  47. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  48. .. c:type:: PyComplexObject
  49. This subtype of :c:type:`PyObject` represents a Python complex number object.
  50. .. c:var:: PyTypeObject PyComplex_Type
  51. This instance of :c:type:`PyTypeObject` represents the Python complex number
  52. type. It is the same object as :class:`complex` in the Python layer.
  53. .. c:function:: int PyComplex_Check(PyObject *p)
  54. Return true if its argument is a :c:type:`PyComplexObject` or a subtype of
  55. :c:type:`PyComplexObject`.
  56. .. c:function:: int PyComplex_CheckExact(PyObject *p)
  57. Return true if its argument is a :c:type:`PyComplexObject`, but not a subtype of
  58. :c:type:`PyComplexObject`.
  59. .. c:function:: PyObject* PyComplex_FromCComplex(Py_complex v)
  60. Create a new Python complex number object from a C :c:type:`Py_complex` value.
  61. .. c:function:: PyObject* PyComplex_FromDoubles(double real, double imag)
  62. Return a new :c:type:`PyComplexObject` object from *real* and *imag*.
  63. .. c:function:: double PyComplex_RealAsDouble(PyObject *op)
  64. Return the real part of *op* as a C :c:type:`double`.
  65. .. c:function:: double PyComplex_ImagAsDouble(PyObject *op)
  66. Return the imaginary part of *op* as a C :c:type:`double`.
  67. .. c:function:: Py_complex PyComplex_AsCComplex(PyObject *op)
  68. Return the :c:type:`Py_complex` value of the complex number *op*.
  69. If *op* is not a Python complex number object but has a :meth:`__complex__`
  70. method, this method will first be called to convert *op* to a Python complex
  71. number object. Upon failure, this method returns ``-1.0`` as a real value.