Esempio n. 1
0
def create_math_ufunc(math_name, nargs, name, doc):
    assert 1 <= nargs <= 2
    if nargs == 1:
        return core.create_ufunc(
            name, ('e->e', 'f->f', 'd->d'),
            'out0 = %s(in0)' % math_name, doc=doc)
    else:
        return core.create_ufunc(
            name, ('ee->e', 'ff->f', 'dd->d'),
            'out0 = %s(in0, in1)' % math_name, doc=doc)
Esempio n. 2
0
def create_math_ufunc(math_name, nargs, name, doc):
    assert 1 <= nargs <= 2
    if nargs == 1:
        return core.create_ufunc(name, ('e->e', 'f->f', 'd->d'),
                                 'out0 = %s(in0)' % math_name,
                                 doc=doc)
    else:
        return core.create_ufunc(name, ('ee->e', 'ff->f', 'dd->d'),
                                 'out0 = %s(in0, in1)' % math_name,
                                 doc=doc)
Esempio n. 3
0
    r = in0 % in1;
    in0 = in1;
    in1 = r;
  }
  if (in0 < 0)
    return -in0;
  return in0;
}
'''

gcd = core.create_ufunc(
    'cupy_gcd',
    (('??->?', _negative_gcd_error), 'bb->b', 'BB->B', 'hh->h', 'HH->H',
     'ii->i', 'II->I', 'll->l', 'LL->L', 'qq->q', 'QQ->Q'),
    'out0 = gcd(in0, in1)',
    preamble=_gcd_preamble,
    doc='''Computes gcd of ``x1`` and ``x2`` elementwise.

    .. seealso:: :data:`numpy.gcd`

    ''')

_lcm_preamble = '''
template <typename T> inline __device__ T lcm(T in0, T in1) {
  T r = gcd(in0, in1);
  if (r == 0)
    return 0;
  r = in0 / r * in1;
  if (r < 0)
    return -r;
  return r;
Esempio n. 4
0
    .. seealso:: :data:`numpy.exp`

    ''')

expm1 = ufunc.create_math_ufunc(
    'expm1', 1, 'cupy_expm1', '''Computes ``exp(x) - 1`` elementwise.

    .. seealso:: :data:`numpy.expm1`

    ''')

exp2 = core.create_ufunc('cupy_exp2', ('e->e', 'f->f', 'd->d', 'F->F', 'D->D'),
                         'out0 = pow(in0_type(2), in0)',
                         doc='''Elementwise exponentiation with base 2.

    .. seealso:: :data:`numpy.exp2`

    ''')

log = ufunc.create_math_ufunc(
    'log', 1, 'cupy_log', '''Elementwise natural logarithm function.

    .. seealso:: :data:`numpy.log`

    ''')

log10 = ufunc.create_math_ufunc(
    'log10', 1, 'cupy_log10', '''Elementwise common logarithm function.

    .. seealso:: :data:`numpy.log10`
Esempio n. 5
0
File: ops.py Progetto: 2php/chainer

logical_or = core.create_comparison(
    'logical_or', '||',
    '''Computes the logical OR of two arrays.

    .. seealso:: :data:`numpy.logical_or`

    ''')


logical_not = core.create_ufunc(
    'cupy_logical_not',
    ('?->?', 'b->?', 'B->?', 'h->?', 'H->?', 'i->?', 'I->?', 'l->?', 'L->?',
     'q->?', 'Q->?', 'e->?', 'f->?', 'd->?'),
    'out0 = !in0',
    doc='''Computes the logical NOT of an array.

    .. seealso:: :data:`numpy.logical_not`

    ''')


logical_xor = core.create_ufunc(
    'cupy_logical_xor',
    ('??->?', 'bb->?', 'BB->?', 'hh->?', 'HH->?', 'ii->?', 'II->?', 'll->?',
     'LL->?', 'qq->?', 'QQ->?', 'ee->?', 'ff->?', 'dd->?'),
    'out0 = !in0 != !in1',
    doc='''Computes the logical XOR of two arrays.

    .. seealso:: :data:`numpy.logical_xor`
Esempio n. 6
0
    .. seealso:: :func:`numpy.where`

    """

    missing = (x is None, y is None).count(True)

    if missing == 1:
        raise ValueError("Must provide both 'x' and 'y' or neither.")
    if missing == 2:
        return nonzero(condition)

    return _where_ufunc(condition.astype('?'), x, y)

_where_ufunc = core.create_ufunc(
    'cupy_where',
    ('???->?', '?bb->b', '?BB->B', '?hh->h', '?HH->H', '?ii->i', '?II->I',
     '?ll->l', '?LL->L', '?qq->q', '?QQ->Q', '?ee->e', '?ff->f',
     # On CUDA 6.5 these combinations don't work correctly (on CUDA >=7.0, it
     # works).
     # See issue #551.
     '?hd->d', '?Hd->d',
     '?dd->d'),
    'out0 = in0 ? in1 : in2')


# TODO(okuta): Implement searchsorted


# TODO(okuta): Implement extract
Esempio n. 7
0
import ast

import numpy

from cupy._logic import ops
from cupy._math import arithmetic
from cupy._logic import comparison
from cupy._binary import elementwise
from cupy import core

from cupyx.jit import _types

_numpy_scalar_true_divide = core.create_ufunc(
    'numpy_scalar_true_divide',
    ('??->d', '?i->d', 'i?->d', 'bb->f', 'bi->d', 'BB->f', 'Bi->d', 'hh->f',
     'HH->f', 'ii->d', 'II->d', 'll->d', 'LL->d', 'qq->d', 'QQ->d', 'ee->e',
     'ff->f', 'dd->d', 'FF->F', 'DD->D'),
    'out0 = (out0_type)in0 / (out0_type)in1',
)

_numpy_scalar_invert = core.create_ufunc(
    'numpy_scalar_invert',
    ('?->?', 'b->b', 'B->B', 'h->h', 'H->H', 'i->i', 'I->I', 'l->l', 'L->L',
     'q->q', 'Q->Q'),
    'out0 = ~in0',
)

_numpy_scalar_logical_not = core.create_ufunc(
    'numpy_scalar_logical_not',
    ('?->?', 'b->?', 'B->?', 'h->?', 'H->?', 'i->?', 'I->?', 'l->?', 'L->?',
     'q->?', 'Q->?', 'e->?', 'f->?', 'd->?',
Esempio n. 8
0
    ''')


ceil = ufunc.create_math_ufunc(
    'ceil', 1, 'cupy_ceil',
    '''Rounds each element of an array to its ceiling integer.

    .. seealso:: :data:`numpy.ceil`

    ''')


trunc = ufunc.create_math_ufunc(
    'trunc', 1, 'cupy_trunc',
    '''Rounds each element of an array towards zero.

    .. seealso:: :data:`numpy.trunc`

    ''')


fix = core.create_ufunc(
    'cupy_fix', ('e->e', 'f->f', 'd->d'),
    'out0 = (in0 >= 0.0) ? floor(in0): ceil(in0)',
    doc='''If given value x is positive, it return floor(x).
    Else, it return ceil(x).
    .. seealso:: :data:`numpy.fix`

    ''')
Esempio n. 9
0
from cupy import core

erf = core.create_ufunc('cupyx_scipy_erf', ('f->f', 'd->d'),
                        'out0 = erf(in0)',
                        doc='''Error function.

    .. seealso:: :meth:`scipy.special.erf`

    ''')

erfc = core.create_ufunc('cupyx_scipy_erfc', ('f->f', 'd->d'),
                         'out0 = erfc(in0)',
                         doc='''Complementary error function.

    .. seealso:: :meth:`scipy.special.erfc`

    ''')

erfcx = core.create_ufunc('cupyx_scipy_erfcx', ('f->f', 'd->d'),
                          'out0 = erfcx(in0)',
                          doc='''Scaled complementary error function.

    .. seealso:: :meth:`scipy.special.erfcx`

    ''')
Esempio n. 10
0
    """
    # TODO(okuta): check type
    return a.clip(a_min, a_max, out=out)


# sqrt_fixed is deprecated.
# numpy.sqrt is fixed in numpy 1.11.2.
sqrt = sqrt_fixed = core.sqrt


square = core.create_ufunc(
    'cupy_square',
    ('b->b', 'B->B', 'h->h', 'H->H', 'i->i', 'I->I', 'l->l', 'L->L', 'q->q',
     'Q->Q', 'e->e', 'f->f', 'd->d'),
    'out0 = in0 * in0',
    doc='''Elementwise square function.

    .. seealso:: :data:`numpy.square`

    ''')


absolute = core.absolute


# TODO(beam2d): Implement it
# fabs


_unsigned_sign = 'out0 = in0 > 0'
sign = core.create_ufunc(
Esempio n. 11
0
from cupy import core


add = core.add


reciprocal = core.create_ufunc(
    'cupy_reciprocal',
    ('b', 'B', 'h', 'H', 'i', 'I', 'l', 'L', 'q', 'Q',
     ('e', 'out0 = 1 / in0'),
     ('f', 'out0 = 1 / in0'),
     ('d', 'out0 = 1 / in0')),
    'out0 = in0 == 0 ? 0 : (1 / in0)',
    doc='''Computes ``1 / x`` elementwise.

    .. seealso:: :data:`numpy.reciprocal`

    ''')


negative = core.negative


multiply = core.multiply


divide = core.divide


power = core.power
Esempio n. 12
0

expm1 = ufunc.create_math_ufunc(
    'expm1', 1, 'cupy_expm1',
    '''Computes ``exp(x) - 1`` elementwise.

    .. seealso:: :data:`numpy.expm1`

    ''')


exp2 = core.create_ufunc(
    'cupy_exp2',
    ('e->e', 'f->f', ('d->d', 'out0 = pow(2., in0)')),
    'out0 = powf(2.f, in0)',
    doc='''Elementwise exponentiation with base 2.

    .. seealso:: :data:`numpy.exp2`

    ''')


log = ufunc.create_math_ufunc(
    'log', 1, 'cupy_log',
    '''Elementwise natural logarithm function.

    .. seealso:: :data:`numpy.log`

    ''')

Esempio n. 13
0
    .. seealso:: :func:`numpy.clip`

    """
    # TODO(okuta): check type
    return a.clip(a_min, a_max, out=out)


# sqrt_fixed is deprecated.
# numpy.sqrt is fixed in numpy 1.11.2.
sqrt = sqrt_fixed = core.sqrt

square = core.create_ufunc(
    'cupy_square',
    ('b->b', 'B->B', 'h->h', 'H->H', 'i->i', 'I->I', 'l->l', 'L->L', 'q->q',
     'Q->Q', 'e->e', 'f->f', 'd->d', 'F->F', 'D->D'),
    'out0 = in0 * in0',
    doc='''Elementwise square function.

    .. seealso:: :data:`numpy.square`

    ''')

absolute = core.absolute

# TODO(beam2d): Implement it
# fabs

_unsigned_sign = 'out0 = in0 > 0'
_complex_sign = '''
if (in0.real() == 0) {
  out0 = (in0.imag() > 0) - (in0.imag() < 0);
} else {
Esempio n. 14
0
File: _erf.py Progetto: zivzone/cupy
from cupy import core

erf = core.create_ufunc('cupyx_scipy_erf', ('f->f', 'd->d'),
                        'out0 = erf(in0)',
                        doc='''Error function.

    .. seealso:: :meth:`scipy.special.erf`

    ''')

erfc = core.create_ufunc('cupyx_scipy_erfc', ('f->f', 'd->d'),
                         'out0 = erfc(in0)',
                         doc='''Complementary error function.

    .. seealso:: :meth:`scipy.special.erfc`

    ''')

erfcx = core.create_ufunc('cupyx_scipy_erfcx', ('f->f', 'd->d'),
                          'out0 = erfcx(in0)',
                          doc='''Scaled complementary error function.

    .. seealso:: :meth:`scipy.special.erfcx`

    ''')

erfinv = core.create_ufunc('cupyx_scipy_erfinv', ('f->f', 'd->d'),
                           'out0 = erfinv(in0);',
                           doc='''Inverse function of error function.

    .. seealso:: :meth:`scipy.special.erfinv`
Esempio n. 15
0
    if retstep:
        return ret, step
    else:
        return ret


# TODO(okuta): Implement logspace


# TODO(okuta): Implement meshgrid


# mgrid
# ogrid


_arange_ufunc = core.create_ufunc(
    'cupy_arange',
    ('bb->b', 'BB->B', 'hh->h', 'HH->H', 'ii->i', 'II->I', 'll->l', 'LL->L',
     'qq->q', 'QQ->Q', 'ee->e', 'ff->f', 'dd->d'),
    'out0 = in0 + i * in1')


_float_linspace = 'out0 = in0 + i * in1 / in2'
_linspace_ufunc = core.create_ufunc(
    'cupy_linspace',
    ('bbb->b', 'Bbb->B', 'hhh->h', 'Hhh->H', 'iii->i', 'Iii->I', 'lll->l',
     'Lll->L', 'qqq->q', 'Qqq->Q', ('eel->e', _float_linspace),
     ('ffl->f', _float_linspace), ('ddl->d', _float_linspace)),
    'out0 = (in0_type)(in0 + _floor_divide(in1_type(i * in1), in2))')
Esempio n. 16
0
from cupy import core


ndtr = core.create_ufunc(
    'cupyx_scipy_ndtr', ('f->f', 'd->d'),
    'out0 = normcdf(in0)',
    doc='''Cumulative distribution function of normal distribution.

    .. seealso:: :meth:`scipy.special.ndtr`

    ''')
Esempio n. 17
0
from cupy import core

_is_close = core.create_ufunc(
    'cupy_is_close', ('eeee?->?', 'ffff?->?', 'dddd?->?'), '''
    bool equal_nan = in4;
    if (isfinite(in0) && isfinite(in1)) {
      out0 = fabs(in0 - in1) <= in3 + in2 * fabs(in1);
    } else if (equal_nan) {
      out0 = (in0 == in1) || (isnan(in0) && isnan(in1));
    } else {
      out0 = (in0 == in1);
    }
    ''')


def allclose(a, b, rtol=1.e-5, atol=1.e-8, equal_nan=False):
    """Returns True if two arrays are element-wise equal within a tolerance.

    Two values in ``a`` and ``b`` are  considiered equal when the following
    equation is satisfied.

    .. math::

       |a - b| \\le \\mathrm{atol} + \\mathrm{rtol} |b|

    Args:
        a (cupy.ndarray): Input array to compare.
        b (cupy.ndarray): Input array to compare.
        rtol (float): The relative tolerance.
        atol (float): The absolute tolerance.
        equal_nan (bool): If ``True``, NaN's in ``a`` will be considered equal
Esempio n. 18
0
from cupy import core


add = core.add


reciprocal = core.create_ufunc(
    'cupy_reciprocal',
    ('b', 'B', 'h', 'H', 'i', 'I', 'l', 'L', 'q', 'Q',
     ('e', 'out0 = 1 / in0'),
     ('f', 'out0 = 1 / in0'),
     ('d', 'out0 = 1 / in0'),
     ('F', 'out0 = in0_type(1) / in0'),
     ('D', 'out0 = in0_type(1) / in0')),
    'out0 = in0 == 0 ? 0 : (1 / in0)',
    doc='''Computes ``1 / x`` elementwise.

    .. seealso:: :data:`numpy.reciprocal`

    ''')


negative = core.negative


conj = core.conj


angle = core.angle

Esempio n. 19
0
from cupy import core
from cupy.math import ufunc

i0 = ufunc.create_math_ufunc(
    'cyl_bessel_i0', 1, 'cupy_i0',
    '''Modified Bessel function of the first kind, order 0.

    .. seealso:: :func:`numpy.i0`

    ''')

sinc = core.create_ufunc(
    'cupy_sinc', ('e->e', 'f->f', 'd->d'),
    'out0 = abs(in0) > 1e-9 ? sinpi(in0) / (M_PI * in0) : 1',
    doc='''Elementwise sinc function.

    .. seealso:: :func:`numpy.sinc`

    ''')
Esempio n. 20
0
    return a.ravel().nonzero()[0]


_where_ufunc = core.create_ufunc(
    'cupy_where',
    (
        '???->?',
        '?bb->b',
        '?BB->B',
        '?hh->h',
        '?HH->H',
        '?ii->i',
        '?II->I',
        '?ll->l',
        '?LL->L',
        '?qq->q',
        '?QQ->Q',
        '?ee->e',
        '?ff->f',
        # On CUDA 6.5 these combinations don't work correctly (on CUDA >=7.0, it
        # works).
        # See issue #551.
        '?hd->d',
        '?Hd->d',
        '?dd->d',
        '?FF->F',
        '?DD->D'),
    'out0 = in0 ? in1 : in2')


def where(condition, x=None, y=None):
Esempio n. 21
0
    .. seealso:: :func:`numpy.clip`

    '''
    # TODO(okuta): check type
    return a.clip(a_min, a_max, out=out)


sqrt = core.create_ufunc(
    'cupy_sqrt',
    # I think this order is a bug of NumPy, though we select this "buggy"
    # behavior for compatibility with NumPy.
    ('f->f', 'd->d', 'e->e'),
    'out0 = sqrt(in0)',
    doc='''Elementwise positive square-root function.

    .. note::
       This ufunc outputs float32 arrays for float16 arrays input by default as
       well as NumPy 1.9. If you want to override this behavior, specify the
       dtype argument explicitly, or use ``cupy.math.misc.sqrt_fixed`` instead.

    .. seealso:: :data:`numpy.sqrt`

    ''')

sqrt_fixed = core.sqrt_fixed

square = core.create_ufunc(
    'cupy_square', ('b->b', 'B->B', 'h->h', 'H->H', 'i->i', 'I->I', 'l->l',
                    'L->L', 'q->q', 'Q->Q', 'e->e', 'f->f', 'd->d'),
    'out0 = in0 * in0',
    doc='''Elementwise square function.
Esempio n. 22
0
    'ceil',
    1,
    'cupy_ceil',
    '''Rounds each element of an array to its ceiling integer.

    .. seealso:: :data:`numpy.ceil`

    ''',
    support_complex=False)

trunc = ufunc.create_math_ufunc(
    'trunc',
    1,
    'cupy_trunc',
    '''Rounds each element of an array towards zero.

    .. seealso:: :data:`numpy.trunc`

    ''',
    support_complex=False)

fix = core.create_ufunc(
    'cupy_fix', ('e->e', 'f->f', 'd->d'),
    'out0 = (in0 >= 0.0) ? floor(in0): ceil(in0)',
    doc='''If given value x is positive, it return floor(x).
    Else, it return ceil(x).

    .. seealso:: :func:`numpy.fix`

    ''')
Esempio n. 23
0
from cupy import core


gamma = core.create_ufunc(
    'cupyx_scipy_gamma', ('f->f', 'd->d'),
    '''
    if (isinf(in0) && in0 < 0) {
        out0 = -1.0 / 0.0;
    } else if (in0 < 0. && in0 == floor(in0)) {
        out0 = 1.0 / 0.0;
    } else {
        out0 = tgamma(in0);
    }
    ''',
    doc="""Gamma function.

    Args:
        z (cupy.ndarray): The input of gamma function.

    Returns:
        cupy.ndarray: Computed value of gamma function.

    .. seealso:: :data:`scipy.special.gamma`

    """)
Esempio n. 24
0
        return CUDART_INF;
    } else if (delta == 0 || r == 0) {
        return 0;
    } else {
        double u = delta;
        double v = r / delta;
        return u * u * (sqrt(1 + v * v) - 1);
    }
}

'''

entr = core.create_ufunc('cupyx_scipy_entr', ('f->f', 'd->d'),
                         'out0 = out0_type(entr(in0));',
                         preamble=_float_preamble,
                         doc='''Elementwise function for computing entropy.

    .. seealso:: :meth:`scipy.special.entr`

    ''')

kl_div = core.create_ufunc(
    'cupyx_scipy_kl_div', ('ff->f', 'dd->d'),
    'out0 = out0_type(kl_div(in0, in1));',
    preamble=_float_preamble,
    doc='''Elementwise function for computing Kullback-Leibler divergence.

    .. seealso:: :meth:`scipy.special.kl_div`

    ''')

rel_entr = core.create_ufunc(
Esempio n. 25
0

arctan2 = ufunc.create_math_ufunc(
    'atan2', 2, 'cupy_arctan2',
    '''Elementwise inverse-tangent of the ratio of two arrays.

    .. seealso:: :data:`numpy.arctan2`

    ''')


deg2rad = core.create_ufunc(
    'cupy_deg2rad',
    ('e->e', 'f->f', 'd->d'),
    'out0 = in0 * (out0_type)(M_PI / 180)',
    doc='''Converts angles from degrees to radians elementwise.

    .. seealso:: :data:`numpy.deg2rad`, :data:`numpy.radians`

    ''')


rad2deg = core.create_ufunc(
    'cupy_rad2deg',
    ('e->e', 'f->f', 'd->d'),
    'out0 = in0 * (out0_type)(180 / M_PI)',
    doc='''Converts angles from radians to degrees elementwise.

    .. seealso:: :data:`numpy.rad2deg`, :data:`numpy.degrees`

    ''')
Esempio n. 26
0
    .. seealso:: :func:`numpy.clip`

    '''
    # TODO(okuta): check type
    return a(a_min, a_max, out=out)


sqrt = core.create_ufunc(
    'cupy_sqrt',
    # I think this order is a bug of NumPy, though we select this "buggy"
    # behavior for compatibility with NumPy.
    ('f->f', 'd->d', 'e->e'),
    'out0 = sqrt(in0)',
    doc='''Elementwise positive square-root function.

    .. note::
       This ufunc outputs float32 arrays for float16 arrays input by default as
       well as NumPy 1.9. If you want to override this behavior, specify the
       dtype argument explicitly, or use ``cupy.math.misc.sqrt_fixed`` instead.

    .. seealso:: :data:`numpy.sqrt`

    ''')


sqrt_fixed = core.sqrt_fixed


square = core.create_ufunc(
    'cupy_square',
    ('b->b', 'B->B', 'h->h', 'H->H', 'i->i', 'I->I', 'l->l', 'L->L', 'q->q',
Esempio n. 27
0
File: erf.py Progetto: yuhc/ava-cupy
from cupy import core


erf = core.create_ufunc(
    'cupyx_scipy_erf', ('f->f', 'd->d'),
    'out0 = erf(in0)',
    doc='''Error function.

    .. seealso:: :meth:`scipy.special.erf`

    ''')


erfc = core.create_ufunc(
    'cupyx_scipy_erfc', ('f->f', 'd->d'),
    'out0 = erfc(in0)',
    doc='''Complementary error function.

    .. seealso:: :meth:`scipy.special.erfc`

    ''')


erfcx = core.create_ufunc(
    'cupyx_scipy_erfcx', ('f->f', 'd->d'),
    'out0 = erfcx(in0)',
    doc='''Scaled complementary error function.

    .. seealso:: :meth:`scipy.special.erfcx`

    ''')
Esempio n. 28
0
from cupy import core
from cupy.math import ufunc

signbit = core.create_ufunc(
    'cupy_signbit', ('e->?', 'f->?', 'd->?'),
    'out0 = signbit(in0)',
    doc='''Tests elementwise if the sign bit is set (i.e. less than zero).

    .. seealso:: :data:`numpy.signbit`

    ''')

copysign = ufunc.create_math_ufunc(
    'copysign', 2, 'cupy_copysign',
    '''Returns the first argument with the sign bit of the second elementwise.

    .. seealso:: :data:`numpy.copysign`

    ''')

ldexp = core.create_ufunc(
    'cupy_ldexp', ('ei->e', 'fi->f', 'el->e', 'fl->f', 'di->d', 'dl->d'),
    'out0 = ldexp(in0, in1)',
    doc='''Computes ``x1 * 2 ** x2`` elementwise.

    .. seealso:: :data:`numpy.ldexp`

    ''')

frexp = core.create_ufunc(
    'cupy_frexp', ('e->ei', 'f->fi', 'd->di'),
Esempio n. 29
0
def _create_float_test_ufunc(name, doc):
    return core.create_ufunc('cupy_' + name, ('e->?', 'f->?', 'd->?'),
                             'out0 = %s(in0)' % name,
                             doc=doc)
Esempio n. 30
0

arctan2 = ufunc.create_math_ufunc(
    'atan2', 2, 'cupy_arctan2',
    '''Elementwise inverse-tangent of the ratio of two arrays.

    .. seealso:: :data:`numpy.arctan2`

    ''')


deg2rad = core.create_ufunc(
    'cupy_deg2rad',
    ('e->e', 'f->f', 'd->d'),
    'out0 = in0 * (out0_type)(M_PI / 180)',
    doc='''Converts angles from degrees to radians elementwise.

    .. seealso:: :data:`numpy.deg2rad`, :data:`numpy.radians`

    ''')


rad2deg = core.create_ufunc(
    'cupy_rad2deg',
    ('e->e', 'f->f', 'd->d'),
    'out0 = in0 * (out0_type)(180 / M_PI)',
    doc='''Converts angles from radians to degrees elementwise.

    .. seealso:: :data:`numpy.rad2deg`, :data:`numpy.degrees`

    ''')
Esempio n. 31
0
    .. seealso:: :data:`numpy.logical_and`

    ''')

logical_or = core.create_comparison(
    'logical_or', '||', '''Computes the logical OR of two arrays.

    .. seealso:: :data:`numpy.logical_or`

    ''')

logical_not = core.create_ufunc(
    'cupy_logical_not',
    ('?->?', 'b->?', 'B->?', 'h->?', 'H->?', 'i->?', 'I->?', 'l->?', 'L->?',
     'q->?', 'Q->?', 'e->?', 'f->?', 'd->?'),
    'out0 = !in0',
    doc='''Computes the logical NOT of an array.

    .. seealso:: :data:`numpy.logical_not`

    ''')

logical_xor = core.create_ufunc(
    'cupy_logical_xor',
    ('??->?', 'bb->?', 'BB->?', 'hh->?', 'HH->?', 'ii->?', 'II->?', 'll->?',
     'LL->?', 'qq->?', 'QQ->?', 'ee->?', 'ff->?', 'dd->?'),
    'out0 = !in0 != !in1',
    doc='''Computes the logical XOR of two arrays.

    .. seealso:: :data:`numpy.logical_xor`

    ''')
Esempio n. 32
0
from cupy import core
from cupy.math import ufunc


signbit = core.create_ufunc(
    'cupy_signbit',
    ('e->?', 'f->?', 'd->?'),
    'out0 = signbit(in0)',
    doc='''Tests elementwise if the sign bit is set (i.e. less than zero).

    .. seealso:: :data:`numpy.signbit`

    ''')


copysign = ufunc.create_math_ufunc(
    'copysign', 2, 'cupy_copysign',
    '''Returns the first arugment with the sign bit of the second elementwise.

    .. seealso:: :data:`numpy.copysign`

    ''')


ldexp = core.create_ufunc(
    'cupy_ldexp',
    ('ei->e', 'fi->f', 'el->e', 'fl->f', 'di->d', 'dl->d'),
    'out0 = ldexp(in0, in1)',
    doc='''Computes ``x1 * 2 ** x2`` elementwise.

    .. seealso:: :data:`numpy.ldexp`
Esempio n. 33
0
        t = a * b / A[i];
        s = s + t;
        t = fabs(t / s);
        if (t < MACHEP){
            return s;
        }
        k += 1.0;
        a *= x + k;
        b /= w;
        k += 1.0;
    }
    return s;
}
'''

zeta = core.create_ufunc('cupyx_scipy_zeta', ('ff->f', 'dd->d'),
                         'out0 = zeta(in0, in1)',
                         preamble=zeta_definition,
                         doc="""Hurwitz zeta function.

    Args:
        x (cupy.ndarray): Input data, must be real.
        q (cupy.ndarray): Input data, must be real.

    Returns:
        cupy.ndarray: Values of zeta(x, q).

    .. seealso:: :data:`scipy.special.zeta`

    """)
Esempio n. 34
0
    return _where_ufunc(condition.astype("?"), x, y)


_where_ufunc = core.create_ufunc(
    "cupy_where",
    (
        "???->?",
        "?bb->b",
        "?BB->B",
        "?hh->h",
        "?HH->H",
        "?ii->i",
        "?II->I",
        "?ll->l",
        "?LL->L",
        "?qq->q",
        "?QQ->Q",
        "?ee->e",
        "?ff->f",
        # On CUDA 6.5 these combinations don't work correctly (on CUDA >=7.0, it
        # works).
        # See issue #551.
        "?hd->d",
        "?Hd->d",
        "?dd->d",
    ),
    "out0 = in0 ? in1 : in2",
)


# TODO(okuta): Implement searchsorted
Esempio n. 35
0
from cupy import core


j0 = core.create_ufunc(
    'cupyx_scipy_j0', ('f->f', 'd->d'),
    'out0 = j0(in0)',
    doc='''Bessel function of the first kind of order 0.

    .. seealso:: :meth:`scipy.special.j0`

    ''')


j1 = core.create_ufunc(
    'cupyx_scipy_j1', ('f->f', 'd->d'),
    'out0 = j1(in0)',
    doc='''Bessel function of the first kind of order 1.

    .. seealso:: :meth:`scipy.special.j1`

    ''')


y0 = core.create_ufunc(
    'cupyx_scipy_y0', ('f->f', 'd->d'),
    'out0 = y0(in0)',
    doc='''Bessel function of the second kind of order 0.

    .. seealso:: :meth:`scipy.special.y0`

    ''')
Esempio n. 36
0
def _create_float_test_ufunc(name, doc):
    return core.create_ufunc(
        'cupy_' + name, ('e->?', 'f->?', 'd->?'), 'out0 = %s(in0)' % name,
        doc=doc)
Esempio n. 37
0
        while (x > 2.0) {
            x -= 1.0;
            y += 1.0 / x;
        }
    }
    if ((1.0 <= x) && (x <= 2.0)) {
        y += digamma_imp_1_2(x);
        return y;
    }

    /* x is large, use the asymptotic series */
    y += psi_asy(x);
    return y;
}
'''

digamma = core.create_ufunc('cupyx_scipy_digamma', ('f->f', 'd->d'),
                            'out0 = psi(in0)',
                            preamble=polevl_definition + psi_definition,
                            doc="""The digamma function.

    Args:
        x (cupy.ndarray): The input of digamma function.

    Returns:
        cupy.ndarray: Computed value of digamma function.

    .. seealso:: :data:`scipy.special.digamma`

    """)
Esempio n. 38
0
                if step != 1:
                    step = (key[k].stop - start) / float(step - 1)
            nn[k] = (nn[k] * step + start)
        if self.sparse:
            slobj = [cupy.newaxis] * len(size)
            for k in range(len(size)):
                slobj[k] = slice(None, None)
                nn[k] = nn[k][tuple(slobj)]
                slobj[k] = cupy.newaxis
        return nn

    def __len__(self):
        return 0


mgrid = nd_grid(sparse=False)
ogrid = nd_grid(sparse=True)

_arange_ufunc = core.create_ufunc(
    'cupy_arange',
    ('bb->b', 'BB->B', 'hh->h', 'HH->H', 'ii->i', 'II->I', 'll->l', 'LL->L',
     'qq->q', 'QQ->Q', 'ee->e', 'ff->f', 'dd->d',
     ('FF->F', 'out0 = in0 + float(i) * in1'),
     ('DD->D', 'out0 = in0 + double(i) * in1')), 'out0 = in0 + i * in1')

_linspace_ufunc = core.create_ufunc('cupy_linspace', ('dd->d', ),
                                    'out0 = in0 + i * in1')

_linspace_ufunc_underflow = core.create_ufunc('cupy_linspace', ('ddd->d', ),
                                              'out0 = in0 + i * in1 / in2')
Esempio n. 39
0
from cupy import core
from cupy._math import ufunc

i0 = ufunc.create_math_ufunc(
    'cyl_bessel_i0', 1, 'cupy_i0',
    '''Modified Bessel function of the first kind, order 0.

    .. seealso:: :func:`numpy.i0`

    ''')

sinc = core.create_ufunc(
    'cupy_sinc', ('e->e', 'f->f', 'd->d',
                  ('F->F', 'in0_type pi_in0 = (in0_type) M_PI * in0;'
                   'out0 = abs(in0) > 1e-9 ? sin(pi_in0) / (pi_in0) : 1'),
                  ('D->D', 'in0_type pi_in0 = (in0_type) M_PI * in0;'
                   'out0 = abs(in0) > 1e-9 ? sin(pi_in0) / (pi_in0) : 1')),
    'out0 = abs(in0) > 1e-9 ? sinpi(in0) / (M_PI * in0) : 1',
    doc='''Elementwise sinc function.

    .. seealso:: :func:`numpy.sinc`

    ''')
Esempio n. 40
0
File: misc.py Progetto: zhaohb/cupy
    """
    if fusion._is_fusing():
        return fusion._call_ufunc(_math.clip, a, a_min, a_max, out=out)

    # TODO(okuta): check type
    return a.clip(a_min, a_max, out=out)


# sqrt_fixed is deprecated.
# numpy.sqrt is fixed in numpy 1.11.2.
sqrt = sqrt_fixed = core.sqrt

cbrt = core.create_ufunc('cupy_cbrt', ('e->e', 'f->f', 'd->d'),
                         'out0 = cbrt(in0)',
                         doc='''Elementwise cube root function.

    .. seealso:: :data:`numpy.cbrt`

    ''')

square = core.create_ufunc(
    'cupy_square',
    ('b->b', 'B->B', 'h->h', 'H->H', 'i->i', 'I->I', 'l->l', 'L->L', 'q->q',
     'Q->Q', 'e->e', 'f->f', 'd->d', 'F->F', 'D->D'),
    'out0 = in0 * in0',
    doc='''Elementwise square function.

    .. seealso:: :data:`numpy.square`

    ''')
Esempio n. 41
0
        return 0;
    } else {
        u = delta;
        v = r / delta;
        return u * u * (sqrt(1 + v * v) - 1);
    }
}

