source: trunk/cppad/local/std_math_ad.hpp @ 3680

Last change on this file since 3680 was 3680, checked in by bradbell, 5 years ago

merge to branch: trunk
from repository: https://github.com/coin-or/CppAD
start hash code: 071875a4beba3363e5fa9752426aec4762cd1caa
end hash code: 0bef506513a519e1073c6279d5c4cba9e5c3b180

commit 0bef506513a519e1073c6279d5c4cba9e5c3b180
Author: Brad Bell <bradbell@…>
Date: Thu May 7 12:14:32 2015 -0700

Add the acosh function (as an atomic operation when defined by compiler).

commit b3264fa17b2f65b65800423a0e243c9c3ccfe06a
Author: Brad Bell <bradbell@…>
Date: Wed May 6 20:25:38 2015 -0700

CMakeLists.txt: Change so test only check for compliation.

commit dcbac4d4f20cc383f2bd9edb02036659df40b791
Author: Brad Bell <bradbell@…>
Date: Wed May 6 15:06:28 2015 -0700

asinh.cpp: check higher orders, relax accuracy on test.

commit 5f8881993fedd18cccc3c74831133a8f8a9d17b0
Author: Brad Bell <bradbell@…>
Date: Wed May 6 14:36:18 2015 -0700

Change Acos to acos.
acos.cpp: remove trailing white space.

commit e828fa1f7c4c3848c727f14b1b7a8030071ee705
Author: Brad Bell <bradbell@…>
Date: Wed May 6 12:07:35 2015 -0700

Change Acos to acos.
acos.cpp: remove redundant index commands, remove trailing with space.

commit 3d16e5b9fe1bdafa4ad01d1d466bb72b792650fa
Author: Brad Bell <bradbell@…>
Date: Wed May 6 11:30:49 2015 -0700

op_code.hpp: Minor edits to AcosOp? commnets.

commit 58beaaad149b4ac29fae44589d7f8900bf8f4c40
Author: Brad Bell <bradbell@…>
Date: Wed May 6 10:51:43 2015 -0700

for_jac_sweep.hpp: Add missing AsinhOp? case.

commit 623c134870c522ae5e80bcf0f89d230902594c80
Author: Brad Bell <bradbell@…>
Date: Wed May 6 10:27:39 2015 -0700

Fix comment about AsinhOp? operator.

commit 226b14f6f4810f5abf1ca247aae541963efaf4d6
Author: Brad Bell <bradbell@…>
Date: Wed May 6 10:14:08 2015 -0700

Add derivative of F to make order zero case clearer.
acos_reverse.omh: Fix some sign errors.
asin_reverse.omh: Fix typo.
acos_forward.omh: Simplify by distributing minus sign.

commit 4682f4ee73e33b600b180086576e986f636a24dc
Author: Brad Bell <bradbell@…>
Date: Wed May 6 08:15:50 2015 -0700

acos_forward.omh: fix sign that depends on acos versus acosh.

commit 906ae10adf019ddda7f57dd165aab08fc55289c4
Author: Brad Bell <bradbell@…>
Date: Wed May 6 07:09:47 2015 -0700

  1. Fix inclusion of some temporary files in package (e.g., git_commit.sh).
  2. Simplify and improve using git ls-files and ls bin/check_*.
  3. Remove trailing white space.

commit 5096f4706a547bd76caa3766aa2c62802ef7f0bf
Author: Brad Bell <bradbell@…>
Date: Wed May 6 06:41:20 2015 -0700

Combine base type documentation for erf, asinh
(will add more functions to this list list).

commit b3535db5ad95bee90672abcaa686032d23bce2fc
Author: Brad Bell <bradbell@…>
Date: Tue May 5 18:01:11 2015 -0700

  1. Change Arc Cosine/Sine? to Inverse Cosine/Sine?.
  2. Change arcsin-> asin and arccos->acos.
  3. Remove index commands that are duplicates of words in titles.


acos_reverse.omh: Add acosh case to this page.

  • Property svn:keywords set to Id
