-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgentrig.c
182 lines (159 loc) · 12.9 KB
/
gentrig.c
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
#define PY_SSIZE_T_CLEAN
#include <Python.h>
#include <math.h>
#include <numpy/ndarraytypes.h>
#include "numpy/ufuncobject.h"
#include "numpy/npy_3kcompat.h"
// Ensure M_PI is defined, some platforms don't have it by default
#ifndef M_PI
#define M_PI 3.14159265358979323846
#endif
#define sec(x) (1.0 / cos(x))
#define cot(x) (1.0 / tan(x))
static char types[2] = {NPY_DOUBLE, NPY_DOUBLE};
static char types_poly[3] = {NPY_DOUBLE, NPY_DOUBLE, NPY_DOUBLE};
#define ufuncbp(name, expr) \
static void name##_ufunc(char **args, const npy_intp *dimensions, const npy_intp *steps, void *data) \
{ \
npy_intp i; \
npy_intp n = dimensions[0]; \
char *in = args[0], *out = args[1]; \
npy_intp in_step = steps[0], out_step = steps[1]; \
double x; \
for (i = 0; i < n; i++) \
{ \
x = *(double *)in; \
*((double *)out) = expr; \
in += in_step; \
out += out_step; \
} \
}
#define ufuncs(name, expr_sin, expr_cos) \
ufuncbp(sin##name, expr_sin); \
ufuncbp(cos##name, expr_cos); \
ufuncbp(tan##name, (expr_sin) / (expr_cos)); \
ufuncbp(csc##name, 1.0 / (expr_sin)); \
ufuncbp(sec##name, 1.0 / (expr_cos)); \
ufuncbp(cot##name, (expr_cos) / (expr_sin)); \
PyUFuncGenericFunction funcs_sin##name##_ufunc[1] = {&sin##name##_ufunc}; \
PyUFuncGenericFunction funcs_cos##name##_ufunc[1] = {&cos##name##_ufunc}; \
PyUFuncGenericFunction funcs_tan##name##_ufunc[1] = {&tan##name##_ufunc}; \
PyUFuncGenericFunction funcs_csc##name##_ufunc[1] = {&csc##name##_ufunc}; \
PyUFuncGenericFunction funcs_sec##name##_ufunc[1] = {&sec##name##_ufunc}; \
PyUFuncGenericFunction funcs_cot##name##_ufunc[1] = {&cot##name##_ufunc};
#define ufuncs_impl(name) \
PyObject *sin##name, *cos##name, *tan##name, *csc##name, *sec##name, *cot##name; \
\
sin##name = PyUFunc_FromFuncAndData(funcs_sin##name##_ufunc, NULL, types, 1, 1, 1, PyUFunc_None, "sin" #name, "", 0); \
cos##name = PyUFunc_FromFuncAndData(funcs_cos##name##_ufunc, NULL, types, 1, 1, 1, PyUFunc_None, "cos" #name, "", 0); \
tan##name = PyUFunc_FromFuncAndData(funcs_tan##name##_ufunc, NULL, types, 1, 1, 1, PyUFunc_None, "tan" #name, "", 0); \
csc##name = PyUFunc_FromFuncAndData(funcs_csc##name##_ufunc, NULL, types, 1, 1, 1, PyUFunc_None, "csc" #name, "", 0); \
sec##name = PyUFunc_FromFuncAndData(funcs_sec##name##_ufunc, NULL, types, 1, 1, 1, PyUFunc_None, "sec" #name, "", 0); \
cot##name = PyUFunc_FromFuncAndData(funcs_cot##name##_ufunc, NULL, types, 1, 1, 1, PyUFunc_None, "cot" #name, "", 0); \
\
PyDict_SetItemString(d, "sin" #name, sin##name); \
PyDict_SetItemString(d, "cos" #name, cos##name); \
PyDict_SetItemString(d, "tan" #name, tan##name); \
PyDict_SetItemString(d, "csc" #name, csc##name); \
PyDict_SetItemString(d, "sec" #name, sec##name); \
PyDict_SetItemString(d, "cot" #name, cot##name); \
\
Py_DECREF(sin##name); \
Py_DECREF(cos##name); \
Py_DECREF(tan##name); \
Py_DECREF(csc##name); \
Py_DECREF(sec##name); \
Py_DECREF(cot##name);
ufuncs(p, -cbrt(3.0 * x), pow(cbrt(3.0 * x), 2.0));
ufuncs(l, x, 1.0);
/*
Polygonal Trig is a special case from the macro in
that it includes a second argument for number of sides
*/
#define ufuncbp_poly(name, precalc, expr) \
\
static void name##_ufunc(char **args, const npy_intp *dimensions, const npy_intp *steps, void *data) \
{ \
npy_intp i; \
npy_intp n = dimensions[0]; \
char *in1 = args[0], *in2 = args[1], *out = args[2]; \
npy_intp in1_step = steps[0], in2_step = steps[1], out_step = steps[2]; \
double x; \
double y; \
for (i = 0; i < n; i++) \
{ \
x = *(double *)in1; \
y = *(double *)in2; \
precalc; \
*((double *)out) = expr; \
in1 += in1_step; \
\
in2 += in2_step; \
out += out_step; \
} \
}
#define ufuncs_poly(name, precalc, expr_sin, expr_cos) \
ufuncbp_poly(sin##name, precalc, expr_sin); \
ufuncbp_poly(cos##name, precalc, expr_cos); \
ufuncbp_poly(tan##name, precalc, (expr_sin) / (expr_cos)); \
ufuncbp_poly(csc##name, precalc, 1.0 / (expr_sin)); \
ufuncbp_poly(sec##name, precalc, 1.0 / (expr_cos)); \
ufuncbp_poly(cot##name, precalc, (expr_cos) / (expr_sin)); \
PyUFuncGenericFunction funcs_sin##name##_ufunc[1] = {&sin##name##_ufunc}; \
PyUFuncGenericFunction funcs_cos##name##_ufunc[1] = {&cos##name##_ufunc}; \
PyUFuncGenericFunction funcs_tan##name##_ufunc[1] = {&tan##name##_ufunc}; \
PyUFuncGenericFunction funcs_csc##name##_ufunc[1] = {&csc##name##_ufunc}; \
PyUFuncGenericFunction funcs_sec##name##_ufunc[1] = {&sec##name##_ufunc}; \
PyUFuncGenericFunction funcs_cot##name##_ufunc[1] = {&cot##name##_ufunc};
#define ufuncs_impl_poly(name) \
PyObject *sin##name, *cos##name, *tan##name, *csc##name, *sec##name, *cot##name; \
\
sin##name = PyUFunc_FromFuncAndData(funcs_sin##name##_ufunc, NULL, types_poly, 1, 2, 1, PyUFunc_None, "sin" #name, "", 0); \
cos##name = PyUFunc_FromFuncAndData(funcs_cos##name##_ufunc, NULL, types_poly, 1, 2, 1, PyUFunc_None, "cos" #name, "", 0); \
tan##name = PyUFunc_FromFuncAndData(funcs_tan##name##_ufunc, NULL, types_poly, 1, 2, 1, PyUFunc_None, "tan" #name, "", 0); \
csc##name = PyUFunc_FromFuncAndData(funcs_csc##name##_ufunc, NULL, types_poly, 1, 2, 1, PyUFunc_None, "csc" #name, "", 0); \
sec##name = PyUFunc_FromFuncAndData(funcs_sec##name##_ufunc, NULL, types_poly, 1, 2, 1, PyUFunc_None, "sec" #name, "", 0); \
cot##name = PyUFunc_FromFuncAndData(funcs_cot##name##_ufunc, NULL, types_poly, 1, 2, 1, PyUFunc_None, "cot" #name, "", 0); \
\
PyDict_SetItemString(d, "sin" #name, sin##name); \
PyDict_SetItemString(d, "cos" #name, cos##name); \
PyDict_SetItemString(d, "tan" #name, tan##name); \
PyDict_SetItemString(d, "csc" #name, csc##name); \
PyDict_SetItemString(d, "sec" #name, sec##name); \
PyDict_SetItemString(d, "cot" #name, cot##name); \
\
Py_DECREF(sin##name); \
Py_DECREF(cos##name); \
Py_DECREF(tan##name); \
Py_DECREF(csc##name); \
Py_DECREF(sec##name); \
Py_DECREF(cot##name);
#define calc_k_p \
double k = floor((x / 2.0) * cot(M_PI / y) + 0.5); \
double p = ((x / 2.0) * cot(M_PI / y) + 0.5) - floor((x / 2.0) * cot(M_PI / y) + 0.5);
#define expr_sinpoly sec(M_PI / y) * (sin((M_PI / y) * (2 * k - 1)) * (1 - p) + sin((M_PI / y) * (2 * k + 1)) * p)
#define expr_cospoly sec(M_PI / y) * (cos((M_PI / y) * (2 * k - 1)) * (1 - p) + cos((M_PI / y) * (2 * k + 1)) * p)
ufuncs_poly(poly, calc_k_p, expr_sinpoly, expr_cospoly);
static PyMethodDef GentrigMethods[] = {
{NULL, NULL, 0, NULL} /* Sentinel */
};
static struct PyModuleDef gentrig_module_def = {
PyModuleDef_HEAD_INIT,
"_gentrig",
"Internal \"_gentrig\" module",
-1,
GentrigMethods};
PyMODINIT_FUNC PyInit_gentrig(void)
{
PyObject *m, *d;
import_array();
import_umath();
m = PyModule_Create(&gentrig_module_def);
if (!m)
return NULL;
d = PyModule_GetDict(m);
ufuncs_impl(p);
ufuncs_impl(l);
ufuncs_impl_poly(poly);
return m;
}