Skip to content

Utils for indexing arrays with {-n, -(n-1), ..., -1, 0, 1, ..., n-1, n} using array API.

License

Notifications You must be signed in to change notification settings

34j/array-api-negative-index

Array API Negative Index

CI Status Documentation Status Test coverage percentage

uv Ruff pre-commit

PyPI Version Supported Python versions License


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.

Installation

Install this via pip (or your favourite package manager):

pip install array-api-negative-index

Usage

  • 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 by array[i], regardless of whether i 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

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]

flip_symmetric()

b = flip_symmetric(a)
print(f"{a} -> (flip) -> {b}")
[ 0  1  2 -2 -1] -> (flip) -> [ 0 -1 -2  2  1]

to_symmetric()

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]

Contributors ✨

Thanks goes to these wonderful people (emoji key):

This project follows the all-contributors specification. Contributions of any kind welcome!

Credits

Copier

This package was created with Copier and the browniebroke/pypackage-template project template.

About

Utils for indexing arrays with {-n, -(n-1), ..., -1, 0, 1, ..., n-1, n} using array API.

Resources

License

Code of conduct

Contributing

Stars

Watchers

Forks

Sponsor this project

Packages

No packages published

Contributors 2

  •  
  •