1 | /* $Id: acos.cpp 2506 2012-10-24 19:36:49Z bradbell $ */ |
---|

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

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

4 | |
---|

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

6 | the terms of the |
---|

7 | Eclipse 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 Acos examples now used just for valiadation testing |
---|

15 | */ |
---|

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

17 | |
---|

18 | bool Acos(void) |
---|

19 | { bool ok = true; |
---|

20 | |
---|

21 | using CppAD::acos; |
---|

22 | using namespace CppAD; |
---|

23 | |
---|

24 | // independent variable vector |
---|

25 | CPPAD_TESTVECTOR(AD<double>) U(1); |
---|

26 | U[0] = .5; |
---|

27 | Independent(U); |
---|

28 | |
---|

29 | // a temporary values |
---|

30 | AD<double> x = cos(U[0]); |
---|

31 | |
---|

32 | // dependent variable vector |
---|

33 | CPPAD_TESTVECTOR(AD<double>) Z(1); |
---|

34 | Z[0] = acos(x); // acos( cos(u) ) |
---|

35 | |
---|

36 | // create f: U -> Z and vectors used for derivative calculations |
---|

37 | ADFun<double> f(U, Z); |
---|

38 | CPPAD_TESTVECTOR(double) v(1); |
---|

39 | CPPAD_TESTVECTOR(double) w(1); |
---|

40 | |
---|

41 | // check value |
---|

42 | ok &= NearEqual(U[0] , Z[0], 1e-10 , 1e-10); |
---|

43 | |
---|

44 | // forward computation of partials w.r.t. u |
---|

45 | size_t j; |
---|

46 | size_t p = 5; |
---|

47 | double jfac = 1.; |
---|

48 | double value = 1.; |
---|

49 | v[0] = 1.; |
---|

50 | for(j = 1; j < p; j++) |
---|

51 | { jfac *= j; |
---|

52 | w = f.Forward(j, v); |
---|

53 | ok &= NearEqual(jfac*w[0], value, 1e-10 , 1e-10); // d^jz/du^j |
---|

54 | v[0] = 0.; |
---|

55 | value = 0.; |
---|

56 | } |
---|

57 | |
---|

58 | // reverse computation of partials of Taylor coefficients |
---|

59 | CPPAD_TESTVECTOR(double) r(p); |
---|

60 | w[0] = 1.; |
---|

61 | r = f.Reverse(p, w); |
---|

62 | jfac = 1.; |
---|

63 | value = 1.; |
---|

64 | for(j = 0; j < p; j++) |
---|

65 | { ok &= NearEqual(jfac*r[j], value, 1e-10 , 1e-10); // d^jz/du^j |
---|

66 | jfac *= (j + 1); |
---|

67 | value = 0.; |
---|

68 | } |
---|

69 | |
---|

70 | return ok; |
---|

71 | } |
---|