我有一个由 sympy 表示的方程组:
def test1a(A):
t, tt = sym.symbols('t tt')
return sym.cos(t+tt+A)*A
def test1b(B):
t, tt = sym.symbols('t tt')
return sym.sin(t-tt+B)*B
我想在使用以下命令评估结果之前转换为 numpy 表达式fsolve()
:
def testprep(variables, A, B):
t, tt = sym.symbols('t tt')
fA = lambdify([(t, tt)], test1a(A))
fB = lambdify([(t, tt)], test1b(B))
t, tt = variables
return [fA,fB]
def testsolve(A, B):
print(so.fsolve(testprep, [-1, 1], args = (A, B)))
return
运行时,我得到以下结果:
import scipy.optimize as so
import sympy as sym
from sympy.utilities.lambdify import lambdify as lambdify
import numpy as np
def main():
A = 1
B = 2
testsolve(A,B)
return
if __name__ == '__main__':
main()
输出:
error: Result from function call is not a proper array of floats.
作为健全性检查,我根据 numpy 表达式起草了相同的系统并解决了它:
def standard(variables, A, B):
t, tt = variables
eq1 = np.cos(t+tt+A)*A
eq2 = np.sin(t-tt+B)*B
return [eq1, eq2]
def solvestandard(A, B):
print(so.fsolve(standard, np.array([-1, 1]), args = (A,B)))
return
带输出:
[-0.71460184 1.28539816]
我是新手,lambdify( )
对从 sympy 转换为 numpy 的过程不太熟悉。为了使测试用例工作,我需要改变什么?