CppAD: A C++ Algorithmic Differentiation Package  20171217
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
abs.hpp
Go to the documentation of this file.
1 # ifndef CPPAD_CORE_ABS_HPP
2 # define CPPAD_CORE_ABS_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 -------------------------------------------------------------------------------
17 $begin abs$$
18 $spell
19  fabs
20  Vec
21  std
22  faq
23  Taylor
24  Cpp
25  namespace
26  const
27  abs
28 $$
29 
30 $section AD Absolute Value Functions: abs, fabs$$
31 
32 $head Syntax$$
33 $icode%y% = abs(%x%)
34 %$$
35 $icode%y% = fabs(%x%)%$$
36 
37 $head x, y$$
38 See the $cref/possible types/unary_standard_math/Possible Types/$$
39 for a unary standard math function.
40 
41 $head Atomic$$
42 In the case where $icode x$$ is an AD type,
43 this is an $cref/atomic operation/glossary/Operation/Atomic/$$.
44 
45 $head Complex Types$$
46 The functions $code abs$$ and $icode fabs$$
47 are not defined for the base types
48 $code std::complex<float>$$ or $code std::complex<double>$$
49 because the complex $code abs$$ function is not complex differentiable
50 (see $cref/complex types faq/Faq/Complex Types/$$).
51 
52 $head Derivative$$
53 CppAD defines the derivative of the $code abs$$ function is
54 the $cref sign$$ function; i.e.,
55 $latex \[
56 {\rm abs}^{(1)} ( x ) = {\rm sign} (x ) =
57 \left\{ \begin{array}{rl}
58  +1 & {\rm if} \; x > 0 \\
59  0 & {\rm if} \; x = 0 \\
60  -1 & {\rm if} \; x < 0
61 \end{array} \right.
62 \] $$
63 The result for $icode%x% == 0%$$ used to be a directional derivative.
64 
65 $head Example$$
66 $children%
67  example/general/fabs.cpp
68 %$$
69 The file
70 $cref fabs.cpp$$
71 contains an example and test of this function.
72 It returns true if it succeeds and false otherwise.
73 
74 $end
75 -------------------------------------------------------------------------------
76 */
77 
78 // BEGIN CppAD namespace
79 namespace CppAD {
80 
81 template <class Base>
83 {
84  AD<Base> result;
85  result.value_ = abs(value_);
87 
88  if( Variable(*this) )
89  { // add this operation to the tape
92  local::ADTape<Base> *tape = tape_this();
93 
94  // corresponding operand address
95  tape->Rec_.PutArg(taddr_);
96  // put operator in the tape
97  result.taddr_ = tape->Rec_.PutOp(local::AbsOp);
98  // make result a variable
99  result.tape_id_ = tape->id_;
100  }
101  return result;
102 }
103 
104 template <class Base>
105 inline AD<Base> abs(const AD<Base> &x)
106 { return x.abs_me(); }
107 
108 template <class Base>
110 { return x.ADBase().abs_me(); }
111 
112 } // END CppAD namespace
113 
114 # endif
Base value_
Definition: ad.hpp:38
Definition: ad.hpp:34
size_t NumArg(OpCode op)
Number of arguments for a specified operator.
Definition: op_code.hpp:175
AD< Base > abs(const AD< Base > &x)
Definition: abs.hpp:105
size_t NumRes(OpCode op)
Number of variables resulting from the specified operation.
Definition: op_code.hpp:281
AD abs_me(void) const
Definition: abs.hpp:82
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
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.
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
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