Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Better integration with numpy #753

Open
sunqm opened this issue Feb 14, 2024 · 5 comments
Open

Better integration with numpy #753

sunqm opened this issue Feb 14, 2024 · 5 comments
Labels
enhancement new feature requests (or implementation)

Comments

@sunqm
Copy link
Contributor

sunqm commented Feb 14, 2024

The functionalities of mpmath matrix class has overlap with the numpy ndarray class. But matrix class and numpy array class are not compatible. Currently, linalg functions such as qr, lu, norm can only work with mpmath matrix. It seems not difficult to change the code base to make these functions support numpy arrays.

It would be convenient if mpmath matrix and the relevant functions are compatible with numpy arrays. This allows users to use numpy array to manage mpmath.mpf objects, as numpy array has so many functionalities to manipulate array and matrix objects that mpmath matrix does not have.

Will the integration with numpy be considered in the future?

@skirpichev skirpichev added the enhancement new feature requests (or implementation) label Feb 15, 2024
@skirpichev
Copy link
Collaborator

see also #217, #618

@sunqm
Copy link
Contributor Author

sunqm commented Feb 15, 2024

Technically, it's not that difficult to make mpmath aligned better with numpy, IMO. The question is the goal and the perspective of the package: whether numpy compatibility will be considered in the future releases. If so, I'm happy to make contributions. At beginning, it is not necessary to integrate every feature of numpy. Upgrading the existing functionalities does not look like too much work. It just needs to make the matrix class a subclass of numpy array, and refactor the relevant linalg and calculas functions following the numpy array APIs.

@oscarbenjamin
Copy link

mpmath does not depend on numpy so it is not possible to subclass numpy's ndarray.

Adding an array API compatible module as an interface within mpmath could be reasonable though.

@sunqm
Copy link
Contributor Author

sunqm commented Feb 16, 2024

It may not be necessary to sublcass numpy array or install numpy. At some point, this NEP https://numpy.org/neps/nep-0041-improved-dtype-support.html#fixed-high-precision-math will be available in numpy. Then the numpy array can provide the functionalities of mp.matrix .

In mpmath, functions in the linalg module just need to follow the numpy API conventions. This will allow mpmath linalg functions supporting numpy arrays automatically. For example, one of the simplest change would be to replace the rows and cols in mp.matrix with an attribute shape. functions like eig, schur will automatically support numpy arrays.

@skirpichev
Copy link
Collaborator

In mpmath, functions in the linalg module just need to follow the numpy API conventions.

That does make sense. Just keep in mind, that linalg functions and the matrix class are part of the mpmath's public API. We should support old attributes for some time, add deprecation warnings, etc.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement new feature requests (or implementation)
Projects
None yet
Development

No branches or pull requests

3 participants