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

Array comparison

ndtools provides TotalEquality and TotalOrdering that implement missing equality and ordering operations for multidimensional arrays, respectively. TotalEquality will implement missing __ne__ from user-defined __eq__ or missing __eq__ from user-defined __ne__. The following example implements an equatable object that checks whether each array element is even or not:

import numpy as np
from ndtools import TotalEquality

class Even(TotalEquality):
    def __eq__(self, array):
        return array % 2 == 0

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])

TotalOrdering will implement missing ordering operators (__ge__, __gt__, __le__, __lt__). Similar to functools.total_ordering, at least one of them, and __eq__ or __ne__ must be user-defined. The following example implements an equatable object that defines equivalence with a certain range:

import numpy as np
from dataclasses import dataclass
from ndtools import TotalOrdering

@dataclass
class Range(TotalOrdering)
    lower: float
    upper: float

    def __eq__(self, array):
        return (array >= self.lower) & (array < self.upper)

    def __ge__(self, array):
        return array < self.upper

Range(1, 2) == np.arange(3)  # -> array([False, True, False])
Range(1, 2) < np.arange(3)   # -> array([False, False, True])
Range(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) == Range(1, 2) # -> array([False, True, False])
np.arange(3) < Range(1, 2)  # -> array([True, False, False])
np.arange(3) > Range(1, 2)  # -> array([False, False, True])

Equatable combination

ndtools provides All, Any, and Combinable that implement logical operations between equatable objects. Equatable classes that inherit from Combinable can perform logical operations between the class instance and other equatable object. Then instance & object will return All([instance, other]) and instance | object will return Any[instance, other]).

import numpy as np
from ndtools import Combinable, TotalEquality

class Even(Combinable, TotalEquality):
    def __eq__(self, array):
        return array % 2 == 0

class Odd(Combinable, TotalEquality):
    def __eq__(self, array):
        return array % 2 == 1

Even() & Odd()  # -> All([Even(), Odd()])
Even() | Odd()  # -> Any([Even(), Odd()])

np.arange(3) == Even() & Odd()  # -> array([False, False, False])
np.arange(3) == Even() | Odd()  # -> array([True, True, 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.3.0.tar.gz (53.6 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.3.0-py3-none-any.whl (8.2 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for ndtools-0.3.0.tar.gz
Algorithm Hash digest
SHA256 bfd90354aa09239c4473efe96600cd73b7b7a3a24f20fae68bf416017febcd27
MD5 f291080a465734dd8489cab4fc436f83
BLAKE2b-256 0de079719611a7f11dfff7426e546ed6e431ceda328a30050936bc0a1fa0d68f

See more details on using hashes here.

File details

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

File metadata

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

File hashes

Hashes for ndtools-0.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 c9939b4af790dbd4efed8e2003c5dd6f49c25a5488b5030ea60b7fab5363852e
MD5 de318a7755b9396947db945b4007c853
BLAKE2b-256 d9cc5f33324a77a417165d7aa2111cbae5a71c6dba7ae97a99f19dddaa190605

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