Skip to main content

Collection of tools to extend multidimensional array operations

Project description

ndtools

Release Python Downloads Tests

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

ndtools-0.1.0.tar.gz (51.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

ndtools-0.1.0-py3-none-any.whl (5.5 kB view details)

Uploaded Python 3

File details

Details for the file ndtools-0.1.0.tar.gz.

File metadata

  • Download URL: ndtools-0.1.0.tar.gz
  • Upload date:
  • Size: 51.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.6.14

File hashes

Hashes for ndtools-0.1.0.tar.gz
Algorithm Hash digest
SHA256 dc9d838da3637ef39bddca1c140b20e4fad812925b942d6c48b90e18eb1dcf1d
MD5 7c54b5d0f29507bba058c4d9e51bb73a
BLAKE2b-256 30ae09470b86f499bd5c4daad8dfb022aba21db57f3113b6cef8248a90260f70

See more details on using hashes here.

File details

Details for the file ndtools-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: ndtools-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 5.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.6.14

File hashes

Hashes for ndtools-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 df9c84eae43848878c9ca663a9780e91a2080b3009cb48f1ec6cc08a530156df
MD5 19c402580ea3ca110e4bc1a557002651
BLAKE2b-256 fab7d485bad90958bc559c4861c22ce7649962e3ccf4cb24bee76022fdac72f9

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page