1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
|
#include "parts.h"
#include "util.h"
static PyObject *
complex_fromccomplex(PyObject *Py_UNUSED(module), PyObject *obj)
{
Py_complex complex;
if (!PyArg_Parse(obj, "D", &complex)) {
return NULL;
}
return PyComplex_FromCComplex(complex);
}
static PyObject *
complex_asccomplex(PyObject *Py_UNUSED(module), PyObject *obj)
{
Py_complex complex;
NULLABLE(obj);
complex = PyComplex_AsCComplex(obj);
if (complex.real == -1. && PyErr_Occurred()) {
return NULL;
}
return PyComplex_FromCComplex(complex);
}
static PyObject*
_py_c_neg(PyObject *Py_UNUSED(module), PyObject *num)
{
Py_complex complex;
complex = PyComplex_AsCComplex(num);
if (complex.real == -1. && PyErr_Occurred()) {
return NULL;
}
return PyComplex_FromCComplex(_Py_c_neg(complex));
}
#define _PY_C_FUNC2(suffix) \
static PyObject * \
_py_c_##suffix(PyObject *Py_UNUSED(module), PyObject *args) \
{ \
Py_complex a, b, res; \
\
if (!PyArg_ParseTuple(args, "DD", &a, &b)) { \
return NULL; \
} \
\
errno = 0; \
res = _Py_c_##suffix(a, b); \
return Py_BuildValue("Di", &res, errno); \
};
#define _PY_CR_FUNC2(suffix) \
static PyObject * \
_py_cr_##suffix(PyObject *Py_UNUSED(module), PyObject *args) \
{ \
Py_complex a, res; \
double b; \
\
if (!PyArg_ParseTuple(args, "Dd", &a, &b)) { \
return NULL; \
} \
\
errno = 0; \
res = _Py_cr_##suffix(a, b); \
return Py_BuildValue("Di", &res, errno); \
};
#define _PY_RC_FUNC2(suffix) \
static PyObject * \
_py_rc_##suffix(PyObject *Py_UNUSED(module), PyObject *args) \
{ \
Py_complex b, res; \
double a; \
\
if (!PyArg_ParseTuple(args, "dD", &a, &b)) { \
return NULL; \
} \
\
errno = 0; \
res = _Py_rc_##suffix(a, b); \
return Py_BuildValue("Di", &res, errno); \
};
_PY_C_FUNC2(sum)
_PY_CR_FUNC2(sum)
_PY_C_FUNC2(diff)
_PY_CR_FUNC2(diff)
_PY_RC_FUNC2(diff)
_PY_C_FUNC2(prod)
_PY_CR_FUNC2(prod)
_PY_C_FUNC2(quot)
_PY_CR_FUNC2(quot)
_PY_RC_FUNC2(quot)
_PY_C_FUNC2(pow)
static PyObject*
_py_c_abs(PyObject *Py_UNUSED(module), PyObject* obj)
{
Py_complex complex;
double res;
NULLABLE(obj);
complex = PyComplex_AsCComplex(obj);
if (complex.real == -1. && PyErr_Occurred()) {
return NULL;
}
errno = 0;
res = _Py_c_abs(complex);
return Py_BuildValue("di", res, errno);
}
static PyMethodDef test_methods[] = {
{"complex_fromccomplex", complex_fromccomplex, METH_O},
{"complex_asccomplex", complex_asccomplex, METH_O},
{"_py_c_sum", _py_c_sum, METH_VARARGS},
{"_py_cr_sum", _py_cr_sum, METH_VARARGS},
{"_py_c_diff", _py_c_diff, METH_VARARGS},
{"_py_cr_diff", _py_cr_diff, METH_VARARGS},
{"_py_rc_diff", _py_rc_diff, METH_VARARGS},
{"_py_c_neg", _py_c_neg, METH_O},
{"_py_c_prod", _py_c_prod, METH_VARARGS},
{"_py_cr_prod", _py_cr_prod, METH_VARARGS},
{"_py_c_quot", _py_c_quot, METH_VARARGS},
{"_py_cr_quot", _py_cr_quot, METH_VARARGS},
{"_py_rc_quot", _py_rc_quot, METH_VARARGS},
{"_py_c_pow", _py_c_pow, METH_VARARGS},
{"_py_c_abs", _py_c_abs, METH_O},
{NULL},
};
int
_PyTestCapi_Init_Complex(PyObject *mod)
{
if (PyModule_AddFunctions(mod, test_methods) < 0) {
return -1;
}
return 0;
}
|