"""

entr = core.create_ufunc(
    "cupyx_scipy_entr",
    ("f->f", "d->d"),
    "out0 = out0_type(entr(in0));",
    preamble=_float_preamble,
    doc="""Elementwise function for computing entropy.

    .. seealso:: :meth:`scipy.special.entr`

    """,
)

kl_div = core.create_ufunc(
    "cupyx_scipy_kl_div",
    ("ff->f", "dd->d"),
    "out0 = out0_type(kl_div(in0, in1));",
    preamble=_float_preamble,
    doc="""Elementwise function for computing Kullback-Leibler divergence.

    .. seealso:: :meth:`scipy.special.kl_div`
Esempio n. 42
0
    else:
        return ret


# TODO(okuta): Implement logspace


# TODO(okuta): Implement meshgrid


# mgrid
# ogrid


_arange_ufunc = core.create_ufunc(
    'cupy_arange',
    ('bb->b', 'BB->B', 'hh->h', 'HH->H', 'ii->i', 'II->I', 'll->l', 'LL->L',
     'qq->q', 'QQ->Q', 'ee->e', 'ff->f', 'dd->d'),
    'out0 = in0 + i * in1')


_linspace_ufunc = core.create_ufunc(
    'cupy_linspace',
    ('dd->d',),
    'out0 = in0 + i * in1')

_linspace_ufunc_underflow = core.create_ufunc(
    'cupy_linspace',
    ('ddd->d',),
    'out0 = in0 + i * in1 / in2')
Esempio n. 43
0
from cupy import core

gammaln = core.create_ufunc(
    'cupyx_scipy_gammaln', ('f->f', 'd->d'),
    '''
    if (isinf(in0) && in0 < 0) {
        out0 = -1.0 / 0.0;
    } else {
        out0 = lgamma(in0);
    }
    ''',
    doc="""Logarithm of the absolute value of the Gamma function.

    Args:
        x (cupy.ndarray): Values on the real line at which to compute
        ``gammaln``.

    Returns:
        cupy.ndarray: Values of ``gammaln`` at x.

    .. seealso:: :data:`scipy.special.gammaln`

    """)