使用 python 等同系数
Equating coefficients using python
考虑到两个方程,这实际上是一项非常简单的手动任务,但我想知道是否可以使 python 中的系数相等(可能使用 numpy 或 sympy/scipy? ).
所以我的方程式看起来像这样:
y = 1.0066*x**2-1.8908*x-0.888
U = 0.5kB*(r-r0)**2
现在理想情况下,应该首先将第二个方程相乘得到上面的方程式,然后 kB 应该通过三个系数 a (=1.0066)、b( =1.8908) 和 c (0.888),其中 r0 也是一个常数(基本上给了我 3 个 kB 值)
所以唯一的变量是 x 和 r ,但我对它们并不感兴趣
是否可以执行这样的任务?请记住,我只是一个初学者
谢谢
您可以使用 SymPy
将右侧表示为关于符号变量 x
、r
、r0
和 kB
的表达式:
x, r, r0, kB = sym.symbols('x,r,r0,kB')
y = 1.0066*x**2-1.8908*x-0.888
U = 0.5*kB*(r-r0)**2
现在我们可以将y
和U
转换为关于x
和r
的多项式:
In [39]: sym.poly(y, x)
Out[39]: Poly(1.0066*x**2 - 1.8908*x - 0.888, x, domain='RR')
In [40]: sym.poly(U, r)
Out[40]: Poly(0.5*kB*r**2 - 1.0*kB*r0*r + 0.5*kB*r0**2, r, domain='RR[r0,kB]')
sym.Poly
s have a all_coeffs
方法 returns 系数列表:
In [41]: sym.poly(y, x).all_coeffs()
Out[41]: [1.00660000000000, -1.89080000000000, -0.888000000000000]
In [42]: sym.poly(U, r).all_coeffs()
Out[42]: [0.5*kB, -1.0*kB*r0, 0.5*kB*r0**2]
我们可以使用zip
来配对来自两个列表的系数:
In [43]: list(zip(sym.poly(y, x).all_coeffs(), sym.poly(U, r).all_coeffs()))
Out[43]:
[(1.00660000000000, 0.5*kB),
(-1.89080000000000, -1.0*kB*r0),
(-0.888000000000000, 0.5*kB*r0**2)]
然后用sympy.Eq
使表达式相等,用sympy.solve
to solve them for kB
. The Python construct I'm using here to generate the list is called a list comprehension:
In [44]: [sym.solve(sym.Eq(a, b), [kB]) for a, b in zip(sym.poly(y, x).all_coeffs(), sym.poly(U, r).all_coeffs())]
Out[44]: [[2.01320000000000], [1.8908/r0], [-1.776/r0**2]]
综合起来:
import sympy as sym
x, r, r0, kB = sym.symbols('x,r,r0,kB')
y = 1.0066*x**2-1.8908*x-0.888
U = 0.5*kB*(r-r0)**2
result = [sym.solve(sym.Eq(a, b), [kB]) for a, b in
zip(sym.poly(U, r).all_coeffs(), sym.poly(y, x).all_coeffs())]
print(result)
打印
[[2.01320000000000], [1.8908/r0], [-1.776/r0**2]]
考虑到两个方程,这实际上是一项非常简单的手动任务,但我想知道是否可以使 python 中的系数相等(可能使用 numpy 或 sympy/scipy? ). 所以我的方程式看起来像这样:
y = 1.0066*x**2-1.8908*x-0.888
U = 0.5kB*(r-r0)**2
现在理想情况下,应该首先将第二个方程相乘得到上面的方程式,然后 kB 应该通过三个系数 a (=1.0066)、b( =1.8908) 和 c (0.888),其中 r0 也是一个常数(基本上给了我 3 个 kB 值) 所以唯一的变量是 x 和 r ,但我对它们并不感兴趣
是否可以执行这样的任务?请记住,我只是一个初学者
谢谢
您可以使用 SymPy
将右侧表示为关于符号变量 x
、r
、r0
和 kB
的表达式:
x, r, r0, kB = sym.symbols('x,r,r0,kB')
y = 1.0066*x**2-1.8908*x-0.888
U = 0.5*kB*(r-r0)**2
现在我们可以将y
和U
转换为关于x
和r
的多项式:
In [39]: sym.poly(y, x)
Out[39]: Poly(1.0066*x**2 - 1.8908*x - 0.888, x, domain='RR')
In [40]: sym.poly(U, r)
Out[40]: Poly(0.5*kB*r**2 - 1.0*kB*r0*r + 0.5*kB*r0**2, r, domain='RR[r0,kB]')
sym.Poly
s have a all_coeffs
方法 returns 系数列表:
In [41]: sym.poly(y, x).all_coeffs()
Out[41]: [1.00660000000000, -1.89080000000000, -0.888000000000000]
In [42]: sym.poly(U, r).all_coeffs()
Out[42]: [0.5*kB, -1.0*kB*r0, 0.5*kB*r0**2]
我们可以使用zip
来配对来自两个列表的系数:
In [43]: list(zip(sym.poly(y, x).all_coeffs(), sym.poly(U, r).all_coeffs()))
Out[43]:
[(1.00660000000000, 0.5*kB),
(-1.89080000000000, -1.0*kB*r0),
(-0.888000000000000, 0.5*kB*r0**2)]
然后用sympy.Eq
使表达式相等,用sympy.solve
to solve them for kB
. The Python construct I'm using here to generate the list is called a list comprehension:
In [44]: [sym.solve(sym.Eq(a, b), [kB]) for a, b in zip(sym.poly(y, x).all_coeffs(), sym.poly(U, r).all_coeffs())]
Out[44]: [[2.01320000000000], [1.8908/r0], [-1.776/r0**2]]
综合起来:
import sympy as sym
x, r, r0, kB = sym.symbols('x,r,r0,kB')
y = 1.0066*x**2-1.8908*x-0.888
U = 0.5*kB*(r-r0)**2
result = [sym.solve(sym.Eq(a, b), [kB]) for a, b in
zip(sym.poly(U, r).all_coeffs(), sym.poly(y, x).all_coeffs())]
print(result)
打印
[[2.01320000000000], [1.8908/r0], [-1.776/r0**2]]