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 On Fri, Jul 28, 2023 at 2:37 AM Idan Pazi <[email protected]> wrote: > > 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. > > On Wednesday, 26 July 2023 at 20:50:45 UTC+3 [email protected] wrote: >> >> 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 >> >> On Wed, Jul 26, 2023 at 11:37 AM Idan Pazi <[email protected]> wrote: >> > >> > 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 >> > >> > -- >> > 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 [email protected]. >> > To view this discussion on the web visit >> > https://groups.google.com/d/msgid/sympy/208b8d87-9e7b-4fe9-86fc-9575a0b57002n%40googlegroups.com. > > -- > 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 [email protected]. > To view this discussion on the web visit > https://groups.google.com/d/msgid/sympy/c5abf9ae-7c65-4935-9685-3791da6fa20an%40googlegroups.com. -- 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 [email protected]. To view this discussion on the web visit https://groups.google.com/d/msgid/sympy/CAKgW%3D6%2BGCh4BpDf48BCbF4ZuSbtjqQsjo-bw0fB2jirip81QQg%40mail.gmail.com.
