@@ -124,15 +124,13 @@ def atleast_1d(*arys):
124
124
"""
125
125
Convert inputs to arrays with at least one dimension.
126
126
127
- Scalar inputs are converted to 1-dimensional arrays, whilst
128
- higher-dimensional inputs are preserved.
129
-
130
127
For full documentation refer to :obj:`numpy.atleast_1d`.
131
128
132
129
Parameters
133
130
----------
134
- arys : {dpnp_array, usm_ndarray}
135
- One or more input arrays.
131
+ arys : {dpnp.ndarray, usm_ndarray}
132
+ One or more array-like sequences. Arrays that already have one or more
133
+ dimensions are preserved.
136
134
137
135
Returns
138
136
-------
@@ -142,14 +140,20 @@ def atleast_1d(*arys):
142
140
143
141
See Also
144
142
--------
145
- atleast_2d, atleast_3d
143
+ :obj:`dpnp.atleast_2d` : View inputs as arrays with at least two dimensions.
144
+ :obj:`dpnp.atleast_3d` : View inputs as arrays with at least three dimensions.
146
145
147
146
Examples
148
147
--------
149
148
>>> import dpnp as np
150
- >>> np.atleast_1d(1.0)
149
+ >>> x = np.array(1.0)
150
+ >>> np.atleast_1d(x)
151
151
array([1.])
152
152
153
+ >>> y = np.array([3, 4])
154
+ >>> np.atleast_1d(x, y)
155
+ [array([1.]), array([3, 4])]
156
+
153
157
>>> x = np.arange(9.0).reshape(3,3)
154
158
>>> np.atleast_1d(x)
155
159
array([[0., 1., 2.],
@@ -158,18 +162,21 @@ def atleast_1d(*arys):
158
162
>>> np.atleast_1d(x) is x
159
163
True
160
164
161
- >>> np.atleast_1d(1, [3, 4])
162
- [array([1]), array([3, 4])]
163
-
164
165
"""
165
166
166
167
res = []
167
168
for ary in arys :
168
- ary = dpnp .asanyarray (ary )
169
+ if not dpnp .is_supported_array_type (ary ):
170
+ raise TypeError (
171
+ "Each input array must be any of supported type, "
172
+ f"but got { type (ary )} "
173
+ )
169
174
if ary .ndim == 0 :
170
175
result = ary .reshape (1 )
171
176
else :
172
177
result = ary
178
+ if isinstance (result , dpt .usm_ndarray ):
179
+ result = dpnp_array ._create_from_usm_ndarray (result )
173
180
res .append (result )
174
181
if len (res ) == 1 :
175
182
return res [0 ]
@@ -183,36 +190,57 @@ def atleast_2d(*arys):
183
190
184
191
For full documentation refer to :obj:`numpy.atleast_2d`.
185
192
186
- Limitations
187
- -----------
188
- Input arrays is supported as :obj:`dpnp.ndarray`.
193
+ Parameters
194
+ ----------
195
+ arys : {dpnp.ndarray, usm_ndarray}
196
+ One or more array-like sequences. Arrays that already have two or more
197
+ dimensions are preserved.
198
+
199
+ Returns
200
+ -------
201
+ out : dpnp.ndarray
202
+ An array, or list of arrays, each with ``a.ndim >= 2``.
203
+ Copies are avoided where possible, and views with two or more
204
+ dimensions are returned.
205
+
206
+ See Also
207
+ --------
208
+ :obj:`dpnp.atleast_1d` : Convert inputs to arrays with at least one dimension.
209
+ :obj:`dpnp.atleast_3d` : View inputs as arrays with at least three dimensions.
210
+
211
+ Examples
212
+ --------
213
+ >>> import dpnp as np
214
+ >>> x = np.array(3.0)
215
+ >>> np.atleast_2d(x)
216
+ array([[3.]])
217
+
218
+ >>> x = np.arange(3.0)
219
+ >>> np.atleast_2d(x)
220
+ array([[0., 1., 2.]])
221
+
189
222
"""
190
223
191
- all_is_array = True
192
- arys_desc = []
224
+ res = []
193
225
for ary in arys :
194
- if not dpnp .isscalar (ary ):
195
- ary_desc = dpnp .get_dpnp_descriptor (
196
- ary , copy_when_nondefault_queue = False
226
+ if not dpnp .is_supported_array_type (ary ):
227
+ raise TypeError (
228
+ "Each input array must be any of supported type, "
229
+ f"but got { type (ary )} "
197
230
)
198
- if ary_desc :
199
- arys_desc .append (ary_desc )
200
- continue
201
- all_is_array = False
202
- break
203
-
204
- if not use_origin_backend (arys [0 ]) and all_is_array :
205
- result = []
206
- for ary_desc in arys_desc :
207
- res = dpnp_atleast_2d (ary_desc ).get_pyobj ()
208
- result .append (res )
209
-
210
- if len (result ) == 1 :
211
- return result [0 ]
231
+ if ary .ndim == 0 :
232
+ result = ary .reshape (1 , 1 )
233
+ elif ary .ndim == 1 :
234
+ result = ary [dpnp .newaxis , :]
212
235
else :
213
- return result
214
-
215
- return call_origin (numpy .atleast_2d , * arys )
236
+ result = ary
237
+ if isinstance (result , dpt .usm_ndarray ):
238
+ result = dpnp_array ._create_from_usm_ndarray (result )
239
+ res .append (result )
240
+ if len (res ) == 1 :
241
+ return res [0 ]
242
+ else :
243
+ return res
216
244
217
245
218
246
def atleast_3d (* arys ):
@@ -221,36 +249,63 @@ def atleast_3d(*arys):
221
249
222
250
For full documentation refer to :obj:`numpy.atleast_3d`.
223
251
224
- Limitations
225
- -----------
226
- Input arrays is supported as :obj:`dpnp.ndarray`.
252
+ Parameters
253
+ ----------
254
+ arys : {dpnp.ndarray, usm_ndarray}
255
+ One or more array-like sequences. Arrays that already have three or more
256
+ dimensions are preserved.
257
+
258
+ Returns
259
+ -------
260
+ out : dpnp.ndarray
261
+ An array, or list of arrays, each with ``a.ndim >= 3``. Copies are
262
+ avoided where possible, and views with three or more dimensions are
263
+ returned.
264
+
265
+ See Also
266
+ --------
267
+ :obj:`dpnp.atleast_1d` : Convert inputs to arrays with at least one dimension.
268
+ :obj:`dpnp.atleast_2d` : View inputs as arrays with at least three dimensions.
269
+
270
+ Examples
271
+ --------
272
+ >>> import dpnp as np
273
+ >>> x = np.array(3.0)
274
+ >>> np.atleast_3d(x)
275
+ array([[[3.]]])
276
+
277
+ >>> x = np.arange(3.0)
278
+ >>> np.atleast_3d(x).shape
279
+ (1, 3, 1)
280
+
281
+ >>> x = np.arange(12.0).reshape(4, 3)
282
+ >>> np.atleast_3d(x).shape
283
+ (4, 3, 1)
284
+
227
285
"""
228
286
229
- all_is_array = True
230
- arys_desc = []
287
+ res = []
231
288
for ary in arys :
232
- if not dpnp .isscalar (ary ):
233
- ary_desc = dpnp .get_dpnp_descriptor (
234
- ary , copy_when_nondefault_queue = False
289
+ if not dpnp .is_supported_array_type (ary ):
290
+ raise TypeError (
291
+ "Each input array must be any of supported type, "
292
+ f"but got { type (ary )} "
235
293
)
236
- if ary_desc :
237
- arys_desc .append (ary_desc )
238
- continue
239
- all_is_array = False
240
- break
241
-
242
- if not use_origin_backend (arys [0 ]) and all_is_array :
243
- result = []
244
- for ary_desc in arys_desc :
245
- res = dpnp_atleast_3d (ary_desc ).get_pyobj ()
246
- result .append (res )
247
-
248
- if len (result ) == 1 :
249
- return result [0 ]
294
+ if ary .ndim == 0 :
295
+ result = ary .reshape (1 , 1 , 1 )
296
+ elif ary .ndim == 1 :
297
+ result = ary [dpnp .newaxis , :, dpnp .newaxis ]
298
+ elif ary .ndim == 2 :
299
+ result = ary [:, :, dpnp .newaxis ]
250
300
else :
251
- return result
252
-
253
- return call_origin (numpy .atleast_3d , * arys )
301
+ result = ary
302
+ if isinstance (result , dpt .usm_ndarray ):
303
+ result = dpnp_array ._create_from_usm_ndarray (result )
304
+ res .append (result )
305
+ if len (res ) == 1 :
306
+ return res [0 ]
307
+ else :
308
+ return res
254
309
255
310
256
311
def broadcast_to (array , / , shape , subok = False ):
0 commit comments