source: trunk/test_more/log1p.cpp @ 3793

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

merge to branch: trunk
from repository: https://github.com/coin-or/CppAD
start hash code: 494270b7d9413bba508ae4944e44e33b6f7be05b
end hash code: 4f37fd28086cb8ea9d063a034b715db0621a820f

commit 4f37fd28086cb8ea9d063a034b715db0621a820f
Author: Brad Bell <bradbell@…>
Date: Sun May 10 06:56:06 2015 -0700

Automatic changes for and generated by deprecated auto-tools install process.

commit f90fc447255412bda926eac132e9472a6e611bd4
Author: Brad Bell <bradbell@…>
Date: Sun May 10 06:29:53 2015 -0700

Add log1p function.

commit 7f792b718f437458af399e1feb67006779a98706
Author: Brad Bell <bradbell@…>
Date: Sun May 10 05:25:03 2015 -0700

op_code.hpp: fix comment about Expm1 operator.

commit b4be5e4692149773b061391f8ba2957e51ab7c33
Author: Brad Bell <bradbell@…>
Date: Sun May 10 05:03:25 2015 -0700

expm1.hpp: Fix title, description, and latex \exp command.

commit 148f381814fec10c5ad4480e43f59822c2c7ef30
Author: Brad Bell <bradbell@…>
Date: Sun May 10 04:32:58 2015 -0700

Change Log -> log and remove trailing white space.

File size: 2.0 KB
Line 
1/* $Id$ */
2/* --------------------------------------------------------------------------
3CppAD: C++ Algorithmic Differentiation: Copyright (C) 2003-15 Bradley M. Bell
4
5CppAD is distributed under multiple licenses. This distribution is under
6the terms of the
7                    Eclipse Public License Version 1.0.
8
9A copy of this license is included in the COPYING file of this distribution.
10Please visit http://www.coin-or.org/CppAD/ for information on other licenses.
11-------------------------------------------------------------------------- */
12
13# include <cppad/cppad.hpp>
14
15bool log1p(void)
16{       bool ok = true;
17
18        using CppAD::AD;
19        using CppAD::NearEqual;
20
21        // 10 times machine epsilon
22        double eps = 10. * std::numeric_limits<double>::epsilon();
23
24        // domain space vector
25        size_t n  = 1;
26        double x0 = 0.5;
27        CPPAD_TESTVECTOR(AD<double>) ax(n);
28        ax[0]     = x0;
29
30        // declare independent variables and start tape recording
31        CppAD::Independent(ax);
32
33        // a temporary value
34        AD<double> expm1_of_x0 = CppAD::expm1(ax[0]);
35
36        // range space vector
37        size_t m = 1;
38        CPPAD_TESTVECTOR(AD<double>) ay(m);
39        ay[0] = CppAD::log1p(expm1_of_x0);
40
41        // create f: x -> y and stop tape recording
42        CppAD::ADFun<double> f(ax, ay);
43
44        // check value
45        ok &= NearEqual(ay[0] , x0,  eps, eps);
46
47        // forward computation of first partial w.r.t. x[0]
48        CPPAD_TESTVECTOR(double) dx(n);
49        CPPAD_TESTVECTOR(double) dy(m);
50        dx[0] = 1.;
51        dy    = f.Forward(1, dx);
52        ok   &= NearEqual(dy[0], 1., eps, eps);
53
54        // forward computation of higher order partials w.r.t. x[0]
55        size_t n_order = 5;
56        for(size_t order = 2; order < n_order; order++)
57        {       dx[0] = 0.;
58                dy    = f.Forward(order, dx);
59                ok   &= NearEqual(dy[0], 0., eps, eps);
60        }
61        // reverse computation of derivatives
62        CPPAD_TESTVECTOR(double)  w(m);
63        CPPAD_TESTVECTOR(double) dw(n_order * n);
64        w[0]  = 1.;
65        dw    = f.Reverse(n_order, w);
66        ok   &= NearEqual(dw[0], 1., eps, eps);
67        for(size_t order = 1; order < n_order; order++)
68                ok   &= NearEqual(dw[order * n + 0], 0., eps, eps);
69
70        return ok;
71}
72
73// END C++
Note: See TracBrowser for help on using the repository browser.