如何传递与 scipy 一起使用的 sympy 表达式? [英] How to pass sympy expressions to be used with scipy?

查看:62
本文介绍了如何传递与 scipy 一起使用的 sympy 表达式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用 scipy.optimize 中的 root 来求解由循环创建的非线性方程组.我想用一种方法创建方程,然后用另一种方法求解.我用 sympy 创建了方程,并想用 scipy 解决它们.我的真实代码有太多循环,每次来自 scipy 的 root 都会迭代这些循环.

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.

这是我尝试过的非常简化的版本.我用 sympy 符号创建了方程,然后我使用 lambdifysympy 中取出方程,但求解器向我发送了一个错误.

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()

它向我发送了这个错误 TypeError: root() got an unexpected keyword argument 'jac'.如果我删除 jac,它会向 sme 发送这个 TypeError: unsupported operand type(s) for/: 'int' and 'list'

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'

这些是 jac 的回溯

文件",第 1 行,在runfile('C:/Users/gian_/Documents/Irri-trickle/speed/i.py', wdir='C:/Users/gian_/Documents/Irri-trickle/speed')

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

文件C:\Users\gian_\Anaconda3\lib\site-packages\spyder_kernels\customize\spydercustomize.py",第 827 行,在运行文件中execfile(文件名,命名空间)

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

文件C:\Users\gian_\Anaconda3\lib\site-packages\spyder_kernels\customize\spydercustomize.py",第 110 行,在 execfile 中exec(compile(f.read(), filename, 'exec'), 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)

文件C:/Users/gian_/Documents/Irri-trickle/speed/i.py",第 54 行,在求解器()

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

文件C:/Users/gian_/Documents/Irri-trickle/speed/i.py",第 49 行,在求解器中sol = root(f, Guess, method='krylov') # Entrega el resultado

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() 得到了一个意外的关键字参数 'jac'

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

文件",第 1 行,在runfile('C:/Users/gian_/Documents/Irri-trickle/speed/i.py', wdir='C:/Users/gian_/Documents/Irri-trickle/speed')

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

文件C:\Users\gian_\Anaconda3\lib\site-packages\spyder_kernels\customize\spydercustomize.py",第 827 行,在运行文件中execfile(文件名,命名空间)

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

文件C:\Users\gian_\Anaconda3\lib\site-packages\spyder_kernels\customize\spydercustomize.py",第 110 行,在 execfile 中exec(compile(f.read(), filename, 'exec'), 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)

文件C:/Users/gian_/Documents/Irri-trickle/speed/i.py",第 54 行,在求解器()

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

文件C:/Users/gian_/Documents/Irri-trickle/speed/i.py",第 49 行,在求解器中sol = root(f, Guess, jac = False) # Entrega el resultado

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() 得到了一个意外的关键字参数 'jac'

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

推荐答案

提供的代码也给了我一个错误:TypeError: _lambdifygenerated() missing 1 required positional argument: 'y1'.用 y = DeferredVector('y') 替换 y 的声明解决了这个问题,正如 这篇文章.假设您使用的是最新版本的 sympy 和 scipy.

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.

我不明白关于 jac 的奇怪错误信息,因为你输入的很好.也许您无意中使用了另一个包中的 root?

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

这篇关于如何传递与 scipy 一起使用的 sympy 表达式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