-
Notifications
You must be signed in to change notification settings - Fork 89
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
Co-authored-by: Amanda Potts <[email protected]>
- Loading branch information
Showing
4 changed files
with
80 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
from typing import Iterable, Tuple, Union | ||
|
||
from numpy import ndarray | ||
|
||
from arkouda.numpy.dtypes import all_scalars, isSupportedDType | ||
from arkouda.pdarrayclass import pdarray | ||
from arkouda.strings import Strings | ||
|
||
__all__ = ["shape"] | ||
|
||
|
||
def shape(a: Union[pdarray, Strings, all_scalars]) -> Tuple: | ||
""" | ||
Return the shape of an array. | ||
Parameters | ||
---------- | ||
a : pdarray | ||
Input array. | ||
Returns | ||
------- | ||
shape : tuple of ints | ||
The elements of the shape tuple give the lengths of the | ||
corresponding array dimensions. | ||
Examples | ||
-------- | ||
>>> import arkouda as ak | ||
>>> ak.shape(ak.eye(3,2)) | ||
(3, 2) | ||
>>> ak.shape([[1, 3]]) | ||
(1, 2) | ||
>>> ak.shape([0]) | ||
(1,) | ||
>>> ak.shape(0) | ||
() | ||
""" | ||
if isinstance(a, (pdarray, Strings, ndarray, Iterable)) and not isinstance(a, str): | ||
try: | ||
result = a.shape | ||
except AttributeError: | ||
from arkouda import array | ||
|
||
result = array(a).shape | ||
return result | ||
elif isSupportedDType(a): | ||
return () | ||
else: | ||
raise TypeError("shape requires type pdarray, ndarray, Iterable, or numeric scalar.") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import pytest | ||
|
||
import arkouda as ak | ||
import numpy as np | ||
|
||
|
||
class TestFromNumericFunctions: | ||
|
||
@pytest.mark.parametrize("x", [0, [0], [1, 2, 3], np.ndarray([0, 1, 2]), [[1, 3]], np.eye(3, 2)]) | ||
def test_shape(self, x): | ||
assert ak.shape(x) == np.shape(x) | ||
|
||
def test_shape_pdarray(self): | ||
a = ak.arange(5) | ||
assert ak.shape(a) == np.shape(a.to_ndarray()) | ||
|
||
def test_shape_strings(self): | ||
a = ak.array(["a", "b", "c"]) | ||
assert ak.shape(a) == np.shape(a.to_ndarray()) | ||
|
||
@pytest.mark.skip_if_max_rank_less_than(2) | ||
def test_shape_multidim_pdarray(self): | ||
assert ak.shape(ak.eye(3, 2)) == (3, 2) |