将额外的参数传递给 broyden1 [英] Passing extra arguments to broyden1

查看:51
本文介绍了将额外的参数传递给 broyden1的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试执行 scipy broyden1 函数带有额外参数(在示例中称为数据"),代码如下:

Im trying to execute scipy broyden1 function with extra parameters (called "data" in the example), here is the code:

data = [radar_wavelen, satpos, satvel, ellipsoid_semimajor_axis, ellipsoid_semiminor_axis, srange]
target_xyz = broyden1(Pixlinexyx_2Bsolved, start_xyz, args=data)

def Pixlinexyx_2Bsolved(target, *data):
    radar_wavelen, satpos, satvel, ellipsoid_semimajor_axis, ellipsoid_semiminor_axis, srange = data

    print target
    print radar_wavelen, satpos, satvel, ellipsoid_semimajor_axis, ellipsoid_semiminor_axis, srange

Pixlinexyx_2Bsolved 是我要查找其根的函数.

Pixlinexyx_2Bsolved is the function whose root I want to find.

start_xyz 是对解决方案的初步猜测:

start_xyz is initial guess of the solution:

start_xyz = [4543557.208584103, 1097477.4119051248, 4176990.636060918]

data 是这个包含大量数字的列表,将在 Pixlinexyx_2Bsolved 函数中使用:

And data is this list containing a lot of numbers, that will be used inside the Pixlinexyx_2Bsolved function:

<代码>数据= [0.056666,[5147114.2523595653,1584731.770061729,4715875.3525346108],[5162.8213179936156,-365.24378919717839,-5497.6237250296626],6378144.0430000005,6356758.789000001,850681.12442702544]

当我调用函数 broyden1(如示例代码的第二行)时,出现下一个错误:

When I call the function broyden1 (as in the second line of example code) I get the next error:

target_xyz = broyden1(Pixlinexyx_2Bsolved, start_xyz, args=data)
  File "<string>", line 5, in broyden1
TypeError: __init__() got an unexpected keyword argument 'args'

我做错了什么?

现在,看到 fsolve,它似乎能够在可调用函数中获得额外的参数...这里 和我的问题类似.

Now, seeing the documentation of fsolve, it seems to be able to get extra args in the callable func... Here is a similar question as mine.

推荐答案

scipy 的 问题跟踪器 包括使用 python 的 functools 的解决方案-模块(此处:PEP 309 -- 部分函数应用).

There is a similar question at scipy's issue-tracker including a solution using python's functools-module (here: PEP 309 -- Partial Function Application ).

基于上述链接和来自 文档:

Small example based on the above link and the original problem from the docs:

import numpy as np
import scipy.optimize

""" No external data """
def F(x):
   return np.cos(x) + x[::-1] - [1, 2, 3, 4]
x = scipy.optimize.broyden1(F, [1,1,1,1], f_tol=1e-14)
print(x)

""" External data """
from functools import partial
def G(data, x):
    return np.cos(x) + x[::-1] - data
data = [1,2,3,4]
G_partial = partial(G, data)
x = scipy.optimize.broyden1(G_partial, [1,1,1,1], f_tol=1e-14)
print(x)

[ 4.04674914  3.91158389  2.71791677  1.61756251]
[ 4.04674914  3.91158389  2.71791677  1.61756251]

这篇关于将额外的参数传递给 broyden1的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