Skip to content

Commit 20cb4b0

Browse files
authored
atleast_1d, atleast_2d, atleast_3d (#336)
* add atleast_1d, atleast_2d, atleast_3d
1 parent 12bb79f commit 20cb4b0

File tree

4 files changed

+510
-0
lines changed

4 files changed

+510
-0
lines changed

dpnp/backend_manipulation.pyx

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ from dpnp.dpnp_utils cimport *
3737

3838

3939
__all__ += [
40+
"dpnp_atleast_2d",
41+
"dpnp_atleast_3d",
4042
"dpnp_copyto",
4143
"dpnp_repeat",
4244
"dpnp_transpose"
@@ -48,6 +50,37 @@ ctypedef void(*fptr_custom_elemwise_transpose_1in_1out_t)(void * , dparray_shape
4850
dparray_shape_type &, void * , size_t)
4951

5052

53+
cpdef dparray dpnp_atleast_2d(dparray arr):
54+
cdef size_t arr_ndim = arr.ndim
55+
cdef long arr_size = arr.size
56+
if arr_ndim == 1:
57+
result = dparray((1, arr_size), dtype=arr.dtype)
58+
for i in range(arr_size):
59+
result[0, i] = arr[i]
60+
return result
61+
else:
62+
return arr
63+
64+
65+
cpdef dparray dpnp_atleast_3d(dparray arr):
66+
cdef size_t arr_ndim = arr.ndim
67+
cdef dparray_shape_type arr_shape = arr.shape
68+
cdef long arr_size = arr.size
69+
if arr_ndim == 1:
70+
result = dparray((1, 1, arr_size), dtype=arr.dtype)
71+
for i in range(arr_size):
72+
result[0, 0, i] = arr[i]
73+
return result
74+
elif arr_ndim == 2:
75+
result = dparray((1, arr_shape[0], arr_shape[1]), dtype=arr.dtype)
76+
for i in range(arr_shape[0]):
77+
for j in range(arr_shape[1]):
78+
result[0, i, j] = arr[i, j]
79+
return result
80+
else:
81+
return arr
82+
83+
5184
cpdef dparray dpnp_copyto(dparray dst, dparray src, where=True):
5285
cdef dparray_shape_type shape_src = src.shape
5386
cdef long size_src = src.size

dpnp/dpnp_iface_manipulation.py

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,9 @@
5050

5151

5252
__all__ = [
53+
"atleast_1d",
54+
"atleast_2d",
55+
"atleast_3d",
5356
"copyto",
5457
"moveaxis",
5558
"ravel",
@@ -60,6 +63,85 @@
6063
]
6164

6265

66+
def atleast_1d(*arys):
67+
"""
68+
Convert inputs to arrays with at least one dimension.
69+
Scalar inputs are converted to 1-dimensional arrays, whilst
70+
higher-dimensional inputs are preserved.
71+
72+
For full documentation refer to :obj:`numpy.atleast_1d`.
73+
74+
Limitations
75+
-----------
76+
Input arrays is supported as :obj:`dpnp.ndarray`.
77+
78+
"""
79+
80+
return call_origin(numpy.atleast_1d, *arys)
81+
82+
83+
def atleast_2d(*arys):
84+
"""
85+
View inputs as arrays with at least two dimensions.
86+
87+
For full documentation refer to :obj:`numpy.atleast_2d`.
88+
89+
Limitations
90+
-----------
91+
Input arrays is supported as :obj:`dpnp.ndarray`.
92+
"""
93+
94+
all_is_dparray = True
95+
for ary in arys:
96+
if not isinstance(ary, dparray):
97+
all_is_dparray = False
98+
break
99+
100+
if not use_origin_backend(arys[0]) and all_is_dparray:
101+
result = []
102+
for ary in arys:
103+
res = dpnp_atleast_2d(ary)
104+
result.append(res)
105+
106+
if len(result) == 1:
107+
return result[0]
108+
else:
109+
return result
110+
111+
return call_origin(numpy.atleast_2d, *arys)
112+
113+
114+
def atleast_3d(*arys):
115+
"""
116+
View inputs as arrays with at least three dimensions.
117+
118+
For full documentation refer to :obj:`numpy.atleast_3d`.
119+
120+
Limitations
121+
-----------
122+
Input arrays is supported as :obj:`dpnp.ndarray`.
123+
"""
124+
125+
all_is_dparray = True
126+
for ary in arys:
127+
if not isinstance(ary, dparray):
128+
all_is_dparray = False
129+
break
130+
131+
if not use_origin_backend(arys[0]) and all_is_dparray:
132+
result = []
133+
for ary in arys:
134+
res = dpnp_atleast_3d(ary)
135+
result.append(res)
136+
137+
if len(result) == 1:
138+
return result[0]
139+
else:
140+
return result
141+
142+
return call_origin(numpy.atleast_3d, *arys)
143+
144+
63145
def copyto(dst, src, casting='same_kind', where=True):
64146
"""
65147
Copies values from one array to another, broadcasting as necessary.

tests/skipped_tests.tbl

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -861,6 +861,71 @@ tests/third_party/cupy/math_tests/test_trigonometric.py::TestUnwrap::test_unwrap
861861
tests/third_party/cupy/math_tests/test_trigonometric.py::TestUnwrap::test_unwrap_2dim_with_axis
862862
tests/third_party/cupy/math_tests/test_trigonometric.py::TestUnwrap::test_unwrap_2dim_with_discont
863863
tests/third_party/cupy/math_tests/test_trigonometric.py::TestUnwrap::test_unwrap_2dim_without_axis
864+
tests/third_party/cupy/manipulation_tests/test_dims.py::TestDims::test_broadcast_to
865+
tests/third_party/cupy/manipulation_tests/test_dims.py::TestDims::test_broadcast_to_fail
866+
tests/third_party/cupy/manipulation_tests/test_dims.py::TestDims::test_broadcast_to_fail_numpy19
867+
tests/third_party/cupy/manipulation_tests/test_dims.py::TestDims::test_broadcast_to_numpy19
868+
tests/third_party/cupy/manipulation_tests/test_dims.py::TestDims::test_broadcast_to_short_shape
869+
tests/third_party/cupy/manipulation_tests/test_dims.py::TestDims::test_broadcast_to_short_shape_numpy19
870+
tests/third_party/cupy/manipulation_tests/test_dims.py::TestDims::test_expand_dims0
871+
tests/third_party/cupy/manipulation_tests/test_dims.py::TestDims::test_expand_dims1
872+
tests/third_party/cupy/manipulation_tests/test_dims.py::TestDims::test_expand_dims2
873+
tests/third_party/cupy/manipulation_tests/test_dims.py::TestDims::test_expand_dims_negative1
874+
tests/third_party/cupy/manipulation_tests/test_dims.py::TestDims::test_expand_dims_negative2
875+
tests/third_party/cupy/manipulation_tests/test_dims.py::TestDims::test_expand_dims_out_of_range
876+
tests/third_party/cupy/manipulation_tests/test_dims.py::TestDims::test_expand_dims_repeated_axis
877+
tests/third_party/cupy/manipulation_tests/test_dims.py::TestDims::test_expand_dims_tuple_axis
878+
tests/third_party/cupy/manipulation_tests/test_dims.py::TestDims::test_external_squeeze
879+
tests/third_party/cupy/manipulation_tests/test_dims.py::TestDims::test_squeeze1
880+
tests/third_party/cupy/manipulation_tests/test_dims.py::TestDims::test_squeeze2
881+
tests/third_party/cupy/manipulation_tests/test_dims.py::TestDims::test_squeeze_failure
882+
tests/third_party/cupy/manipulation_tests/test_dims.py::TestDims::test_squeeze_int_axis1
883+
tests/third_party/cupy/manipulation_tests/test_dims.py::TestDims::test_squeeze_int_axis2
884+
tests/third_party/cupy/manipulation_tests/test_dims.py::TestDims::test_squeeze_int_axis_failure1
885+
tests/third_party/cupy/manipulation_tests/test_dims.py::TestDims::test_squeeze_int_axis_failure2
886+
tests/third_party/cupy/manipulation_tests/test_dims.py::TestDims::test_squeeze_scalar1
887+
tests/third_party/cupy/manipulation_tests/test_dims.py::TestDims::test_squeeze_scalar2
888+
tests/third_party/cupy/manipulation_tests/test_dims.py::TestDims::test_squeeze_scalar_failure1
889+
tests/third_party/cupy/manipulation_tests/test_dims.py::TestDims::test_squeeze_scalar_failure2
890+
tests/third_party/cupy/manipulation_tests/test_dims.py::TestDims::test_squeeze_scalar_failure3
891+
tests/third_party/cupy/manipulation_tests/test_dims.py::TestDims::test_squeeze_scalar_failure4
892+
tests/third_party/cupy/manipulation_tests/test_dims.py::TestDims::test_squeeze_tuple_axis1
893+
tests/third_party/cupy/manipulation_tests/test_dims.py::TestDims::test_squeeze_tuple_axis2
894+
tests/third_party/cupy/manipulation_tests/test_dims.py::TestDims::test_squeeze_tuple_axis3
895+
tests/third_party/cupy/manipulation_tests/test_dims.py::TestDims::test_squeeze_tuple_axis4
896+
tests/third_party/cupy/manipulation_tests/test_dims.py::TestDims::test_squeeze_tuple_axis_failure1
897+
tests/third_party/cupy/manipulation_tests/test_dims.py::TestDims::test_squeeze_tuple_axis_failure2
898+
tests/third_party/cupy/manipulation_tests/test_dims.py::TestDims::test_squeeze_tuple_axis_failure3
899+
tests/third_party/cupy/manipulation_tests/test_dims.py::TestBroadcast_param_0_{shapes=[(), ()]}::test_broadcast
900+
tests/third_party/cupy/manipulation_tests/test_dims.py::TestBroadcast_param_0_{shapes=[(), ()]}::test_broadcast_arrays
901+
tests/third_party/cupy/manipulation_tests/test_dims.py::TestBroadcast_param_1_{shapes=[(0,), (0,)]}::test_broadcast
902+
tests/third_party/cupy/manipulation_tests/test_dims.py::TestBroadcast_param_1_{shapes=[(0,), (0,)]}::test_broadcast_arrays
903+
tests/third_party/cupy/manipulation_tests/test_dims.py::TestBroadcast_param_2_{shapes=[(1,), (1,)]}::test_broadcast
904+
tests/third_party/cupy/manipulation_tests/test_dims.py::TestBroadcast_param_2_{shapes=[(1,), (1,)]}::test_broadcast_arrays
905+
tests/third_party/cupy/manipulation_tests/test_dims.py::TestBroadcast_param_3_{shapes=[(2,), (2,)]}::test_broadcast
906+
tests/third_party/cupy/manipulation_tests/test_dims.py::TestBroadcast_param_3_{shapes=[(2,), (2,)]}::test_broadcast_arrays
907+
tests/third_party/cupy/manipulation_tests/test_dims.py::TestBroadcast_param_4_{shapes=[(0,), (1,)]}::test_broadcast
908+
tests/third_party/cupy/manipulation_tests/test_dims.py::TestBroadcast_param_4_{shapes=[(0,), (1,)]}::test_broadcast_arrays
909+
tests/third_party/cupy/manipulation_tests/test_dims.py::TestBroadcast_param_5_{shapes=[(2, 3), (1, 3)]}::test_broadcast
910+
tests/third_party/cupy/manipulation_tests/test_dims.py::TestBroadcast_param_5_{shapes=[(2, 3), (1, 3)]}::test_broadcast_arrays
911+
tests/third_party/cupy/manipulation_tests/test_dims.py::TestBroadcast_param_6_{shapes=[(2, 1, 3, 4), (3, 1, 4)]}::test_broadcast
912+
tests/third_party/cupy/manipulation_tests/test_dims.py::TestBroadcast_param_6_{shapes=[(2, 1, 3, 4), (3, 1, 4)]}::test_broadcast_arrays
913+
tests/third_party/cupy/manipulation_tests/test_dims.py::TestBroadcast_param_7_{shapes=[(4, 3, 2, 3), (2, 3)]}::test_broadcast
914+
tests/third_party/cupy/manipulation_tests/test_dims.py::TestBroadcast_param_7_{shapes=[(4, 3, 2, 3), (2, 3)]}::test_broadcast_arrays
915+
tests/third_party/cupy/manipulation_tests/test_dims.py::TestBroadcast_param_8_{shapes=[(2, 0, 1, 1, 3), (2, 1, 0, 0, 3)]}::test_broadcast
916+
tests/third_party/cupy/manipulation_tests/test_dims.py::TestBroadcast_param_8_{shapes=[(2, 0, 1, 1, 3), (2, 1, 0, 0, 3)]}::test_broadcast_arrays
917+
tests/third_party/cupy/manipulation_tests/test_dims.py::TestBroadcast_param_9_{shapes=[(0, 1, 1, 3), (2, 1, 0, 0, 3)]}::test_broadcast
918+
tests/third_party/cupy/manipulation_tests/test_dims.py::TestBroadcast_param_9_{shapes=[(0, 1, 1, 3), (2, 1, 0, 0, 3)]}::test_broadcast_arrays
919+
tests/third_party/cupy/manipulation_tests/test_dims.py::TestBroadcast_param_10_{shapes=[(0, 1, 1, 0, 3), (5, 2, 0, 1, 0, 0, 3), (2, 1, 0, 0, 0, 3)]}::test_broadcast
920+
tests/third_party/cupy/manipulation_tests/test_dims.py::TestBroadcast_param_10_{shapes=[(0, 1, 1, 0, 3), (5, 2, 0, 1, 0, 0, 3), (2, 1, 0, 0, 0, 3)]}::test_broadcast_arrays
921+
tests/third_party/cupy/manipulation_tests/test_dims.py::TestInvalidBroadcast_param_0_{shapes=[(3,), (2,)]}::test_invalid_broadcast
922+
tests/third_party/cupy/manipulation_tests/test_dims.py::TestInvalidBroadcast_param_0_{shapes=[(3,), (2,)]}::test_invalid_broadcast_arrays
923+
tests/third_party/cupy/manipulation_tests/test_dims.py::TestInvalidBroadcast_param_1_{shapes=[(3, 2), (2, 3)]}::test_invalid_broadcast
924+
tests/third_party/cupy/manipulation_tests/test_dims.py::TestInvalidBroadcast_param_1_{shapes=[(3, 2), (2, 3)]}::test_invalid_broadcast_arrays
925+
tests/third_party/cupy/manipulation_tests/test_dims.py::TestInvalidBroadcast_param_2_{shapes=[(3, 2), (3, 4)]}::test_invalid_broadcast
926+
tests/third_party/cupy/manipulation_tests/test_dims.py::TestInvalidBroadcast_param_2_{shapes=[(3, 2), (3, 4)]}::test_invalid_broadcast_arrays
927+
tests/third_party/cupy/manipulation_tests/test_dims.py::TestInvalidBroadcast_param_3_{shapes=[(0,), (2,)]}::test_invalid_broadcast
928+
tests/third_party/cupy/manipulation_tests/test_dims.py::TestInvalidBroadcast_param_3_{shapes=[(0,), (2,)]}::test_invalid_broadcast_arrays
864929
tests/third_party/cupy/random_tests/test_distributions.py::TestDistributionsBeta_param_0_{a_shape=(), b_shape=(), dtype=float64, shape=(4, 3, 2)}::test_beta
865930
tests/third_party/cupy/random_tests/test_distributions.py::TestDistributionsBeta_param_10_{a_shape=(), b_shape=(3, 2), dtype=float16, shape=(4, 3, 2)}::test_beta
866931
tests/third_party/cupy/random_tests/test_distributions.py::TestDistributionsBeta_param_11_{a_shape=(), b_shape=(3, 2), dtype=float16, shape=(3, 2)}::test_beta

0 commit comments

Comments
 (0)