File size: 9.5 KB
Line 
1/* $Id: std_math_ad.hpp 3680 2015-05-07 19:17:37Z bradbell $ */
2# ifndef CPPAD_STD_MATH_AD_INCLUDED
3# define CPPAD_STD_MATH_AD_INCLUDED
4
5/* --------------------------------------------------------------------------
6CppAD: C++ Algorithmic Differentiation: Copyright (C) 2003-15 Bradley M. Bell
7
8CppAD is distributed under multiple licenses. This distribution is under
9the terms of the
10                    Eclipse Public License Version 1.0.
11
12A copy of this license is included in the COPYING file of this distribution.
13Please visit http://www.coin-or.org/CppAD/ for information on other licenses.
14-------------------------------------------------------------------------- */
15/*
16-------------------------------------------------------------------------------
17$begin std_math_ad$$
18$spell
19        acosh
20        Vec
21        std
22        atan
23        const
24        acos
25        asin
26        atan
27        cos
28        exp
29        fabs
30        sqrt
31        CppAD
32        namespace
33        tanh
34$$
35
36$index standard, AD math unary$$
37$index math, AD unary$$
38$index unary, AD math$$
39
40$index acos, AD$$
41$index asin, AD$$
42$index atan, AD$$
43$index cos, AD$$
44$index cosh, AD$$
45$index exp, AD$$
46$index fabs, AD$$
47$index log, AD$$
48$index log10, AD$$
49$index sin, AD$$
50$index sinh, AD$$
51$index sqrt, AD$$
52$index tan, AD$$
53$index tanh, AD$$
54
55$section AD Standard Math Unary Functions$$
56
57$head Syntax$$
58$icode%y% = %fun%(%x%)%$$
59
60
61$head Purpose$$
62Evaluates the one argument standard math function
63$icode fun$$ where its argument is an
64$cref/AD of/glossary/AD of Base/$$ $icode Base$$ object.
65
66$head x$$
67The argument $icode x$$ has one of the following prototypes
68$codei%
69        const AD<%Base%>               &%x%
70        const VecAD<%Base%>::reference &%x%
71%$$
72
73$head y$$
74The result $icode y$$ has prototype
75$codei%
76        AD<%Base%> %y%
77%$$
78
79
80$head Operation Sequence$$
81Most of these functions are AD of $icode Base$$
82$cref/atomic operations/glossary/Operation/Atomic/$$.
83In all cases,
84The AD of $icode Base$$
85operation sequence used to calculate $icode y$$ is
86$cref/independent/glossary/Operation/Independent/$$
87of $icode x$$.
88
89$head fun$$
90A definition of $icode fun$$ is included
91for each of the following functions:
92$code acos$$,
93$code asin$$,
94$code atan$$,
95$code cos$$,
96$code cosh$$,
97$code exp$$,
98$code fabs$$,
99$code log$$,
100$code log10$$,
101$code sin$$,
102$code sinh$$,
103$code sqrt$$,
104$code tan$$,
105$code tanh$$.
106
107
108$head Examples$$
109The following files
110contain examples and tests of these functions.
111Each test returns true if it succeeds and false otherwise.
112$children%
113        example/acos.cpp%
114        example/asin.cpp%
115        example/atan.cpp%
116        example/cos.cpp%
117        example/cosh.cpp%
118        example/exp.cpp%
119        example/log.cpp%
120        example/log10.cpp%
121        example/sin.cpp%
122        example/sinh.cpp%
123        example/sqrt.cpp%
124        example/tan.cpp%
125        example/tanh.cpp
126%$$
127$table
128$rref abs.cpp$$
129$rref Acos.cpp$$
130$rref Asin.cpp$$
131$rref atan.cpp$$
132$rref cos.cpp$$
133$rref cosh.cpp$$
134$rref exp.cpp$$
135$rref log.cpp$$
136$rref log10.cpp$$
137$rref sin.cpp$$
138$rref sinh.cpp$$
139$rref sqrt.cpp$$
140$rref tan.cpp$$
141$rref tanh.cpp$$
142$tend
143
144
145$head Derivatives$$
146Each of these functions satisfy a standard math function differential equation.
147Calculating derivatives using this differential equation
148is discussed for
149both $cref/forward/ForwardTheory/Standard Math Functions/$$
150and $cref/reverse/ReverseTheory/Standard Math Functions/$$ mode.
151The exact form of the differential equation
152for each of these functions is listed below:
153
154$subhead acos$$
155$latex \[
156\begin{array}{lcr}
157        \D{[ {\rm acos} (x) ]}{x} & = & - (1 - x * x)^{-1/2}
158\end{array}
159\] $$
160
161$subhead asin$$
162$latex \[
163\begin{array}{lcr}
164        \D{[ {\rm asin} (x) ]}{x} & = & (1 - x * x)^{-1/2}
165\end{array}
166\] $$
167
168$subhead atan$$
169$latex \[
170\begin{array}{lcr}
171        \D{[ {\rm atan} (x) ]}{x} & = & \frac{1}{1 + x^2}
172\end{array}
173\] $$
174
175$subhead cos$$
176$latex \[
177\begin{array}{lcr}
178        \D{[ \cos (x) ]}{x} & = & - \sin (x)  \\
179        \D{[ \sin (x) ]}{x} & = & \cos (x)
180\end{array}
181\] $$
182
183$subhead cosh$$
184$latex \[
185\begin{array}{lcr}
186        \D{[ \cosh (x) ]}{x} & = & \sinh (x)  \\
187        \D{[ \sin (x) ]}{x}  & = & \cosh (x)
188\end{array}
189\] $$
190
191$subhead exp$$
192$latex \[
193\begin{array}{lcr}
194        \D{[ \exp (x) ]}{x} & = & \exp (x)
195\end{array}
196\] $$
197
198$subhead log$$
199$latex \[
200\begin{array}{lcr}
201        \D{[ \log (x) ]}{x} & = & \frac{1}{x}
202\end{array}
203\] $$
204
205$subhead log10$$
206This function is special in that it's derivatives are calculated
207using the relation
208$latex \[
209\begin{array}{lcr}
210        {\rm log10} (x) & = & \log(x) / \log(10)
211\end{array}
212\] $$
213
214$subhead sin$$
215$latex \[
216\begin{array}{lcr}
217        \D{[ \sin (x) ]}{x} & = & \cos (x) \\
218        \D{[ \cos (x) ]}{x} & = & - \sin (x)
219\end{array}
220\] $$
221
222$subhead sinh$$
223$latex \[
224\begin{array}{lcr}
225        \D{[ \sinh (x) ]}{x} & = & \cosh (x)   \\
226        \D{[ \cosh (x) ]}{x} & = & \sinh (x)
227\end{array}
228\] $$
229
230$subhead sqrt$$
231$latex \[
232\begin{array}{lcr}
233        \D{[ {\rm sqrt} (x) ]}{x} & = & \frac{1}{2 {\rm sqrt} (x) }
234\end{array}
235\] $$
236
237$subhead tan$$
238$latex \[
239\begin{array}{lcr}
240        \D{[ \tan (x) ]}{x} & = & 1 + \tan (x)^2
241\end{array}
242\] $$
243
244$subhead tanh$$
245$latex \[
246\begin{array}{lcr}
247        \D{[ \tanh (x) ]}{x} & = & 1 - \tanh (x)^2
248\end{array}
249\] $$
250
251$end
252-------------------------------------------------------------------------------
253*/
254
255/*!
256\file std_math_ad.hpp
257Define AD<Base> standard math functions (using their Base versions)
258*/
259
260/*!
261\def CPPAD_STANDARD_MATH_UNARY_AD(Name, Op)
262Defines function Name with argument type AD<Base> and tape operation Op
263
264The macro defines the function x.Name() where x has type AD<Base>.
265It then uses this funciton to define Name(x) where x has type
266AD<Base> or VecAD_reference<Base>.
267
268If x is a variable, the tape unary operator Op is used
269to record the operation and the result is identified as correspoding
270to this operation; i.e., Name(x).taddr_ idendifies the operation and
271Name(x).tape_id_ identifies the tape.
272
273This macro is used to define AD<Base> versions of
274acos, asin, atan, cos, cosh, exp, fabs, log, sin, sinh, sqrt, tan, tanh.
275*/
276
277# define CPPAD_STANDARD_MATH_UNARY_AD(Name, Op)                   \
278    template <class Base>                                         \
279    inline AD<Base> Name(const AD<Base> &x)                       \
280    {   return x.Name(); }                                        \
281    template <class Base>                                         \
282    inline AD<Base> AD<Base>::Name (void) const                   \
283    {                                                             \
284        AD<Base> result;                                          \
285        result.value_ = CppAD::Name(value_);                      \
286        CPPAD_ASSERT_UNKNOWN( Parameter(result) );                \
287                                                                  \
288        if( Variable(*this) )                                     \
289        {   CPPAD_ASSERT_UNKNOWN( NumArg(Op) == 1 );              \
290            ADTape<Base> *tape = tape_this();                     \
291            tape->Rec_.PutArg(taddr_);                            \
292            result.taddr_ = tape->Rec_.PutOp(Op);                 \
293            result.tape_id_    = tape->id_;                       \
294        }                                                         \
295        return result;                                            \
296    }                                                             \
297    template <class Base>                                         \
298    inline AD<Base> Name(const VecAD_reference<Base> &x)          \
299    {   return Name( x.ADBase() ); }
300
301//  BEGIN CppAD namespace
302namespace CppAD {
303
304     CPPAD_STANDARD_MATH_UNARY_AD(acos, AcosOp)
305     CPPAD_STANDARD_MATH_UNARY_AD(asin, AsinOp)
306     CPPAD_STANDARD_MATH_UNARY_AD(atan, AtanOp)
307     CPPAD_STANDARD_MATH_UNARY_AD(cos, CosOp)
308     CPPAD_STANDARD_MATH_UNARY_AD(cosh, CoshOp)
309     CPPAD_STANDARD_MATH_UNARY_AD(exp, ExpOp)
310     CPPAD_STANDARD_MATH_UNARY_AD(fabs, AbsOp)
311     CPPAD_STANDARD_MATH_UNARY_AD(log, LogOp)
312     CPPAD_STANDARD_MATH_UNARY_AD(sin, SinOp)
313     CPPAD_STANDARD_MATH_UNARY_AD(sinh, SinhOp)
314     CPPAD_STANDARD_MATH_UNARY_AD(sqrt, SqrtOp)
315     CPPAD_STANDARD_MATH_UNARY_AD(tan, TanOp)
316     CPPAD_STANDARD_MATH_UNARY_AD(tanh, TanhOp)
317
318# if CPPAD_COMPILER_HAS_ASINH
319     CPPAD_STANDARD_MATH_UNARY_AD(asinh, AsinhOp)
320# endif
321# if CPPAD_COMPILER_HAS_ACOSH
322     CPPAD_STANDARD_MATH_UNARY_AD(acosh, AcoshOp)
323# endif
324
325# if CPPAD_COMPILER_HAS_ERF
326        // Error function is a special case
327        template <class Base>
328        inline AD<Base> erf(const AD<Base> &x)
329        {       return x.erf(); }
330        template <class Base>
331        inline AD<Base> AD<Base>::erf (void) const
332        {
333                AD<Base> result;
334                result.value_ = CppAD::erf(value_);
335                CPPAD_ASSERT_UNKNOWN( Parameter(result) );
336
337                if( Variable(*this) )
338                {       CPPAD_ASSERT_UNKNOWN( NumArg(ErfOp) == 3 );
339                        ADTape<Base> *tape = tape_this();
340                        // arg[0] = argument to erf function
341                        tape->Rec_.PutArg(taddr_);
342                        // arg[1] = zero
343                        addr_t p  = tape->Rec_.PutPar( Base(0) );
344                        tape->Rec_.PutArg(p);
345                        // arg[2] = 2 / sqrt(pi)
346                        p = tape->Rec_.PutPar(Base(
347                                1.0 / std::sqrt( std::atan(1.0) )
348                        ));
349                        tape->Rec_.PutArg(p);
350                        //
351                        result.taddr_ = tape->Rec_.PutOp(ErfOp);
352                        result.tape_id_    = tape->id_;
353                }
354                return result;
355        }
356        template <class Base>
357        inline AD<Base> erf(const VecAD_reference<Base> &x)
358        {       return erf( x.ADBase() ); }
359# endif
360
361     /*!
362        Compute the log of base 10 of x where  has type AD<Base>
363
364        \tparam Base
365        is the base type (different from base for log)
366        for this AD type, see base_require.
367
368        \param x
369        is the argument for the log10 function.
370
371        \result
372        if the result is y, then \f$ x = 10^y \f$.
373        */
374     template <class Base>
375     inline AD<Base> log10(const AD<Base> &x)
376     {    return CppAD::log(x) / CppAD::log( Base(10) ); }
377     template <class Base>
378     inline AD<Base> log10(const VecAD_reference<Base> &x)
379     {    return CppAD::log(x.ADBase()) / CppAD::log( Base(10) ); }
380}
381
382# undef CPPAD_STANDARD_MATH_UNARY_AD
383
384# endif
Note: See TracBrowser for help on using the repository browser.