简体   繁体   English

将额外的参数传递给broyden1

[英]Passing extra arguments to broyden1

Im trying to execute scipy broyden1 function with extra parameters (called "data" in the example), here is the code: 我试图用额外的参数(在示例中称为“数据”)执行scipy broyden1函数,下面是代码:

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 is the function whose root I want to find. Pixlinexyx_2Bsolved是我要查找其根的函数。

start_xyz is initial guess of the solution: start_xyz是该解决方案的初始猜测:

start_xyz = [4543557.208584103, 1097477.4119051248, 4176990.636060918]

And data is this list containing a lot of numbers, that will be used inside the Pixlinexyx_2Bsolved function: 数据是包含大量数字的列表,这些数字将在Pixlinexyx_2Bsolved函数内部使用:

data = [0.056666, [5147114.2523595653, 1584731.770061729, 4715875.3525346108], [5162.8213179936156, -365.24378919717839, -5497.6237250296626], 6378144.0430000005, 6356758.789000001, 850681.12442702544]

When I call the function broyden1 (as in the second line of example code) I get the next error: 当我调用函数broyden1(如示例代码的第二行)时,出现下一个错误:

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

What I'm doing wrong? 我做错了什么?

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. 现在,看到fsolve的文档,似乎可以在可调用的函数中获取额外的args。 是与我类似的问题。

There is a similar question at scipy's issue-tracker including a solution using python's functools -module (here: PEP 309 -- Partial Function Application ). scipy的问题跟踪程序中存在一个类似的问题,其中包括使用python的functools -module(此处为PEP 309- 部分函数应用程序 )的解决方案。

Small example based on the above link and the original problem from the docs : 基于上述链接和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)

Out

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM