Skip to content
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

cunumeric.ndim #495

Merged
merged 4 commits into from
Aug 2, 2022
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
3 changes: 1 addition & 2 deletions cunumeric/_sphinxext/_comparison_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@
"iterable",
"max",
"min",
"ndim",
"product",
"recfromcsv",
"recfromtxt",
Expand Down Expand Up @@ -335,7 +334,7 @@ class SectionConfig:
"var",
)

MISC = ("kron",)
MISC = ("kron", "ndim")

PACK = ("packbits", "unpackbits")

Expand Down
30 changes: 30 additions & 0 deletions cunumeric/module.py
Original file line number Diff line number Diff line change
Expand Up @@ -966,6 +966,36 @@ def triu(m: ndarray, k: int = 0) -> ndarray:
# Basic operations


@add_boilerplate("a")
def ndim(a: ndarray) -> int:
"""

Return the number of dimensions of an array.

Parameters
----------
a : array_like
Input array. If it is not already an ndarray, a conversion is
attempted.

Returns
-------
number_of_dimensions : int
The number of dimensions in `a`. Scalars are zero-dimensional.

See Also
--------
ndarray.ndim : equivalent method
shape : dimensions of array
ndarray.shape : dimensions of array

Availability
--------
Multiple GPUs, Multiple CPUs
"""
return a.ndim


@add_boilerplate("a")
def shape(a: ndarray) -> NdShape:
"""
Expand Down
1 change: 1 addition & 0 deletions docs/cunumeric/source/api/manipulation.rst
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ Basic operations
.. autosummary::
:toctree: generated/

ndim
shape


Expand Down
4 changes: 4 additions & 0 deletions docs/cunumeric/source/api/math.rst
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@ Sums, products, differences

prod
sum
cumprod
cumsum
nancumprod
nancumsum


Exponents and logarithms
Expand Down
4 changes: 2 additions & 2 deletions docs/cunumeric/source/api/ndarray.rst
Original file line number Diff line number Diff line change
Expand Up @@ -156,12 +156,12 @@ Calculation
.. ndarray.round
.. ndarray.trace
ndarray.sum
.. ndarray.cumsum
ndarray.cumsum
ndarray.mean
.. ndarray.var
.. ndarray.std
ndarray.prod
.. ndarray.cumprod
ndarray.cumprod
ndarray.all
ndarray.any
ndarray.unique
Expand Down
40 changes: 40 additions & 0 deletions tests/integration/test_ndim.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# Copyright 2022 NVIDIA Corporation
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

import numpy as np
import pytest

import cunumeric as num
from legate.core import LEGATE_MAX_DIM


@pytest.mark.parametrize("ndim", range(LEGATE_MAX_DIM + 1))
def test_ndarray(ndim):
shape = (4,) * ndim
a = num.ones(shape)
a_np = np.array(a)

assert np.ndim(a_np) == num.ndim(a)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you also check a few non-ndarray values? e.g.

42
[0,1,2]
[[0,1,2],[3,4,5]]


@pytest.mark.parametrize("input", (42, [0, 1, 2], [[0, 1, 2], [3, 4, 5]]))
def test_python_values(input):
assert np.ndim(input) == num.ndim(input)


if __name__ == "__main__":
import sys

sys.exit(pytest.main(sys.argv))