SymPy numbers and NumPy interoperability

111 views
Skip to first unread message

Idan Pazi

unread,
Jul 26, 2023, 1:37:21 PM7/26/23
to sympy
Hello,
Is there a way I could configure NumPy or SymPy so that NumPy functions would accept SymPy numbers (e.g. sympy.Rational)?

For example:
>>> import numpy as np
>>> import sympy
>>> np.linspace(0, sympy.Rational(1,2), 2)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<__array_function__ internals>", line 180, in linspace
  File "C:\Users\idank\mambaforge\lib\site-packages\numpy\core\function_base.py", line 130, in linspace
    dt = result_type(start, stop, float(num))
  File "<__array_function__ internals>", line 180, in result_type
TypeError: Cannot interpret '0.500000000000000' as a data type

>>> np.isnan(sympy.Rational(1,2))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: ufunc 'isnan' not supported for the input types, and the inputs could not be safely coerced to any supported types according to the casting rule ''safe''


Background:
I'm using IPython+SymPy+NumPy as a calculator.
For convenience, I automatically convert all divisions and floats to sympy.Rational
(and display results both as fractions and as decimals)
After such conversions stuff like np.log(2.33) fails.
For more details, see - https://github.com/idanpa/calcpy

(SymPy version 1.12, NumPy version 1.25.1)

Thanks!
Idan 

Aaron Meurer

unread,
Jul 26, 2023, 1:50:45 PM7/26/23
to sy...@googlegroups.com
In general, using SymPy expressions with NumPy functions is not
supported, but I think it does make sense to make Number classes
(Integer, Float, Rational) work. Can you open an issue about this at
https://github.com/sympy/sympy/issues.

I don't know exactly what needs to be done to make this work.
Hopefully NumPy provides the proper hooks. Ideally, Rational and Float
would be converted to np.float64 and Integer would be converted to
np.int64 when used in functions like linspace. Although changing this
could potentially be an issue if people are using numpy arrays of
Rational currently (which I wouldn't necessarily recommend).

Aaron Meurer
> --
> You received this message because you are subscribed to the Google Groups "sympy" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to sympy+un...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/sympy/208b8d87-9e7b-4fe9-86fc-9575a0b57002n%40googlegroups.com.

Idan Pazi

unread,
Jul 28, 2023, 4:37:43 AM7/28/23
to sympy
Issue:
SymPy numbers and NumPy interoperability · Issue #25432 · sympy/sympy (github.com)
So it seems like there is no nice solution for this currently, and changes are needed in NumPy.

Aaron Meurer

unread,
Jul 28, 2023, 3:51:18 PM7/28/23
to sy...@googlegroups.com
I don't know why Oscar closed that issue. NumPy does have a lot of
methods for defining how custom objects interact with it. I did some
basic tests and it seems like adding __array__ to Number would do
exactly what we want here.

Aaron Meurer
> To view this discussion on the web visit https://groups.google.com/d/msgid/sympy/c5abf9ae-7c65-4935-9685-3791da6fa20an%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages