_gammasgn.py 1.02 KB
Newer Older
root's avatar
root committed
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
"""
The source code here is an adaptation with minimal changes from the following
file in SciPy's bundled Cephes library:

https://github.com/scipy/scipy/blob/master/scipy/special/cephes/gammasgn.c

Cephes Math Library Release 2.0:  April, 1987
Copyright 1984, 1987 by Stephen L. Moshier
Direct inquiries to 30 Frost Street, Cambridge, MA 02140
"""

from cupy import _core


gammasgn_definition = """
__device__ double gammasgn(double x)
{
    double fx;

    if (isnan(x)) {
      return x;
    }
    if (x > 0) {
        return 1.0;
    }
    else {
        fx = floor(x);
        if (x - fx == 0.0) {
            return 0.0;
        }
        else if ((int)fx % 2) {
            return -1.0;
        }
        else {
            return 1.0;
        }
    }
}
"""

gammasgn = _core.create_ufunc(
    "cupyx_scipy_gammasgn",
    ("f->f", "d->d"),
    "out0 = out0_type(gammasgn(in0));",
    preamble=gammasgn_definition,
    doc="""Elementwise function for scipy.special.gammasgn

    .. seealso:: :meth:`scipy.special.gammasgn`

    """,
)