ADMB Documentation  11.1.1916
 All Classes Files Functions Variables Typedefs Friends Defines
fvar_fn1.cpp
Go to the documentation of this file.
00001 /*
00002  * $Id: fvar_fn1.cpp 1718 2014-03-03 21:53:08Z johnoel $
00003  *
00004  * Author: David Fournier
00005  * Copyright (c) 2008-2012 Regents of the University of California
00006  */
00011 #include "fvar.hpp"
00012 
00013 #include <stdio.h>
00014 #include <math.h>
00015 
00016 
00017 void gradfree(dlink *);
00018 
00019 //extern prevariable * FRETURN;
00020 //extern int RETURN_INDEX;
00021 //extern dlist * GRAD_LIST;          //js
00022 //extern grad_stack  * GRAD_STACK1;
00023 
00028 prevariable& exp(const prevariable& v1)
00029     {
00030       if (++gradient_structure::RETURN_PTR > gradient_structure::MAX_RETURN)
00031         gradient_structure::RETURN_PTR = gradient_structure::MIN_RETURN;
00032       double tmp=::exp(v1.v->x);
00033       gradient_structure::RETURN_PTR->v->x=tmp;
00034       gradient_structure::GRAD_STACK1->set_gradient_stack(default_evaluation,
00035         &(gradient_structure::RETURN_PTR->v->x), &(v1.v->x),tmp);
00036       return(*gradient_structure::RETURN_PTR);
00037     }
00038 
00043 prevariable& atan(const prevariable& v1)
00044     {
00045       if (++gradient_structure::RETURN_PTR > gradient_structure::MAX_RETURN)
00046         gradient_structure::RETURN_PTR = gradient_structure::MIN_RETURN;
00047       gradient_structure::RETURN_PTR->v->x= ::atan(v1.v->x);
00048       gradient_structure::GRAD_STACK1->set_gradient_stack(default_evaluation,
00049         &(gradient_structure::RETURN_PTR->v->x),
00050         &(v1.v->x),1./(1.+v1.v->x * v1.v->x) );
00051       return(*gradient_structure::RETURN_PTR);
00052     }
00053 
00058 prevariable& ldexp(const prevariable& v1, const int& exponent)
00059     {
00060       if (++gradient_structure::RETURN_PTR > gradient_structure::MAX_RETURN)
00061         gradient_structure::RETURN_PTR = gradient_structure::MIN_RETURN;
00062       gradient_structure::RETURN_PTR->v->x=::ldexp(v1.v->x, exponent);
00063       gradient_structure::GRAD_STACK1->set_gradient_stack(default_evaluation,
00064         &(gradient_structure::RETURN_PTR->v->x), &(v1.v->x),pow(2.0,exponent));
00065       return(*gradient_structure::RETURN_PTR);
00066     }
00067 
00072 prevariable& sqrt(const prevariable& v1)
00073     {
00074       double tmp=v1.v->x;
00075       if (tmp==0.0)
00076       {
00077         cerr << "Attempting to take the derivative of sqrt(prevariable x)"
00078          " at x=0\n";
00079         ad_exit(1);
00080       }
00081       tmp=::sqrt(tmp);
00082       if (++gradient_structure::RETURN_PTR > gradient_structure::MAX_RETURN)
00083         gradient_structure::RETURN_PTR = gradient_structure::MIN_RETURN;
00084       gradient_structure::RETURN_PTR->v->x=tmp;
00085       gradient_structure::GRAD_STACK1->set_gradient_stack(default_evaluation,
00086         &(gradient_structure::RETURN_PTR->v->x), &(v1.v->x),1./(2.*tmp));
00087       return(*gradient_structure::RETURN_PTR);
00088     }
00089 
00094 prevariable& sqr(const prevariable& v1)
00095     {
00096       double tmp=v1.v->x;
00097       if (tmp==0.0)
00098       {
00099         cerr << "Attempting to take the derivative of sqrt(prevariable x)"
00100          " at x=0\n";
00101         ad_exit(1);
00102       }
00103       tmp=::sqrt(tmp);
00104       if (++gradient_structure::RETURN_PTR > gradient_structure::MAX_RETURN)
00105         gradient_structure::RETURN_PTR = gradient_structure::MIN_RETURN;
00106       gradient_structure::RETURN_PTR->v->x=tmp;
00107       gradient_structure::GRAD_STACK1->set_gradient_stack(default_evaluation,
00108         &(gradient_structure::RETURN_PTR->v->x), &(v1.v->x),1./(2.*tmp));
00109       return(*gradient_structure::RETURN_PTR);
00110     }
00111 
00116 prevariable& tan(const prevariable& v1)
00117     {
00118       double t = ::tan(v1.v->x);
00119       if (++gradient_structure::RETURN_PTR > gradient_structure::MAX_RETURN)
00120         gradient_structure::RETURN_PTR = gradient_structure::MIN_RETURN;
00121       gradient_structure::RETURN_PTR->v->x= t;
00122       gradient_structure::GRAD_STACK1->set_gradient_stack(default_evaluation,
00123         &(gradient_structure::RETURN_PTR->v->x), &(v1.v->x), 1+t*t);
00124       return(*gradient_structure::RETURN_PTR);
00125     }
00126 
00131 prevariable& tanh(const prevariable& v1)
00132     {
00133       double t = ::tanh(v1.v->x);
00134       if (++gradient_structure::RETURN_PTR > gradient_structure::MAX_RETURN)
00135         gradient_structure::RETURN_PTR = gradient_structure::MIN_RETURN;
00136       gradient_structure::RETURN_PTR->v->x= t;
00137       gradient_structure::GRAD_STACK1->set_gradient_stack(default_evaluation,
00138         &(gradient_structure::RETURN_PTR->v->x), &(v1.v->x), 1-t*t);
00139       return(*gradient_structure::RETURN_PTR);
00140     }
00141 
00146 prevariable& acos(const prevariable& v1)
00147     {
00148       if (++gradient_structure::RETURN_PTR > gradient_structure::MAX_RETURN)
00149         gradient_structure::RETURN_PTR = gradient_structure::MIN_RETURN;
00150       gradient_structure::RETURN_PTR->v->x=::acos(v1.v->x);
00151       gradient_structure::GRAD_STACK1->set_gradient_stack(default_evaluation,
00152         &(gradient_structure::RETURN_PTR->v->x),
00153         &(v1.v->x),-1./::sqrt(1.- v1.v->x * v1.v->x));
00154       return(*gradient_structure::RETURN_PTR);
00155     }
00156 
00161 prevariable& asin(const prevariable& v1)
00162     {
00163       if (++gradient_structure::RETURN_PTR > gradient_structure::MAX_RETURN)
00164         gradient_structure::RETURN_PTR = gradient_structure::MIN_RETURN;
00165       gradient_structure::RETURN_PTR->v->x=::asin(v1.v->x);
00166       gradient_structure::GRAD_STACK1->set_gradient_stack(default_evaluation,
00167         &(gradient_structure::RETURN_PTR->v->x),
00168         &(v1.v->x),1./::sqrt(1.- v1.v->x * v1.v->x));
00169       return(*gradient_structure::RETURN_PTR);
00170     }
00171 
00176 prevariable& pow(const prevariable& v1, const prevariable& v2)
00177     {
00178       if (++gradient_structure::RETURN_PTR > gradient_structure::MAX_RETURN)
00179         gradient_structure::RETURN_PTR = gradient_structure::MIN_RETURN;
00180       double x=::pow(v1.v->x,(v2.v->x)-1);
00181       double y=x* v1.v->x;
00182       gradient_structure::RETURN_PTR->v->x=y;
00183       gradient_structure::GRAD_STACK1->set_gradient_stack(default_evaluation,
00184         &(gradient_structure::RETURN_PTR->v->x),
00185         &(v1.v->x), v2.v->x * x  ,&(v2.v->x),
00186         y * ::log(v1.v->x));
00187       return(*gradient_structure::RETURN_PTR);
00188     }
00189 
00194 prevariable& pow( CGNU_DOUBLE u, const prevariable& v1)
00195     {
00196       if (++gradient_structure::RETURN_PTR > gradient_structure::MAX_RETURN)
00197         gradient_structure::RETURN_PTR = gradient_structure::MIN_RETURN;
00198       double y=::pow(u,(v1.v->x));
00199 
00200       gradient_structure::RETURN_PTR->v->x=y;
00201       gradient_structure::GRAD_STACK1->set_gradient_stack(default_evaluation,
00202         &(gradient_structure::RETURN_PTR->v->x), &(v1.v->x), y * ::log(u));
00203 
00204       return(*gradient_structure::RETURN_PTR);
00205     }
00206 
00211 prevariable& sinh(const prevariable& v1)
00212     {
00213       if (++gradient_structure::RETURN_PTR > gradient_structure::MAX_RETURN)
00214         gradient_structure::RETURN_PTR = gradient_structure::MIN_RETURN;
00215       gradient_structure::RETURN_PTR->v->x=::sinh(v1.v->x);
00216       gradient_structure::GRAD_STACK1->set_gradient_stack(default_evaluation,
00217         &(gradient_structure::RETURN_PTR->v->x), &(v1.v->x),::cosh(v1.v->x));
00218       return(*gradient_structure::RETURN_PTR);
00219     }
00220 
00225 prevariable& cosh(const prevariable& v1)
00226     {
00227       if (++gradient_structure::RETURN_PTR > gradient_structure::MAX_RETURN)
00228         gradient_structure::RETURN_PTR = gradient_structure::MIN_RETURN;
00229       gradient_structure::RETURN_PTR->v->x=::cosh(v1.v->x);
00230       gradient_structure::GRAD_STACK1->set_gradient_stack(default_evaluation,
00231         &(gradient_structure::RETURN_PTR->v->x), &(v1.v->x),::sinh(v1.v->x));
00232       return(*gradient_structure::RETURN_PTR);
00233     }
00234 
00239 prevariable& atan2(const prevariable& v1, const prevariable& v2)
00240 {
00241   if (value(v1) == 0 && value(v2) == 0)
00242   {
00243     cerr << "Error: The ADMB function \"atan2(y, x)\" is undefined "
00244     "for y and x equal zero.\n";
00245     ad_exit(1);
00246   }
00247   if (value(v1) == 0 && value(v2) > 0)
00248   {
00249     return atan(v1/v2);
00250   }
00251   dvariable x = (sqrt(v2 * v2 + v1 * v1) - v2)/v1;
00252   return atan(x) * 2.0;
00253 }
00254 
00259 prevariable& atan2(const prevariable& v1, CGNU_DOUBLE v2)
00260 {
00261   if (value(v1) == 0 && v2 == 0)
00262   {
00263     cerr << "Error: The ADMB function \"atan2(y, x)\" is undefined "
00264     "for y and x equal zero.\n";
00265     ad_exit(1);
00266   }
00267   if (value(v1) == 0 && v2 > 0)
00268   {
00269     return atan(v1/v2);
00270   }
00271   dvariable x = (sqrt(v2 * v2 + v1 * v1) - v2)/v1;
00272   return atan(x) * 2.0;
00273 }
00274 
00279 prevariable& atan2( CGNU_DOUBLE v1, const prevariable& v2)
00280 {
00281   if (v1 == 0 && value(v2) == 0)
00282   {
00283     cerr << "Error: The ADMB function \"atan2(y, x)\" is undefined "
00284     "for y and x equal zero.\n";
00285     ad_exit(1);
00286   }
00287   if (v1 == 0 && value(v2) > 0)
00288   {
00289     return atan(v1/v2);
00290   }
00291   dvariable x = (sqrt(v2 * v2 + v1 * v1) - v2)/v1;
00292   return atan(x) * 2.0;
00293 }