// This file copyright (C) 2006 Andres Jordan // and Simon Casassus // All rights reserved. There is no warranty. You are allowed to // redistribute this software/documentation under certain conditions. // For details, see the file COPYING in the PDL distribution. If this file // is separated from the PDL distribution, the copyright notice should be // included in the file. #include #include #include #include static SV* ext_funname1; static int ene; void DFF(int* n, double* x, double* vector); int my_f (const gsl_vector * v, void * params, gsl_vector * df); void DFF(int* n, double* xval, double* vector){ //this version tries just to get the output SV* funname; double* xpass; int i; int count; I32 ax ; pdl* px; SV* pxsv; pdl* pvector; SV* pvectorsv; int ndims; PDL_Long *pdims; dSP; ENTER; SAVETMPS; ndims = 1; pdims = (PDL_Long *) PDL->smalloc((STRLEN) ((ndims) * sizeof(*pdims)) ); pdims[0] = (PDL_Long) ene; PUSHMARK(SP); XPUSHs(sv_2mortal(newSVpv("PDL", 0))); PUTBACK; perl_call_method("initialize", G_SCALAR); SPAGAIN; pxsv = POPs; PUTBACK; px = PDL->SvPDLV(pxsv); PDL->converttype( &px, PDL_D, PDL_PERM ); PDL->children_changesoon(px,PDL_PARENTDIMSCHANGED|PDL_PARENTDATACHANGED); PDL->setdims (px,pdims,ndims); px->state &= ~PDL_NOMYDIMS; px->state |= PDL_ALLOCATED; PDL->changed(px,PDL_PARENTDIMSCHANGED|PDL_PARENTDATACHANGED,0); px->data = (void *) xval; /* get function name on the perl side */ funname = ext_funname1; PUSHMARK(SP); XPUSHs(pxsv); PUTBACK; count=call_sv(funname,G_SCALAR); SPAGAIN; SP -= count ; ax = (SP - PL_stack_base) + 1 ; if (count!=1) croak("error calling perl function\n"); /* recover output value */ pvectorsv = ST(0); pvector = PDL->SvPDLV(pvectorsv); PDL->make_physical(pvector); xpass = (double *) pvector->data; for(i=0;ix, 0), gsl_vector_get (s->x, 1), gsl_vector_get (s->f, 0), gsl_vector_get (s->f, 1)); return 1; } int fsolver (double *xfree, int nelem, double epsabs, int method) { gsl_multiroot_fsolver_type *T; gsl_multiroot_fsolver *s; int status; size_t i, iter = 0; size_t n = nelem; double p[1] = { nelem }; int iloop; // struct func_params p = {1.0, 10.0}; gsl_multiroot_function func = {&my_f, n, p}; gsl_vector *x = gsl_vector_alloc (n); for (iloop=0;iloopf, epsabs); } while (status == GSL_CONTINUE && iter < 1000); if (status) warn ("Final status = %s\n", gsl_strerror (status)); for (iloop=0;iloopx, iloop); } gsl_multiroot_fsolver_free (s); gsl_vector_free (x); return 0; }