summaryrefslogtreecommitdiff
path: root/numpy/core/src/umath/funcs.inc.src
blob: c2732f92523ffda531e9dbdc5272c94c876704e8 (plain)
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
/* -*- c -*- */

/*
 * This file is for the definitions of the non-c99 functions used in ufuncs.
 * All the complex ufuncs are defined here along with a smattering of real and
 * object functions.
 */

#define NPY_NO_DEPRECATED_API NPY_API_VERSION
#include "npy_pycompat.h"
#include "npy_import.h"


/*
 *****************************************************************************
 **                        PYTHON OBJECT FUNCTIONS                          **
 *****************************************************************************
 */

static PyObject *
Py_square(PyObject *o)
{
    return PyNumber_Multiply(o, o);
}

static PyObject *
Py_get_one(PyObject *NPY_UNUSED(o))
{
    return PyInt_FromLong(1);
}

static PyObject *
Py_reciprocal(PyObject *o)
{
    PyObject *one = PyInt_FromLong(1);
    PyObject *result;

    if (!one) {
        return NULL;
    }
#if defined(NPY_PY3K)
    result = PyNumber_TrueDivide(one, o);
#else
    result = PyNumber_Divide(one, o);
#endif
    Py_DECREF(one);
    return result;
}

/*
 * Define numpy version of PyNumber_Power as binary function.
 */
static PyObject *
npy_ObjectPower(PyObject *x, PyObject *y)
{
    return PyNumber_Power(x, y, Py_None);
}

/**begin repeat
 * #Kind = Max, Min#
 * #OP = Py_GE, Py_LE#
 */
static PyObject *
npy_Object@Kind@(PyObject *i1, PyObject *i2)
{
    PyObject *result;
    int cmp;

    cmp = PyObject_RichCompareBool(i1, i2, @OP@);
    if (cmp < 0) {
        return NULL;
    }
    if (cmp == 1) {
        result = i1;
    }
    else {
        result = i2;
    }
    Py_INCREF(result);
    return result;
}
/**end repeat**/

/* Emulates Python's 'a or b' behavior */
static PyObject *
npy_ObjectLogicalOr(PyObject *i1, PyObject *i2)
{
    if (i1 == NULL) {
        Py_XINCREF(i2);
        return i2;
    }
    else if (i2 == NULL) {
        Py_INCREF(i1);
        return i1;
    }
    else {
        int retcode = PyObject_IsTrue(i1);
        if (retcode == -1) {
            return NULL;
        }
        else if (retcode) {
            Py_INCREF(i1);
            return i1;
        }
        else {
            Py_INCREF(i2);
            return i2;
        }
    }
}

/* Emulates Python's 'a and b' behavior */
static PyObject *
npy_ObjectLogicalAnd(PyObject *i1, PyObject *i2)
{
    if (i1 == NULL) {
        return NULL;
    }
    else if (i2 == NULL) {
        return NULL;
    }
    else {
        int retcode = PyObject_IsTrue(i1);
        if (retcode == -1) {
            return NULL;
        }
        else if (!retcode) {
            Py_INCREF(i1);
            return i1;
        }
        else {
            Py_INCREF(i2);
            return i2;
        }
    }
}


/* Emulates Python's 'not b' behavior */
static PyObject *
npy_ObjectLogicalNot(PyObject *i1)
{
    if (i1 == NULL) {
        return NULL;
    }
    else {
        int retcode = PyObject_Not(i1);
        if (retcode == -1) {
            return NULL;
        }
        else if (retcode) {
            Py_INCREF(Py_True);
            return Py_True;
        }
        else {
            Py_INCREF(Py_False);
            return Py_False;
        }
    }
}

static PyObject *
npy_ObjectFloor(PyObject *obj) {
    PyObject *math_floor_func = NULL;

    npy_cache_import("math", "floor", &math_floor_func);
    if (math_floor_func == NULL) {
        return NULL;
    }
    return PyObject_CallFunction(math_floor_func, "O", obj);
}

