Documentation: https://array-api-negative-index.readthedocs.io
Source Code: https://github.com/34j/array-api-negative-index
Utils for indexing arrays with {-n, -(n-1), ..., -1, 0, 1, ..., n-1, n}
using array API.
Install this via pip (or your favourite package manager):
pip install array-api-negative-index
- This package provides a utility for packing array elements in the order
[0, 1, 2, ..., n, -n, ..., -2, -1]
. - This allows one to access the
i
-th element byarray[i]
, regardless of whetheri
is positive, 0 or negative, thanks to "Negative Indexing" in array API. - This is useful for representing the order
m
in spherical harmonics for example.
from array_api_negative_index import to_symmetric, flip_symmetric, arange_asymmetric
import numpy as np
a = arange_asymmetric(3, xp=np)
print(a)
[ 0 1 2 -2 -1]
Not to confuse with np.arange(-stop + 1, stop)
!
This caused me to create bugs many times.
a = np.arange(-2, 3)
print(a)
[-2, -1, 0, 1, 2]
b = flip_symmetric(a)
print(f"{a} -> (flip) -> {b}")
[ 0 1 2 -2 -1] -> (flip) -> [ 0 -1 -2 2 1]
c = np.asarray([0, 3, 5])
d = to_symmetric(c, asymmetric=True, conjugate=False)
print(f"{c} -> (to_symmetric) -> {d}")
[0 3 5] -> (to_symmetric) -> [ 0 3 5 -5 -3]
Thanks goes to these wonderful people (emoji key):
This project follows the all-contributors specification. Contributions of any kind welcome!
This package was created with Copier and the browniebroke/pypackage-template project template.