17 #include "tbci/vector.h"
18 #include "tbci/matrix.h"
19 #include "tbci/constants.h"
20 #include "tbci/solver/gauss_jordan.h"
44 #define DELTA(i,j) ((i==j)?1:0)
53 #if defined(SIGHANDLE) && defined(HAVE_SIGNALS)
54 volatile int sigint = 0;
56 void breakhandler (
int signum)
58 STD__ cerr <<
"\n SIGINT !\n";
62 STD__ cerr <<
"\n ... aborted due to too many ignored SIGINTs !\n";
65 else signal (signum, breakhandler);
81 if (value==0)
return 1;
82 if (n<(
int)ceil(
MATH__ sqrt(2*
log((
double)value)/
log((
double)basis))) || basis<=1)
return 0;
100 template <typename T>
119 STD__ cerr <<
"\n error: partial_del: derivate variable index out of range \n";
123 buf_plus2 =
x; buf_minus2 =
x; buf_plus2(mu) += hh*2; buf_minus2 -= hh*2;
124 a(0,0) = (-(*func)(buf_plus2,
p) + 8.0*(*func)(
buf_plus,
p)
125 - 8.0*(*func)(
buf_minus,
p) + (*func)(buf_minus2,
p)) / (hh*12.0);
135 buf_plus2 =
x; buf_minus2 =
x; buf_plus2(mu) += hh*2; buf_minus2 -= hh*2;
136 a(0,
i) = (-(*func)(buf_plus2,
p) + 8.0*(*func)(
buf_plus,
p)
137 - 8.0*(*func)(
buf_minus,
p) + (*func)(buf_minus2,
p)) / (hh*12.0);
144 a(j,
i) = (
a(j-1,
i)*fac -
a(j-1,
i-1))/(fac-1.0);
158 template <typename T>
171 template <typename T>
195 template <
typename T>
201 T getval () {
return value; }
202 minimum () { value = 1e38; }
205 INST(template <typename T>
class Vector friend TVector<T> grid_mins(
T (*func)(
const Vector<T>&,
const Vector<T>&),
Vector<T>&,
Vector<T>&,
Vector<T>&,
Vector<T>&,
Vector<T>&,
int,
char,
int,
nsList<minimum<T>,
T>& );)
206 template <typename T>
210 int res,
char v,
int anzmin,
215 int anzpar = pmin.
size();
217 minimum<T> min2; min2.koord.resize (anzpar);
224 bool overflow;
int k;
228 min = minima.setlast();
229 STD__ cerr <<
"Parameter space discretisation in process ...\n";
231 for (
REGISTER int j = 0; j < anzpar; j++)
237 for (
long int i = 0;
i <
len;
i++)
239 actval = chisquare (func, actpar, x, y, sigma);
240 if (actval < min->value)
242 if (v==
'y' || v==
'Y')
243 STD__ cerr <<
"Found new min " << actval <<
" at " << actpar <<
'\n';
247 if (anzmin > 1) minima.qsort ();
248 min = minima.setlast ();
250 min2.koord = actpar; min2.value = actval;
251 min = minima.sortin_r ( min2 );
254 if (!(
i % 1000))
STD__ cerr <<
i <<
'\r';
256 overflow =
true; k = 0;
257 while (overflow && (k < anzpar))
260 if (actpar(k) > plim(k)) actpar(k) =
pmin(k);
261 else overflow =
false;
265 if (sigint) { sigint--;
return 0; }
269 STD__ cerr <<
"\n ... done.\n\n";
270 absmin = minima.getfirst()->koord;
275 INST(template <typename T>
class Vector friend TVector<T> grid_min(
T (*func)(
const Vector<T>&,
const Vector<T>&),
Vector<T>&,
Vector<T>&,
Vector<T>&,
Vector<T>&,
Vector<T>&,
Vector<T>&,
long int,
int,
char);)
276 template <typename T>
281 long int res,
int max_it,
char v)
285 int plen=pmin.
size();
298 len=(
long int)
pow((
double)
res, plen);
301 STD__ cerr <<
"\n parameter space discretisation in progress ... \n ";
302 for (
REGISTER int j = 0; j < plen; j++)
304 for (
long int i=0;i<
len;i++)
308 STD__ cerr <<
"\n error: basis trafo failed\n";
312 for (
int l=0;l<
max_it;l++)
316 for (
int k=0;k<plen;k++)
318 for (
int j=0;j<
xlen;j++)
322 partial_del(func, p, arg, k,
x(j)*
fac,
err);
327 for (
int t=0; t<plen; t++)
332 a(t) +=
sqr(partial_del(func, p, arg, t,
x(u)*fac, err))/
336 for (
int q=0; q<plen; q++)
344 if ( i%1000==0 )
STD__ cerr << i <<
"\r";
346 if (sigint) { sigint--;
return 0; }
349 STD__ cerr <<
" \n done! \n\n\n";
353 INST(template <typename T>
class Vector friend T lev_mar(
Vector<T>&,
Vector<T>&,
Vector<T>&,
Vector<T>&,
Vector<T>&,
Vector<T>&,
T (*func)(
const Vector<T>&,
const Vector<T>&),
Vector<T>,
Vector<T>&,
T,
int,
char);)
354 template <typename T>
359 T tol,
int iter,
char ver)
368 int plen=param.
size();
375 chi=chisquare(func,param,x,y,sig);
376 STD__ cerr <<
" \n chi-squared with start parameters is: " <<
chi;
381 STD__ cerr <<
"\n initializing matrices and starting loop... \n";
387 for (
int k=0;k<plen;k++)
388 for (
int l=0;l<plen;l++)
390 for (
int i=0;i<
len;i++)
393 a(k,l)+=partial_del(func, param, (
Vector<T>&)arg, k,
x(i)*fac, err)*
394 partial_del(func, param, (
Vector<T>&)arg, l,
x(i)*fac, err) /(
sig(i)*
sig(i));
399 for (
int q=0; q<plen; q++)
401 for (
int i=0;i<
len;i++)
409 char exceed =
gaussj (a, b);
416 if ((param2(i) > phigh(i)) && (phigh(i)!=plow(i)) ) exceed = 1;
417 if ((param2(i) < plow(i)) && (phigh(i)!=plow(i)) ) exceed = 1;
420 chi_new = chisquare(func, param2, x, y, sig);
422 if ((chi_new >= chi) || (exceed == 1)) lambda *= lscale(0);
426 lambda*=(
T)1/lscale(1);
429 if (chi_new<chi_min) {
435 if (ver==
'y' || ver==
'Y')
436 STD__ cerr <<
"\n iteration " << j <<
" " << param <<
437 " " << param2 <<
" " << chi_new;
439 if (sigint) { sigint--; param = param_best;
return 1e38; }
442 if (j%10 == 0)
STD__ cerr <<
".";
444 }
while( (chi_dif >= tol) && (j < iter) );
446 STD__ cerr <<
"\n loop finished after " << j <<
" iterations with last delta chi: "
447 << chi_dif <<
"\n and chi-squared: " << chi_new <<
"\n";
452 template <typename T>
459 unsigned long i, ir, j, l, n;
482 if ( j<ir &&
x(j-1,pos) <
x(j,pos) ) j++;
483 if ( rra(pos)<
x(j-1,pos) ) {
const Vector< T > const Vector< T > const Vector< T > int mu
const Vector< T > const Vector< T > & x
const Vector< T > const Vector< T > const Vector< T > & p
const Vector< T > const Vector< T > const Vector< T > int T h
const Vector< T > Vector< T > Vector< T > Vector< T > Vector< T > Vector< T > & sigma
< calculates function values of func at position x with params param and return chisq with given y va...
const Vector< T > Vector< T > Vector< T > Vector< T > Vector< T > Vector< T > & pmin
TBCI__ cplx< T > log(const TBCI__ cplx< T > &z)
STD__ cerr<< "\n parameter space discretisation in progress ... \n ";factor(j)=(pmax(j)-pmin(j))/((T) res-(T) 1);for(long int i=0;i< len;i++){STD__ cerr<< "\n error: basis trafo failed\n";p(j)=factor(j)*index(j)+pmin(j);for(int l=0;l< max_it;l++){err=0.0;for(int k=0;k< plen;k++){for(int j=0;j< xlen;j++){arg(0)=x(j);b(k)+=(y(j)-(*func)(p, arg))/(sig(j)*sig(j))*partial_del(func, p, arg, k, x(j)*fac, err);}}err=0.0;for(int t=0;t< plen;t++){for(REGISTER int u=0;u< xlen;u++){arg(0)=x(u);a(t)+=sqr(partial_del(func, p, arg, t, x(u)*fac, err))/(sig(u)*sig(u));}}p(q)+=b(q)*lambda(q)/(a(q)+1e-30);}for(int q=0;q< xlen;q++){arg(0)=x(q);fval(q)=(*func)(p, arg);}chi=chisq(y, fval, sig);if(chi< min){min=chi;min_pos=p;}if(i%1000==0) STD__ cerr<< i<< "\r";}STD__ cerr<< " \n done! \n\n\n";return min_pos;}INST(template< typename T > class Vector friend T lev_mar(Vector< T > &, Vector< T > &, Vector< T > &, Vector< T > &, Vector< T > &, Vector< T > &, T(*func)(const Vector< T > &, const Vector< T > &), Vector< T >, Vector< T > &, T, int, char);) template< typename T > T lev_mar(Vector< T > &x, Vector< T > &y, Vector< T > &sig, Vector< T > ¶m, Vector< T > &plow, Vector< T > &phigh, T(*func)(const Vector< T > &, const Vector< T > &), Vector< T > lambda, Vector< T > &lscale, T tol, int iter, char ver)
NAMESPACE_TBCI char gaussj(Matrix< T > &a, Matrix< T > &b)
int basis_trafo(long int value, long int basis, Vector< T > &index)
T arg(const TBCI__ cplx< T > &c)
void hpsort(Matrix< T > &x, int pos)
cplx< T > sqr(const cplx< T > &c)
unsigned int columns() const
number of columns
long int Vector< T > & index
Vector< T > fval(x.size())
const Vector< T > Vector< T > & param
TVector< T > min_pos(pmin)
const Vector< T > Vector< T > Vector< T > Vector< T > Vector< T > & sig
unsigned long size() const
Vector< T > buf_minus(xlen)
Vector< T > buf_plus(xlen)
const Vector< T > const Vector< T > const Vector< T > int T T & err
STD__ cerr<< "\n error: partial_del: derivate variable index out of range \n";hh=h;buf_plus=x;buf_minus=x;buf_plus(mu)+=hh;buf_minus(mu)-=hh;a(0, 0)=((*func)(buf_plus, p)-(*func)(buf_minus, p))/(2.0 *hh);err=1.0e30;for(int i=1;i< 10;i++){hh/=SQRT2;buf_plus=x;buf_minus=x;buf_plus(mu)+=hh;buf_minus(mu)-=hh;a(0, i)=((*func)(buf_plus, p)-(*func)(buf_minus, p))/(2.0 *hh);fac=2.0;for(REGISTER int j=1;j<=i;j++){a(j, i)=(a(j-1, i)*fac-a(j-1, i-1))/(fac-1.0);fac *=2.0;errt=MAX(MATH__ fabs(a(j, i)-a(j-1, i)), MATH__ fabs(a(j, i)-a(j-1, i-1)));if(errt<=err){err=errt;ans=a(j, i);}}if(MATH__ fabs(a(i, i)-a(i-1, i-1)) >=MATH__ fabs(2.0 *err)) break;}return ans;}template< typename T > T chisq(Vector< T > &data, Vector< T > &func, Vector< T > &sig)
Temporary Base Class Idiom: Class TVector is used for temporary variables.
const Vector< T > Vector< T > Vector< T > Vector< T > Vector< T > Vector< T > Vector< T > Vector< T > & lambda
const Vector< T > Vector< T > Vector< T > Vector< T > Vector< T > Vector< T > Vector< T > & pmax
const Vector< T > Vector< T > Vector< T > Vector< T > & y
const Vector< T > Vector< T > Vector< T > Vector< T > Vector< T > Vector< T > Vector< T > Vector< T > long int res
A numerically sortable List.
NAMESPACE_TBCI void do_nothing(T dummy)
const unsigned TMatrix< T > const Matrix< T > * a
const Vector< T > Vector< T > Vector< T > Vector< T > Vector< T > Vector< T > Vector< T > Vector< T > long int int max_it
const Vector< T > Vector< T > Vector< T > Vector< T > Vector< T > Vector< T > Vector< T > Vector< T > long int int char v
< find minimun of func on grid with resolution res
TBCI__ cplx< T > pow(const TBCI__ cplx< T > &z, const double n)
unsigned int rows() const
number of rows