ADMB Documentation  11.5.3197
 All Classes Files Functions Variables Typedefs Friends Defines
fvar_fn.cpp
Go to the documentation of this file.
00001 /*
00002  * $Id$
00003  *
00004  * Author: David Fournier
00005  * Copyright (c) 2008-2012 Regents of the University of California
00006  */
00011 #include "fvar.hpp"
00012 
00013 #ifdef __TURBOC__
00014   #pragma hdrstop
00015   #include <iostream.h>
00016   #include <iomanip.h>
00017 #endif
00018 
00019 #ifdef __ZTC__
00020   #include <iostream.hpp>
00021   #include <iomanip.hpp>
00022 #endif
00023 
00024 #include <stdio.h>
00025 #include <math.h>
00026 
00027 void gradfree(dlink *);
00028 
00033 prevariable& pow(const prevariable& v1, const double u)
00034     {
00035 #ifndef OPT_LIB
00036         if (v1.v->x == 0)
00037         {
00038           cerr << "Error -- base = 0 in function"
00039             " prevariable& pow(const prevariable& v1, const double u)" << endl;
00040         }
00041 #endif
00042       if (++gradient_structure::RETURN_PTR > gradient_structure::MAX_RETURN)
00043         gradient_structure::RETURN_PTR = gradient_structure::MIN_RETURN;
00044       double x,y;
00045       x=::pow(v1.v->x,u-1);
00046       y=x* v1.v->x;
00047       gradient_structure::RETURN_PTR->v->x=y;
00048       gradient_structure::GRAD_STACK1->set_gradient_stack(default_evaluation,
00049         &(gradient_structure::RETURN_PTR->v->x), &(v1.v->x), u * x );
00050       return(*gradient_structure::RETURN_PTR);
00051     }
00052 
00057  void prevariable::initialize(void)
00058  {
00059    *this = 0.0;
00060  }
00061 
00066  prevariable& sin(const prevariable& v1)
00067  {
00068    if (++gradient_structure::RETURN_PTR > gradient_structure::MAX_RETURN)
00069      gradient_structure::RETURN_PTR = gradient_structure::MIN_RETURN;
00070       gradient_structure::RETURN_PTR->v->x=::sin(v1.v->x);
00071    gradient_structure::GRAD_STACK1->set_gradient_stack(default_evaluation,
00072      &(gradient_structure::RETURN_PTR->v->x), &(v1.v->x), ::cos(v1.v->x) );
00073    return(*gradient_structure::RETURN_PTR);
00074  }
00075 
00080 prevariable& sigmoid(const prevariable& v1)
00081  {
00082    if (++gradient_structure::RETURN_PTR > gradient_structure::MAX_RETURN)
00083      gradient_structure::RETURN_PTR = gradient_structure::MIN_RETURN;
00084    double tmp=v1.v->x;
00085 
00086    //gradient_structure::RETURN_PTR->v->x=atan(tmp)/1.5708;
00087       gradient_structure::RETURN_PTR->v->x=::atan(tmp)/2.8;
00088 
00089    gradient_structure::GRAD_STACK1->set_gradient_stack(default_evaluation,
00090      &(gradient_structure::RETURN_PTR->v->x), &(v1.v->x),
00091  //                1./((1.+tmp*tmp)*1.5708));
00092                    1./((1.+tmp*tmp)*2.8));
00093    return(*gradient_structure::RETURN_PTR);
00094  }
00095 
00096 
00105 prevariable& fabs(const prevariable& v1)
00106     {
00107       double tmp;
00108 
00109      if (++gradient_structure::RETURN_PTR > gradient_structure::MAX_RETURN)
00110        gradient_structure::RETURN_PTR = gradient_structure::MIN_RETURN;
00111       tmp=v1.v->x;
00112       (gradient_structure::RETURN_PTR->v)->x=fabs(tmp);
00113       if (tmp >=0)
00114       {
00115         gradient_structure::GRAD_STACK1->set_gradient_stack(
00116           default_evaluation,&(gradient_structure::RETURN_PTR->v->x),
00117           &(v1.v->x),1);
00118       }
00119       else
00120       {
00121         gradient_structure::GRAD_STACK1->set_gradient_stack(
00122           default_evaluation,&(gradient_structure::RETURN_PTR->v->x),
00123           &(v1.v->x),-1);
00124       }
00125       return(*gradient_structure::RETURN_PTR);
00126     }
00127 
00135 prevariable& sfabs(const prevariable& v1)
00136     {
00137       #define cutoff 0.001 //  h
00138       #define cutoff2 0.000001
00139       double a = 2./cutoff; // 2.0/h
00140       double b = 1./cutoff2; // 1.0/(h*h)
00141 
00142       double tmp,tmp1;
00143 
00144       if (++gradient_structure::RETURN_PTR > gradient_structure::MAX_RETURN)
00145         gradient_structure::RETURN_PTR = gradient_structure::MIN_RETURN;
00146 
00147       tmp1=v1.v->x;
00148       tmp=::fabs(tmp1);
00149       if (tmp > cutoff)
00150       {
00151         gradient_structure::RETURN_PTR->v->x=tmp;
00152         if (tmp1 >=0)
00153         {
00154           gradient_structure::GRAD_STACK1->set_gradient_stack(
00155             default_evaluation,&(gradient_structure::RETURN_PTR->v->x),
00156             &(v1.v->x),1);
00157         }
00158         else
00159         {
00160           gradient_structure::GRAD_STACK1->set_gradient_stack(
00161             default_evaluation,&(gradient_structure::RETURN_PTR->v->x),
00162             &(v1.v->x),-1);
00163         }
00164       }
00165       else
00166       {
00167         double t2 = tmp * tmp;
00168         if (tmp1 >=0)
00169         {
00170           gradient_structure::RETURN_PTR->v->x=t2*(a-b*tmp1);
00171           gradient_structure::GRAD_STACK1->set_gradient_stack(
00172             default_evaluation,&(gradient_structure::RETURN_PTR->v->x),
00173             &(v1.v->x),tmp*(2*a-3*b*tmp1) );
00174         }
00175         else
00176         {
00177           gradient_structure::RETURN_PTR->v->x=t2*(a+b*tmp1);
00178           gradient_structure::GRAD_STACK1->set_gradient_stack(
00179             default_evaluation,&(gradient_structure::RETURN_PTR->v->x),
00180             &(v1.v->x),tmp1*(2*a+3*b*tmp1));
00181         }
00182       }
00183       return(*gradient_structure::RETURN_PTR);
00184     }
00185 
00190 prevariable& cos(const prevariable& v1)
00191     {
00192       if (++gradient_structure::RETURN_PTR > gradient_structure::MAX_RETURN)
00193         gradient_structure::RETURN_PTR = gradient_structure::MIN_RETURN;
00194       gradient_structure::RETURN_PTR->v->x=::cos(v1.v->x);
00195       gradient_structure::GRAD_STACK1->set_gradient_stack(
00196         default_evaluation,&(gradient_structure::RETURN_PTR->v->x),
00197         &(v1.v->x),-::sin(v1.v->x) );
00198       return(*gradient_structure::RETURN_PTR);
00199     }
00200 
00205 prevariable& log(const prevariable& v1)
00206     {
00207       if (++gradient_structure::RETURN_PTR > gradient_structure::MAX_RETURN)
00208         gradient_structure::RETURN_PTR = gradient_structure::MIN_RETURN;
00209       gradient_structure::RETURN_PTR->v->x=::log(v1.v->x);
00210       gradient_structure::GRAD_STACK1->set_gradient_stack(
00211         default_evaluation,&(gradient_structure::RETURN_PTR->v->x),
00212         &(v1.v->x),1./(v1.v->x) );
00213       return(*gradient_structure::RETURN_PTR);
00214     }
00215 
00220 prevariable& log10(const prevariable& v1)
00221     {
00222       if (++gradient_structure::RETURN_PTR > gradient_structure::MAX_RETURN)
00223         gradient_structure::RETURN_PTR = gradient_structure::MIN_RETURN;
00224       gradient_structure::RETURN_PTR->v->x=::log10(v1.v->x);
00225       gradient_structure::GRAD_STACK1->set_gradient_stack(default_evaluation,
00226         &(gradient_structure::RETURN_PTR->v->x),&(v1.v->x),
00227         1./(v1.v->x)/2.3025851);
00228       return(*gradient_structure::RETURN_PTR);
00229     }