summaryrefslogtreecommitdiff
path: root/lib/Numeric/GSL/gsl-aux.c
diff options
context:
space:
mode:
authorAlberto Ruiz <aruiz@um.es>2010-03-27 17:32:30 +0000
committerAlberto Ruiz <aruiz@um.es>2010-03-27 17:32:30 +0000
commitbd1de48eb723b792cad02ecd8f4434078552839b (patch)
treefc21dea76d55957b91a1fb6ee4a7e6273454f397 /lib/Numeric/GSL/gsl-aux.c
parentadc7249bbe8c6648fbe327dea2077ffb84195673 (diff)
interface to Levenberg-Marquardt
Diffstat (limited to 'lib/Numeric/GSL/gsl-aux.c')
-rw-r--r--lib/Numeric/GSL/gsl-aux.c97
1 files changed, 84 insertions, 13 deletions
diff --git a/lib/Numeric/GSL/gsl-aux.c b/lib/Numeric/GSL/gsl-aux.c
index 0fcde1c..f258fb1 100644
--- a/lib/Numeric/GSL/gsl-aux.c
+++ b/lib/Numeric/GSL/gsl-aux.c
@@ -23,6 +23,7 @@
23#include <gsl/gsl_rng.h> 23#include <gsl/gsl_rng.h>
24#include <gsl/gsl_randist.h> 24#include <gsl/gsl_randist.h>
25#include <gsl/gsl_odeiv.h> 25#include <gsl/gsl_odeiv.h>
26#include <gsl/gsl_multifit_nlin.h>
26#include <string.h> 27#include <string.h>
27#include <stdio.h> 28#include <stdio.h>
28 29
@@ -668,9 +669,10 @@ int root(int method, void f(int, double*, int, double*),
668 669
669// working with the jacobian 670// working with the jacobian
670 671
671typedef struct {int (*f)(int, double*, int, double *); int (*jf)(int, double*, int, int, double*);} Tfjf; 672typedef struct {int (*f)(int, double*, int, double *);
673 int (*jf)(int, double*, int, int, double*);} Tfjf;
672 674
673int f_aux_root(const gsl_vector*x, void *pars, gsl_vector*y) { 675int f_aux(const gsl_vector*x, void *pars, gsl_vector*y) {
674 Tfjf * fjf = ((Tfjf*) pars); 676 Tfjf * fjf = ((Tfjf*) pars);
675 double* p = (double*)calloc(x->size,sizeof(double)); 677 double* p = (double*)calloc(x->size,sizeof(double));
676 double* q = (double*)calloc(y->size,sizeof(double)); 678 double* q = (double*)calloc(y->size,sizeof(double));
@@ -687,20 +689,20 @@ int f_aux_root(const gsl_vector*x, void *pars, gsl_vector*y) {
687 return 0; 689 return 0;
688} 690}
689 691
690int jf_aux_root(const gsl_vector * x, void * pars, gsl_matrix * jac) { 692int jf_aux(const gsl_vector * x, void * pars, gsl_matrix * jac) {
691 Tfjf * fjf = ((Tfjf*) pars); 693 Tfjf * fjf = ((Tfjf*) pars);
692 double* p = (double*)calloc(x->size,sizeof(double)); 694 double* p = (double*)calloc(x->size,sizeof(double));
693 double* q = (double*)calloc((x->size)*(x->size),sizeof(double)); 695 double* q = (double*)calloc((jac->size1)*(jac->size2),sizeof(double));
694 int i,j,k; 696 int i,j,k;
695 for(k=0;k<x->size;k++) { 697 for(k=0;k<x->size;k++) {
696 p[k] = gsl_vector_get(x,k); 698 p[k] = gsl_vector_get(x,k);
697 } 699 }
698 700
699 (fjf->jf)(x->size,p,x->size,x->size,q); 701 (fjf->jf)(x->size,p,jac->size1,jac->size2,q);
700 702
701 k=0; 703 k=0;
702 for(i=0;i<x->size;i++) { 704 for(i=0;i<jac->size1;i++) {
703 for(j=0;j<x->size;j++){ 705 for(j=0;j<jac->size2;j++){
704 gsl_matrix_set(jac,i,j,q[k++]); 706 gsl_matrix_set(jac,i,j,q[k++]);
705 } 707 }
706 } 708 }
@@ -709,9 +711,9 @@ int jf_aux_root(const gsl_vector * x, void * pars, gsl_matrix * jac) {
709 return 0; 711 return 0;
710} 712}
711 713
712int fjf_aux_root(const gsl_vector * x, void * pars, gsl_vector * f, gsl_matrix * g) { 714int fjf_aux(const gsl_vector * x, void * pars, gsl_vector * f, gsl_matrix * g) {
713 f_aux_root(x,pars,f); 715 f_aux(x,pars,f);
714 jf_aux_root(x,pars,g); 716 jf_aux(x,pars,g);
715 return 0; 717 return 0;
716} 718}
717 719
@@ -723,9 +725,9 @@ int rootj(int method, int f(int, double*, int, double*),
723 DEBUGMSG("root_fjf"); 725 DEBUGMSG("root_fjf");
724 gsl_multiroot_function_fdf my_func; 726 gsl_multiroot_function_fdf my_func;
725 // extract function from pars 727 // extract function from pars
726 my_func.f = f_aux_root; 728 my_func.f = f_aux;
727 my_func.df = jf_aux_root; 729 my_func.df = jf_aux;
728 my_func.fdf = fjf_aux_root; 730 my_func.fdf = fjf_aux;
729 my_func.n = xin; 731 my_func.n = xin;
730 Tfjf stfjf; 732 Tfjf stfjf;
731 stfjf.f = f; 733 stfjf.f = f;
@@ -781,8 +783,77 @@ int rootj(int method, int f(int, double*, int, double*),
781 OK 783 OK
782} 784}
783 785
786//-------------- non linear least squares fitting -------------------
787
788int nlfit(int method, int f(int, double*, int, double*),
789 int jac(int, double*, int, int, double*),
790 double epsabs, double epsrel, int maxit, int p,
791 KRVEC(xi), RMAT(sol)) {
792 REQUIRES(solr == maxit && solc == 2+xin,BAD_SIZE);
793 DEBUGMSG("nlfit");
794 const gsl_multifit_fdfsolver_type *T;
795 gsl_multifit_fdfsolver *s;
796 gsl_multifit_function_fdf my_f;
797 // extract function from pars
798 my_f.f = f_aux;
799 my_f.df = jf_aux;
800 my_f.fdf = fjf_aux;
801 my_f.n = p;
802 my_f.p = xin; // !!!!
803 Tfjf stfjf;
804 stfjf.f = f;
805 stfjf.jf = jac;
806 my_f.params = &stfjf;
807 size_t iter = 0;
808 int status;
809
810 KDVVIEW(xi);
811 //DMVIEW(cov);
812
813 switch(method) {
814 case 0 : { T = gsl_multifit_fdfsolver_lmsder; break; }
815 default: ERROR(BAD_CODE);
816 }
817
818 s = gsl_multifit_fdfsolver_alloc (T, my_f.n, my_f.p);
819 gsl_multifit_fdfsolver_set (s, &my_f, V(xi));
820
821 do { status = gsl_multifit_fdfsolver_iterate (s);
822
823 solp[iter*solc+0] = iter+1;
824 solp[iter*solc+1] = gsl_blas_dnrm2 (s->f);
825
826 int k;
827 for(k=0;k<xin;k++) {
828 solp[iter*solc+k+2] = gsl_vector_get(s->x,k);
829 }
830
831 iter++;
832 if (status) /* check if solver is stuck */
833 break;
834
835 status = gsl_multifit_test_delta (s->dx, s->x, epsabs, epsrel);
836 }
837 while (status == GSL_CONTINUE && iter <= maxit);
838
839 int i,j;
840 for (i=iter; i<solr; i++) {
841 solp[i*solc+0] = iter;
842 for(j=1;j<solc;j++) {
843 solp[i*solc+j]=0.;
844 }
845 }
846
847 //gsl_multifit_covar (s->J, 0.0, M(cov));
848
849 gsl_multifit_fdfsolver_free (s);
850 OK
851}
852
853
784////////////////////////////////////////////////////// 854//////////////////////////////////////////////////////
785 855
856
786#define RAN(C,F) case C: { for(k=0;k<rn;k++) { rp[k]= F(gen); }; OK } 857#define RAN(C,F) case C: { for(k=0;k<rn;k++) { rp[k]= F(gen); }; OK }
787 858
788int random_vector(int seed, int code, RVEC(r)) { 859int random_vector(int seed, int code, RVEC(r)) {