Collection of tools to extend multidimensional array operations
Project description
ndtools
Collection of tools to extend multidimensional array operations
Installation
pip install ndtools
Usage
Comparison
ndtools provides total_equality and total_ordering class decorators that fill in missing multidimensional equality and ordering methods, respectively.
total_equality will fill in missing __ne__ from user-defined __eq__ or missing __eq__ from user-defined __ne__.
The following example implements an object that checks whether each array element is even or not:
import numpy as np
from ndtools import total_equality
@total_equality
class Even:
def __eq__(self, array):
return ~((array % 2).astype(bool))
even = Even()
even == np.arange(3) # -> array([True, False, True])
even != np.arange(3) # -> array([False, True, False])
It also supports a more intuitive notation with the array written on the left-hand side and the object on the right-hand side:
np.arange(3) == even # -> array([True, False, True])
np.arange(3) != even # -> array([False, True, False])
total_ordering will fill in missing ordering operators (__ge__, __gt__, __le__, __lt__).
As with functools.total_ordering, at least one of them, and __eq__ or __ne__ must be user-defined.
The following example implements an interval object that defines equivalence with a certain range:
import numpy as np
from dataclasses import dataclass
from ndtools import total_ordering
@dataclass
@total_ordering
class Interval:
lower: float
upper: float
def __eq__(self, array):
return (array >= self.lower) & (array < self.upper)
def __ge__(self, array):
return array < self.upper
Interval(1, 2) == np.arange(3) # -> array([False, True, False])
Interval(1, 2) < np.arange(3) # -> array([False, False, True])
Interval(1, 2) > np.arange(3) # -> array([True, False, False])
It also supports a more intuitive notation with the array written on the left-hand side and the object on the right-hand side:
np.arange(3) == Interval(1, 2) # -> array([False, True, False])
np.arange(3) < Interval(1, 2) # -> array([True, False, False])
np.arange(3) > Interval(1, 2) # -> array([False, False, True])
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file ndtools-0.1.1.tar.gz.
File metadata
- Download URL: ndtools-0.1.1.tar.gz
- Upload date:
- Size: 51.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: uv/0.6.14
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
3185ca85a50c4a69337ed31899467f40963dbdf0830d421969eb6714eca68850
|
|
| MD5 |
16638570d24d3f4935ecf0a62737398c
|
|
| BLAKE2b-256 |
afc68d7c73c79d1e990494bdece387809512092b6156e645d0ec216aa682e51b
|
File details
Details for the file ndtools-0.1.1-py3-none-any.whl.
File metadata
- Download URL: ndtools-0.1.1-py3-none-any.whl
- Upload date:
- Size: 5.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: uv/0.6.14
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
b753d23baa7f69121b91d60924c166012c2ff4d45f9b850119d99a854a7e6f44
|
|
| MD5 |
82961848b50411dbd84471968c0b456a
|
|
| BLAKE2b-256 |
9b1657ecc6e2afe96ea004d7310b37418fb940f66dcf4e2fb32899b82125d310
|