Metadata-Version: 2.1
Name: overload_numpy
Version: 0.0.1
Summary: Overload NumPy Functions
Author-email: Nathaniel Starkman <n.starkman@mail.utoronto.ca>
Maintainer-email: Nathaniel Starkman <n.starkman@mail.utoronto.ca>
License: BSD 3-Clause License
        
        Copyright (c) 2022, Nathaniel Starkman
        All rights reserved.
        
        Redistribution and use in source and binary forms, with or without
        modification, are permitted provided that the following conditions are met:
        
        1. Redistributions of source code must retain the above copyright notice, this
           list of conditions and the following disclaimer.
        
        2. Redistributions in binary form must reproduce the above copyright notice,
           this list of conditions and the following disclaimer in the documentation
           and/or other materials provided with the distribution.
        
        3. Neither the name of the copyright holder nor the names of its
           contributors may be used to endorse or promote products derived from
           this software without specific prior written permission.
        
        THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
        AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
        IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
        DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
        FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
        DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
        SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
        CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
        OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
        OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
        
Project-URL: homepage, https://overload_numpy.readthedocs.io
Project-URL: repository, https://github.com/nstarman/overload_numpy
Project-URL: documentation, https://overload_numpy.readthedocs.io
Keywords: numpy,interoperability
Classifier: Intended Audience :: Science/Research
Classifier: Intended Audience :: Developers
Classifier: License :: OSI Approved :: BSD License
Classifier: Operating System :: OS Independent
Classifier: Programming Language :: Python :: 3
Requires-Python: >=3.8
Description-Content-Type: text/x-rst
Provides-Extra: all
Provides-Extra: test
Provides-Extra: docs
License-File: LICENSE
License-File: AUTHORS.rst

Overload ``NumPy`` functions
############################

Tools for implementing ``__numpy_function__`` on custom functions.

Quick Worked Example
--------------------

This is a simple example.

First, some imports:

    >>> from dataclasses import dataclass
    >>> from typing import ClassVar
    >>> import numpy as np
    >>> from overload_numpy import NumPyOverloader, NDFunctionMixin

Now we can define a ``overload_numpy.NumPyOverloader`` instance:

    >>> VEC_FUNCS = NumPyOverloader()

The overloads apply to an array wrapping class. Let's define one:

    >>> @dataclass
    ... class Vector1D(NDFunctionMixin):
    ...     '''A simple array wrapper.'''
    ...     x: np.ndarray
    ...     NP_FUNC_OVERLOADS: ClassVar[NumPyOverloader] = VEC_FUNCS

Now ``numpy`` functions can be overloaded and registered for ``Vector1D``.

    >>> @VEC_FUNCS.implements(np.concatenate, Vector1D)
    ... def concatenate(vec1ds: tuple[Vector1D, ...]) -> Vector1D:
    ...     return Vector1D(np.concatenate(tuple(v.x for v in vec1ds)))

Time to check this works:

    >>> vec1d = Vector1D(np.arange(3))
    >>> newvec = np.concatenate((vec1d, vec1d))
    >>> newvec
    Vector1D(x=array([0, 1, 2, 0, 1, 2]))

Great. Your turn!


Details
-------

See the Docs.
