/
dims.py
182 lines (126 loc) · 4.82 KB
/
dims.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
179
180
181
182
import six
import warnings
import cupy
from cupy import core
zip_longest = six.moves.zip_longest
six_zip = six.moves.zip
# Shape map for atleast_nd functions
# (minimum dimension, input dimension) -> (output shape)
_atleast_nd_shape_map = {
(1, 0): lambda shape: (1,),
(2, 0): lambda shape: (1, 1),
(2, 1): lambda shape: (1,) + shape,
(3, 0): lambda shape: (1, 1, 1),
(3, 1): lambda shape: (1,) + shape + (1,),
(3, 2): lambda shape: shape + (1,),
}
def _atleast_nd_helper(n, arys):
"""Helper function for atleast_nd functions."""
res = []
for a in arys:
a = cupy.asarray(a)
if a.ndim < n:
new_shape = _atleast_nd_shape_map[(n, a.ndim)](a.shape)
a = a.reshape(*new_shape)
res.append(a)
if len(res) == 1:
res, = res
return res
def atleast_1d(*arys):
"""Converts arrays to arrays with dimensions >= 1.
Args:
arys (tuple of arrays): Arrays to be converted. All arguments must be
:class:`cupy.ndarray` objects. Only zero-dimensional array is
affected.
Returns:
If there are only one input, then it returns its converted version.
Otherwise, it returns a list of converted arrays.
.. seealso:: :func:`numpy.atleast_1d`
"""
return _atleast_nd_helper(1, arys)
def atleast_2d(*arys):
"""Converts arrays to arrays with dimensions >= 2.
If an input array has dimensions less than two, then this function inserts
new axes at the head of dimensions to make it have two dimensions.
Args:
arys (tuple of arrays): Arrays to be converted. All arguments must be
:class:`cupy.ndarray` objects.
Returns:
If there are only one input, then it returns its converted version.
Otherwise, it returns a list of converted arrays.
.. seealso:: :func:`numpy.atleast_2d`
"""
return _atleast_nd_helper(2, arys)
def atleast_3d(*arys):
"""Converts arrays to arrays with dimensions >= 3.
If an input array has dimensions less than three, then this function
inserts new axes to make it have three dimensions. The place of the new
axes are following:
- If its shape is ``()``, then the shape of output is ``(1, 1, 1)``.
- If its shape is ``(N,)``, then the shape of output is ``(1, N, 1)``.
- If its shape is ``(M, N)``, then the shape of output is ``(M, N, 1)``.
- Otherwise, the output is the input array itself.
Args:
arys (tuple of arrays): Arrays to be converted. All arguments must be
:class:`cupy.ndarray` objects.
Returns:
If there are only one input, then it returns its converted version.
Otherwise, it returns a list of converted arrays.
.. seealso:: :func:`numpy.atleast_3d`
"""
return _atleast_nd_helper(3, arys)
broadcast = core.broadcast
def broadcast_arrays(*args):
"""Broadcasts given arrays.
Args:
args (tuple of arrays): Arrays to broadcast for each other.
Returns:
list: A list of broadcasted arrays.
.. seealso:: :func:`numpy.broadcast_arrays`
"""
return list(broadcast(*args).values)
def broadcast_to(array, shape):
"""Broadcast an array to a given shape.
Args:
array (cupy.ndarray): Array to broadcast.
shape (tuple of int): The shape of the desired array.
Returns:
cupy.ndarray: Broadcasted view.
.. seealso:: :func:`numpy.broadcast_to`
"""
return core.broadcast_to(array, shape)
def expand_dims(a, axis):
"""Expands given arrays.
Args:
a (cupy.ndarray): Array to be expanded.
axis (int): Position where new axis is to be inserted.
Returns:
cupy.ndarray: The number of dimensions is one greater than that of
the input array.
.. seealso:: :func:`numpy.expand_dims`
"""
# TODO(okuta): check type
shape = a.shape
if axis < 0:
axis = axis + len(shape) + 1
if axis > a.ndim or axis < 0:
# TODO(unno): Too large and too small axis is deprecated in NumPy 1.13
# We need to fix this behavior after NumPy forbids it.
warnings.warn(
'Both axis > a.ndim and axis < -a.ndim - 1 are deprecated and '
'will raise an AxisError in the future.',
DeprecationWarning)
return a.reshape(shape[:axis] + (1,) + shape[axis:])
def squeeze(a, axis=None):
"""Removes size-one axes from the shape of an array.
Args:
a (cupy.ndarray): Array to be reshaped.
axis (int or tuple of ints): Axes to be removed. This function removes
all size-one axes by default. If one of the specified axes is not
of size one, an exception is raised.
Returns:
cupy.ndarray: An array without (specified) size-one axes.
.. seealso:: :func:`numpy.squeeze`
"""
# TODO(okuta): check type
return a.squeeze(axis)