source: trunk/test_more/sinh.cpp @ 1590

Last change on this file since 1590 was 1370, checked in by bradbell, 11 years ago

trunk: Fix svn_add_id.sh and use it set Id property for some missed files.

  • Property svn:keywords set to Id
File size: 1.9 KB
Line 
1/* $Id: sinh.cpp 1370 2009-05-31 05:31:50Z bradbell $ */
2/* --------------------------------------------------------------------------
3CppAD: C++ Algorithmic Differentiation: Copyright (C) 2003-07 Bradley M. Bell
4
5CppAD is distributed under multiple licenses. This distribution is under
6the terms of the
7                    Common 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 just used for validation testing.
15*/
16# include <cppad/cppad.hpp>
17
18bool Sinh(void)
19{       bool ok = true;
20
21        using CppAD::sinh;
22        using CppAD::cosh;
23        using namespace CppAD;
24
25        // independent variable vector
26        CPPAD_TEST_VECTOR< AD<double> > U(1);
27        U[0]     = 1.;
28        Independent(U);
29
30        // dependent variable vector
31        CPPAD_TEST_VECTOR< AD<double> > Z(1);
32        Z[0] = sinh(U[0]); 
33
34        // create f: U -> Z and vectors used for derivative calculations
35        ADFun<double> f(U, Z); 
36        CPPAD_TEST_VECTOR<double> v(1);
37        CPPAD_TEST_VECTOR<double> w(1);
38
39        // check value
40        double sin_u = sinh( Value(U[0]) );
41        double cos_u = cosh( Value(U[0]) );
42
43        ok &= NearEqual(sin_u, Value(Z[0]),  1e-10 , 1e-10);
44
45        // forward computation of partials w.r.t. u
46        size_t j;
47        size_t p     = 5;
48        double jfac  = 1.;
49        v[0]         = 1.;
50        for(j = 1; j < p; j++)
51        {       w     = f.Forward(j, v);       
52
53                double value;
54                if( j % 2 == 1 )
55                        value = cos_u;
56                else    value = sin_u;
57
58                jfac *= j;
59                ok &= NearEqual(jfac*w[0], value, 1e-10 , 1e-10); // d^jz/du^j
60                v[0]  = 0.;
61        }
62
63        // reverse computation of partials of Taylor coefficients
64        CPPAD_TEST_VECTOR<double> r(p); 
65        w[0]  = 1.;
66        r     = f.Reverse(p, w);
67        jfac  = 1.;
68        for(j = 0; j < p; j++)
69        {
70                double value;
71                if( j % 2 == 0 )
72                        value = cos_u;
73                else    value = sin_u;
74
75                ok &= NearEqual(jfac*r[j], value, 1e-10 , 1e-10); // d^jz/du^j
76
77                jfac *= (j + 1);
78        }
79
80        return ok;
81}
Note: See TracBrowser for help on using the repository browser.