[英]SymPy not evaluating exponential function with subs
I'm attempting to write an implementation of the multivariate Newton-Raphson method, in Python
.我正在尝试在
Python
中编写多变量 Newton-Raphson 方法的实现。 To get started, I'm trying to solve the system:首先,我正在尝试解决系统问题:
10 * x + 3 * y * y - 3
x * x - exp(y) - 2
Although it is my intent to expand this to any arbitrary mxn
system (assuming a solution is possible/exists, of course).尽管我打算将此扩展到任何任意
mxn
系统(当然,假设解决方案是可能的/存在的)。
My code is as follows:我的代码如下:
from dataclasses import dataclass
from sympy import *
x, y, z = symbols('x y z')
@dataclass
class Solve:
@staticmethod
def newton_raphson():
F = Matrix([10 * x + 3 * y * y - 3, x * x - exp(y) - 2])
v = Matrix([x, y])
print(J := F.jacobian(v))
print(J * -1)
xx, yy = 0, 0
for i in range(10):
A = J.subs({"x": xx, "y": yy})
b = F.subs({"x": xx, "y": yy})
update = linsolve((A, b), [x, y])
(xx, yy) = tuple(*update) # should be adding, not setting equal to
print(xx, yy)
mySolver = Solve
mySolver.newton_raphson()
My issue being that, when I perform .subs()
on the matrices F
and J
, everything but the exponential function evaluates.我的问题是,当我对矩阵
F
和J
执行.subs()
时,除指数 function 之外的所有内容都会进行评估。 That is, the line print(xx, yy)
prints:也就是说,行
print(xx, yy)
打印:
3*(573 - 50*exp(3))/(20*(27 - 25*exp(3))) 5*(13 + 20*exp(3))/(4*(-27 + 25*exp(3)))
(-28650000*exp(1755/(4*(-1350*exp(3) + 729 + 625*exp(6))) + 1075*exp(3)/(4*(-1350*exp(3) + 729 + 625*exp(6))) + 9) - 7500000*exp(625*exp(6)/(-1350*exp(3) + 729 + 625*exp(6)) + 9) - 53550000*exp(625*exp(6)/(-1350*exp(3) + 729 + 625*exp(6)) + 6) - 105598890*exp(625*exp(6)/(-1350*exp(3) + 729 + 625*exp(6))) + 92498679*exp(5*(351 + 215*exp(3))/(4*(-1350*exp(3) + 729 + 625*exp(6)))) + 164313360*exp(1755/(4*(-1350*exp(3) + 729 + 625*exp(6))) + 1075*exp(3)/(4*(-1350*exp(3) + 729 + 625*exp(6))) + 3) + 15235500*exp(1755/(4*(-1350*exp(3) + 729 + 625*exp(6))) + 1075*exp(3)/(4*(-1350*exp(3) + 729 + 625*exp(6))) + 6) + 164358750*exp(625*exp(6)/(-1350*exp(3) + 729 + 625*exp(6)) + 3))/(-81000000*exp(625*exp(6)/(-1350*exp(3) + 729 + 625*exp(6)) + 6) - 107010000*exp(1755/(4*(-1350*exp(3) + 729 + 625*exp(6))) + 1075*exp(3)/(4*(-1350*exp(3) + 729 + 625*exp(6))) + 6) - 31492800*exp(625*exp(6)/(-1350*exp(3) + 729 + 625*exp(6))) + 72404280*exp(5*(351 + 215*exp(3))/(4*(-1350*exp(3) + 729 + 625*exp(6)))) + 38032200*exp(1755/(4*(-1350*exp(3) + 729 + 625*exp(6))) + 1075*exp(3)/(4*(-1350*exp(3) + 729 + 625*exp(6))) + 3) + 87480000*exp(625*exp(6)/(-1350*exp(3) + 729 + 625*exp(6)) + 3) + 9000000*exp(1755/(4*(-1350*exp(3) + 729 + 625*exp(6))) + 1075*exp(3)/(4*(-1350*exp(3) + 729 + 625*exp(6))) + 9) + 25000000*exp(625*exp(6)/(-1350*exp(3) + 729 + 625*exp(6)) + 9)) (-16200000*exp(625*exp(6)/(-1350*exp(3) + 729 + 625*exp(6)) + 6) - 40086000*exp(1755/(4*(-1350*exp(3) + 729 + 625*exp(6))) + 1075*exp(3)/(4*(-1350*exp(3) + 729 + 625*exp(6))) + 6) - 68751693*exp(5*(351 + 215*exp(3))/(4*(-1350*exp(3) + 729 + 625*exp(6)))) - 6298560*exp(625*exp(6)/(-1350*exp(3) + 729 + 625*exp(6))) + 17496000*exp(625*exp(6)/(-1350*exp(3) + 729 + 625*exp(6)) + 3) + 68874390*exp(1755/(4*(-1350*exp(3) + 729 + 625*exp(6))) + 1075*exp(3)/(4*(-1350*exp(3) + 729 + 625*exp(6))) + 3) + 10450000*exp(1755/(4*(-1350*exp(3) + 729 + 625*exp(6))) + 1075*exp(3)/(4*(-1350*exp(3) + 729 + 625*exp(6))) + 9) + 5000000*exp(625*exp(6)/(-1350*exp(3) + 729 + 625*exp(6)) + 9))/(8*(-2025000*exp(625*exp(6)/(-1350*exp(3) + 729 + 625*exp(6)) + 6) - 2675250*exp(1755/(4*(-1350*exp(3) + 729 + 625*exp(6))) + 1075*exp(3)/(4*(-1350*exp(3) + 729 + 625*exp(6))) + 6) - 787320*exp(625*exp(6)/(-1350*exp(3) + 729 + 625*exp(6))) + 1810107*exp(5*(351 + 215*exp(3))/(4*(-1350*exp(3) + 729 + 625*exp(6)))) + 950805*exp(1755/(4*(-1350*exp(3) + 729 + 625*exp(6))) + 1075*exp(3)/(4*(-1350*exp(3) + 729 + 625*exp(6))) + 3) + 2187000*exp(625*exp(6)/(-1350*exp(3) + 729 + 625*exp(6)) + 3) + 225000*exp(1755/(4*(-1350*exp(3) + 729 + 625*exp(6))) + 1075*exp(3)/(4*(-1350*exp(3) + 729 + 625*exp(6))) + 9) + 625000*exp(625*exp(6)/(-1350*exp(3) + 729 + 625*exp(6)) + 9)))
...etc
While I need a strictly numerical value, for updating the guesses xx
and yy
.虽然我需要一个严格的数值来更新猜测
xx
和yy
。
Sympy is specialized in symbolic evaluation. Sympy 专门从事符号评估。 exp(3) is a irrational number, and thus can not be represented numerically.
exp(3) 是一个无理数,因此不能用数值表示。 Sympy knows this.
Sympy 知道这一点。
To force a numerical approximation, you can use the N
function from sympy, as in要强制进行数值近似,您可以使用 sympy 中的
N
function,如
A = N(J.subs({"x": xx, "y": yy}))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.