11 std::size_t N = x.size();
12 std::vector<double> r, xp;
13 std::vector<std::vector<double> > J(N, std::vector<double>(N, 0));
14 std::vector<double> r0 =
call(x);
16 for (std::size_t i = 0; i < N; ++i)
23 for(std::size_t j = 0; j < N; ++j)
25 J[j][i] = (r[j]-r0[j])/epsilon;
50 *errstring=std::string(
"");
51 std::vector<double> f0,v,negative_f0;
52 std::vector<std::vector<double> > J;
54 while (iter==0 || fabs(error)>tol){
60 for (
unsigned int i = 0; i<f0.size(); i++){ negative_f0[i] *= -1;}
64 for (
unsigned int i = 0; i<v.size(); i++){ x0[i] += v[i];}
67 *errstring=std::string(
"reached maximum number of iterations");
87 double x, dx, fval=999;
91 while ((iter < 2 || fabs(fval) > ftol) && iter < maxiter)
94 dx = -fval/f->
deriv(x);
104 *errstring=std::string(
"reached maximum number of iterations");
125 double x1=0,x2=0,x3=0,y1=0,y2=0,
x,fval=999;
127 *errstring=std::string(
"");
129 if (fabs(dx)==0){ *errstring=std::string(
"dx cannot be zero");
return _HUGE;}
130 while ((iter<=2 || fabs(fval)>tol) && iter<maxiter)
132 if (iter==1){x1=x0;
x=x1;}
133 if (iter==2){x2=x0+dx;
x=x2;}
136 if (iter==1){y1=fval;}
139 double deltax = x2-x1;
140 if (fabs(deltax)<1e-14)
142 if (fabs(fval) < tol*10)
148 throw ValueError(
"Step is small but not solved to tolerance");
152 x3=x2-y2/(y2-y1)*(x2-x1);
158 *errstring=std::string(
"reached maximum number of iterations");
181 double x1=0,x2=0,x3=0,y1=0,y2=0,
x,fval=999;
183 *errstring=std::string(
"");
185 if (fabs(dx)==0){ *errstring=std::string(
"dx cannot be zero");
return _HUGE;}
186 while ((iter<=3 || fabs(fval)>tol) && iter<100)
188 if (iter==1){x1=x0;
x=x1;}
189 if (iter==2){x2=x0+dx;
x=x2;}
192 if (iter==1){y1=fval;}
196 x3=x2-y2/(y2-y1)*(x2-x1);
211 *errstring=std::string(
"reached maximum number of iterations");
235 double Brent(
FuncWrapper1D *f,
double a,
double b,
double macheps,
double t,
int maxiter, std::string *errstr)
238 *errstr=std::string(
"");
239 double fa,fb,c,fc,m,tol,d,e,p,q,s,r;
244 if (fabs(fb) < t) {
return b;}
246 throw ValueError(
format(
"Brent's method f(b) is NAN for b = %g",b).c_str());
248 if (fabs(fa) < t) {
return a;}
250 throw ValueError(
format(
"Brent's method f(a) is NAN for a = %g",a).c_str());
253 throw ValueError(
format(
"Inputs in Brent [%f,%f] do not bracket the root. Function values are [%f,%f]",a,b,fa,fb));
259 if (fabs(fc)<fabs(fb)){
271 tol=2*macheps*fabs(b)+t;
272 while (fabs(m)>tol && fb!=0){
274 if (fabs(e)<tol || fabs(fa) <= fabs(fb)){
290 p=s*(2*m*q*(q-r)-(b-a)*(r-1));
302 if (2*p<3*m*q-fabs(tol*q) || p<fabs(0.5*s*q)){
323 throw ValueError(
format(
"Brent's method f(t) is NAN for t = %g",b).c_str());
331 if (fabs(fc)<fabs(fb)){
341 tol=2*macheps*fabs(b)+t;
350 throw SolutionError(std::string(
"Brent's method reached maximum number of steps of %d ", maxiter));}
double BoundedSecant(FuncWrapper1D *f, double x0, double xmin, double xmax, double dx, double tol, int maxiter, std::string *errstring)
virtual double call(double)=0
std::vector< double > x(ncmax, 0)
std::vector< std::vector< double > > linsolve(std::vector< std::vector< double > > const &A, std::vector< std::vector< double > > const &B)
double Newton(FuncWrapper1D *f, double x0, double ftol, int maxiter, std::string *errstring)
double Brent(FuncWrapper1D *f, double a, double b, double macheps, double t, int maxiter, std::string *errstr)
virtual double deriv(double)
std::vector< double > NDNewtonRaphson_Jacobian(FuncWrapperND *f, std::vector< double > x0, double tol, int maxiter, std::string *errstring)
double Secant(FuncWrapper1D *f, double x0, double dx, double tol, int maxiter, std::string *errstring)
virtual std::vector< double > call(std::vector< double >)=0
virtual std::vector< std::vector< double > > Jacobian(std::vector< double >)