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)
54volatile int sigint = 0;
56void 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;
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);
181 int anzpkt =
x.size();
186 wfvaldiff = (
y(
i) - (*func)(param,
arg)) / sigma(
i);
187 chi +=
sqr (wfvaldiff);
201 T getval () {
return value; }
202 minimum () { value = 1e38; }
205INST(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>& );)
210 int res,
char v,
int anzmin,
215 int anzpar = pmin.
size();
217 minimum<T> min2; min2.koord.resize (anzpar);
223 long int len = (
long int)
MATH__ pow ((
double)
res, 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++)
233 factor(j) = (pmax(j) - pmin(j)) / (
res - 1);
234 plim(j) = pmax(j) + 0.5 * factor(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))
259 actpar(k) += factor(k);
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;
275INST(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);)
281 long int res,
int max_it,
char v)
285 int plen=pmin.
size();
298 len=(
long int)
pow((
double)
res, plen);
299 for (
int s=0; s<
xlen; s++) {
arg(0)=
x(s); fval(s)=(*func)(
p,
arg); }
300 min=chisq(
y, fval, sig);
301 STD__ cerr <<
"\n parameter space discretisation in progress ... \n ";
302 for (
REGISTER int j = 0; j < plen; j++)
303 factor(j) = (pmax(j) - pmin(j)) / ((
T)
res - (
T)1);
304 for (
long int i=0;
i<len;
i++)
308 STD__ cerr <<
"\n error: basis trafo failed\n";
310 p(j) = factor(j) *
index(j) + pmin(j);
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++)
321 b(k)+=(
y(j)-(*func)(
p,
arg))/(sig(j)*sig(j))*
327 for (
int t=0; t<plen; t++)
336 for (
int q=0; q<plen; q++)
340 for (
int q=0; q<
xlen; q++) {
arg(0)=
x(q); fval(q)=(*func)(
p,
arg); }
342 chi=chisq(
y, fval, sig);
343 if ( chi<
min ) {
min=chi; min_pos=
p; }
344 if (
i%1000==0 )
STD__ cerr <<
i <<
"\r";
346 if (sigint) { sigint--;
return 0; }
349 STD__ cerr <<
" \n done! \n\n\n";
353INST(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);)
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++)
396 a(k,l)*=((
T)1+lambda(k)*(
T)
DELTA(k,l));
399 for (
int q=0; q<plen; q++)
401 for (
int i=0;
i<len;
i++)
411 for (
REGISTER int t=0; t<plen; t++) param2(t)=param(t)+
b(t,0);
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";
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 > const Vector< T > & p
long int Vector< T > & index
const Vector< T > const Vector< T > & x
Vector< T > buf_minus(xlen)
const Vector< T > const Vector< T > const Vector< T > int T h
const Vector< T > const Vector< T > const Vector< T > int T T & err
Vector< T > buf_plus(xlen)
Temporary Base Class Idiom: Class TVector is used for temporary variables.
unsigned long size() const
A numerically sortable List.
double fabs(const TBCI__ cplx< T > &c)
T arg(const TBCI__ cplx< T > &c)
TBCI__ cplx< T > pow(const TBCI__ cplx< T > &z, const double n)
cplx< T > sqr(const cplx< T > &c)
TBCI__ cplx< T > log(const TBCI__ cplx< T > &z)
NAMESPACE_END NAMESPACE_CSTD TBCI__ cplx< T > sqrt(const TBCI__ cplx< T > &z)
NAMESPACE_TBCI char gaussj(Matrix< T > &a, Matrix< T > &b)
const unsigned TMatrix< T > const Matrix< T > * a
const unsigned TMatrix< T > * res
NAMESPACE_TBCI void do_nothing(T dummy)
const Vector< T > const Vector< T > const Vector< T > & p
const Vector< T > const Vector< T > & x
Vector< T > buf_minus(xlen)
Vector< T > buf_plus(xlen)
int basis_trafo(long int value, long int basis, Vector< T > &index)