Skip to content

add support for kwarg ndmin for dpnp.array #2135

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Oct 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 38 additions & 18 deletions dpnp/dpnp_iface_arraycreation.py
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,11 @@ def array(
order : {"C", "F", "A", "K"}, optional
Memory layout of the newly output array.
Default: ``"K"``.
ndmin : int, optional
Specifies the minimum number of dimensions that the resulting array
should have. Ones will be prepended to the shape as needed to meet
this requirement.
Default: ``0``.
device : {None, string, SyclDevice, SyclQueue}, optional
An array API concept of device where the output array is created.
The `device` can be ``None`` (the default), an OneAPI filter selector
Expand All @@ -345,7 +350,6 @@ def array(
Limitations
-----------
Parameter `subok` is supported only with default value ``False``.
Parameter `ndmin` is supported only with default value ``0``.
Parameter `like` is supported only with default value ``None``.
Otherwise, the function raises ``NotImplementedError`` exception.

Expand Down Expand Up @@ -373,6 +377,11 @@ def array(
>>> x
array([1, 2, 3])

Upcasting:

>>> np.array([1, 2, 3.0])
array([ 1., 2., 3.])

More than one dimension:

>>> x2 = np.array([[1, 2], [3, 4]])
Expand All @@ -382,6 +391,16 @@ def array(
array([[1, 2],
[3, 4]])

Minimum dimensions 2:

>>> np.array([1, 2, 3], ndmin=2)
array([[1, 2, 3]])

Type provided:

>>> np.array([1, 2, 3], dtype=complex)
array([ 1.+0.j, 2.+0.j, 3.+0.j])

Creating an array on a different device or with a specified usm_type

>>> x = np.array([1, 2, 3]) # default case
Expand All @@ -399,13 +418,10 @@ def array(
"""

dpnp.check_limitations(subok=subok, like=like)
if ndmin != 0:
raise NotImplementedError(
"Keyword argument `ndmin` is supported only with "
f"default value ``0``, but got {ndmin}"
)
if not isinstance(ndmin, (int, dpnp.integer)):
raise TypeError(f"`ndmin` should be an integer, got {type(ndmin)}")

return dpnp_container.asarray(
result = dpnp_container.asarray(
a,
dtype=dtype,
copy=copy,
Expand All @@ -415,6 +431,14 @@ def array(
sycl_queue=sycl_queue,
)

res_ndim = result.ndim
if res_ndim >= ndmin:
return result

num_axes = ndmin - res_ndim
new_shape = (1,) * num_axes + result.shape
return result.reshape(new_shape)


def asanyarray(
a,
Expand Down Expand Up @@ -635,7 +659,7 @@ def ascontiguousarray(
a, dtype=None, *, like=None, device=None, usm_type=None, sycl_queue=None
):
"""
Return a contiguous array in memory (C order).
Return a contiguous array ``(ndim >= 1)`` in memory (C order).

For full documentation refer to :obj:`numpy.ascontiguousarray`.

Expand Down Expand Up @@ -731,14 +755,12 @@ def ascontiguousarray(

dpnp.check_limitations(like=like)

# at least 1-d array has to be returned
if dpnp.isscalar(a) or hasattr(a, "ndim") and a.ndim == 0:
a = [a]

return asarray(
return dpnp.array(
a,
dtype=dtype,
copy=None,
order="C",
ndmin=1,
device=device,
usm_type=usm_type,
sycl_queue=sycl_queue,
Expand Down Expand Up @@ -849,14 +871,12 @@ def asfortranarray(

dpnp.check_limitations(like=like)

# at least 1-d array has to be returned
if dpnp.isscalar(a) or hasattr(a, "ndim") and a.ndim == 0:
a = [a]

return asarray(
return dpnp.array(
a,
dtype=dtype,
copy=None,
order="F",
ndmin=1,
device=device,
usm_type=usm_type,
sycl_queue=sycl_queue,
Expand Down
Loading
Loading