forked from python/cpython
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcomplex.c
More file actions
155 lines (120 loc) · 3.39 KB
/
complex.c
File metadata and controls
155 lines (120 loc) · 3.39 KB
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
151
152
153
154
155
#include "parts.h"
#include "util.h"
static PyObject *
complex_check(PyObject *Py_UNUSED(module), PyObject *obj)
{
NULLABLE(obj);
return PyLong_FromLong(PyComplex_Check(obj));
}
static PyObject *
complex_checkexact(PyObject *Py_UNUSED(module), PyObject *obj)
{
NULLABLE(obj);
return PyLong_FromLong(PyComplex_CheckExact(obj));
}
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_fromdoubles(PyObject *Py_UNUSED(module), PyObject *args)
{
double real, imag;
if (!PyArg_ParseTuple(args, "dd", &real, &imag)) {
return NULL;
}
return PyComplex_FromDoubles(real, imag);
}
static PyObject *
complex_realasdouble(PyObject *Py_UNUSED(module), PyObject *obj)
{
double real;
NULLABLE(obj);
real = PyComplex_RealAsDouble(obj);
if (real == -1. && PyErr_Occurred()) {
return NULL;
}
return PyFloat_FromDouble(real);
}
static PyObject *
complex_imagasdouble(PyObject *Py_UNUSED(module), PyObject *obj)
{
double imag;
NULLABLE(obj);
imag = PyComplex_ImagAsDouble(obj);
if (imag == -1. && PyErr_Occurred()) {
return NULL;
}
return PyFloat_FromDouble(imag);
}
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 *
test_py_complex(PyObject *Py_UNUSED(module), PyObject *Py_UNUSED(args))
{
// Basic tests on Py_complex functions:
//
// - Py_complex_sum()
// - Py_complex_diff()
// - Py_complex_neg()
// - Py_complex_prod()
// - Py_complex_quot()
// - Py_complex_pow()
// - Py_complex_abs()
Py_complex a = {1.0, 5.0};
Py_complex b = {1.5, 0.5};
Py_complex x = Py_complex_sum(a, b);
assert(x.real == 2.5 && x.imag == 5.5);
x = Py_complex_diff(a, b);
assert(x.real == -0.5 && x.imag == 4.5);
x = Py_complex_neg(a);
assert(x.real == -1.0 && x.imag == -5.0);
a = (Py_complex){1.0, -2.0};
b = (Py_complex){3.0, 4.0};
x = Py_complex_prod(a, b);
assert(x.real == 11.0 && x.imag == -2.0);
a = (Py_complex){6.9, -3.2};
x = Py_complex_quot(a, a);
assert(x.real == 1.0 && x.imag == 0.0);
a = (Py_complex){1.3, 2.7};
Py_complex zero = {0.0, 0.0};
x = Py_complex_pow(a, zero);
assert(x.real == 1.0 && x.imag == 0.0);
x = (Py_complex){3.0, 4.0};
double mod = Py_complex_abs(x);
assert(mod == hypot(3.0, 4.0));
Py_RETURN_NONE;
}
static PyMethodDef test_methods[] = {
{"complex_check", complex_check, METH_O},
{"complex_checkexact", complex_checkexact, METH_O},
{"complex_fromccomplex", complex_fromccomplex, METH_O},
{"complex_fromdoubles", complex_fromdoubles, METH_VARARGS},
{"complex_realasdouble", complex_realasdouble, METH_O},
{"complex_imagasdouble", complex_imagasdouble, METH_O},
{"complex_asccomplex", complex_asccomplex, METH_O},
{"test_py_complex", test_py_complex, METH_NOARGS},
{NULL},
};
int
_PyTestCapi_Init_Complex(PyObject *mod)
{
if (PyModule_AddFunctions(mod, test_methods) < 0) {
return -1;
}
return 0;
}