Dear Oscar, Thanks to your explanations, I managed to use solve_ivp when I used cse=True in lambdify in one of my programs. ( just convert the list to an np.ndarray of the correct shape) The increase in speed in the integration was remarkable: >From around 8.5 sec with cse=False to around 0.13 sec with cse=True, 65 times faster! Thanks! Peter
On Wed 24. Aug 2022 at 14:48 Peter Stahlecker <peter.stahlec...@gmail.com> wrote: > Dear Oscar, > > my expressions into lambdify are of sm.Matrix type. > With cse=False, the output of lambdify is a numpy.ndarray of the same > shape as the sm.Matrix. > With cse=True, the output is is list. > (I guess, this is what you said, would happen.) > > Does this mean, if I somehow manage to convert the lists into ndarrays of > the correct shape, the code should run with cse=True, too? > > Thanks, Peter > > On Wed 24. Aug 2022 at 14:26 Oscar Benjamin <oscar.j.benja...@gmail.com> > wrote: > >> On Wed, 24 Aug 2022 at 08:55, Peter Stahlecker >> <peter.stahlec...@gmail.com> wrote: >> > >> > I have upgraded to sympy 1.11 >> > I wanted to try the cse keyword. >> > >> > If I set cse = False, all seems to work fine. >> > If I set cse = True, lambdify(..) seems to work fine, but solve_ivp(..) >> gives and error. >> >> It looks like cse=True doesn't work properly when lambdifying a Matrix: >> >> In [3]: lambdify((x,), Matrix([[x**2, 0], [0, x**2]]), cse=False)(1) >> Out[3]: >> array([[1, 0], >> [0, 1]]) >> >> In [4]: lambdify((x,), Matrix([[x**2, 0], [0, x**2]]), cse=True)(1) >> Out[4]: >> [array([[1, 0], >> [0, 1]])] >> >> Note that with cse=True the Matrix is in a list of length 1. If the >> matrix doesn't have any nontrivial subexpressions then it doesn't >> happen: >> >> In [6]: lambdify((x,), Matrix([[x, 0], [0, x]]), cse=True)(1) >> Out[6]: >> array([[1, 0], >> [0, 1]]) >> >> Looking at the generated code we have: >> >> In [11]: print(inspect.getsource(lambdify((x,), Matrix([[x**2, 0], [0, >> x**2]]), cse=True))) >> def _lambdifygenerated(x): >> x0 = x**2 >> return [array([[x0, 0], [0, x0]])] >> >> In [12]: print(inspect.getsource(lambdify((x,), Matrix([[x**2, 0], [0, >> x**2]]), cse=False))) >> def _lambdifygenerated(x): >> return array([[x**2, 0], [0, x**2]]) >> >> So for some reason the generated code puts the output in a list. Looks >> like this code is responsible: >> >> https://github.com/sympy/sympy/blob/5eb59bdad2f4e1a675acb31e7f0c72c8abca708c/sympy/utilities/lambdify.py#L1114-L1126 >> >> It seems to presume that expr (which in this case is a Matrix) is >> supposed to be a list or tuple. First it tries to add a list to it and >> then if that fails it tries to add a tuple and then if that fails it >> just puts expr into a list (expr = [expr]). I wish I could just ban >> the use of try/except in the sympy codebase because it's almost never >> used in a good way. >> >> Basically that code will fail any time expr is not a tuple or a list e.g.: >> >> In [3]: lambdify(x, x**2 + y*x**2, cse=True)(1) >> Out[3]: [y + 1] >> >> A workaround is just to extract the element from the list: >> >> In [4]: lambdify(x, x**2 + y*x**2, cse=True)(1)[0] >> Out[4]: y + 1 >> >> The bug comes from here: >> https://github.com/sympy/sympy/pull/23538 >> >> -- >> Oscar >> >> -- >> 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+unsubscr...@googlegroups.com. >> To view this discussion on the web visit >> https://groups.google.com/d/msgid/sympy/CAHVvXxSoDEGZLfZ%3D0JYkdoQ%3D0Dq%3DSVP_MqnmOvp9%2Bp7Q__uxSg%40mail.gmail.com >> . >> > -- > Best regards, > > Peter Stahlecker > -- Best regards, Peter Stahlecker -- 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+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/sympy/CABKqA0YKmQ3RwByJMwmtXKS5rXi-SZ42tGeQrjChk%3D_UQ90Q9Q%40mail.gmail.com.