1 | /* $Id: cosh.cpp 1370 2009-05-31 05:31:50Z bradbell $ */ |
---|

2 | /* -------------------------------------------------------------------------- |
---|

3 | CppAD: C++ Algorithmic Differentiation: Copyright (C) 2003-07 Bradley M. Bell |
---|

4 | |
---|

5 | CppAD is distributed under multiple licenses. This distribution is under |
---|

6 | the terms of the |
---|

7 | Common Public License Version 1.0. |
---|

8 | |
---|

9 | A copy of this license is included in the COPYING file of this distribution. |
---|

10 | Please visit http://www.coin-or.org/CppAD/ for information on other licenses. |
---|

11 | -------------------------------------------------------------------------- */ |
---|

12 | |
---|

13 | /* |
---|

14 | Old example now just used for validation testing. |
---|

15 | */ |
---|

16 | # include <cppad/cppad.hpp> |
---|

17 | |
---|

18 | bool Cosh(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] = cosh(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 sinh_u = sinh( Value(U[0]) ); |
---|

41 | double cosh_u = cosh( Value(U[0]) ); |
---|

42 | |
---|

43 | ok &= NearEqual(cosh_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 = sinh_u; |
---|

56 | else value = cosh_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 = sinh_u; |
---|

73 | else value = cosh_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 | } |
---|