diff options
Diffstat (limited to 'lib/Numeric/GSL/gsl-aux.c')
-rw-r--r-- | lib/Numeric/GSL/gsl-aux.c | 121 |
1 files changed, 118 insertions, 3 deletions
diff --git a/lib/Numeric/GSL/gsl-aux.c b/lib/Numeric/GSL/gsl-aux.c index 80c23fc..c6b052f 100644 --- a/lib/Numeric/GSL/gsl-aux.c +++ b/lib/Numeric/GSL/gsl-aux.c | |||
@@ -511,17 +511,17 @@ int minimizeWithDeriv(int method, double f(int, double*), void df(int, double*, | |||
511 | 511 | ||
512 | //--------------------------------------------------------------- | 512 | //--------------------------------------------------------------- |
513 | 513 | ||
514 | typedef void TrawfunV(int, double*, double*); | 514 | typedef void TrawfunV(int, double*, int, double*); |
515 | 515 | ||
516 | int only_f_aux_root(const gsl_vector*x, void *pars, gsl_vector*y) { | 516 | int only_f_aux_root(const gsl_vector*x, void *pars, gsl_vector*y) { |
517 | TrawfunV * f = (TrawfunV*) pars; | 517 | TrawfunV * f = (TrawfunV*) pars; |
518 | double* p = (double*)calloc(x->size,sizeof(double)); | 518 | double* p = (double*)calloc(x->size,sizeof(double)); |
519 | double* q = (double*)calloc(x->size,sizeof(double)); | 519 | double* q = (double*)calloc(y->size,sizeof(double)); |
520 | int k; | 520 | int k; |
521 | for(k=0;k<x->size;k++) { | 521 | for(k=0;k<x->size;k++) { |
522 | p[k] = gsl_vector_get(x,k); | 522 | p[k] = gsl_vector_get(x,k); |
523 | } | 523 | } |
524 | f(x->size,p,q); | 524 | f(x->size,p,y->size,q); |
525 | for(k=0;k<y->size;k++) { | 525 | for(k=0;k<y->size;k++) { |
526 | gsl_vector_set(y,k,q[k]); | 526 | gsl_vector_set(y,k,q[k]); |
527 | } | 527 | } |
@@ -588,3 +588,118 @@ int root(int method, void f(int, double*, int, double*), | |||
588 | gsl_multiroot_fsolver_free(s); | 588 | gsl_multiroot_fsolver_free(s); |
589 | OK | 589 | OK |
590 | } | 590 | } |
591 | |||
592 | // working with the jacobian | ||
593 | |||
594 | typedef struct {int (*f)(int, double*, int, double *); int (*jf)(int, double*, int, int, double*);} Tfjf; | ||
595 | |||
596 | int f_aux_root(const gsl_vector*x, void *pars, gsl_vector*y) { | ||
597 | Tfjf * fjf = ((Tfjf*) pars); | ||
598 | double* p = (double*)calloc(x->size,sizeof(double)); | ||
599 | double* q = (double*)calloc(y->size,sizeof(double)); | ||
600 | int k; | ||
601 | for(k=0;k<x->size;k++) { | ||
602 | p[k] = gsl_vector_get(x,k); | ||
603 | } | ||
604 | (fjf->f)(x->size,p,y->size,q); | ||
605 | for(k=0;k<y->size;k++) { | ||
606 | gsl_vector_set(y,k,q[k]); | ||
607 | } | ||
608 | free(p); | ||
609 | free(q); | ||
610 | return 0; | ||
611 | } | ||
612 | |||
613 | int jf_aux_root(const gsl_vector * x, void * pars, gsl_matrix * jac) { | ||
614 | Tfjf * fjf = ((Tfjf*) pars); | ||
615 | double* p = (double*)calloc(x->size,sizeof(double)); | ||
616 | double* q = (double*)calloc((x->size)*(x->size),sizeof(double)); | ||
617 | int i,j,k; | ||
618 | for(k=0;k<x->size;k++) { | ||
619 | p[k] = gsl_vector_get(x,k); | ||
620 | } | ||
621 | |||
622 | (fjf->jf)(x->size,p,x->size,x->size,q); | ||
623 | |||
624 | k=0; | ||
625 | for(i=0;i<x->size;i++) { | ||
626 | for(j=0;j<x->size;j++){ | ||
627 | gsl_matrix_set(jac,i,j,q[k++]); | ||
628 | } | ||
629 | } | ||
630 | free(p); | ||
631 | free(q); | ||
632 | return 0; | ||
633 | } | ||
634 | |||
635 | int fjf_aux_root(const gsl_vector * x, void * pars, gsl_vector * f, gsl_matrix * g) { | ||
636 | f_aux_root(x,pars,f); | ||
637 | jf_aux_root(x,pars,g); | ||
638 | return 0; | ||
639 | } | ||
640 | |||
641 | int rootj(int method, int f(int, double*, int, double*), | ||
642 | int jac(int, double*, int, int, double*), | ||
643 | double epsabs, int maxit, | ||
644 | KRVEC(xi), RMAT(sol)) { | ||
645 | REQUIRES(solr == maxit && solc == 1+2*xin,BAD_SIZE); | ||
646 | DEBUGMSG("root_fjf"); | ||
647 | gsl_multiroot_function_fdf my_func; | ||
648 | // extract function from pars | ||
649 | my_func.f = f_aux_root; | ||
650 | my_func.df = jf_aux_root; | ||
651 | my_func.fdf = fjf_aux_root; | ||
652 | my_func.n = xin; | ||
653 | Tfjf stfjf; | ||
654 | stfjf.f = f; | ||
655 | stfjf.jf = jac; | ||
656 | my_func.params = &stfjf; | ||
657 | size_t iter = 0; | ||
658 | int status; | ||
659 | const gsl_multiroot_fdfsolver_type *T; | ||
660 | gsl_multiroot_fdfsolver *s; | ||
661 | // Starting point | ||
662 | KDVVIEW(xi); | ||
663 | switch(method) { | ||
664 | case 0 : {T = gsl_multiroot_fdfsolver_hybridsj;; break; } | ||
665 | case 1 : {T = gsl_multiroot_fdfsolver_hybridj; break; } | ||
666 | case 2 : {T = gsl_multiroot_fdfsolver_newton; break; } | ||
667 | case 3 : {T = gsl_multiroot_fdfsolver_gnewton; break; } | ||
668 | default: ERROR(BAD_CODE); | ||
669 | } | ||
670 | s = gsl_multiroot_fdfsolver_alloc (T, my_func.n); | ||
671 | |||
672 | gsl_multiroot_fdfsolver_set (s, &my_func, V(xi)); | ||
673 | |||
674 | do { | ||
675 | status = gsl_multiroot_fdfsolver_iterate (s); | ||
676 | |||
677 | solp[iter*solc+0] = iter; | ||
678 | |||
679 | int k; | ||
680 | for(k=0;k<xin;k++) { | ||
681 | solp[iter*solc+k+1] = gsl_vector_get(s->x,k); | ||
682 | } | ||
683 | for(k=xin;k<2*xin;k++) { | ||
684 | solp[iter*solc+k+1] = gsl_vector_get(s->f,k-xin); | ||
685 | } | ||
686 | |||
687 | iter++; | ||
688 | if (status) /* check if solver is stuck */ | ||
689 | break; | ||
690 | |||
691 | status = | ||
692 | gsl_multiroot_test_residual (s->f, epsabs); | ||
693 | } | ||
694 | while (status == GSL_CONTINUE && iter < maxit); | ||
695 | |||
696 | int i,j; | ||
697 | for (i=iter; i<solr; i++) { | ||
698 | solp[i*solc+0] = iter; | ||
699 | for(j=1;j<solc;j++) { | ||
700 | solp[i*solc+j]=0.; | ||
701 | } | ||
702 | } | ||
703 | gsl_multiroot_fdfsolver_free(s); | ||
704 | OK | ||
705 | } \ No newline at end of file | ||