1
- __all__ = ['atleast_1d' , 'atleast_2d' , 'atleast_3d' , 'block' , 'hstack ' ,
2
- 'stack' , 'vstack' ]
1
+ __all__ = ['atleast_1d' , 'atleast_2d' , 'atleast_3d' , 'atleast_nd ' ,
2
+ 'block' , 'hstack' , ' stack' , 'vstack' ]
3
3
4
4
import functools
5
5
import itertools
17
17
overrides .array_function_dispatch , module = 'numpy' )
18
18
19
19
20
+ def _unpack (lst ):
21
+ if len (lst ) == 1 :
22
+ return lst [0 ]
23
+ return lst
24
+
25
+
20
26
def _atleast_1d_dispatcher (* arys ):
21
27
return arys
22
28
@@ -42,7 +48,7 @@ def atleast_1d(*arys):
42
48
43
49
See Also
44
50
--------
45
- atleast_2d, atleast_3d
51
+ atleast_2d, atleast_3d, atleast_nd
46
52
47
53
Examples
48
54
--------
@@ -61,18 +67,7 @@ def atleast_1d(*arys):
61
67
[array([1]), array([3, 4])]
62
68
63
69
"""
64
- res = []
65
- for ary in arys :
66
- ary = asanyarray (ary )
67
- if ary .ndim == 0 :
68
- result = ary .reshape (1 )
69
- else :
70
- result = ary
71
- res .append (result )
72
- if len (res ) == 1 :
73
- return res [0 ]
74
- else :
75
- return res
70
+ return _unpack ([atleast_nd (a , 1 , 0 ) for a in arys ])
76
71
77
72
78
73
def _atleast_2d_dispatcher (* arys ):
@@ -87,9 +82,9 @@ def atleast_2d(*arys):
87
82
Parameters
88
83
----------
89
84
arys1, arys2, ... : array_like
90
- One or more array-like sequences. Non-array inputs are converted
91
- to arrays. Arrays that already have two or more dimensions are
92
- preserved.
85
+ One or more array-like sequences. Non-array inputs are
86
+ converted to arrays. Arrays that already have two or more
87
+ dimensions are preserved.
93
88
94
89
Returns
95
90
-------
@@ -100,7 +95,7 @@ def atleast_2d(*arys):
100
95
101
96
See Also
102
97
--------
103
- atleast_1d, atleast_3d
98
+ atleast_1d, atleast_3d, atleast_nd
104
99
105
100
Examples
106
101
--------
@@ -117,20 +112,7 @@ def atleast_2d(*arys):
117
112
[array([[1]]), array([[1, 2]]), array([[1, 2]])]
118
113
119
114
"""
120
- res = []
121
- for ary in arys :
122
- ary = asanyarray (ary )
123
- if ary .ndim == 0 :
124
- result = ary .reshape (1 , 1 )
125
- elif ary .ndim == 1 :
126
- result = ary [_nx .newaxis , :]
127
- else :
128
- result = ary
129
- res .append (result )
130
- if len (res ) == 1 :
131
- return res [0 ]
132
- else :
133
- return res
115
+ return _unpack ([atleast_nd (a , 2 , 0 ) for a in arys ])
134
116
135
117
136
118
def _atleast_3d_dispatcher (* arys ):
@@ -145,22 +127,31 @@ def atleast_3d(*arys):
145
127
Parameters
146
128
----------
147
129
arys1, arys2, ... : array_like
148
- One or more array-like sequences. Non-array inputs are converted to
149
- arrays. Arrays that already have three or more dimensions are
150
- preserved.
130
+ One or more array-like sequences. Non-array inputs are
131
+ converted to arrays. Arrays that already have three or more
132
+ dimensions are preserved.
151
133
152
134
Returns
153
135
-------
154
136
res1, res2, ... : ndarray
155
- An array, or list of arrays, each with ``a.ndim >= 3``. Copies are
156
- avoided where possible, and views with three or more dimensions are
157
- returned. For example, a 1-D array of shape ``(N,)`` becomes a view
158
- of shape ``(1, N, 1 )``, and a 2-D array of shape ``(M , N)`` becomes a
159
- view of shape ``(M, N, 1)``.
137
+ An array, or list of arrays, each with ``a.ndim >= 3``. Copies
138
+ are avoided where possible, and views with three or more
139
+ dimensions are returned. For example, a 1-D array of shape
140
+ ``(N, )`` becomes a view of shape ``(1 , N, 1 )``, and a 2-D array
141
+ of shape ``(M, N)`` becomes a view of shape ``(M, N, 1)``.
160
142
161
143
See Also
162
144
--------
163
- atleast_1d, atleast_2d
145
+ atleast_1d, atleast_2d, atleast_nd
146
+
147
+ Notes
148
+ -----
149
+ As mentioned in the `Returns` section, the results of this
150
+ function are not consistent with any of the other `atleast*`
151
+ functions. `atleast_2d` prepends the unit dimension to a 1D array
152
+ while `atleast_3d` appends it to a 2D array. The 1D array case
153
+ both appends and prepends a dimension, while `atleast_nd` can only
154
+ add dimensions to one end at a time.
164
155
165
156
Examples
166
157
--------
@@ -187,22 +178,105 @@ def atleast_3d(*arys):
187
178
[[[1 2]]] (1, 1, 2)
188
179
189
180
"""
190
- res = []
191
- for ary in arys :
192
- ary = asanyarray (ary )
193
- if ary .ndim == 0 :
194
- result = ary .reshape (1 , 1 , 1 )
195
- elif ary .ndim == 1 :
196
- result = ary [_nx .newaxis , :, _nx .newaxis ]
197
- elif ary .ndim == 2 :
198
- result = ary [:, :, _nx .newaxis ]
199
- else :
200
- result = ary
201
- res .append (result )
202
- if len (res ) == 1 :
203
- return res [0 ]
204
- else :
205
- return res
181
+ return _unpack ([atleast_nd (atleast_nd (a , 2 , 0 ), 3 , - 1 ) for a in arys ])
182
+
183
+
184
+ def _atleast_nd_dispatcher (ary , ndim , pos = None ):
185
+ return (ary ,)
186
+
187
+
188
+ @array_function_dispatch (_atleast_nd_dispatcher )
189
+ def atleast_nd (ary , ndim , pos = 0 ):
190
+ """
191
+ View input as array with at least `ndim` dimensions.
192
+
193
+ New unit dimensions are inserted at the index given by `pos` if
194
+ necessary.
195
+
196
+ Parameters
197
+ ----------
198
+ ary : array_like
199
+ The input array. Non-array inputs are converted to arrays.
200
+ Arrays that already have `ndim` or more dimensions are
201
+ preserved.
202
+ ndim : int
203
+ The minimum number of dimensions required.
204
+ pos : int, optional
205
+ The index to insert the new dimensions. May range from
206
+ ``-ary.ndim - 1`` to ``+ary.ndim`` (inclusive). Non-negative
207
+ indices indicate locations before the corresponding axis:
208
+ ``pos=0`` means to insert at the very beginning. Negative
209
+ indices indicate locations after the corresponding axis:
210
+ ``pos=-1`` means to insert at the very end. 0 and -1 are always
211
+ guaranteed to work. Any other number will depend on the
212
+ dimensions of the existing array. Default is 0.
213
+
214
+ Returns
215
+ -------
216
+ res : ndarray
217
+ An array with ``res.ndim >= ndim``. A view is returned for array
218
+ inputs. Dimensions are prepended if `pos` is 0, so for example,
219
+ a 1-D array of shape ``(N,)`` with ``ndim=4`` becomes a view of
220
+ shape ``(1, 1, 1, N)``. Dimensions are appended if `pos` is -1,
221
+ so for example a 2-D array of shape ``(M, N)`` becomes a view of
222
+ shape ``(M, N, 1, 1)`` when ``ndim=4``.
223
+
224
+ See Also
225
+ --------
226
+ atleast_1d, atleast_2d, atleast_3d
227
+
228
+ Notes
229
+ -----
230
+ This function does not follow the convention of the other
231
+ ``atleast_*d`` functions in numpy in that it only accepts a single
232
+ array argument. To process multiple arrays, use a comprehension or
233
+ loop around the function call. See examples below.
234
+
235
+ Setting ``pos=0`` is equivalent to how the array would be
236
+ interpreted by numpy's broadcasting rules. There is no need to call
237
+ this function for simple broadcasting. This is also roughly
238
+ (but not exactly) equivalent to
239
+ ``np.array(ary, copy=False, subok=True, ndmin=ndim)``.
240
+
241
+ It is easy to create functions for specific dimensions similar to
242
+ the other ``atleast_*d`` functions using Python's
243
+ `functools.partial` function. An example is shown below.
244
+
245
+ Examples
246
+ --------
247
+ >>> np.atleast_nd(3.0, 4)
248
+ array([[[[ 3.]]]])
249
+
250
+ >>> x = np.arange(3.0)
251
+ >>> np.atleast_nd(x, 2).shape
252
+ (1, 3)
253
+
254
+ >>> x = np.arange(12.0).reshape(4, 3)
255
+ >>> np.atleast_nd(x, 5).shape
256
+ (1, 1, 1, 4, 3)
257
+ >>> np.atleast_nd(x, 5).base is x.base
258
+ True
259
+
260
+ >>> [np.atleast_nd(x, 2) for x in ((1, 2), [[3, 4]], [[[5, 6]]])]
261
+ [array([[1, 2]]), array([[3, 4]]), array([[[5, 6]]])]
262
+
263
+ >>> np.atleast_nd((1, 2), 5, pos=0).shape
264
+ (1, 1, 1, 1, 2)
265
+ >>> np.atleast_nd((1, 2), 5, pos=-1).shape
266
+ (2, 1, 1, 1, 1)
267
+
268
+ >>> from functools import partial
269
+ >>> atleast_4d = partial(np.atleast_nd, ndim=4)
270
+ >>> atleast_4d([1, 2, 3])
271
+ [[[[1, 2, 3]]]]
272
+ """
273
+ ary = array (ary , copy = False , subok = True )
274
+ pos = normalize_axis_index (pos , ary .ndim + 1 )
275
+ extra = operator .index (ndim ) - ary .ndim
276
+ if extra > 0 :
277
+ ind = pos * (slice (None ),) + extra * (None ,) + (Ellipsis ,)
278
+ ary = ary [ind ]
279
+ return ary
206
280
207
281
208
282
def _arrays_for_stack_dispatcher (arrays , stacklevel = 4 ):
0 commit comments