Skip to content

Commit

Permalink
cunumeric.ndim (nv-legate#495)
Browse files Browse the repository at this point in the history
* Add cunumeric.ndim

* Add Avail. info to ndim, add to docs and API comparison table

* Add some missing function references to docs

* Test cases passing Python values to cunumeric.ndim

Co-authored-by: Manolis Papadakis <manopapad@gmail.com>
  • Loading branch information
2 people authored and sbak5 committed Aug 17, 2022
1 parent 635d81c commit 72be3c2
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 4 deletions.
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 @@ -972,6 +972,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)


@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))

0 comments on commit 72be3c2

Please sign in to comment.