gpt4 book ai didi

python - 导数及其符号表达式的转换

转载 作者:太空宇宙 更新时间:2023-11-03 20:23:12 25 4
gpt4 key购买 nike

我需要获得导数以便稍后在数值计算中使用它。而且我的代码根本不起作用。我尝试使用lambdaify,但错误是“无法将符号转换为int”。我阅读了类似问题的其他答案,但仍然不起作用。

import numpy as np
from scipy.integrate import odeint
from scipy.integrate import quad
from scipy.integrate import solve_bvp as bvp
import matplotlib.pyplot as plt
from scipy.special import genlaguerre as L
from scipy.special import gamma as G
from math import factorial
from math import sqrt
import sympy as sym
from sympy.utilities.lambdify import lambdify


mp = 938.2720813
mn = 939.5654133

mu = (mn + mp)/4

hbar = 197.3270533

h2m = hbar**2/(2*mu)

V0 = 20
Rv = 1.5

Q0 = 1.5
Rq = 4.5

EIm = 0.3
ERe = 1

V = lambda r : -V0*np.exp(-r/Rv)
Q = lambda r : -Q0*np.exp(-r/Rq)


chi = lambda r, l : sqrt(factorial(l)*beta**3/(G(3 + l))) * r * L(l,2)(beta * r) * np.exp(- beta * r / 2)

r, l, beta = sym.symbols('r, l, beta')

def chifD(r, l, beta):
return sqrt(factorial(l)*beta**3/(G(3 + l))) * r * L(l,2)(beta * r) * np.exp(- beta * r / 2)

def chiD(r, l, beta):
return sym.diff(chifD(r ,l, beta), r)

print(chiD(r, l, beta))

chiLambdified = lambdify(((r,l, beta),),chiD(r,l, beta),"numpy")

print(chiD(1, 1, 1))

最佳答案

我很确定您正在混合不应该的模块。我不断修改你的 chi 函数,直到得到有用的东西。除了删除所有 mathscipy 函数之外,没有什么办法返回的不是 TypeError 的内容。

# works with sympy sqrt.
chi = lambda r, l : sym.sqrt(l*r)
print(chi(r,l))

# doesn't work with math or numpy sqrt.
chi = lambda r, l : math.sqrt(l*r)
print(chi(r,l))

如果您想使用函数,则必须使用 sympy 附带的函数,使用基本运算符定义自己的函数(以便接受 sympy 对象)或找到更简单的方法来定义操作:sqrt()可以简单地替换为 **(1/2)。

也许你应该寻找一种方法,使 sympy 接受来自其他模块的函数,因为自己编写 scipy 中的所有特殊函数看起来会很痛苦。

关于python - 导数及其符号表达式的转换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58026872/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com