ADMB Documentation  11.5.3197
 All Classes Files Functions Variables Typedefs Friends Defines
fvar_op2.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 // file: fvar_ops.cpp
00012 // operators involving prevariables
00013 
00014 
00015 #include "fvar.hpp"
00016 
00017 #ifdef __TURBOC__
00018   #pragma hdrstop
00019   #include <iostream.h>
00020 #endif
00021 
00022 #ifdef __ZTC__
00023   #include <iostream.hpp>
00024 #endif
00025 
00026 #include <stdio.h>
00027 #include <math.h>
00028 
00029 
00030 void gradfree(dlink *);
00031 
00032 //extern prevariable * FRETURN;
00033 //extern int RETURN_INDEX;
00034 //extern dlist * GRAD_LIST; //js
00035 //extern grad_stack  * gradient_structure::GRAD_STACK1; //js
00036 
00041 prevariable& operator+(const double x, const prevariable& v2)
00042 {
00043   if (++gradient_structure::RETURN_PTR > gradient_structure::MAX_RETURN)
00044     gradient_structure::RETURN_PTR = gradient_structure::MIN_RETURN;
00045 
00046   gradient_structure::RETURN_PTR->v->x = x + (v2.v->x);
00047   gradient_structure::GRAD_STACK1->set_gradient_stack(default_evaluation1,
00048     &(gradient_structure::RETURN_PTR->v->x),&(v2.v->x));
00049   return(*gradient_structure::RETURN_PTR);
00050 }
00051 
00056 prevariable& operator+(const prevariable& v1, const double x)
00057 {
00058   if (++gradient_structure::RETURN_PTR > gradient_structure::MAX_RETURN)
00059     gradient_structure::RETURN_PTR = gradient_structure::MIN_RETURN;
00060 
00061   gradient_structure::RETURN_PTR->v->x=v1.v->x+ x;
00062   gradient_structure::GRAD_STACK1->set_gradient_stack(default_evaluation1,
00063     &(gradient_structure::RETURN_PTR->v->x),&(v1.v->x));
00064   return(*gradient_structure::RETURN_PTR);
00065 }
00066 
00071 prevariable& operator-(const prevariable& v1, const prevariable& v2)
00072 {
00073   if (++gradient_structure::RETURN_PTR > gradient_structure::MAX_RETURN)
00074     gradient_structure::RETURN_PTR = gradient_structure::MIN_RETURN;
00075   gradient_structure::RETURN_PTR->v->x=v1.v->x - v2.v->x;
00076   gradient_structure::GRAD_STACK1->set_gradient_stack(default_evaluation4m,
00077     &(gradient_structure::RETURN_PTR->v->x),&(v1.v->x),&(v2.v->x));
00078   return(*gradient_structure::RETURN_PTR);
00079 }
00080 
00085 prevariable& operator-(const prevariable& v1, const double x)
00086 {
00087   if (++gradient_structure::RETURN_PTR > gradient_structure::MAX_RETURN)
00088     gradient_structure::RETURN_PTR = gradient_structure::MIN_RETURN;
00089   gradient_structure::RETURN_PTR->v->x=v1.v->x - x;
00090   gradient_structure::GRAD_STACK1->set_gradient_stack(default_evaluation1,
00091     &(gradient_structure::RETURN_PTR->v->x), &(v1.v->x));
00092   return(*gradient_structure::RETURN_PTR);
00093 }
00094 
00099 prevariable& operator-(const double x, const prevariable& v2)
00100 {
00101   if (++gradient_structure::RETURN_PTR > gradient_structure::MAX_RETURN)
00102     gradient_structure::RETURN_PTR = gradient_structure::MIN_RETURN;
00103   gradient_structure::RETURN_PTR->v->x = x - v2.v->x;
00104   gradient_structure::GRAD_STACK1->set_gradient_stack(default_evaluation1m,
00105     &(gradient_structure::RETURN_PTR->v->x), &(v2.v->x));
00106   return(*gradient_structure::RETURN_PTR);
00107 }
00108 
00113 prevariable& operator-(const prevariable& v1)
00114 {
00115   if (++gradient_structure::RETURN_PTR > gradient_structure::MAX_RETURN)
00116     gradient_structure::RETURN_PTR = gradient_structure::MIN_RETURN;
00117   gradient_structure::RETURN_PTR->v->x = -(v1.v->x);
00118   gradient_structure::GRAD_STACK1->set_gradient_stack(default_evaluation1m,
00119     &(gradient_structure::RETURN_PTR->v->x),&(v1.v->x));
00120   return(*gradient_structure::RETURN_PTR);
00121 }
00122 
00127 prevariable& operator/(const prevariable& v1, const prevariable& v2)
00128 {
00129   if (++gradient_structure::RETURN_PTR > gradient_structure::MAX_RETURN)
00130     gradient_structure::RETURN_PTR = gradient_structure::MIN_RETURN;
00131   double x = 1 / v2.v->x;
00132   double y = v1.v->x * x;
00133   gradient_structure::RETURN_PTR->v->x = y;
00134   gradient_structure::GRAD_STACK1->set_gradient_stack(default_evaluation3,
00135      &(gradient_structure::RETURN_PTR->v->x),&(v1.v->x),x,&(v2.v->x),-y*x);
00136 
00137   return(*gradient_structure::RETURN_PTR);
00138 }
00139 
00144 prevariable& operator/(const double u, const prevariable& v2)
00145 {
00146   if (++gradient_structure::RETURN_PTR > gradient_structure::MAX_RETURN)
00147     gradient_structure::RETURN_PTR = gradient_structure::MIN_RETURN;
00148 
00149   double x = 1 / v2.v->x;
00150   double y = u * x;
00151   gradient_structure::RETURN_PTR->v->x = y;
00152 
00153   gradient_structure::GRAD_STACK1->set_gradient_stack(default_evaluation,
00154     &(gradient_structure::RETURN_PTR->v->x), &(v2.v->x),-y*x);
00155 
00156   return(*gradient_structure::RETURN_PTR);
00157 }
00158 
00163 prevariable& operator/(const prevariable& v1, const double u)
00164 {
00165   if (++gradient_structure::RETURN_PTR > gradient_structure::MAX_RETURN)
00166     gradient_structure::RETURN_PTR = gradient_structure::MIN_RETURN;
00167 
00168   double x = 1 / u;
00169   double y = v1.v->x * x;
00170 
00171   gradient_structure::RETURN_PTR->v->x = y;
00172 
00173   gradient_structure::GRAD_STACK1->set_gradient_stack(default_evaluation,
00174     &(gradient_structure::RETURN_PTR->v->x), &(v1.v->x),x);
00175 
00176   return(*gradient_structure::RETURN_PTR);
00177 }