37 #include <iomanip.hpp>
62 #if defined(UNIXKLUDGE) || \
63 ((defined(__SUN__) || defined(__GNU__)) && !defined(__GNUDOS__))
67 #if defined(__HP__) || defined(linux)
68 extern "C" void onintr(
int k);
70 extern "C" int onintr(
int* k);
82 #include <iostream.hxx>
110 const int&
J,
long int&
ifn,
const double&
crit1,
121 double mylinmin(
const double& fret,
const double& Phi_i,
const dvector& theta1,
128 const double& b,
double& ap,
const double& bp);
131 double min(
const double&,
const double&);
132 double max(
const double&,
const double&);
138 #define FREEALL free_vector(xi,1,n);free_vector(h,1,n);free_vector(g,1,n);
151 if (disp_inited == 0)
181 cerr <<
" Improving fbest old fbest = " <<
fbest <<
"\n" <<
182 " new fbest = " << fret <<
"\n";
201 ad_printf(
"Gradient magnitude criterion satisfied\n");
202 ad_printf(
"%d variables; iteration %ld; function evaluation %ld\n",
204 ad_printf(
"Function value %le; maximum gradient component mag %le\n",
217 if (this->
frp_flag==1)
goto label800;
218 if (this->
frp_flag==2)
goto label2000;
219 if (this->
frp_flag==3)
goto label3000;
221 #if defined(__SUN__) && !defined(__GNUDOS__) && !defined(_MSC_VER)
225 signal(SIGINT, (SIG_PF)&
onintr);
228 #if (defined(__GNU_) && !defined(__GNUDOS__)) || defined(UNIXKLUDGE)
229 signal(SIGINT, &onintr);
254 #if !defined (__WAT32__) && !defined (_MSC_VER)
258 ad_printf(
"%d variables; iteration %ld; function evaluation %ld\n",
260 ad_printf(
"Function value %le; maximum gradient component mag %le\n",
267 for (
int j=1;
j<=n;
j++)
281 #if ((defined(__SUN__) || defined(__GNU__)) && !defined(__GNUDOS__)) \
282 || defined(UNIXKLUDGE)
288 int c = toupper(
getch());
299 if ( c ==
'Q' || c ==
'N' )
308 ad_printf(
"%d variables; iteration %ld; function evaluation %ld\n",
310 ad_printf(
"Function value %le; maximum gradient component mag %le\n",
328 if (this->
lin_flag ==0)
goto label10;
347 for (
int i=1; i<=9; i++)
349 (*funval)[i]= (*funval)[i+1];
353 (*funval)[10] = fret;
362 ad_printf(
"%d variables; iteration %ld; function evaluation %ld\n",
364 ad_printf(
"Function value %le; maximum gradient component mag %le\n",
414 ad_printf(
"Maximum number of function evaluations exceeded\n");
415 ad_printf(
"%d variables; iteration %ld; function evaluation %ld\n",
417 ad_printf(
"Function value %le; maximum gradient component mag %le\n",
431 #if !defined (__WAT32__) && !defined (_MSC_VER)
435 ad_printf(
"%d variables; iteration %ld; function evaluation %ld\n",
437 ad_printf(
"Function value %le; maximum gradient component mag %le\n",
454 this->
dgg=this->gg=0.0;
456 for (
int j=1;
j<=n;
j++)
458 this->gg += g[
j]*g[
j];
460 this->
dgg += (xi[
j]+g[
j])*xi[
j];
470 for (
int j=1;
j<=n;
j++)
473 xi[
j]=h[
j]=g[
j]+this->
gam*h[
j];
479 cerr <<
"Too many iterations in FRPRMN\n";
503 *(cs.
theta) = theta1;
536 cerr <<
"doing extrapolation\n";
572 cerr <<
"We have a right bracket\n";
614 theta=theta+cs.
rho_i* *(cs.
d) ;
626 const int& _J,
long int&
ifn,
const double&
crit1,
627 int&
int_flag,
const double& _rho_1,
const double& Psi_2,
const dvector& g1)
629 double& fret = (
double&)_fret;
630 double&
rho_1 = (
double&)_rho_1;
637 static double rho_star;
642 #ifdef CUBIC_INTERPOLATION
643 static double gamma1;
646 if (int_flag ==1)
goto label200;
651 gamma=left_bracket_gradient*
d;
652 #ifdef CUBIC_INTERPOLATION
653 gamma1=right_bracket_gradient*
d;
658 #ifdef CUBIC_INTERPOLATION
659 cout <<
"f'(x) " << gamma <<
" "
660 <<
"f'(y) " << gamma1 <<
endl;
663 cout <<
"f(x) " << left_bracket_value <<
" "
664 <<
"f(y) " << right_bracket_value <<
endl;
666 cout <<
"x " << left_bracket <<
" "
667 <<
"y " << right_bracket <<
endl;
676 #ifdef CUBIC_INTERPOLATION
678 left_bracket_value,right_bracket_value,gamma,gamma1);
680 double step=gamma*rho_0*rho_0/
685 if (step<.25*width)step=.25*width;
686 if (step>.75*width)step=.75*width;
687 rho_star=left_bracket+step;
692 if (rho_star < left_bracket )
694 cout <<
" rho_star out of range ..value changed\n";
697 if (rho_star > right_bracket)
699 cout <<
" rho_star out of range ..value changed\n";
703 if (rho_star <= rho_min)
721 if (Psi_2 < left_bracket_value && (d*g1) < 0)
724 cout <<
" Before interpolation -- new left side\n";
728 left_bracket =rho_star;
729 left_bracket_value=Psi_2;
730 left_bracket_gradient=g1;
733 cout <<
" After interpolation -- new left side\n";
740 cout <<
" Before interpolation -- new right side\n";
743 right_bracket =rho_star;
744 right_bracket_value=Psi_2;
745 right_bracket_gradient=g1;
749 cout <<
" After interpolation -- new right side\n";
757 double cos_theta=d*g1/(
norm(d)*
norm(g1));
759 cerr <<
" The cosine of angle between the search direction and \n"
760 " the gradient is " << cos_theta <<
"\n";
763 if (cos_theta > crit1
764 && (right_bracket-left_bracket)> 1.e-10)
773 if (cos_theta < -crit1 && (right_bracket-left_bracket)> 1.e-10)
779 if (cos_theta < crit1)
781 cerr <<
" Leaving interpolation routine because"
782 " the cosine of angle between the \n search direction and "
783 " the gradient is < crit1 = " << crit1 <<
"\n";
801 const int& _ifnex,
int&
ext_flag,
const double& _rho_1,
const double& rf,
805 int&
ifnex = (
int&)_ifnex;
806 double&
rho_1 = (
double&)_rho_1;
812 if (ext_flag==1)
goto label1500;
830 if (Psi_1 >= left_bracket_value || d*g1 >0)
goto label2000;
831 left_bracket_value=
Psi_1;
832 left_bracket_gradient=g1;
839 right_bracket_value=
Psi_1;
840 right_bracket_gradient=g1;
854 ii.fill_seqadd(one,one);
857 cout <<
"lb " << setprecision(4) << setw(12) << left_bracket
858 <<
"rb " << setprecision(4) << setw(12) << right_bracket
859 << setprecision(4) << setw(12) << theta <<
"\n";
868 const double& bb,
double& ap,
const double& bp)
873 M(1,1)=1;
M(2,1)=0;
M(3,1)=1;
M(4,1)=0;
875 M(1,2)=u;
M(2,2)=1;
M(3,2)=v;
M(4,2)=1;
877 for (
int i=3;i<=4;i++)
880 M(2,i)=(i-1)*
M(1,i-1);
882 M(4,i)=(i-1)*
M(3,i-1);
904 if (y<0)
return (u+v)/2.;
910 if (y<0)
return (u+v)/2.;
911 q=-.5*(b-
sqrt(b*b-4*a*c));
917 double sgn1=b+2*a*x1;
936 #undef CUBIC_INTERPOLATION
1009 const int & _ireturn)
1011 double& f = (
double&)_f;
1017 static double fsave;
1018 static double s, f1, f2,
g2, xsave;
1024 if (ireturn == 4 )
goto label4;
1025 else if (ireturn == 5)
goto label5;
1030 cout <<
"\n Enter index (1 ... "<< n <<
") of derivative to check.";
1031 cout <<
" To check all derivatives, enter 0: ";
1035 cout <<
"\n Checking all derivatives. Press X to terminate checking.\n";
1044 cout <<
" Enter step size (to quit derivative checker, enter 0): ";
1047 "\n X Function Analytical Finite Diff; Index\n";
1051 for (i=n1; i<=n2; i++)
1070 ad_printf(
" %12.5e %12.5e %12.5e %12.5e ; %5d \n",
1071 x(i), f,
g(i), g2, i);
void fmmdisp(const dvector &x, const dvector &g, const int &nvar, int scroll_flag, int noprintx)
Description not yet available.
double right_bracket_value
void derch(const double &_f, const dvector &_x, const dvector &_gg, int n, const int &_ireturn)
Description not yet available.
Description not yet available.
~fmmc()
Description not yet available.
Description not yet available.
Vector of double precision numbers.
unsigned char screenwidth
int indexmin() const
Get minimum valid index.
double left_bracket_value
void fmin(const double &f, const dvector &p, const dvector &gg)
Description not yet available.
void clrscr()
Author: David Fournier Copyright (c) 2008-2012 Regents of the University of California.
fmmc(const int &n)
Description not yet available.
double do_interpolate(const double &fret, const double &left_bracket, double &left_bracket_value, const dvector &left_bracket_gradient, double &right_bracket, const double &right_bracket_value, dvector &right_bracket_gradient, const dvector &theta, const dvector &d, const int &J, long int &ifn, const double &crit1, int &int_flag, const double &rho_1, const double &Psi_2, const dvector &g1)
Description not yet available.
df1_two_variable fabs(const df1_two_variable &x)
unsigned char screenheight
double norm(const d3_array &a)
Return computed norm value of a.
double Phi(const dvector &)
prnstream & endl(prnstream &)
void gotoxy(int x, int y)
Array of integers(int) with indexes from index_min to indexmax.
d3_array sqrt(const d3_array &arr3)
Author: David Fournier Copyright (c) 2008-2012 Regents of the University of California.
int indexmax() const
Get maximum valid index.
double mylinmin(const double &fret, const double &Phi_i, const dvector &theta1, const dvector &q_i, fmmc &cs)
version of mylinmin which uses the deviative to help bracket the minimum
Author: David Fournier Copyright (c) 2008-2012 Regents of the University of California.
Description not yet available.
unsigned int size() const
Get number of elements in array.
double cubic_interpolation(const double &u, const double &v, const double &a, const double &b, double &ap, const double &bp)
Description not yet available.
void gettextinfo(struct text_info *r)
void do_extrapolate(const double &left_bracket, const double &left_bracket_value, dvector &left_bracket_gradient, const double &right_bracket, double &right_bracket_value, const dvector &right_bracket_gradient, const dvector &theta, dvector &d, const int &J, const double &rho_0, long int &ifn, const int &ifnex, int &ext_flag, const double &rho_1, const double &rf, const dvector &g1)
Description not yet available.
dvector * left_bracket_gradient
dvector * right_bracket_gradient
void bracket_report(const dvector &theta, const double &left_bracket, double &right_bracket, const dvector &d)
Description not yet available.
df1_one_variable inv(const df1_one_variable &x)
int ad_printf(FILE *stream, const char *format, Args...args)
d3_array pow(const d3_array &m, int e)
Description not yet available.