将额外的参数传递给broyden1

时间:2018-04-26 10:18:19

标签: python python-2.7 scipy

我试图用额外的参数执行scipy broyden1函数(在示例中称为“data”),这里是代码:

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]

数据是包含大量数字的列表,将在 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的文档,它似乎能够在可调用函数中获得额外的args ... Here与我的问题类似。

1 个答案:

答案 0 :(得分:3)

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

基于上述链接的小示例以及docs的原始问题:

 <?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Contact;

use Auth;

use App\Http\Requests\contactRequest;

class ContactController extends Controller
{
    public function __construct()
    {
        $this->middleware('auth');
    }

    public function index()
    {
        $listcontact = Auth::user()->contacts;

        return view('contact.index', ['lists' => $listcontact]);
    }

    public function create()
    {
        return view('contact.create');
    }

            public function store(contactRequest $request)
    {
        $contact = new Contact();

        $contact->nom=$request->input('nom');
        $contact->tel=$request->input('tel');
        $contact->genre=$request->input('genre');
        $contact->age=$request->input('age');
        $contact->ville=$request->input('ville');

        $contact->user_idd=Auth::user()->id;

        $contact->save();

        session()->flash('success', 'Contact bien enregistré!!');

        return redirect('contacts');
    }

    public function edit($id)
    {
        $contact = Contact::find($id);

        return view('contact.edit', ['cont' => $contact]);
    }

    public function update(contactRequest $request, $id)
    {
        $contact = Contact::find($id);

        $contact->nom = $request->input('nom');
        $contact->tel = $request->input('tel');
        $contact->genre = $request->input('genre');
        $contact->age = $request->input('age');
        $contact->ville = $request->input('ville');

        $contact->save();

        session()->flash('edited', 'Contact bien modifié!!');

        return redirect('contacts');
    }

    public function destroy(Request $request, $id)
    {
        $contact = Contact::find($id);

        $contact->delete();

        return redirect('contacts');
    }
}

离开

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)