ndtools package#

class All(initlist=None)[source]#

Bases: UserList[Any], Combinable, Equatable

Implement logical conjunction between equatables.

It should contain equatables like All([eqatable_0, equatable_1, ...]). Then the equality operation on the target array will perform like (array == equatable_0) & array == equatable_1) & ....

Examples

import numpy as np
from ndtools import Combinable, Equatable

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

    def __ne__(self, array):
        return ~(self == array)

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

    def __ne__(self, array):
        return ~(self == array)

Even() & Odd()  # -> All([Even(), Odd()])
np.arange(3) == Even() & Odd()  # -> array([False, False, False])
class Any(initlist=None)[source]#

Bases: UserList[Any], Combinable, Equatable

Implement logical disjunction between equatables.

It should contain equatables like Any([eqatable_0, equatable_1, ...]). Then the equality operation on the target array will perform like (array == equatable_0) | array == equatable_1) & ....

Examples

import numpy as np
from ndtools import Combinable, Equatable

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

    def __ne__(self, array):
        return ~(self == array)

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

    def __ne__(self, array):
        return ~(self == array)

Even() | Odd()  # -> Any([Even(), Odd()])
np.arange(3) == Even() | Odd()  # -> array([True, True, True])
class Combinable[source]#

Bases: object

Implement logical operations between objects.

Classes that inherit from this mix-in class can perform logical operations between the class instance and other object. Then instance & object will return All([instance, other]) and instance | object will return Any[instance, other]), where All and Any are the implementation of logical conjunction and logical disjunction, respectively. In general, Combinable should be used with the Equatable abstract base class to implement combinable equatables.

Examples

import numpy as np
from ndtools import Combinable, Equatable

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

    def __ne__(self, array):
        return ~(self == array)

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

    def __ne__(self, array):
        return ~(self == array)

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])
class Equatable[source]#

Bases: ABC

Implement equality operations for multidimensional arrays.

Classes that inherit from this abstract base class and implement both __eq__ and __ne__ special methods can perform their own equality operations on multidimensional arrays. These special methods should be implemented for the target array like def __eq__(self, array). Then the class instance and the array can perform instance == array and array == instance.

Raises:

TypeError – Raised if both __eq__ and __ne__ are not defined.

Examples

import numpy as np
from ndtools import Equatable

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

    def __ne__(self, array):
        return ~(self == array)

Even() == np.arange(3)  # -> array([True, False, True])
np.arange(3) == Even()  # -> array([True, False, True])

Even() != np.arange(3)  # -> array([False, True, False])
np.arange(3) != Even()  # -> array([False, True, False])
class Orderable[source]#

Bases: ABC

Implement ordering operations for multidimensional arrays.

Classes that inherit from this abstract base class and implement all of __eq__, __ge__, __gt__, __le__, __lt__, and __ne__ special methods can perform their own ordering operations on multidimensional arrays. These special methods should be implemented for the target array like def __ge__(self, array). Then the class instance and the array can perform instance >= array and array <= instance.

Raises:

TypeError – Raised if all of __eq__, __ge__, __gt__, __le__, __lt__, and __ne__ are not defined.

Examples

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

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

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

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

    def __gt__(self, array):
        return array < self.lower

    def __le__(self, array):
        return ~(self > array)

    def __lt__(self, array):
        return ~(self >= array)

    def __ne__(self, array):
        return ~(self == array)

Range(1, 2) == np.arange(3)  # -> array([False, True, False])
np.arange(3) == Range(1, 2)  # -> array([False, True, False])

Range(1, 2) >= np.arange(3)  # -> array([True, True, False])
np.arange(3) <= Range(1, 2)  # -> array([True, True, False])
class TotalEquality[source]#

Bases: Equatable

Implement missing equality operations for multidimensional arrays.

Raises:

ValueError – Raised if none of the equality operators (==, !=) is defined.

Examples

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

Even() != np.arange(3)  # -> array([False, True, False])
np.arange(3) != Even()  # -> array([False, True, False])
class TotalOrdering[source]#

Bases: Orderable

Implement missing ordering operations for multidimensional arrays.

Raises:

ValueError – Raise if none of the ordering operator (>=, >, <=, <) is defined.

Examples

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

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

Submodules#