问题描述
鉴于对值数组 x
的初步猜测,我试图找到最接近 x
的系统的根.如果您熟悉查找系统的根,则将了解为方程式 f
的系统查找根可以满足:
Given an initial guess for an array of values x
, I am trying to find the root of a system that is closest to x
. If you are familiar with finding roots of a system, you will understand that finding a root for a system of equations f
satisfies:
0 = f_1(x)
0 = f_2(x)
....
0 = f_n(x)
其中 f_i
是 f
scipy
中有一个软件包可以完全执行此操作: scipy.optimize.newton_krylov
.例如:
There is a package within scipy
that will do this exactly: scipy.optimize.newton_krylov
. For example:
import scipy.optimize as sp
def f(x):
f0 = (x[0]**2) + (3*(x[1]**3)) - 2
f1 = x[0] * (x[1]**2)
return [f0, f1]
# Nearest root is [sqrt(2), 0]
print sp.newton_krylov(f, [2, .01], iter=100, f_tol=Dc('1e-15'))
>>> [ 1.41421356e+00 3.49544535e-10] # Close enough!
但是,我正在使用 十进制
在python中打包,因为我正在做非常精确的工作. decimal
提供了比普通十进制精度更高的精度. scipy.optimize.newton_krylov
返回浮点精度值.有没有办法以任意精确的十进制精度得到我的答案?
However, I am using the decimal
package within python because I am doing extremely precise work. decimal
offers more than normal decimal precision. scipy.optimize.newton_krylov
returns float-precision values. Is there a way to get my answer at an arbitrarily precise decimal precision?
推荐答案
我找到了 mpmath
模块,其中包含 mpmath.findroot
一个>. mpmath
对其所有数字均使用任意小数点精度. mpmath.findroot
将在其中找到最接近的根宽容.这是对同一问题使用 mpmath
的示例,其精度更高:
I have found the mpmath
module, which contains mpmath.findroot
. mpmath
uses arbitrary decimal-point precision for all of its numbers. mpmath.findroot
will find the nearest root within tolerance. Here is an example of using mpmath
for the same problem, to a higher precision:
import scipy.optimize as sp
import mpmath
from mpmath import mpf
mpmath.mp.dps = 15
def mp_f(x1, x2):
f1 = (x1**2) + (3*(x2**3)) - 2
f2 = x1 * (x2**2)
return f1, f2
def f(x):
f0 = (x[0]**2) + (3*(x[1]**3)) - 2
f1 = x[0] * (x[1]**2)
return [f0, f1]
tmp_solution = sp.newton_krylov(f, [2, .01], f_tol=Dc('1e-10'))
print tmp_solution
>>> [ 1.41421356e+00 4.87315249e-06]
for _ in range(8):
tmp_solution = mpmath.findroot(mp_f, (tmp_solution[0], tmp_solution[1]))
print tmp_solution
mpmath.mp.dps += 10 # Increase precision
>>> [ 1.4142135623731]
[4.76620313173184e-9]
>>> [ 1.414213562373095048801689]
[4.654573673348783724565804e-12]
>>> [ 1.4142135623730950488016887242096981]
[4.5454827012374811707063801808968925e-15]
>>> [ 1.41421356237309504880168872420969807856967188]
[4.43894795688326535096068850443292395286770757e-18]
>>> [ 1.414213562373095048801688724209698078569671875376948073]
[4.334910114213471839327827177504976152074382061299675453e-21]
>>> [ 1.414213562373095048801688724209698078569671875376948073176679738]
[4.2333106584123451747941381835420647823192649980317402073699554127e-24]
>>> [ 1.41421356237309504880168872420969807856967187537694807317667973799073247846]
[4.1340924398558139440207202654766836515453497962889870471467483995909717197e-27]
>>> [ 1.41421356237309504880168872420969807856967187537694807317667973799073247846210703885]
[4.037199648296693366576484784520203892002447351324378380584214947262318103197216393589e-30]
可以任意提高精度.
这篇关于找出方程组的根以任意十进制精度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!