1 | /* $Id: neg.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 and test now only used for testing |
---|

15 | */ |
---|

16 | // BEGIN PROGRAM |
---|

17 | |
---|

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

19 | |
---|

20 | bool Neg(void) |
---|

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

22 | |
---|

23 | using namespace CppAD; |
---|

24 | |
---|

25 | // independent variable vector, indices, values, and declaration |
---|

26 | CPPAD_TEST_VECTOR< AD<double> > U(2); |
---|

27 | size_t s = 0; |
---|

28 | size_t t = 1; |
---|

29 | U[s] = 3.; |
---|

30 | U[t] = 4.; |
---|

31 | Independent(U); |
---|

32 | |
---|

33 | // dependent variable vector and indices |
---|

34 | CPPAD_TEST_VECTOR< AD<double> > Z(1); |
---|

35 | size_t x = 0; |
---|

36 | |
---|

37 | // dependent variable values |
---|

38 | Z[x] = - U[t]; // - AD<double> |
---|

39 | |
---|

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

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

42 | CPPAD_TEST_VECTOR<double> v( f.Domain() ); |
---|

43 | CPPAD_TEST_VECTOR<double> w( f.Range() ); |
---|

44 | |
---|

45 | // check values |
---|

46 | ok &= ( Z[x] == -4. ); |
---|

47 | |
---|

48 | // forward computation of partials w.r.t. s |
---|

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

50 | v[t] = 0.; |
---|

51 | w = f.Forward(1, v); |
---|

52 | ok &= ( w[x] == 0. ); // dx/ds |
---|

53 | |
---|

54 | // forward computation of partials w.r.t. t |
---|

55 | v[s] = 0.; |
---|

56 | v[t] = 1.; |
---|

57 | w = f.Forward(1, v); |
---|

58 | ok &= ( w[x] == -1. ); // dx/dt |
---|

59 | |
---|

60 | // reverse computation of second partials of z |
---|

61 | CPPAD_TEST_VECTOR<double> r( f.Domain() * 2 ); |
---|

62 | w[x] = 1.; |
---|

63 | r = f.Reverse(2, w); |
---|

64 | ok &= ( r[2 * s + 1] == 0. ); // d^2 x / (ds ds) |
---|

65 | ok &= ( r[2 * t + 1] == 0. ); // d^2 x / (ds dt) |
---|

66 | |
---|

67 | return ok; |
---|

68 | } |
---|

69 | // END PROGRAM |
---|