/
matrix.py
178 lines (127 loc) · 4.5 KB
/
matrix.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
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
import numpy
import cupy
from cupy import _core
def diag(v, k=0):
"""Returns a diagonal or a diagonal array.
Args:
v (array-like): Array or array-like object.
k (int): Index of diagonals. Zero indicates the main diagonal, a
positive value an upper diagonal, and a negative value a lower
diagonal.
Returns:
cupy.ndarray: If ``v`` indicates a 1-D array, then it returns a 2-D
array with the specified diagonal filled by ``v``. If ``v`` indicates a
2-D array, then it returns the specified diagonal of ``v``. In latter
case, if ``v`` is a :class:`cupy.ndarray` object, then its view is
returned.
.. seealso:: :func:`numpy.diag`
"""
if isinstance(v, cupy.ndarray):
ndim = v.ndim
else:
ndim = numpy.ndim(v)
if ndim == 1:
v = cupy.array(v)
if ndim == 2:
# to save bandwidth, don't copy non-diag elements to GPU
v = numpy.array(v)
if ndim == 1:
size = v.size + abs(k)
ret = cupy.zeros((size, size), dtype=v.dtype)
ret.diagonal(k)[:] = v
return ret
elif ndim == 2:
return cupy.array(v.diagonal(k))
else:
raise ValueError('Input must be 1- or 2-d.')
def diagflat(v, k=0):
"""Creates a diagonal array from the flattened input.
Args:
v (array-like): Array or array-like object.
k (int): Index of diagonals. See :func:`cupy.diag` for detail.
Returns:
cupy.ndarray: A 2-D diagonal array with the diagonal copied from ``v``.
.. seealso:: :func:`numpy.diagflat`
"""
if numpy.isscalar(v):
v = numpy.asarray(v)
return cupy.diag(v.ravel(), k)
_tri_kernel = _core.ElementwiseKernel(
'int32 m, int32 k',
'T out',
'''
int row = i / m;
int col = i % m;
out = (col <= row + k);
''',
'cupy_tri',
)
def tri(N, M=None, k=0, dtype=float):
"""Creates an array with ones at and below the given diagonal.
Args:
N (int): Number of rows.
M (int): Number of columns. ``M == N`` by default.
k (int): The sub-diagonal at and below which the array is filled. Zero
is the main diagonal, a positive value is above it, and a negative
value is below.
dtype: Data type specifier.
Returns:
cupy.ndarray: An array with ones at and below the given diagonal.
.. seealso:: :func:`numpy.tri`
"""
if M is None:
M = N
out = cupy.empty((N, M), dtype=dtype)
return _tri_kernel(M, k, out)
def tril(m, k=0):
"""Returns a lower triangle of an array.
Args:
m (array-like): Array or array-like object.
k (int): The diagonal above which to zero elements. Zero is the main
diagonal, a positive value is above it, and a negative value is
below.
Returns:
cupy.ndarray: A lower triangle of an array.
.. seealso:: :func:`numpy.tril`
"""
m = cupy.asarray(m)
mask = tri(*m.shape[-2:], k=k, dtype=bool)
return cupy.where(mask, m, m.dtype.type(0))
def triu(m, k=0):
"""Returns an upper triangle of an array.
Args:
m (array-like): Array or array-like object.
k (int): The diagonal below which to zero elements. Zero is the main
diagonal, a positive value is above it, and a negative value is
below.
Returns:
cupy.ndarray: An upper triangle of an array.
.. seealso:: :func:`numpy.triu`
"""
m = cupy.asarray(m)
mask = tri(*m.shape[-2:], k=k-1, dtype=bool)
return cupy.where(mask, m.dtype.type(0), m)
def vander(x, N=None, increasing=False):
"""Returns a Vandermonde matrix.
Args:
x (array-like): 1-D array or array-like object.
N (int, optional): Number of columns in the output.
``N = len(x)`` by default.
increasing (bool, optional): Order of the powers of the columns.
If True, the powers increase from right to left,
if False (the default) they are reversed.
Returns:
cupy.ndarray: A Vandermonde matrix.
.. seealso:: :func:`numpy.vander`
"""
x = cupy.asarray(x)
if x.ndim != 1:
raise ValueError("x must be a one-dimensional array or sequence.")
if N is None:
N = len(x)
v = cupy.empty((len(x), N), dtype=numpy.promote_types(x.dtype, int))
tmp = v[:, ::-1] if not increasing else v
cupy.power(x.reshape(-1, 1), cupy.arange(N), out=tmp)
return v
# TODO(okuta): Implement mat
# TODO(okuta): Implement bmat