25#include "tbci/basics.h"
26#include "tbci/vector.h"
27#include "tbci/matrix.h"
28#include "tbci/constants.h"
29#include "tbci/solver/gauss_jordan.h"
46# pragma interface "LM_fit.h"
53#define DELTA(i,j) ((i==j)?1:0)
63 int Basis_Trafo (
long int,
long int,
Vector<T>&);)
74 if (value == 0)
return 1;
114 STD__ cerr <<
"\n error: partial_del: derivate variable index out of range \n";
118 buf_plus2 =
x; buf_minus2 =
x; buf_plus2(
mu) += hh*2; buf_minus2 -= hh*2;
119 a(0,0) = (-(*func)(buf_plus2,
p) + 8.0*(*func)(
buf_plus,
p)
120 - 8.0*(*func)(
buf_minus,
p) + (*func)(buf_minus2,
p)) / (hh*12.0);
125 for (
int i = 1;
i <
NTAB;
i++) {
129 buf_plus2 =
x; buf_minus2 =
x; buf_plus2(
mu) += hh*2; buf_minus2 -= hh*2;
130 a(0,
i) = (-(*func)(buf_plus2,
p) + 8.0*(*func)(
buf_plus,
p)
131 - 8.0*(*func)(
buf_minus,
p) + (*func)(buf_minus2,
p)) / (hh*12.0);
151INST(template <typename T>
class Vector friend \
167INST(template <typename T>
class Vector friend \
180 int anzpkt =
x.size();
184 wfvaldiff = (z(
i) - (*func)(param,
arg)) / sigma(
i);
185 chi +=
sqr (wfvaldiff);
191INST(template <typename T>
class Vector friend \
196 long int,
int,
char);)
203 long int res,
int max_it,
char v)
207 int plen=pmin.
size();
221 for (
int k=0;k<
xlen;k++) {
arg(0)=
x(k);
arg(1)=
y(k); fval(k)=(*func)(
p,
arg); }
222 min=Chisq(z, fval, sig);
223 STD__ cout <<
"\n parameter space discretisation with " << len <<
" points in progress ... \n ";
224 for (
REGISTER int j = 0; j < plen; j++)
225 factor(j) = (pmax(j) - pmin(j)) / ((
T)
res - (
T)1);
226 for (
long int i=0;
i<len;
i++) {
229 STD__ cerr <<
"\n error: basis trafo failed\n";
231 p(j) = factor(j) *
index(j) + pmin(j);
234 for (
int l=0; l<max_it; l++) {
237 for (
int k=0; k<plen; k++) {
238 for (
int j=0; j<
xlen; j++) {
240 b(k)+=(z(j)-(*func)(
p,
arg))/(sig(j)*sig(j))*
246 for (
int k=0; k<plen; k++) {
253 for (
int k=0; k<plen; k++)
258 for (
int l=0; l<
xlen; l++) {
arg(0)=
x(l);
arg(1)=
y(l); fval(l)=(*func)(
p,
arg); }
260 chi=Chisq(z, fval, sig);
261 if ( chi<
min ) {
min=chi; min_pos=
p; }
262 if (
i%1000==0 )
STD__ cerr <<
i <<
"\t\t\t\r";
264 if (sigint) { sigint--;
return 0; }
272INST(template <typename T>
class Vector friend \
282 T tol,
int iter,
char ver)
290 int plen = param.
size();
297 chi = Chisquare_2D<T> (func, param,
x,
y, z, sig);
298 STD__ cout <<
" \n chi-squared with start parameters is: " << chi;
303 STD__ cout <<
"\n initializing matrices and starting loop... \n";
309 for (k=0; k<plen; k++)
310 for (
int l=0; l<plen; l++) {
311 for (
int i=0;
i<len;
i++) {
313 a(k,l) += Partial_Del(func, param,
arg, k, param(k)*
fac,
err)*
314 Partial_Del(func, param,
arg, l, param(l)*
fac,
err) /(sig(
i)*sig(
i));
321 a(k,l) *= ((
T)1+lambda(k)*(
T)
DELTA(k,l));
324 for (k=0;k<plen;k++) {
325 for (
int i=0;
i<len;
i++) {
327 b(k,0) += (z(
i)-(*func)(param,
arg))/(sig(
i)*sig(
i))*
328 Partial_Del(func, param,
arg, k, param(k)*
fac,
err);
337 for (
REGISTER int f=0; f<plen; f++) {
338 if ((param2(f) > phigh(f)) && (phigh(f)!=plow(f)) ) exceed = 1;
339 if ((param2(f) < plow(f)) && (phigh(f)!=plow(f)) ) exceed = 1;
342 chi_new = Chisquare_2D <T> (func, param2,
x,
y, z, sig);
344 if ((chi_new >= chi) || (exceed == 1)) lambda *= lscale(0);
348 lambda*=(
T)1/lscale(1);
351 if (chi_new<chi_min) {
357 if (ver==
'y' || ver==
'Y')
358 STD__ cerr <<
"\n iteration " << j <<
" " << param <<
359 " " << param2 <<
" " << chi_new;
361 if (sigint) { sigint--; param = param_best;
return 1e38; }
364 if (j%10 == 0)
STD__ cerr <<
".";
366 }
while( (chi_dif >= tol) && (j < iter) );
368 STD__ cout <<
"\n loop finished after " << j <<
" iterations with last delta chi: "
369 << chi_dif <<
"\n and chi-squared: " << chi_new <<
STD__ endl;
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)
#define DELTA(i, j)
the kronecker delta
Temporary Base Class Idiom: Class TVector is used for temporary variables.
unsigned long size() const
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