/
data.py
94 lines (65 loc) · 2.09 KB
/
data.py
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
import cupy
from cupy.sparse import base
_ufuncs = [
'arcsin', 'arcsinh', 'arctan', 'arctanh', 'ceil', 'deg2rad', 'expm1',
'floor', 'log1p', 'rad2deg', 'rint', 'sign', 'sin', 'sinh', 'sqrt', 'tan',
'tanh', 'trunc',
]
class _data_matrix(base.spmatrix):
def __init__(self, data):
self.data = data
@property
def dtype(self):
"""Data type of the matrix."""
return self.data.dtype
def _with_data(self, data):
raise NotImplementedError
def __abs__(self):
"""Elementwise abosulte."""
return self._with_data(abs(self.data))
def __neg__(self):
"""Elementwise negative."""
return self._with_data(-self.data)
def astype(self, t):
"""Casts the array to given data type.
Args:
dtype: Type specifier.
Returns:
A copy of the array with a given type.
"""
return self._with_data(self.data.astype(t))
def count_nonzero(self):
"""Returns number of non-zero entries.
.. note::
This method counts the actual number of non-zero entories, which
does not include explicit zero entries.
Instead ``nnz`` returns the number of entries including explicit
zeros.
Returns:
Number of non-zero entries.
"""
return cupy.count_nonzero(self.data)
def power(self, n, dtype=None):
"""Elementwise power function.
Args:
n: Exponent.
dtype: Type specifier.
"""
if dtype is None:
data = self.data.copy()
else:
data = self.data.astype(dtype, copy=True)
data **= n
return self._with_data(data)
def _install_ufunc(func_name):
def f(self):
ufunc = getattr(cupy, func_name)
result = ufunc(self.data)
return self._with_data(result)
f.__doc__ = 'Elementwise %s.' % func_name
f.__name__ = func_name
setattr(_data_matrix, func_name, f)
def _install_ufuncs():
for func_name in _ufuncs:
_install_ufunc(func_name)
_install_ufuncs()