1 | /* $Id: std_math.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 | Test the using standard math functions with AD< AD<double> > |
---|

15 | */ |
---|

16 | |
---|

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

18 | |
---|

19 | typedef CppAD::AD<double> ADdouble; |
---|

20 | typedef CppAD::AD< ADdouble > ADDdouble; |
---|

21 | |
---|

22 | bool std_math(void) |
---|

23 | { using CppAD::NearEqual; |
---|

24 | bool ok = true; |
---|

25 | ADDdouble half = .5; |
---|

26 | ADDdouble one = 1.; |
---|

27 | ADDdouble two = 2.; |
---|

28 | ADDdouble ten = 10.; |
---|

29 | ADDdouble small = 1e-6; |
---|

30 | ADDdouble pi_4 = 3.141592653 / 4.; |
---|

31 | ADDdouble root_2 = sqrt(two); |
---|

32 | |
---|

33 | ADDdouble y = acos(one / root_2); |
---|

34 | ok &= NearEqual( pi_4, y, small, small ); |
---|

35 | |
---|

36 | y = cos(pi_4); |
---|

37 | ok &= NearEqual( one / root_2, y, small, small ); |
---|

38 | |
---|

39 | y = asin(one / root_2); |
---|

40 | ok &= NearEqual( pi_4, y, small, small ); |
---|

41 | |
---|

42 | y = sin(pi_4); |
---|

43 | ok &= NearEqual( one / root_2, y, small, small ); |
---|

44 | |
---|

45 | y = atan(one); |
---|

46 | ok &= NearEqual( pi_4, y, small, small ); |
---|

47 | |
---|

48 | y = tan(pi_4); |
---|

49 | ok &= NearEqual( one, y, small, small ); |
---|

50 | |
---|

51 | y = two * cosh(one); |
---|

52 | ok &= NearEqual( exp(one) + exp(-one), y, small, small ); |
---|

53 | |
---|

54 | y = two * sinh(one); |
---|

55 | ok &= NearEqual( exp(one) - exp(-one), y, small, small ); |
---|

56 | |
---|

57 | y = log( exp(one) ); |
---|

58 | ok &= NearEqual( one, y, small, small ); |
---|

59 | |
---|

60 | y = log10( exp( log(ten) ) ); |
---|

61 | ok &= NearEqual( one, y, small, small ); |
---|

62 | |
---|

63 | return ok; |
---|

64 | } |
---|