-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from lucascolley/atleast_nd
- Loading branch information
Showing
12 changed files
with
2,045 additions
and
249 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 |
---|---|---|
|
@@ -70,6 +70,7 @@ instance/ | |
|
||
# Sphinx documentation | ||
docs/_build/ | ||
docs/generated/ | ||
|
||
# PyBuilder | ||
.pybuilder/ | ||
|
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,10 @@ | ||
# API Reference | ||
|
||
```{eval-rst} | ||
.. currentmodule:: array_api_extra | ||
.. autosummary:: | ||
:nosignatures: | ||
:toctree: generated | ||
atleast_nd | ||
``` |
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
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 |
---|---|---|
@@ -1,5 +1,7 @@ | ||
from __future__ import annotations | ||
|
||
from ._funcs import atleast_nd | ||
|
||
__version__ = "0.1.dev0" | ||
|
||
__all__ = ["__version__"] | ||
__all__ = ["__version__", "atleast_nd"] |
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,48 @@ | ||
from __future__ import annotations | ||
|
||
from typing import TYPE_CHECKING | ||
|
||
if TYPE_CHECKING: | ||
from ._typing import Array, ModuleType | ||
|
||
__all__ = ["atleast_nd"] | ||
|
||
|
||
def atleast_nd(x: Array, *, ndim: int, xp: ModuleType) -> Array: | ||
""" | ||
Recursively expand the dimension of an array to at least `ndim`. | ||
Parameters | ||
---------- | ||
x : array | ||
ndim : int | ||
The minimum number of dimensions for the result. | ||
xp : array_namespace | ||
The standard-compatible namespace for `x`. | ||
Returns | ||
------- | ||
res : array | ||
An array with ``res.ndim`` >= `ndim`. | ||
If ``x.ndim`` >= `ndim`, `x` is returned. | ||
If ``x.ndim`` < `ndim`, `x` is expanded by prepending new axes | ||
until ``res.ndim`` equals `ndim`. | ||
Examples | ||
-------- | ||
>>> import array_api_strict as xp | ||
>>> import array_api_extra as xpx | ||
>>> x = xp.asarray([1]) | ||
>>> xpx.atleast_nd(x, ndim=3, xp=xp) | ||
Array([[[1]]], dtype=array_api_strict.int64) | ||
>>> x = xp.asarray([[[1, 2], | ||
... [3, 4]]]) | ||
>>> xpx.atleast_nd(x, ndim=1, xp=xp) is x | ||
True | ||
""" | ||
if x.ndim < ndim: | ||
x = xp.expand_dims(x, axis=0) | ||
x = atleast_nd(x, ndim=ndim, xp=xp) | ||
return x |
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,9 @@ | ||
from __future__ import annotations | ||
|
||
from types import ModuleType | ||
from typing import TYPE_CHECKING, Any | ||
|
||
if TYPE_CHECKING: | ||
Array = Any # To be changed to a Protocol later (see array-api#589) | ||
|
||
__all__ = ["Array", "ModuleType"] |
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,69 @@ | ||
from __future__ import annotations | ||
|
||
# array-api-strict#6 | ||
import array_api_strict as xp # type: ignore[import-untyped] | ||
from numpy.testing import assert_array_equal | ||
|
||
from array_api_extra import atleast_nd | ||
|
||
|
||
class TestAtLeastND: | ||
def test_0D(self): | ||
x = xp.asarray(1) | ||
|
||
y = atleast_nd(x, ndim=0, xp=xp) | ||
assert_array_equal(y, x) | ||
|
||
y = atleast_nd(x, ndim=1, xp=xp) | ||
assert_array_equal(y, xp.ones((1,))) | ||
|
||
y = atleast_nd(x, ndim=5, xp=xp) | ||
assert_array_equal(y, xp.ones((1, 1, 1, 1, 1))) | ||
|
||
def test_1D(self): | ||
x = xp.asarray([0, 1]) | ||
|
||
y = atleast_nd(x, ndim=0, xp=xp) | ||
assert_array_equal(y, x) | ||
|
||
y = atleast_nd(x, ndim=1, xp=xp) | ||
assert_array_equal(y, x) | ||
|
||
y = atleast_nd(x, ndim=2, xp=xp) | ||
assert_array_equal(y, xp.asarray([[0, 1]])) | ||
|
||
y = atleast_nd(x, ndim=5, xp=xp) | ||
assert_array_equal(y, xp.reshape(xp.arange(2), (1, 1, 1, 1, 2))) | ||
|
||
def test_2D(self): | ||
x = xp.asarray([[3]]) | ||
|
||
y = atleast_nd(x, ndim=0, xp=xp) | ||
assert_array_equal(y, x) | ||
|
||
y = atleast_nd(x, ndim=2, xp=xp) | ||
assert_array_equal(y, x) | ||
|
||
y = atleast_nd(x, ndim=3, xp=xp) | ||
assert_array_equal(y, 3 * xp.ones((1, 1, 1))) | ||
|
||
y = atleast_nd(x, ndim=5, xp=xp) | ||
assert_array_equal(y, 3 * xp.ones((1, 1, 1, 1, 1))) | ||
|
||
def test_5D(self): | ||
x = xp.ones((1, 1, 1, 1, 1)) | ||
|
||
y = atleast_nd(x, ndim=0, xp=xp) | ||
assert_array_equal(y, x) | ||
|
||
y = atleast_nd(x, ndim=4, xp=xp) | ||
assert_array_equal(y, x) | ||
|
||
y = atleast_nd(x, ndim=5, xp=xp) | ||
assert_array_equal(y, x) | ||
|
||
y = atleast_nd(x, ndim=6, xp=xp) | ||
assert_array_equal(y, xp.ones((1, 1, 1, 1, 1, 1))) | ||
|
||
y = atleast_nd(x, ndim=9, xp=xp) | ||
assert_array_equal(y, xp.ones((1, 1, 1, 1, 1, 1, 1, 1, 1))) |