Metadata-Version: 2.4
Name: ndtools
Version: 0.2.0
Summary: Collection of tools to extend multidimensional array operations
Project-URL: homepage, https://astropenguin.github.io/ndtools/v0.2.0
Project-URL: repository, https://github.com/astropenguin/ndtools
Author-email: Akio Taniguchi <taniguchi.akio@gmail.com>
License: MIT License
        
        Copyright (c) 2025 Akio Taniguchi
        
        Permission is hereby granted, free of charge, to any person obtaining a copy
        of this software and associated documentation files (the "Software"), to deal
        in the Software without restriction, including without limitation the rights
        to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
        copies of the Software, and to permit persons to whom the Software is
        furnished to do so, subject to the following conditions:
        
        The above copyright notice and this permission notice shall be included in all
        copies or substantial portions of the Software.
        
        THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
        IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
        FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
        AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
        LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
        OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
        SOFTWARE.
License-File: LICENSE
Keywords: comparison,duck-array,ndarray
Classifier: License :: OSI Approved :: MIT License
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.9
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Requires-Python: <3.14,>=3.9
Requires-Dist: numpy<3.0,>=2.0
Requires-Dist: typing-extensions<5.0,>=4.0
Description-Content-Type: text/markdown

# ndtools

[![Release](https://img.shields.io/pypi/v/ndtools?label=Release&color=cornflowerblue&style=flat-square)](https://pypi.org/project/ndtools/)
[![Python](https://img.shields.io/pypi/pyversions/ndtools?label=Python&color=cornflowerblue&style=flat-square)](https://pypi.org/project/ndtools/)
[![Downloads](https://img.shields.io/pypi/dm/ndtools?label=Downloads&color=cornflowerblue&style=flat-square)](https://pepy.tech/project/ndtools)
[![Tests](https://img.shields.io/github/actions/workflow/status/astropenguin/ndtools/tests.yaml?label=Tests&style=flat-square)](https://github.com/astropenguin/ndtools/actions)

Collection of tools to extend multidimensional array operations

## Installation

```shell
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:
```python
import numpy as np
from ndtools import total_equality

@total_equality
class Even:
    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:
```python
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`](https://docs.python.org/3/library/functools.html#functools.total_ordering), at least one of them, and `__eq__` or `__ne__` must be user-defined.
The following example implements a range object that defines equivalence with a certain range:
```python
import numpy as np
from dataclasses import dataclass
from ndtools import total_ordering

@dataclass
@total_ordering
class Range:
    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:
```python
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])
```

> [!TIP]
> Mix-in versions of them, `TotalEquality` and `TotalOrdering`, are also available.