static PyObject *
npy_ObjectCeil(PyObject *obj) {
    PyObject *math_ceil_func = NULL;

    npy_cache_import("math", "ceil", &math_ceil_func);
    if (math_ceil_func == NULL) {
        return NULL;
    }
    return PyObject_CallFunction(math_ceil_func, "O", obj);
}

static PyObject *
npy_ObjectTrunc(PyObject *obj) {
    PyObject *math_trunc_func = NULL;

    npy_cache_import("math", "trunc", &math_trunc_func);
    if (math_trunc_func == NULL) {
        return NULL;
    }
    return PyObject_CallFunction(math_trunc_func, "O", obj);
}

static PyObject *
npy_ObjectGCD(PyObject *i1, PyObject *i2)
{
    PyObject *gcd = NULL;

    /* use math.gcd if available, and valid on the provided types */
#if PY_VERSION_HEX >= 0x03050000
    {
        static PyObject *math_gcd_func = NULL;

        npy_cache_import("math", "gcd", &math_gcd_func);
        if (math_gcd_func == NULL) {
            return NULL;
        }
        gcd = PyObject_CallFunction(math_gcd_func, "OO", i1, i2);
        if (gcd != NULL) {
            return gcd;
        }
        /* silence errors, and fall back on pure-python gcd */
        PyErr_Clear();
    }
#endif

    /* otherwise, use our internal one, written in python */
    {
        static PyObject *internal_gcd_func = NULL;

        npy_cache_import("numpy.core._internal", "_gcd", &internal_gcd_func);
        if (internal_gcd_func == NULL) {
            return NULL;
        }
        gcd = PyObject_CallFunction(internal_gcd_func, "OO", i1, i2);
        if (gcd == NULL) {
            return NULL;
        }
        /* _gcd has some unusual behaviour regarding sign */
        return PyNumber_Absolute(gcd);
    }
}

static PyObject *
npy_ObjectLCM(PyObject *i1, PyObject *i2)
{
    /* lcm(a, b) = abs(a // gcd(a, b) * b) */

    PyObject *gcd = npy_ObjectGCD(i1, i2);
    PyObject *tmp;
    if(gcd == NULL) {
        return NULL;
    }
    /* Floor divide preserves integer types - we know the division will have
     * no remainder
     */
    tmp = PyNumber_FloorDivide(i1, gcd);
    if(tmp == NULL) {
        return NULL;
    }

    tmp = PyNumber_Multiply(tmp, i2);
    if(tmp == NULL) {
        return NULL;
    }

    /* even though we fix gcd to be positive, we need to do it again here */
    return PyNumber_Absolute(tmp);
}


static PyObject *
npy_ObjectClip(PyObject *arr, PyObject *min, PyObject *max) {
    PyObject *o = npy_ObjectMax(arr, min);
    if (o == NULL) {
        return NULL;
    }
    Py_SETREF(o, npy_ObjectMin(o, max));
    return o;
}

/*
 *****************************************************************************
 **                           COMPLEX FUNCTIONS                             **
 *****************************************************************************
 */


/*
 * Don't pass structures between functions (only pointers) because how
 * structures are passed is compiler dependent and could cause segfaults if
 * umath_ufunc_object.inc is compiled with a different compiler than an
 * extension that makes use of the UFUNC API
 */

/**begin repeat
 *
 * #ctype = npy_cfloat, npy_cdouble, npy_clongdouble#
 * #ftype = npy_float, npy_double, npy_longdouble#
 * #c = f, ,l#
 */

static void
nc_neg@c@(@ctype@ *a, @ctype@ *r)
{
    r->real = -a->real;
    r->imag = -a->imag;
    return;
}

static void
nc_pos@c@(@ctype@ *a, @ctype@ *r)
{
    r->real = +a->real;
    r->imag = +a->imag;
    return;
}

