Skip to content

Commit 290ebb5

Browse files
ENH: pad: add delegation (#72)
Co-authored-by: Guido Imperiale <[email protected]>
1 parent ec890f1 commit 290ebb5

21 files changed

+797
-621
lines changed

docs/api-reference.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
expand_dims
1414
kron
1515
nunique
16+
pad
1617
setdiff1d
1718
sinc
1819
```

docs/index.md

Lines changed: 36 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ contributors.md
1010
```
1111

1212
This is a library housing "array-agnostic" implementations of functions built on
13-
top of [the Python array API standard](https://data-apis.org/array-api/).
13+
top of [the Python array API standard](https://data-apis.org/array-api/), as
14+
well as delegation to existing implementations for known array library backends.
1415

1516
The intended users of this library are "array-consuming" libraries which are
1617
using [array-api-compat](https://data-apis.org/array-api-compat/) to make their
@@ -23,7 +24,7 @@ It is currently used by:
2324

2425
- [SciPy](https://github.com/scipy/scipy) - Fundamental algorithms for
2526
scientific computing.
26-
- ...
27+
- _your library? Let us know!_
2728

2829
(installation)=
2930

@@ -33,16 +34,18 @@ It is currently used by:
3334
[on PyPI](https://pypi.org/project/array-api-extra/):
3435

3536
```shell
37+
uv add array-api-extra
38+
# or
3639
python -m pip install array-api-extra
3740
```
3841

3942
And
4043
[on conda-forge](https://prefix.dev/channels/conda-forge/packages/array-api-extra):
4144

4245
```shell
43-
mamba install array-api-extra
44-
# or
4546
pixi add array-api-extra
47+
# or
48+
mamba install array-api-extra
4649
```
4750

4851
```{warning}
@@ -52,7 +55,7 @@ a specific version, or vendor the library inside your own.
5255
```
5356

5457
```{note}
55-
This library depends on array-api-compat. We aim for compatibility with
58+
This library depends on `array-api-compat`. We aim for compatibility with
5659
the latest released version of array-api-compat, and your mileage may vary
5760
with older or dev versions.
5861
```
@@ -69,8 +72,8 @@ and copy it into the appropriate place in your library, like:
6972
cp -a array-api-extra/src/array_api_extra mylib/vendored/
7073
```
7174

72-
`array-api-extra` depends on `array-api-compat`. You may either add a dependency
73-
in your own project to `array-api-compat` or vendor it too:
75+
You may either add a dependency to array-api-compat in your own project, or
76+
vendor it too:
7477

7578
1. Clone
7679
[the array-api-compat repository](https://github.com/data-apis/array-api-compat)
@@ -81,14 +84,14 @@ in your own project to `array-api-compat` or vendor it too:
8184
```
8285

8386
2. Create a new hook file which array-api-extra will use instead of the
84-
top-level `array-api-compat` if present:
87+
top-level array-api-compat if present:
8588

8689
```
8790
echo 'from mylib.vendored.array_api_compat import *' > mylib/vendored/_array_api_compat_vendor.py
8891
```
8992

90-
This also allows overriding `array-api-compat` functions if you so wish. E.g.
91-
your `mylib/vendored/_array_api_compat_vendor.py` could look like this:
93+
This also allows overriding array-api-compat functions if you so wish. E.g. your
94+
`mylib/vendored/_array_api_compat_vendor.py` could look like this:
9295

9396
```python
9497
from mylib.vendored.array_api_compat import *
@@ -104,6 +107,13 @@ def array_namespace(*xs, **kwargs):
104107
return _array_namespace_orig(*xs, **kwargs)
105108
```
106109

110+
```{tip}
111+
See [an example of this in SciPy][scipy-vendor-example].
112+
```
113+
114+
[scipy-vendor-example]:
115+
https://github.com/scipy/scipy/blob/main/scipy/_lib/_array_api_compat_vendor.py
116+
107117
(usage)=
108118

109119
## Usage
@@ -115,9 +125,9 @@ import array_api_extra as xpx
115125

116126
...
117127
xp = array_namespace(x)
118-
y = xp.sum(x)
128+
y = xp.sum(x) # use functions from `xp` as normal
119129
...
120-
return xpx.atleast_nd(y, ndim=2, xp=xp)
130+
return xpx.atleast_nd(y, ndim=2, xp=xp) # use functions from `xpx`, passing `xp=xp`
121131
```
122132

123133
```{note}
@@ -131,13 +141,13 @@ is called internally to determine the namespace.
131141
```
132142

133143
In the examples shown in the docstrings of functions from this library,
134-
[`array-api-strict`](https://data-apis.org/array-api-strict/) is used as the
135-
array namespace `xp`. In reality, code using this library will be written to
136-
work with any compatible array namespace as `xp`, not any particular
137-
implementation.
144+
[array-api-strict](https://data-apis.org/array-api-strict/) is used as the array
145+
namespace `xp`. In reality, code using this library will be written to work with
146+
any compatible array namespace as `xp`, not any particular implementation.
138147

139-
Some functions may only work with array libraries supported by array-api-compat.
140-
This will be clearly indicated in the docs.
148+
Some functions may only work with specific array libraries supported by
149+
array-api-compat. This should be clearly indicated in the docs - please open an
150+
issue if this is not the case!
141151

142152
(scope)=
143153

@@ -151,13 +161,17 @@ Functions that are in-scope for this library will:
151161
libraries.
152162
- Be implemented with type annotations and
153163
[numpydoc-style docstrings](https://numpydoc.readthedocs.io/en/latest/format.html).
154-
- Be tested against `array-api-strict`.
164+
- Be tested against array-api-strict and various existing backends.
155165

156166
Functions are implemented purely in terms of the array API standard where
157167
possible. Where functions must use library-specific helpers for libraries
158168
supported by array-api-compat, this will be clearly marked in their API
159169
reference page.
160170

171+
Delegation is added for some functions to use native implementations for the
172+
given array type, instead of the array-agnostic implementations, as this may
173+
increase performance.
174+
161175
In particular, the following kinds of function are also in-scope:
162176

163177
- Functions which implement
@@ -168,15 +182,12 @@ In particular, the following kinds of function are also in-scope:
168182

169183
The following features are currently out-of-scope for this library:
170184

171-
- Delegation to known, existing array libraries (unless necessary).
172-
- It is quite simple to wrap functions in this library to also use existing
173-
implementations. Such delegation will not live in this library for now, but
174-
the array-agnostic functions in this library could form an array-agnostic
175-
backend for such delegating functions in the future, here or elsewhere.
176185
- Functions which accept "array-like" input, or standard-incompatible
177186
namespaces.
178187
- It is possible to prepare input arrays and a standard-compatible namespace
179-
via `array-api-compat` downstream in consumer libraries.
188+
via array-api-compat downstream in consumer libraries. The `xp` argument can
189+
also be omitted to infer the standard-compatible namespace using
190+
`array_namespace` internally.
180191
- Functions which are specific to a particular domain.
181192
- These functions may belong better in an array-consuming library which is
182193
specific to that domain.

0 commit comments

Comments
 (0)