source: trunk/test_more/log10.cpp @ 3520

Last change on this file since 3520 was 2506, checked in by bradbell, 7 years ago

Change Licenses: CPL-1.0 -> EPL-1.0, GPL-2.0->GPL-3.0

  • Property svn:keywords set to Id
File size: 1.7 KB
Line 
1/* $Id: log10.cpp 2506 2012-10-24 19:36:49Z bradbell $ */
2/* --------------------------------------------------------------------------
3CppAD: C++ Algorithmic Differentiation: Copyright (C) 2003-12 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/*
14Old example now used just for validation testing.
15*/
16# include <cppad/cppad.hpp>
17
18bool Log10(void)
19{       bool ok = true;
20
21        using CppAD::log10;
22        using CppAD::log;
23        using namespace CppAD;
24
25        // independent variable vector, indices, values, and declaration
26        CPPAD_TESTVECTOR(AD<double>) U(1);
27        size_t s = 0;
28        U[s]     = 10.;
29        Independent(U);
30
31        // dependent variable vector, indices, and values
32        CPPAD_TESTVECTOR(AD<double>) Z(2);
33        size_t x = 0;
34        size_t y = 1;
35        Z[x]     = log10(U[s]);
36        Z[y]     = log10(Z[x]);
37
38        // define f : U -> Z and vectors for derivative calculations
39        ADFun<double> f(U, Z);
40        CPPAD_TESTVECTOR(double) v( f.Domain() );
41        CPPAD_TESTVECTOR(double) w( f.Range() );
42
43        // check values
44        ok &= NearEqual(Z[x] , 1.,  1e-10 , 1e-10);
45        ok &= NearEqual(Z[y] , 0.,  1e-10 , 1e-10);
46
47        // forward computation of partials w.r.t. s
48        double l10 = log(10.);
49        v[s] = 1.;
50        w    = f.Forward(1, v);
51        ok &= NearEqual(w[x], 1./(U[s]*l10)         , 1e-10 , 1e-10); // dx/ds
52        ok &= NearEqual(w[y], 1./(U[s]*Z[x]*l10*l10), 1e-10 , 1e-10); // dy/ds
53
54        // reverse computation of partials of y
55        w[x] = 0.;
56        w[y] = 1.;
57        v    = f.Reverse(1,w);
58        ok &= NearEqual(v[s], 1./(U[s]*Z[x]*l10*l10), 1e-10 , 1e-10); // dy/ds
59
60        return ok;
61}
Note: See TracBrowser for help on using the repository browser.