ADMB Documentation  -a65f1c97
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
fvar_fn1.cpp
Go to the documentation of this file.
1 /*
2  * $Id$
3  *
4  * Author: David Fournier
5  * Copyright (c) 2008-2012 Regents of the University of California
6  */
11 #include "fvar.hpp"
12 
13 #include <stdio.h>
14 #include <cmath>
15 
23 {
24  double tmp;
25  //Avoid underflow for large negative values
26  // https://docs.microsoft.com/en-us/cpp/c-runtime-library/reference/exp-expf
27  if (v1.v->x <= -708.3964)
28  {
29  cout.flush();
30  tmp = 0.0;
31  }
32  else
33  {
34  tmp = std::exp(v1.v->x);
35  }
36 
37 #ifdef DIAG
38 
41  #if !defined(__SUNPRO_CC) && !(defined(_MSC_VER) && (_MSC_VER <= 1700))
42  if (!std::isfinite(tmp))
43  {
44  // cerr << "Error: Result of \"exp(prevariable(" << value(v1) << ")) = "
45  // << tmp << "\" is not finite.\n";
46  // ad_exit(1);
47  }
48  #endif
49 #endif
50 
52 
53  RETURN_PTR->v->x=tmp;
55  &(RETURN_PTR->v->x), &(v1.v->x),tmp);
56 
57  return *RETURN_PTR;
58 }
59 
65 {
67 
68  RETURN_PTR->v->x= ::atan(v1.v->x);
70  &(RETURN_PTR->v->x),
71  &(v1.v->x),1./(1.+v1.v->x * v1.v->x) );
72 
73  return *RETURN_PTR;
74 }
75 
80 prevariable& ldexp(const prevariable& v1, const int& exponent)
81 {
83 
84  RETURN_PTR->v->x=::ldexp(v1.v->x, exponent);
86  &(RETURN_PTR->v->x), &(v1.v->x),pow(2.0,exponent));
87 
88  return *RETURN_PTR;
89 }
90 
96 {
97  double tmp = v1.v->x;
98  if (tmp == 0.0)
99  {
100  cerr << "Attempting to take the derivative of sqrt(prevariable x)"
101  " at x=0\n";
102  ad_exit(1);
103  }
104  tmp = ::sqrt(tmp);
105 
107  RETURN_PTR->v->x=tmp;
109  &(RETURN_PTR->v->x), &(v1.v->x),1./(2.*tmp));
110 
111  return *RETURN_PTR;
112 }
113 
120 {
121 /*
122  double tmp=v1.v->x;
123  if (tmp==0.0)
124  {
125  cerr << "Attempting to take the derivative of sqrt(prevariable x)"
126  " at x=0\n";
127  ad_exit(1);
128  }
129  tmp=::sqrt(tmp);
130  if (++gradient_structure::RETURN_PTR > gradient_structure::MAX_RETURN)
131  gradient_structure::RETURN_PTR = gradient_structure::MIN_RETURN;
132  gradient_structure::RETURN_PTR->v->x=tmp;
133  gradient_structure::get()->GRAD_STACK1->set_gradient_stack(default_evaluation,
134  &(gradient_structure::RETURN_PTR->v->x), &(v1.v->x),1./(2.*tmp));
135  return(*gradient_structure::RETURN_PTR);
136 */
137  return pow(v1, 2.0);
138 }
139 
145 {
146  double t = ::tan(v1.v->x);
147 
149  RETURN_PTR->v->x= t;
151  &(RETURN_PTR->v->x), &(v1.v->x), 1+t*t);
152 
153  return *RETURN_PTR;
154 }
155 
161 {
162  double t = ::tanh(v1.v->x);
164 
165  RETURN_PTR->v->x= t;
167  &(RETURN_PTR->v->x), &(v1.v->x), 1.0-t*t);
168 
169  return *RETURN_PTR;
170 }
176 {
178 
179  RETURN_PTR->v->x=::acos(v1.v->x);
181  &(RETURN_PTR->v->x),
182  &(v1.v->x),-1./::sqrt(1.- v1.v->x * v1.v->x));
183 
184  return *RETURN_PTR;
185 }
186 
192 {
194 
195  RETURN_PTR->v->x=::asin(v1.v->x);
197  &(RETURN_PTR->v->x),
198  &(v1.v->x),1./::sqrt(1.- v1.v->x * v1.v->x));
199 
200  return *RETURN_PTR;
201 }
202 
207 prevariable& pow(const prevariable& v1, const prevariable& v2)
208 {
209  double x=::pow(v1.v->x,(v2.v->x)-1);
210  double y=x* v1.v->x;
211 
213 
214  RETURN_PTR->v->x=y;
216  &(RETURN_PTR->v->x),
217  &(v1.v->x), v2.v->x * x ,&(v2.v->x),
218  y * ::log(v1.v->x));
219 
220  return *RETURN_PTR;
221 }
222 
227 prevariable& pow(const double u, const prevariable& v1)
228 {
229  double y = ::pow(u,(v1.v->x));
230 
232 
233  RETURN_PTR->v->x=y;
235  &(RETURN_PTR->v->x), &(v1.v->x), y * ::log(u));
236 
237  return *RETURN_PTR;
238 }
239 
245 {
247 
248  RETURN_PTR->v->x=::sinh(v1.v->x);
250  &(RETURN_PTR->v->x), &(v1.v->x),::cosh(v1.v->x));
251 
252  return *RETURN_PTR;
253 }
254 
260 {
262 
263  RETURN_PTR->v->x=::cosh(v1.v->x);
265  &(RETURN_PTR->v->x), &(v1.v->x),::sinh(v1.v->x));
266 
267  return *RETURN_PTR;
268 }
269 
274 prevariable& atan2(const prevariable& v1, const prevariable& v2)
275 {
276  if (value(v1) == 0 && value(v2) == 0)
277  {
278  cerr << "Error: The ADMB function \"atan2(y, x)\" is undefined "
279  "for y and x equal zero.\n";
280  ad_exit(1);
281  }
282  if (value(v1) == 0 && value(v2) > 0)
283  {
284  return atan(v1/v2);
285  }
286  dvariable x = (sqrt(v2 * v2 + v1 * v1) - v2)/v1;
287  return atan(x) * 2.0;
288 }
289 
294 prevariable& atan2(const prevariable& v1, const double v2)
295 {
296  if (value(v1) == 0 && v2 == 0)
297  {
298  cerr << "Error: The ADMB function \"atan2(y, x)\" is undefined "
299  "for y and x equal zero.\n";
300  ad_exit(1);
301  }
302  if (value(v1) == 0 && v2 > 0)
303  {
304  return atan(v1/v2);
305  }
306  dvariable x = (sqrt(v2 * v2 + v1 * v1) - v2)/v1;
307  return atan(x) * 2.0;
308 }
309 
314 prevariable& atan2(const double v1, const prevariable& v2)
315 {
316  if (v1 == 0 && value(v2) == 0)
317  {
318  cerr << "Error: The ADMB function \"atan2(y, x)\" is undefined "
319  "for y and x equal zero.\n";
320  ad_exit(1);
321  }
322  if (v1 == 0 && value(v2) > 0)
323  {
324  return atan(v1/v2);
325  }
326  dvariable x = (sqrt(v2 * v2 + v1 * v1) - v2)/v1;
327  return atan(x) * 2.0;
328 }
Base class for dvariable.
Definition: fvar.hpp:1315
d3_array tan(const d3_array &arr3)
Returns d3_array results with computed tan from elements in arr3.
Definition: d3arr2a.cpp:73
d3_array sqr(const d3_array &arr3)
Returns d3_array with square values from arr3.
Definition: d3arr2c.cpp:25
dvector atan2(const dvector &t1, const dvector &t2)
dvector tanh(const dvector &vec)
Returns dvector with hyperbolic tangent for each value of vec.
Definition: dvect6.cpp:126
#define x
df1_one_variable atan(const df1_one_variable &x)
Definition: df11fun.cpp:312
dvector cosh(const dvector &vec)
Returns dvector with hyperbolic cosine for each value of vec.
Definition: dvect6.cpp:105
exitptr ad_exit
Definition: gradstrc.cpp:53
bool isfinite(const ad< T, V > &x)
Definition: tiny_ad.hpp:149
void set_gradient_stack(void(*func)(void), double *dep_addr, double *ind_addr1=NULL, double mult1=0, double *ind_addr2=NULL, double mult2=0)
Description not yet available.
Definition: fvar.hpp:1045
d3_array sqrt(const d3_array &arr3)
Author: David Fournier Copyright (c) 2008-2012 Regents of the University of California.
Definition: d3arr2c.cpp:11
static dvariable * next_RETURN_PTR()
Definition: gradstrc.cpp:457
Author: David Fournier Copyright (c) 2008-2012 Regents of the University of California.
d3_array exp(const d3_array &arr3)
Returns d3_array results with computed exp from elements in arr3.
Definition: d3arr2a.cpp:28
void default_evaluation(void)
Description not yet available.
Definition: def_eval.cpp:61
double_and_int * v
pointer to the data
Definition: fvar.hpp:1333
dvector sinh(const dvector &vec)
Returns dvector with hyperbolic sine for each value of vec.
Definition: dvect6.cpp:84
dvector value(const df1_one_vector &v)
Definition: df11fun.cpp:69
static _THREAD grad_stack * GRAD_STACK1
prevariable & ldexp(const prevariable &v1, const int &exponent)
Description not yet available.
Definition: fvar_fn1.cpp:80
Fundamental data type for reverse mode automatic differentiation.
Definition: fvar.hpp:1518
d3_array log(const d3_array &arr3)
Author: David Fournier Copyright (c) 2008-2012 Regents of the University of California.
Definition: d3arr2a.cpp:13
double x
&lt; value of the variable
Definition: fvar.hpp:195
dvector acos(const dvector &vec)
Returns dvector with principal value of the arc cosine of vec, expressed in radians.
Definition: dvect6.cpp:244
d3_array pow(const d3_array &m, int e)
Description not yet available.
Definition: d3arr6.cpp:17
dvector asin(const dvector &vec)
Returns dvector with principal value of the arc sine of vec, expressed in radians.
Definition: dvect6.cpp:229