How to pass sympy expressions to be used with scipy?

ⅰ亾dé卋堺 提交于 2020-01-25 06:53:09

问题


I want to solve a systen of non-linear equations created by loops using root from scipy.optimize. I want to create the equations with one method and then solve them with another. I created the equations with sympy and want to solve them with scipy. My real code has too many loops and every time root from scipy iterates those loops.

This is a very simplified version of what I tried. I made the equations with sympy symbols and then I used lambdify to take the equations out of sympy but the solver sends me an error.

from scipy.optimize import root
from sympy import Symbol, lambdify

y = [Symbol('x%d' % i) for i in range(2)]

def ecuacion():

    global c,cakeo,mellado

    c = []
    cakeo = y[0] +1 + y[1]
    c.append(cakeo)
    mellado = y[1] + 4 + -y[0]
    c.append(mellado)
    print(c)
    return c

ecuacion()

f = lambdify(y, c, 'numpy')

def solver():

    Guess = [1,-1]
    sol = root(f, Guess,method='hybr', jac=False) # Entrega el resultado       
    print(sol.x)
    return sol

solver()

It sends me this error TypeError: root() got an unexpected keyword argument 'jac'. If I delete jac, it send sme this TypeError: unsupported operand type(s) for /: 'int' and 'list'

these are the tracebacks with jac

File "", line 1, in runfile('C:/Users/gian_/Documents/Irri-trickle/speed/i.py', wdir='C:/Users/gian_/Documents/Irri-trickle/speed')

File "C:\Users\gian_\Anaconda3\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 827, in runfile execfile(filename, namespace)

File "C:\Users\gian_\Anaconda3\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 110, in execfile exec(compile(f.read(), filename, 'exec'), namespace)

File "C:/Users/gian_/Documents/Irri-trickle/speed/i.py", line 54, in solver()

File "C:/Users/gian_/Documents/Irri-trickle/speed/i.py", line 49, in solver sol = root(f, Guess, method='krylov') # Entrega el resultado

TypeError: root() got an unexpected keyword argument 'jac'

File "", line 1, in runfile('C:/Users/gian_/Documents/Irri-trickle/speed/i.py', wdir='C:/Users/gian_/Documents/Irri-trickle/speed')

File "C:\Users\gian_\Anaconda3\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 827, in runfile execfile(filename, namespace)

File "C:\Users\gian_\Anaconda3\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 110, in execfile exec(compile(f.read(), filename, 'exec'), namespace)

File "C:/Users/gian_/Documents/Irri-trickle/speed/i.py", line 54, in solver()

File "C:/Users/gian_/Documents/Irri-trickle/speed/i.py", line 49, in solver sol = root(f, Guess, jac = False) # Entrega el resultado

TypeError: root() got an unexpected keyword argument 'jac'


回答1:


The provided code also gave me an error: TypeError: _lambdifygenerated() missing 1 required positional argument: 'y1'. Replacing the declaration ofy by y = DeferredVector('y') solved the problem, as suggested in this post. Supposing you are using recent versions of sympy and scipy.

I don't understand the strange error message about jac, as you entered it just fine. Maybe you are inadvertently using root from another package?

Try importing it with an unambiguous name as in:

from scipy.optimize import root as scipy_root
from sympy import Symbol, lambdify, DeferredVector

y = DeferredVector('y')
...

def solver():
    guess = [1,-1]
    sol = scipy_root(f, guess, method='hybr', jac=False) # Entrega el resultado
    print(sol.x)
    return sol


来源:https://stackoverflow.com/questions/58772201/how-to-pass-sympy-expressions-to-be-used-with-scipy

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!