sympy / sympy

A computer algebra system written in pure Python

Home Page:https://sympy.org/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

ccode, using numpy 2

fbordeu opened this issue · comments

Numpy 2 is commig. Using the release numpy =.RC2 of conda-forge (in python 3.12) I have the following error during a ccode command with a simple numpy vector:

il python=3.12, numpy=1.26.4 and sympy=1.12 I have

>>> import numpy as np
>>> from sympy import ccode 
>>> a = np.array([1.0,0],dtype=np.float64)
>>> ccode(a[0])
'1.0'

but in python=3.12, numpy=2.0.0rc2 and sympy=1.12 I have

>>> import numpy as np
>>> from sympy import ccode 
>>> a = np.array([1.0,0],dtype=np.float64)
>>> ccode(a[0])
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Users\felip\mambaforge\envs\numpy2\Lib\site-packages\sympy\printing\codeprinter.py", line 739, in ccode
    return c_code_printers[standard.lower()](settings).doprint(expr, assign_to)      
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^      
  File "C:\Users\felip\mambaforge\envs\numpy2\Lib\site-packages\sympy\printing\codeprinter.py", line 153, in doprint
    expr = _handle_assign_to(expr, assign_to)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\felip\mambaforge\envs\numpy2\Lib\site-packages\sympy\printing\codeprinter.py", line 137, in _handle_assign_to
    return sympify(expr)
           ^^^^^^^^^^^^^
  File "C:\Users\felip\mambaforge\envs\numpy2\Lib\site-packages\sympy\core\sympify.py", line 383, in sympify
    return conv(a)
           ^^^^^^^
  File "C:\Users\felip\mambaforge\envs\numpy2\Lib\site-packages\sympy\core\numbers.py", line 1070, in __new__
    num = _convert_numpy_types(num)
          ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\felip\mambaforge\envs\numpy2\Lib\site-packages\sympy\core\sympify.py", line 95, in _convert_numpy_types
    return Float(a, precision=prec)
           ^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\felip\mambaforge\envs\numpy2\Lib\site-packages\sympy\core\numbers.py", line 1126, in __new__
    _mpf_ = mlib.from_str(num, precision, rnd)
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\felip\mambaforge\envs\numpy2\Lib\site-packages\mpmath\libmp\libmpf.py", line 1331, in from_str
    man, exp = str_to_man_exp(x, base=10)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\felip\mambaforge\envs\numpy2\Lib\site-packages\mpmath\libmp\libmpf.py", line 1294, in str_to_man_exp
    float(x)
ValueError: could not convert string to float: 'np.float64(1.0)'
>>>

the offending line is:

file : sympify.py
93) a = str(list(np.reshape(np.asarray(a),(1, np.size(a)))[0]))[1:-1]

if we change this line by
a = np.array2string(np.reshape(np.asarray(a),(1, np.size(a)))[0],separator=", ")[1:-1]

What do you think??

for reference:

https://conda-forge.org/news/2024/05/15/numpy-2-migration/

Thanks to all for the excellent library.

closing the issue because sympy 1.13 already solved the problem. sorry noice.

It is also fixed in sympy 1.12.1