CppAD: A C++ Algorithmic Differentiation Package  20171217
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
std_math_98.hpp
Go to the documentation of this file.
1 # ifndef CPPAD_CORE_STD_MATH_98_HPP
2 # define CPPAD_CORE_STD_MATH_98_HPP
3 
4 /* --------------------------------------------------------------------------
5 CppAD: C++ Algorithmic Differentiation: Copyright (C) 2003-17 Bradley M. Bell
6 
7 CppAD is distributed under multiple licenses. This distribution is under
8 the terms of the
9  Eclipse Public License Version 1.0.
10 
11 A copy of this license is included in the COPYING file of this distribution.
12 Please visit http://www.coin-or.org/CppAD/ for information on other licenses.
13 -------------------------------------------------------------------------- */
14 /*
15 -------------------------------------------------------------------------------
16 $begin acos$$
17 $spell
18  acos
19 $$
20 
21 $section Inverse Sine Function: acos$$
22 
23 $head Syntax$$
24 $icode%y% = acos(%x%)%$$
25 
26 $head x, y$$
27 See the $cref/possible types/unary_standard_math/Possible Types/$$
28 for a unary standard math function.
29 
30 $head Atomic$$
31 This is an $cref/atomic operation/glossary/Operation/Atomic/$$.
32 
33 $head Derivative$$
34 $latex \[
35 \begin{array}{lcr}
36  \R{acos}^{(1)} (x) & = & - (1 - x * x)^{-1/2}
37 \end{array}
38 \] $$
39 
40 $head Example$$
41 $children%
42  example/general/acos.cpp
43 %$$
44 The file
45 $cref acos.cpp$$
46 contains an example and test of this function.
47 It returns true if it succeeds and false otherwise.
48 
49 $end
50 -------------------------------------------------------------------------------
51 $begin asin$$
52 $spell
53  asin
54 $$
55 
56 $section Inverse Sine Function: asin$$
57 
58 $head Syntax$$
59 $icode%y% = asin(%x%)%$$
60 
61 $head x, y$$
62 See the $cref/possible types/unary_standard_math/Possible Types/$$
63 for a unary standard math function.
64 
65 $head Atomic$$
66 This is an $cref/atomic operation/glossary/Operation/Atomic/$$.
67 
68 $head Derivative$$
69 $latex \[
70 \begin{array}{lcr}
71  \R{asin}^{(1)} (x) & = & (1 - x * x)^{-1/2}
72 \end{array}
73 \] $$
74 
75 $head Example$$
76 $children%
77  example/general/asin.cpp
78 %$$
79 The file
80 $cref asin.cpp$$
81 contains an example and test of this function.
82 It returns true if it succeeds and false otherwise.
83 
84 $end
85 -------------------------------------------------------------------------------
86 $begin atan$$
87 $spell
88  atan
89 $$
90 
91 $section Inverse Tangent Function: atan$$
92 
93 $head Syntax$$
94 $icode%y% = atan(%x%)%$$
95 
96 $head x, y$$
97 See the $cref/possible types/unary_standard_math/Possible Types/$$
98 for a unary standard math function.
99 
100 $head Atomic$$
101 This is an $cref/atomic operation/glossary/Operation/Atomic/$$.
102 
103 $head Derivative$$
104 $latex \[
105 \begin{array}{lcr}
106  \R{atan}^{(1)} (x) & = & \frac{1}{1 + x^2}
107 \end{array}
108 \] $$
109 
110 $head Example$$
111 $children%
112  example/general/atan.cpp
113 %$$
114 The file
115 $cref atan.cpp$$
116 contains an example and test of this function.
117 It returns true if it succeeds and false otherwise.
118 
119 $end
120 -------------------------------------------------------------------------------
121 $begin cos$$
122 $spell
123  cos
124 $$
125 
126 $section The Cosine Function: cos$$
127 
128 $head Syntax$$
129 $icode%y% = cos(%x%)%$$
130 
131 $head x, y$$
132 See the $cref/possible types/unary_standard_math/Possible Types/$$
133 for a unary standard math function.
134 
135 $head Atomic$$
136 This is an $cref/atomic operation/glossary/Operation/Atomic/$$.
137 
138 $head Derivative$$
139 $latex \[
140 \begin{array}{lcr}
141  \R{cos}^{(1)} (x) & = & - \sin(x)
142 \end{array}
143 \] $$
144 
145 $head Example$$
146 $children%
147  example/general/cos.cpp
148 %$$
149 The file
150 $cref cos.cpp$$
151 contains an example and test of this function.
152 It returns true if it succeeds and false otherwise.
153 
154 $end
155 -------------------------------------------------------------------------------
156 $begin cosh$$
157 $spell
158  cosh
159 $$
160 
161 $section The Hyperbolic Cosine Function: cosh$$
162 
163 $head Syntax$$
164 $icode%y% = cosh(%x%)%$$
165 
166 $head x, y$$
167 See the $cref/possible types/unary_standard_math/Possible Types/$$
168 for a unary standard math function.
169 
170 $head Atomic$$
171 This is an $cref/atomic operation/glossary/Operation/Atomic/$$.
172 
173 $head Derivative$$
174 $latex \[
175 \begin{array}{lcr}
176  \R{cosh}^{(1)} (x) & = & \sinh(x)
177 \end{array}
178 \] $$
179 
180 $head Example$$
181 $children%
182  example/general/cosh.cpp
183 %$$
184 The file
185 $cref cosh.cpp$$
186 contains an example and test of this function.
187 It returns true if it succeeds and false otherwise.
188 
189 $end
190 -------------------------------------------------------------------------------
191 $begin exp$$
192 $spell
193  exp
194 $$
195 
196 $section The Exponential Function: exp$$
197 
198 $head Syntax$$
199 $icode%y% = exp(%x%)%$$
200 
201 $head x, y$$
202 See the $cref/possible types/unary_standard_math/Possible Types/$$
203 for a unary standard math function.
204 
205 $head Atomic$$
206 This is an $cref/atomic operation/glossary/Operation/Atomic/$$.
207 
208 $head Derivative$$
209 $latex \[
210 \begin{array}{lcr}
211  \R{exp}^{(1)} (x) & = & \exp(x)
212 \end{array}
213 \] $$
214 
215 $head Example$$
216 $children%
217  example/general/exp.cpp
218 %$$
219 The file
220 $cref exp.cpp$$
221 contains an example and test of this function.
222 It returns true if it succeeds and false otherwise.
223 
224 $end
225 -------------------------------------------------------------------------------
226 $begin log$$
227 $spell
228 $$
229 
230 $section The Exponential Function: log$$
231 
232 $head Syntax$$
233 $icode%y% = log(%x%)%$$
234 
235 $head x, y$$
236 See the $cref/possible types/unary_standard_math/Possible Types/$$
237 for a unary standard math function.
238 
239 $head Atomic$$
240 This is an $cref/atomic operation/glossary/Operation/Atomic/$$.
241 
242 $head Derivative$$
243 $latex \[
244 \begin{array}{lcr}
245  \R{log}^{(1)} (x) & = & \frac{1}{x}
246 \end{array}
247 \] $$
248 
249 $head Example$$
250 $children%
251  example/general/log.cpp
252 %$$
253 The file
254 $cref log.cpp$$
255 contains an example and test of this function.
256 It returns true if it succeeds and false otherwise.
257 
258 $end
259 -------------------------------------------------------------------------------
260 $begin log10$$
261 $spell
262  CppAD
263 $$
264 
265 $section The Base 10 Logarithm Function: log10$$
266 
267 $head Syntax$$
268 $icode%y% = log10(%x%)%$$
269 
270 $head x, y$$
271 See the $cref/possible types/unary_standard_math/Possible Types/$$
272 for a unary standard math function.
273 
274 $head Method$$
275 CppAD uses the representation
276 $latex \[
277 \begin{array}{lcr}
278  {\rm log10} (x) & = & \log(x) / \log(10)
279 \end{array}
280 \] $$
281 
282 $head Example$$
283 $children%
284  example/general/log10.cpp
285 %$$
286 The file
287 $cref log10.cpp$$
288 contains an example and test of this function.
289 It returns true if it succeeds and false otherwise.
290 
291 $end
292 -------------------------------------------------------------------------------
293 $begin sin$$
294 $spell
295  sin
296 $$
297 
298 $section The Sine Function: sin$$
299 
300 $head Syntax$$
301 $icode%y% = sin(%x%)%$$
302 
303 $head x, y$$
304 See the $cref/possible types/unary_standard_math/Possible Types/$$
305 for a unary standard math function.
306 
307 $head Atomic$$
308 This is an $cref/atomic operation/glossary/Operation/Atomic/$$.
309 
310 $head Derivative$$
311 $latex \[
312 \begin{array}{lcr}
313  \R{sin}^{(1)} (x) & = & \cos(x)
314 \end{array}
315 \] $$
316 
317 $head Example$$
318 $children%
319  example/general/sin.cpp
320 %$$
321 The file
322 $cref sin.cpp$$
323 contains an example and test of this function.
324 It returns true if it succeeds and false otherwise.
325 
326 $end
327 -------------------------------------------------------------------------------
328 $begin sinh$$
329 $spell
330  sinh
331 $$
332 
333 $section The Hyperbolic Sine Function: sinh$$
334 
335 $head Syntax$$
336 $icode%y% = sinh(%x%)%$$
337 
338 $head x, y$$
339 See the $cref/possible types/unary_standard_math/Possible Types/$$
340 for a unary standard math function.
341 
342 $head Atomic$$
343 This is an $cref/atomic operation/glossary/Operation/Atomic/$$.
344 
345 $head Derivative$$
346 $latex \[
347 \begin{array}{lcr}
348  \R{sinh}^{(1)} (x) & = & \cosh(x)
349 \end{array}
350 \] $$
351 
352 $head Example$$
353 $children%
354  example/general/sinh.cpp
355 %$$
356 The file
357 $cref sinh.cpp$$
358 contains an example and test of this function.
359 It returns true if it succeeds and false otherwise.
360 
361 $end
362 -------------------------------------------------------------------------------
363 $begin sqrt$$
364 $spell
365  sqrt
366 $$
367 
368 $section The Square Root Function: sqrt$$
369 
370 $head Syntax$$
371 $icode%y% = sqrt(%x%)%$$
372 
373 $head x, y$$
374 See the $cref/possible types/unary_standard_math/Possible Types/$$
375 for a unary standard math function.
376 
377 $head Atomic$$
378 This is an $cref/atomic operation/glossary/Operation/Atomic/$$.
379 
380 $head Derivative$$
381 $latex \[
382 \begin{array}{lcr}
383  \R{sqrt}^{(1)} (x) & = & \frac{1}{2 \R{sqrt} (x) }
384 \end{array}
385 \] $$
386 
387 $head Example$$
388 $children%
389  example/general/sqrt.cpp
390 %$$
391 The file
392 $cref sqrt.cpp$$
393 contains an example and test of this function.
394 It returns true if it succeeds and false otherwise.
395 
396 $end
397 -------------------------------------------------------------------------------
398 $begin tan$$
399 $spell
400  tan
401 $$
402 
403 $section The Tangent Function: tan$$
404 
405 $head Syntax$$
406 $icode%y% = tan(%x%)%$$
407 
408 $head x, y$$
409 See the $cref/possible types/unary_standard_math/Possible Types/$$
410 for a unary standard math function.
411 
412 $head Atomic$$
413 This is an $cref/atomic operation/glossary/Operation/Atomic/$$.
414 
415 $head Derivative$$
416 $latex \[
417 \begin{array}{lcr}
418  \R{tan}^{(1)} (x) & = & 1 + \tan (x)^2
419 \end{array}
420 \] $$
421 
422 $head Example$$
423 $children%
424  example/general/tan.cpp
425 %$$
426 The file
427 $cref tan.cpp$$
428 contains an example and test of this function.
429 It returns true if it succeeds and false otherwise.
430 
431 $end
432 -------------------------------------------------------------------------------
433 $begin tanh$$
434 $spell
435  tanh
436 $$
437 
438 $section The Hyperbolic Tangent Function: tanh$$
439 
440 $head Syntax$$
441 $icode%y% = tanh(%x%)%$$
442 
443 $head x, y$$
444 See the $cref/possible types/unary_standard_math/Possible Types/$$
445 for a unary standard math function.
446 
447 $head Atomic$$
448 This is an $cref/atomic operation/glossary/Operation/Atomic/$$.
449 
450 $head Derivative$$
451 $latex \[
452 \begin{array}{lcr}
453  \R{tanh}^{(1)} (x) & = & 1 - \tanh (x)^2
454 \end{array}
455 \] $$
456 
457 $head Example$$
458 $children%
459  example/general/tanh.cpp
460 %$$
461 The file
462 $cref tanh.cpp$$
463 contains an example and test of this function.
464 It returns true if it succeeds and false otherwise.
465 
466 $end
467 -------------------------------------------------------------------------------
468 */
469 
470 /*!
471 \file std_math_98.hpp
472 Define AD<Base> standard math functions (using their Base versions)
473 */
474 
475 /*!
476 \def CPPAD_STANDARD_MATH_UNARY_AD(Name, Op)
477 Defines function Name with argument type AD<Base> and tape operation Op
478 
479 The macro defines the function x.Name() where x has type AD<Base>.
480 It then uses this funciton to define Name(x) where x has type
481 AD<Base> or VecAD_reference<Base>.
482 
483 If x is a variable, the tape unary operator Op is used
484 to record the operation and the result is identified as correspoding
485 to this operation; i.e., Name(x).taddr_ idendifies the operation and
486 Name(x).tape_id_ identifies the tape.
487 
488 This macro is used to define AD<Base> versions of
489 acos, asin, atan, cos, cosh, exp, fabs, log, sin, sinh, sqrt, tan, tanh.
490 */
491 
492 # define CPPAD_STANDARD_MATH_UNARY_AD(Name, Op) \
493  template <class Base> \
494  inline AD<Base> Name(const AD<Base> &x) \
495  { return x.Name##_me(); } \
496  template <class Base> \
497  inline AD<Base> AD<Base>::Name##_me (void) const \
498  { \
499  AD<Base> result; \
500  result.value_ = CppAD::Name(value_); \
501  CPPAD_ASSERT_UNKNOWN( Parameter(result) ); \
502  \
503  if( Variable(*this) ) \
504  { CPPAD_ASSERT_UNKNOWN( NumArg(Op) == 1 ); \
505  local::ADTape<Base> *tape = tape_this(); \
506  tape->Rec_.PutArg(taddr_); \
507  result.taddr_ = tape->Rec_.PutOp(Op); \
508  result.tape_id_ = tape->id_; \
509  } \
510  return result; \
511  } \
512  template <class Base> \
513  inline AD<Base> Name(const VecAD_reference<Base> &x) \
514  { return x.ADBase().Name##_me(); }
515 
516 // BEGIN CppAD namespace
517 namespace CppAD {
518 
532 
533 # if CPPAD_USE_CPLUSPLUS_2011
539 # endif
540 
541 # if CPPAD_USE_CPLUSPLUS_2011
542  // Error function is a special case
543  template <class Base>
544  inline AD<Base> erf(const AD<Base> &x)
545  { return x.erf_me(); }
546  template <class Base>
547  inline AD<Base> AD<Base>::erf_me (void) const
548  {
549  AD<Base> result;
550  result.value_ = CppAD::erf(value_);
551  CPPAD_ASSERT_UNKNOWN( Parameter(result) );
552 
553  if( Variable(*this) )
555  local::ADTape<Base> *tape = tape_this();
556  // arg[0] = argument to erf function
557  tape->Rec_.PutArg(taddr_);
558  // arg[1] = zero
559  addr_t p = tape->Rec_.PutPar( Base(0.0) );
560  tape->Rec_.PutArg(p);
561  // arg[2] = 2 / sqrt(pi)
562  p = tape->Rec_.PutPar(Base(
563  1.0 / std::sqrt( std::atan(1.0) )
564  ));
565  tape->Rec_.PutArg(p);
566  //
567  result.taddr_ = tape->Rec_.PutOp(local::ErfOp);
568  result.tape_id_ = tape->id_;
569  }
570  return result;
571  }
572  template <class Base>
574  { return x.ADBase().erf_me(); }
575 # endif
576 
577  /*!
578  Compute the log of base 10 of x where has type AD<Base>
579 
580  \tparam Base
581  is the base type (different from base for log)
582  for this AD type, see base_require.
583 
584  \param x
585  is the argument for the log10 function.
586 
587  \result
588  if the result is y, then \f$ x = 10^y \f$.
589  */
590  template <class Base>
591  inline AD<Base> log10(const AD<Base> &x)
592  { return CppAD::log(x) / CppAD::log( Base(10) ); }
593  template <class Base>
595  { return CppAD::log(x.ADBase()) / CppAD::log( Base(10) ); }
596 }
597 
598 # undef CPPAD_STANDARD_MATH_UNARY_AD
599 
600 # endif
std::complex< double > erf(const std::complex< double > &x)
AD< Base > cosh(const AD< Base > &x)
Base value_
Definition: ad.hpp:38
CPPAD_TAPE_ADDR_TYPE addr_t
Definition: declare_ad.hpp:44
AD< Base > log(const AD< Base > &x)
Definition: ad.hpp:34
std::complex< double > atan(const std::complex< double > &x)
AD< Base > sinh(const AD< Base > &x)
size_t NumArg(OpCode op)
Number of arguments for a specified operator.
Definition: op_code.hpp:175
std::complex< double > acos(const std::complex< double > &x)
AD< Base > tan(const AD< Base > &x)
AD< Base > exp(const AD< Base > &x)
AD< Base > log10(const AD< Base > &x)
Compute the log of base 10 of x where has type AD&lt;Base&gt;
std::complex< double > asinh(const std::complex< double > &x)
AD erf_me(void) const
AD< Base > ADBase(void) const
Conversion from VecAD_reference to AD&lt;Base&gt;. puts the correspond vecad load instruction in the tape...
Definition: vec_ad.hpp:392
std::complex< double > acosh(const std::complex< double > &x)
std::complex< double > expm1(const std::complex< double > &x)
AD< Base > sqrt(const AD< Base > &x)
AD< Base > tanh(const AD< Base > &x)
std::complex< double > atanh(const std::complex< double > &x)
CPPAD_INLINE_FRIEND_TEMPLATE_FUNCTION bool Variable(const AD< Base > &x)
Definition: par_var.hpp:99
#define CPPAD_ASSERT_UNKNOWN(exp)
Check that exp is true, if not terminate execution.
AD< Base > sin(const AD< Base > &x)
AD< Base > cos(const AD< Base > &x)
std::complex< double > asin(const std::complex< double > &x)
local::recorder< Base > Rec_
This is where the information is recorded.
Definition: ad_tape.hpp:106
tape_id_t tape_id_
Definition: ad.hpp:41
Class used to hold a reference to an element of a VecAD object.
Definition: vec_ad.hpp:352
Class used to hold tape that records AD&lt;Base&gt; operations.
Definition: ad_tape.hpp:26
#define CPPAD_STANDARD_MATH_UNARY_AD(Name, Op)
Defines function Name with argument type AD&lt;Base&gt; and tape operation Op.
CPPAD_INLINE_FRIEND_TEMPLATE_FUNCTION bool Parameter(const AD< Base > &x)
Definition: par_var.hpp:80
addr_t taddr_
Definition: ad.hpp:44
tape_id_t id_
Unique identifier for this tape.
Definition: ad_tape.hpp:101
std::complex< double > fabs(const std::complex< double > &x)
std::complex< double > log1p(const std::complex< double > &x)