gpt4 book ai didi

python - 向 broyden1 传递额外参数

转载 作者:行者123 更新时间:2023-11-30 22:16:11 26 4
gpt4 key购买 nike

我正在尝试执行 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 是我要查找其根的函数。

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

start_xyz = [4543557.208584103, 1097477.4119051248, 4176990.636060918]

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

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

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

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

我做错了什么?

现在,查看 fsolve 的文档,似乎能够在可调用函数中获得额外的参数... Here和我的问题类似。

最佳答案

scipy的issue-tracker有类似的问题包括使用 python 的解决方案 functools -模块(此处:PEP 309 -- Partial Function Application)。

基于上述链接和 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]

关于python - 向 broyden1 传递额外参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50040461/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com