File size: 1,327 Bytes
c835f15
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from cupy import _core


def _negative_gcd_error():
    raise TypeError("gcd cannot be computed with boolean arrays")


def _negative_lcm_error():
    raise TypeError("lcm cannot be computed with boolean arrays")


_gcd_preamble = '''
template <typename T> inline __device__ T gcd(T in0, T in1) {
  T r;
  while (in1 != 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 = _gcd_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;
}
'''

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

    .. seealso:: :data:`numpy.lcm`

    ''')