static void
nc_sqrt@c@(@ctype@ *x, @ctype@ *r)
{
    *r = npy_csqrt@c@(*x);
    return;
}

static void
nc_rint@c@(@ctype@ *x, @ctype@ *r)
{
    r->real = npy_rint@c@(x->real);
    r->imag = npy_rint@c@(x->imag);
}

static void
nc_log@c@(@ctype@ *x, @ctype@ *r)
{
    *r = npy_clog@c@(*x);
    return;
}

static void
nc_log1p@c@(@ctype@ *x, @ctype@ *r)
{
    @ftype@ l = npy_hypot@c@(x->real + 1,x->imag);
    r->imag = npy_atan2@c@(x->imag, x->real + 1);
    r->real = npy_log@c@(l);
    return;
}

static void
nc_exp@c@(@ctype@ *x, @ctype@ *r)
{
    *r = npy_cexp@c@(*x);
    return;
}

static void
nc_exp2@c@(@ctype@ *x, @ctype@ *r)
{
    @ctype@ a;
    a.real = x->real*NPY_LOGE2@c@;
    a.imag = x->imag*NPY_LOGE2@c@;
    nc_exp@c@(&a, r);
    return;
}

static void
nc_expm1@c@(@ctype@ *x, @ctype@ *r)
{
    @ftype@ a = npy_exp@c@(x->real);
    r->real = a*npy_cos@c@(x->imag) - 1.0@c@;
    r->imag = a*npy_sin@c@(x->imag);
    return;
}

static void
nc_pow@c@(@ctype@ *a, @ctype@ *b, @ctype@ *r)
{
   *r = npy_cpow@c@(*a, *b);
    return;
}

static void
nc_acos@c@(@ctype@ *x, @ctype@ *r)
{
    *r = npy_cacos@c@(*x);
    return;
}

static void
nc_acosh@c@(@ctype@ *x, @ctype@ *r)
{
    *r = npy_cacosh@c@(*x);
    return;
}

static void
nc_asin@c@(@ctype@ *x, @ctype@ *r)
{
    *r = npy_casin@c@(*x);
    return;
}


static void
nc_asinh@c@(@ctype@ *x, @ctype@ *r)
{
    *r = npy_casinh@c@(*x);
    return;
}

static void
nc_atan@c@(@ctype@ *x, @ctype@ *r)
{
    *r = npy_catan@c@(*x);
    return;
}

static void
nc_atanh@c@(@ctype@ *x, @ctype@ *r)
{
    *r = npy_catanh@c@(*x);
    return;
}

static void
nc_cos@c@(@ctype@ *x, @ctype@ *r)
{
    *r = npy_ccos@c@(*x);
    return;
}

static void
nc_cosh@c@(@ctype@ *x, @ctype@ *r)
{
    *r = npy_ccosh@c@(*x);
    return;
}

static void
nc_log10@c@(@ctype@ *x, @ctype@ *r)
{
    nc_log@c@(x, r);
    r->real *= NPY_LOG10E@c@;
    r->imag *= NPY_LOG10E@c@;
    return;
}

static void
nc_log2@c@(@ctype@ *x, @ctype@ *r)
{
    nc_log@c@(x, r);
    r->real *= NPY_LOG2E@c@;
    r->imag *= NPY_LOG2E@c@;
    return;
}

static void
nc_sin@c@(@ctype@ *x, @ctype@ *r)
{
    *r = npy_csin@c@(*x);
    return;
}

static void
nc_sinh@c@(@ctype@ *x, @ctype@ *r)
{
    *r = npy_csinh@c@(*x);
    return;
}

static void
nc_tan@c@(@ctype@ *x, @ctype@ *r)
{
   *r = npy_ctan@c@(*x);
   return;
}

static void
nc_tanh@c@(@ctype@ *x, @ctype@ *r)
{
    *r = npy_ctanh@c@(*x);
    return;
}

/**end repeat**/