CppAD: A C++ Algorithmic Differentiation Package  20171217
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
sign.hpp
Go to the documentation of this file.
1 # ifndef CPPAD_CORE_SIGN_HPP
2 # define CPPAD_CORE_SIGN_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 sign$$
17 $spell
18  CppAD
19  Dirac
20 $$
21 $section The Sign: sign$$
22 
23 $head Syntax$$
24 $icode%y% = sign(%x%)%$$
25 
26 $head Description$$
27 Evaluates the $code sign$$ function which is defined by
28 $latex \[
29 {\rm sign} (x) =
30 \left\{ \begin{array}{rl}
31  +1 & {\rm if} \; x > 0 \\
32  0 & {\rm if} \; x = 0 \\
33  -1 & {\rm if} \; x < 0
34 \end{array} \right.
35 \] $$
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 This is an $cref/atomic operation/glossary/Operation/Atomic/$$.
43 
44 $head Derivative$$
45 CppAD computes the derivative of the $code sign$$ function as zero for all
46 argument values $icode x$$.
47 The correct mathematical derivative is different and
48 is given by
49 $latex \[
50  {\rm sign}^{(1)} (x) = 2 \delta (x)
51 \] $$
52 where $latex \delta (x)$$ is the Dirac Delta function.
53 
54 $head Example$$
55 $children%
56  example/general/sign.cpp
57 %$$
58 The file
59 $cref sign.cpp$$
60 contains an example and test of this function.
61 It returns true if it succeeds and false otherwise.
62 
63 $end
64 -------------------------------------------------------------------------------
65 */
66 
67 // BEGIN CppAD namespace
68 namespace CppAD {
69 
70 template <class Base>
72 {
73  AD<Base> result;
74  result.value_ = sign(value_);
76 
77  if( Variable(*this) )
78  { // add this operation to the tape
81  local::ADTape<Base> *tape = tape_this();
82 
83  // corresponding operand address
84  tape->Rec_.PutArg(taddr_);
85  // put operator in the tape
86  result.taddr_ = tape->Rec_.PutOp(local::SignOp);
87  // make result a variable
88  result.tape_id_ = tape->id_;
89  }
90  return result;
91 }
92 
93 template <class Base>
94 inline AD<Base> sign(const AD<Base> &x)
95 { return x.sign_me(); }
96 
97 template <class Base>
99 { return x.ADBase().sign_me(); }
100 
101 } // END CppAD namespace
102 
103 # endif
Base value_
Definition: ad.hpp:38
Definition: ad.hpp:34
AD sign_me(void) const
Definition: sign.hpp:71
size_t NumArg(OpCode op)
Number of arguments for a specified operator.
Definition: op_code.hpp:175
size_t NumRes(OpCode op)
Number of variables resulting from the specified operation.
Definition: op_code.hpp:281
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.
std::complex< double > sign(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
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