forked from scipy/scipy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
__minpack.h
702 lines (561 loc) · 22.6 KB
/
__minpack.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
/* This file is used to make _multipackmodule.c */
/* $Revision$ */
/* module_methods:
{"_hybrd", minpack_hybrd, METH_VARARGS, doc_hybrd},
{"_hybrj", minpack_hybrj, METH_VARARGS, doc_hybrj},
{"_lmdif", minpack_lmdif, METH_VARARGS, doc_lmdif},
{"_lmder", minpack_lmder, METH_VARARGS, doc_lmder},
{"_chkder", minpack_chkder, METH_VARARGS, doc_chkder},
*/
/* link libraries:
minpack
linpack_lite
blas
*/
/* python files:
minpack.py
*/
#if defined(NO_APPEND_FORTRAN)
#if defined(UPPERCASE_FORTRAN)
/* nothing to do in that case */
#else
#define CHKDER chkder
#define HYBRD hybrd
#define HYBRJ hybrj
#define LMDIF lmdif
#define LMDER lmder
#define LMSTR lmstr
#endif
#else
#if defined(UPPERCASE_FORTRAN)
#define CHKDER CHKDER_
#define HYBRD HYBRD_
#define HYBRJ HYBRJ_
#define LMDIF LMDIF_
#define LMDER LMDER_
#define LMSTR LMSTR_
#else
#define CHKDER chkder_
#define HYBRD hybrd_
#define HYBRJ hybrj_
#define LMDIF lmdif_
#define LMDER lmder_
#define LMSTR lmstr_
#endif
#endif
extern void CHKDER(int*,int*,double*,double*,double*,int*,double*,double*,int*,double*);
extern void HYBRD(void*,int*,double*,double*,double*,int*,int*,int*,double*,double*,int*,double*,int*,int*,int*,double*,int*,double*,int*,double*,double*,double*,double*,double*);
extern void HYBRJ(void*,int*,double*,double*,double*,int*,double*,int*,double*,int*,double*,int*,int*,int*,int*,double*,int*,double*,double*,double*,double*,double*);
extern void LMDIF(void*,int*,int*,double*,double*,double*,double*,double*,int*,double*,double*,int*,double*,int*,int*,int*,double*,int*,int*,double*,double*,double*,double*,double*);
extern void LMDER(void*,int*,int*,double*,double*,double*,int*,double*,double*,double*,int*,double*,int*,double*,int*,int*,int*,int*,int*,double*,double*,double*,double*,double*);
extern void LMSTR(void*,int*,int*,double*,double*,double*,int*,double*,double*,double*,int*,double*,int*,double*,int*,int*,int*,int*,int*,double*,double*,double*,double*,double*);
int raw_multipack_calling_function(int *n, double *x, double *fvec, int *iflag)
{
/* This is the function called from the Fortran code it should
-- use call_python_function to get a multiarrayobject result
-- check for errors and return -1 if any
-- otherwise place result of calculation in *fvec
*/
PyArrayObject *result_array = NULL;
result_array = (PyArrayObject *)call_python_function(multipack_python_function, *n, x, multipack_extra_arguments, 1, minpack_error);
if (result_array == NULL) {
*iflag = -1;
return -1;
}
memcpy(fvec, PyArray_DATA(result_array), (*n)*sizeof(double));
Py_DECREF(result_array);
return 0;
}
int jac_multipack_calling_function(int *n, double *x, double *fvec, double *fjac, int *ldfjac, int *iflag)
{
/* This is the function called from the Fortran code it should
-- use call_python_function to get a multiarrayobject result
-- check for errors and return -1 if any
-- otherwise place result of calculation in *fvec or *fjac.
If iflag = 1 this should compute the function.
If iflag = 2 this should compute the jacobian (derivative matrix)
*/
PyArrayObject *result_array;
if (*iflag == 1) {
result_array = (PyArrayObject *)call_python_function(multipack_python_function, *n, x, multipack_extra_arguments, 1, minpack_error);
if (result_array == NULL) {
*iflag = -1;
return -1;
}
memcpy(fvec, PyArray_DATA(result_array), (*n)*sizeof(double));
}
else { /* iflag == 2 */
result_array = (PyArrayObject *)call_python_function(multipack_python_jacobian, *n, x, multipack_extra_arguments, 2, minpack_error);
if (result_array == NULL) {
*iflag = -1;
return -1;
}
if (multipack_jac_transpose == 1)
MATRIXC2F(fjac, PyArray_DATA(result_array), *n, *ldfjac)
else
memcpy(fjac, PyArray_DATA(result_array), (*n)*(*ldfjac)*sizeof(double));
}
Py_DECREF(result_array);
return 0;
}
int raw_multipack_lm_function(int *m, int *n, double *x, double *fvec, int *iflag)
{
/* This is the function called from the Fortran code it should
-- use call_python_function to get a multiarrayobject result
-- check for errors and return -1 if any
-- otherwise place result of calculation in *fvec
*/
PyArrayObject *result_array = NULL;
result_array = (PyArrayObject *)call_python_function(multipack_python_function,*n, x, multipack_extra_arguments, 1, minpack_error);
if (result_array == NULL) {
*iflag = -1;
return -1;
}
memcpy(fvec, PyArray_DATA(result_array), (*m)*sizeof(double));
Py_DECREF(result_array);
return 0;
}
int jac_multipack_lm_function(int *m, int *n, double *x, double *fvec, double *fjac, int *ldfjac, int *iflag)
{
/* This is the function called from the Fortran code it should
-- use call_python_function to get a multiarrayobject result
-- check for errors and return -1 if any
-- otherwise place result of calculation in *fvec or *fjac.
If iflag = 1 this should compute the function.
If iflag = 2 this should compute the jacobian (derivative matrix)
*/
PyArrayObject *result_array;
if (*iflag == 1) {
result_array = (PyArrayObject *)call_python_function(multipack_python_function, *n, x, multipack_extra_arguments, 1, minpack_error);
if (result_array == NULL) {
*iflag = -1;
return -1;
}
memcpy(fvec, PyArray_DATA(result_array), (*m)*sizeof(double));
}
else { /* iflag == 2 */
result_array = (PyArrayObject *)call_python_function(multipack_python_jacobian, *n, x, multipack_extra_arguments, 2, minpack_error);
if (result_array == NULL) {
*iflag = -1;
return -1;
}
if (multipack_jac_transpose == 1)
MATRIXC2F(fjac, PyArray_DATA(result_array), *n, *ldfjac)
else
memcpy(fjac, PyArray_DATA(result_array), (*n)*(*ldfjac)*sizeof(double));
}
Py_DECREF(result_array);
return 0;
}
int smjac_multipack_lm_function(int *m, int *n, double *x, double *fvec, double *fjrow, int *iflag)
{
/* This is the function called from the Fortran code it should
-- use call_python_function to get a multiarrayobject result
-- check for errors and return -1 if any
-- otherwise place result of calculation in *fvec or *fjac.
If iflag = 1 this should compute the function.
If iflag = i this should compute the (i-1)-st row of the jacobian.
*/
int row;
PyObject *newargs, *ob_row;
PyArrayObject *result_array;
if (*iflag == 1) {
result_array = (PyArrayObject *)call_python_function(multipack_python_function, *n, x, multipack_extra_arguments, 1, minpack_error);
if (result_array == NULL) {
*iflag = -1;
return -1;
}
memcpy(fvec, PyArray_DATA(result_array), (*m)*sizeof(double));
}
else { /* iflag == i */
/* append row number to argument list and call row-based jacobian */
row = *iflag - 2;
if ((ob_row = PyInt_FromLong((long)row)) == NULL) {
*iflag = -1;
return -1;
}
newargs = PySequence_Concat( ob_row, multipack_extra_arguments);
Py_DECREF(ob_row);
if (newargs == NULL) {
PyErr_SetString(minpack_error, "Internal error constructing argument list.");
*iflag = -1;
return -1;
}
result_array = (PyArrayObject *)call_python_function(multipack_python_jacobian, *n, x, newargs, 2, minpack_error);
if (result_array == NULL) {
Py_DECREF(newargs);
*iflag = -1;
return -1;
}
memcpy(fjrow, PyArray_DATA(result_array), (*n)*sizeof(double));
}
Py_DECREF(result_array);
return 0;
}
static char doc_hybrd[] = "[x,infodict,info] = _hybrd(fun, x0, args, full_output, xtol, maxfev, ml, mu, epsfcn, factor, diag)";
static PyObject *minpack_hybrd(PyObject *dummy, PyObject *args) {
PyObject *fcn, *x0, *extra_args = NULL, *o_diag = NULL;
int full_output = 0, maxfev = -10, ml = -10, mu = -10;
double xtol = 1.49012e-8, epsfcn = 0.0, factor = 1.0e2;
int mode = 2, nprint = 0, info, nfev, ldfjac;
npy_intp n,lr;
int n_int, lr_int; /* for casted storage to pass int into HYBRD */
double *x, *fvec, *diag, *fjac, *r, *qtf;
PyArrayObject *ap_x = NULL, *ap_fvec = NULL;
PyArrayObject *ap_fjac = NULL, *ap_r = NULL, *ap_qtf = NULL;
PyArrayObject *ap_diag = NULL;
npy_intp dims[2];
int allocated = 0;
double *wa = NULL;
STORE_VARS(); /* Define storage variables for global variables. */
if (!PyArg_ParseTuple(args, "OO|OidiiiddO", &fcn, &x0, &extra_args, &full_output, &xtol, &maxfev, &ml, &mu, &epsfcn, &factor, &o_diag)) return NULL;
INIT_FUNC(fcn,extra_args,minpack_error);
/* Initial input vector */
ap_x = (PyArrayObject *)PyArray_ContiguousFromObject(x0, NPY_DOUBLE, 1, 1);
if (ap_x == NULL) goto fail;
x = (double *) PyArray_DATA(ap_x);
n = PyArray_DIMS(ap_x)[0];
lr = n * (n + 1) / 2;
if (ml < 0) ml = n-1;
if (mu < 0) mu = n-1;
if (maxfev < 0) maxfev = 200*(n+1);
/* Setup array to hold the function evaluations */
ap_fvec = (PyArrayObject *)call_python_function(fcn, n, x, extra_args, 1, minpack_error);
if (ap_fvec == NULL) goto fail;
fvec = (double *) PyArray_DATA(ap_fvec);
if (PyArray_NDIM(ap_fvec) == 0)
n = 1;
else if (PyArray_DIMS(ap_fvec)[0] < n)
n = PyArray_DIMS(ap_fvec)[0];
SET_DIAG(ap_diag,o_diag,mode);
dims[0] = n; dims[1] = n;
ap_r = (PyArrayObject *)PyArray_SimpleNew(1,&lr,NPY_DOUBLE);
ap_qtf = (PyArrayObject *)PyArray_SimpleNew(1,&n,NPY_DOUBLE);
ap_fjac = (PyArrayObject *)PyArray_SimpleNew(2,dims,NPY_DOUBLE);
if (ap_r == NULL || ap_qtf == NULL || ap_fjac ==NULL) goto fail;
r = (double *) PyArray_DATA(ap_r);
qtf = (double *) PyArray_DATA(ap_qtf);
fjac = (double *) PyArray_DATA(ap_fjac);
ldfjac = dims[1];
if ((wa = malloc(4*n * sizeof(double)))==NULL) {
PyErr_NoMemory();
goto fail;
}
allocated = 1;
/* Call the underlying FORTRAN routines. */
n_int = n; lr_int = lr; /* cast/store/pass into HYBRD */
HYBRD(raw_multipack_calling_function, &n_int, x, fvec, &xtol, &maxfev, &ml, &mu, &epsfcn, diag, &mode, &factor, &nprint, &info, &nfev, fjac, &ldfjac, r, &lr_int, qtf, wa, wa+n, wa+2*n, wa+3*n);
RESTORE_FUNC();
if (info < 0) goto fail; /* Python Terminated */
free(wa);
Py_DECREF(extra_args);
Py_DECREF(ap_diag);
if (full_output) {
return Py_BuildValue("N{s:N,s:i,s:N,s:N,s:N}i",PyArray_Return(ap_x),"fvec",PyArray_Return(ap_fvec),"nfev",nfev,"fjac",PyArray_Return(ap_fjac),"r",PyArray_Return(ap_r),"qtf",PyArray_Return(ap_qtf),info);
}
else {
Py_DECREF(ap_fvec);
Py_DECREF(ap_fjac);
Py_DECREF(ap_r);
Py_DECREF(ap_qtf);
return Py_BuildValue("Ni",PyArray_Return(ap_x),info);
}
fail:
RESTORE_FUNC();
Py_XDECREF(extra_args);
Py_XDECREF(ap_x);
Py_XDECREF(ap_fvec);
Py_XDECREF(ap_diag);
Py_XDECREF(ap_fjac);
Py_XDECREF(ap_r);
Py_XDECREF(ap_qtf);
if (allocated) free(wa);
return NULL;
}
static char doc_hybrj[] = "[x,infodict,info] = _hybrj(fun, Dfun, x0, args, full_output, col_deriv, xtol, maxfev, factor, diag)";
static PyObject *minpack_hybrj(PyObject *dummy, PyObject *args) {
PyObject *fcn, *Dfun, *x0, *extra_args = NULL, *o_diag = NULL;
int full_output = 0, maxfev = -10, col_deriv = 1;
double xtol = 1.49012e-8, factor = 1.0e2;
int mode = 2, nprint = 0, info, nfev, njev, ldfjac;
npy_intp n, lr;
int n_int, lr_int;
double *x, *fvec, *diag, *fjac, *r, *qtf;
PyArrayObject *ap_x = NULL, *ap_fvec = NULL;
PyArrayObject *ap_fjac = NULL, *ap_r = NULL, *ap_qtf = NULL;
PyArrayObject *ap_diag = NULL;
npy_intp dims[2];
int allocated = 0;
double *wa = NULL;
STORE_VARS();
if (!PyArg_ParseTuple(args, "OOO|OiididO", &fcn, &Dfun, &x0, &extra_args, &full_output, &col_deriv, &xtol, &maxfev, &factor, &o_diag)) return NULL;
INIT_JAC_FUNC(fcn,Dfun,extra_args,col_deriv,minpack_error);
/* Initial input vector */
ap_x = (PyArrayObject *)PyArray_ContiguousFromObject(x0, NPY_DOUBLE, 1, 1);
if (ap_x == NULL) goto fail;
x = (double *) PyArray_DATA(ap_x);
n = PyArray_DIMS(ap_x)[0];
lr = n * (n + 1) / 2;
if (maxfev < 0) maxfev = 100*(n+1);
/* Setup array to hold the function evaluations */
ap_fvec = (PyArrayObject *)call_python_function(fcn, n, x, extra_args, 1, minpack_error);
if (ap_fvec == NULL) goto fail;
fvec = (double *) PyArray_DATA(ap_fvec);
if (PyArray_NDIM(ap_fvec) == 0)
n = 1;
else if (PyArray_DIMS(ap_fvec)[0] < n)
n = PyArray_DIMS(ap_fvec)[0];
SET_DIAG(ap_diag,o_diag,mode);
dims[0] = n; dims[1] = n;
ap_r = (PyArrayObject *)PyArray_SimpleNew(1,&lr,NPY_DOUBLE);
ap_qtf = (PyArrayObject *)PyArray_SimpleNew(1,&n,NPY_DOUBLE);
ap_fjac = (PyArrayObject *)PyArray_SimpleNew(2,dims,NPY_DOUBLE);
if (ap_r == NULL || ap_qtf == NULL || ap_fjac ==NULL) goto fail;
r = (double *) PyArray_DATA(ap_r);
qtf = (double *) PyArray_DATA(ap_qtf);
fjac = (double *) PyArray_DATA(ap_fjac);
ldfjac = dims[1];
if ((wa = malloc(4*n * sizeof(double)))==NULL) {
PyErr_NoMemory();
goto fail;
}
allocated = 1;
/* Call the underlying FORTRAN routines. */
n_int = n; lr_int = lr; /* cast/store/pass into HYBRJ */
HYBRJ(jac_multipack_calling_function, &n_int, x, fvec, fjac, &ldfjac, &xtol, &maxfev, diag, &mode, &factor, &nprint, &info, &nfev, &njev, r, &lr_int, qtf, wa, wa+n, wa+2*n, wa+3*n);
RESTORE_JAC_FUNC();
if (info < 0) goto fail; /* Python Terminated */
free(wa);
Py_DECREF(extra_args);
Py_DECREF(ap_diag);
if (full_output) {
return Py_BuildValue("N{s:N,s:i,s:i,s:N,s:N,s:N}i",PyArray_Return(ap_x),"fvec",PyArray_Return(ap_fvec),"nfev",nfev,"njev",njev,"fjac",PyArray_Return(ap_fjac),"r",PyArray_Return(ap_r),"qtf",PyArray_Return(ap_qtf),info);
}
else {
Py_DECREF(ap_fvec);
Py_DECREF(ap_fjac);
Py_DECREF(ap_r);
Py_DECREF(ap_qtf);
return Py_BuildValue("Ni",PyArray_Return(ap_x),info);
}
fail:
RESTORE_JAC_FUNC();
Py_XDECREF(extra_args);
Py_XDECREF(ap_x);
Py_XDECREF(ap_fvec);
Py_XDECREF(ap_fjac);
Py_XDECREF(ap_diag);
Py_XDECREF(ap_r);
Py_XDECREF(ap_qtf);
if (allocated) free(wa);
return NULL;
}
/************************ Levenberg-Marquardt *******************/
static char doc_lmdif[] = "[x,infodict,info] = _lmdif(fun, x0, args, full_output, ftol, xtol, gtol, maxfev, epsfcn, factor, diag)";
static PyObject *minpack_lmdif(PyObject *dummy, PyObject *args) {
PyObject *fcn, *x0, *extra_args = NULL, *o_diag = NULL;
int full_output = 0, maxfev = -10;
double xtol = 1.49012e-8, ftol = 1.49012e-8;
double gtol = 0.0, epsfcn = 0.0, factor = 1.0e2;
int m, mode = 2, nprint = 0, info = 0, nfev, ldfjac, *ipvt;
npy_intp n;
int n_int; /* for casted storage to pass int into LMDIF */
double *x, *fvec, *diag, *fjac, *qtf;
PyArrayObject *ap_x = NULL, *ap_fvec = NULL;
PyArrayObject *ap_fjac = NULL, *ap_ipvt = NULL, *ap_qtf = NULL;
PyArrayObject *ap_diag = NULL;
npy_intp dims[2];
int allocated = 0;
double *wa = NULL;
STORE_VARS();
if (!PyArg_ParseTuple(args, "OO|OidddiddO", &fcn, &x0, &extra_args, &full_output, &ftol, &xtol, >ol, &maxfev, &epsfcn, &factor, &o_diag)) return NULL;
INIT_FUNC(fcn,extra_args,minpack_error);
/* Initial input vector */
ap_x = (PyArrayObject *)PyArray_ContiguousFromObject(x0, NPY_DOUBLE, 1, 1);
if (ap_x == NULL) goto fail;
x = (double *) PyArray_DATA(ap_x);
n = PyArray_DIMS(ap_x)[0];
dims[0] = n;
SET_DIAG(ap_diag,o_diag,mode);
if (maxfev < 0) maxfev = 200*(n+1);
/* Setup array to hold the function evaluations and find it's size*/
ap_fvec = (PyArrayObject *)call_python_function(fcn, n, x, extra_args, 1, minpack_error);
if (ap_fvec == NULL) goto fail;
fvec = (double *) PyArray_DATA(ap_fvec);
m = (PyArray_NDIM(ap_fvec) > 0 ? PyArray_DIMS(ap_fvec)[0] : 1);
dims[0] = n; dims[1] = m;
ap_ipvt = (PyArrayObject *)PyArray_SimpleNew(1,&n,NPY_INT);
ap_qtf = (PyArrayObject *)PyArray_SimpleNew(1,&n,NPY_DOUBLE);
ap_fjac = (PyArrayObject *)PyArray_SimpleNew(2,dims,NPY_DOUBLE);
if (ap_ipvt == NULL || ap_qtf == NULL || ap_fjac ==NULL) goto fail;
ipvt = (int *) PyArray_DATA(ap_ipvt);
qtf = (double *) PyArray_DATA(ap_qtf);
fjac = (double *) PyArray_DATA(ap_fjac);
ldfjac = dims[1];
wa = (double *)malloc((3*n + m)* sizeof(double));
if (wa == NULL) {
PyErr_NoMemory();
goto fail;
}
allocated = 1;
/* Call the underlying FORTRAN routines. */
n_int = n; /* to provide int*-pointed storage for int argument of LMDIF */
LMDIF(raw_multipack_lm_function, &m, &n_int, x, fvec, &ftol, &xtol, >ol, &maxfev, &epsfcn, diag, &mode, &factor, &nprint, &info, &nfev, fjac, &ldfjac, ipvt, qtf, wa, wa+n, wa+2*n, wa+3*n);
RESTORE_FUNC();
if (info < 0) goto fail; /* Python error */
free(wa);
Py_DECREF(extra_args);
Py_DECREF(ap_diag);
if (full_output) {
return Py_BuildValue("N{s:N,s:i,s:N,s:N,s:N}i",PyArray_Return(ap_x),"fvec",PyArray_Return(ap_fvec),"nfev",nfev,"fjac",PyArray_Return(ap_fjac),"ipvt",PyArray_Return(ap_ipvt),"qtf",PyArray_Return(ap_qtf),info);
}
else {
Py_DECREF(ap_fvec);
Py_DECREF(ap_fjac);
Py_DECREF(ap_ipvt);
Py_DECREF(ap_qtf);
return Py_BuildValue("Ni",PyArray_Return(ap_x),info);
}
fail:
RESTORE_FUNC();
Py_XDECREF(extra_args);
Py_XDECREF(ap_x);
Py_XDECREF(ap_fvec);
Py_XDECREF(ap_fjac);
Py_XDECREF(ap_diag);
Py_XDECREF(ap_ipvt);
Py_XDECREF(ap_qtf);
if (allocated) free(wa);
return NULL;
}
static char doc_lmder[] = "[x,infodict,info] = _lmder(fun, Dfun, x0, args, full_output, col_deriv, ftol, xtol, gtol, maxfev, factor, diag)";
static PyObject *minpack_lmder(PyObject *dummy, PyObject *args) {
PyObject *fcn, *x0, *Dfun, *extra_args = NULL, *o_diag = NULL;
int full_output = 0, maxfev = -10, col_deriv = 1;
double xtol = 1.49012e-8, ftol = 1.49012e-8;
double gtol = 0.0, factor = 1.0e2;
int m, mode = 2, nprint = 0, info, nfev, njev, ldfjac, *ipvt;
npy_intp n;
int n_int;
double *x, *fvec, *diag, *fjac, *qtf;
PyArrayObject *ap_x = NULL, *ap_fvec = NULL;
PyArrayObject *ap_fjac = NULL, *ap_ipvt = NULL, *ap_qtf = NULL;
PyArrayObject *ap_diag = NULL;
npy_intp dims[2];
int allocated = 0;
double *wa = NULL;
STORE_VARS();
if (!PyArg_ParseTuple(args, "OOO|OiidddidO", &fcn, &Dfun, &x0, &extra_args, &full_output, &col_deriv, &ftol, &xtol, >ol, &maxfev, &factor, &o_diag)) return NULL;
INIT_JAC_FUNC(fcn,Dfun,extra_args,col_deriv,minpack_error);
/* Initial input vector */
ap_x = (PyArrayObject *)PyArray_ContiguousFromObject(x0, NPY_DOUBLE, 1, 1);
if (ap_x == NULL) goto fail;
x = (double *) PyArray_DATA(ap_x);
n = PyArray_DIMS(ap_x)[0];
if (maxfev < 0) maxfev = 100*(n+1);
/* Setup array to hold the function evaluations */
ap_fvec = (PyArrayObject *)call_python_function(fcn, n, x, extra_args, 1, minpack_error);
if (ap_fvec == NULL) goto fail;
fvec = (double *) PyArray_DATA(ap_fvec);
SET_DIAG(ap_diag,o_diag,mode);
m = (PyArray_NDIM(ap_fvec) > 0 ? PyArray_DIMS(ap_fvec)[0] : 1);
dims[0] = n; dims[1] = m;
ap_ipvt = (PyArrayObject *)PyArray_SimpleNew(1,&n,NPY_INT);
ap_qtf = (PyArrayObject *)PyArray_SimpleNew(1,&n,NPY_DOUBLE);
ap_fjac = (PyArrayObject *)PyArray_SimpleNew(2,dims,NPY_DOUBLE);
if (ap_ipvt == NULL || ap_qtf == NULL || ap_fjac ==NULL) goto fail;
ipvt = (int *) PyArray_DATA(ap_ipvt);
qtf = (double *) PyArray_DATA(ap_qtf);
fjac = (double *) PyArray_DATA(ap_fjac);
ldfjac = dims[1];
wa = (double *)malloc((3*n + m)* sizeof(double));
if (wa == NULL) {
PyErr_NoMemory();
goto fail;
}
allocated = 1;
/* Call the underlying FORTRAN routines. */
n_int = n;
LMDER(jac_multipack_lm_function, &m, &n_int, x, fvec, fjac, &ldfjac, &ftol, &xtol, >ol, &maxfev, diag, &mode, &factor, &nprint, &info, &nfev, &njev, ipvt, qtf, wa, wa+n, wa+2*n, wa+3*n);
RESTORE_JAC_FUNC();
if (info < 0) goto fail; /* Python error */
free(wa);
Py_DECREF(extra_args);
Py_DECREF(ap_diag);
if (full_output) {
return Py_BuildValue("N{s:N,s:i,s:i,s:N,s:N,s:N}i",PyArray_Return(ap_x),"fvec",PyArray_Return(ap_fvec),"nfev",nfev,"njev",njev,"fjac",PyArray_Return(ap_fjac),"ipvt",PyArray_Return(ap_ipvt),"qtf",PyArray_Return(ap_qtf),info);
}
else {
Py_DECREF(ap_fvec);
Py_DECREF(ap_fjac);
Py_DECREF(ap_ipvt);
Py_DECREF(ap_qtf);
return Py_BuildValue("Ni",PyArray_Return(ap_x),info);
}
fail:
RESTORE_JAC_FUNC();
Py_XDECREF(extra_args);
Py_XDECREF(ap_x);
Py_XDECREF(ap_fvec);
Py_XDECREF(ap_fjac);
Py_XDECREF(ap_diag);
Py_XDECREF(ap_ipvt);
Py_XDECREF(ap_qtf);
if (allocated) free(wa);
return NULL;
}
/** Check gradient function **/
static char doc_chkder[] = "_chkder(m,n,x,fvec,fjac,ldfjac,xp,fvecp,mode,err)";
static PyObject *minpack_chkder(PyObject *self, PyObject *args)
{
PyArrayObject *ap_fvecp = NULL, *ap_fjac = NULL, *ap_err = NULL;
PyArrayObject *ap_x = NULL, *ap_fvec = NULL, *ap_xp = NULL;
PyObject *o_x, *o_fvec, *o_fjac, *o_fvecp;
double *xp, *fvecp, *fjac, *fvec, *x;
double *err;
int mode, m, n, ldfjac;
if (!PyArg_ParseTuple(args,"iiOOOiO!OiO!",&m, &n, &o_x, &o_fvec, &o_fjac, &ldfjac, &PyArray_Type, (PyObject **)&ap_xp, &o_fvecp, &mode, &PyArray_Type, (PyObject **)&ap_err)) return NULL;
ap_x = (PyArrayObject *)PyArray_ContiguousFromObject(o_x,NPY_DOUBLE,1,1);
if (ap_x == NULL) goto fail;
if (n != PyArray_DIMS(ap_x)[0])
PYERR(minpack_error,"Input data array (x) must have length n");
x = (double *) PyArray_DATA(ap_x);
if (!PyArray_IS_C_CONTIGUOUS(ap_xp) || (PyArray_TYPE(ap_xp) != NPY_DOUBLE))
PYERR(minpack_error,"Seventh argument (xp) must be contiguous array of type Float64.");
if (mode == 1) {
fvec = NULL;
fjac = NULL;
xp = (double *)PyArray_DATA(ap_xp);
fvecp = NULL;
err = NULL;
CHKDER(&m, &n, x, fvec, fjac, &ldfjac, xp, fvecp, &mode, err);
}
else if (mode == 2) {
if (!PyArray_IS_C_CONTIGUOUS(ap_err) || (PyArray_TYPE(ap_err) != NPY_DOUBLE))
PYERR(minpack_error,"Last argument (err) must be contiguous array of type Float64.");
ap_fvec = (PyArrayObject *)PyArray_ContiguousFromObject(o_fvec,NPY_DOUBLE,1,1);
ap_fjac = (PyArrayObject *)PyArray_ContiguousFromObject(o_fjac,NPY_DOUBLE,2,2);
ap_fvecp = (PyArrayObject *)PyArray_ContiguousFromObject(o_fvecp,NPY_DOUBLE,1,1);
if (ap_fvec == NULL || ap_fjac == NULL || ap_fvecp == NULL) goto fail;
fvec = (double *)PyArray_DATA(ap_fvec);
fjac = (double *)PyArray_DATA(ap_fjac);
xp = (double *)PyArray_DATA(ap_xp);
fvecp = (double *)PyArray_DATA(ap_fvecp);
err = (double *)PyArray_DATA(ap_err);
CHKDER(&m, &n, x, fvec, fjac, &m, xp, fvecp, &mode, err);
Py_DECREF(ap_fvec);
Py_DECREF(ap_fjac);
Py_DECREF(ap_fvecp);
}
else
PYERR(minpack_error,"Invalid mode, must be 1 or 2.");
Py_DECREF(ap_x);
Py_INCREF(Py_None);
return Py_None;
fail:
Py_XDECREF(ap_fvec);
Py_XDECREF(ap_fjac);
Py_XDECREF(ap_fvecp);
Py_XDECREF(ap_x);
return NULL;
}