1 | /* $Id: mul_zero_one.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 use of the special parameters zero and one with the multiply operator |
---|

15 | */ |
---|

16 | |
---|

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

18 | |
---|

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

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

21 | |
---|

22 | bool MulZeroOne(void) |
---|

23 | { |
---|

24 | using namespace CppAD; |
---|

25 | |
---|

26 | bool ok = true; |
---|

27 | |
---|

28 | size_t i; |
---|

29 | for(i = 0; i < 3; i++) |
---|

30 | { // run through the cases x = 0, 1, 2 |
---|

31 | |
---|

32 | size_t j; |
---|

33 | for(j = 0; j < 3; j++) |
---|

34 | { // run through the cases y = 0, 1, 2 |
---|

35 | |
---|

36 | CPPAD_TEST_VECTOR< ADdouble > x(1); |
---|

37 | x[0] = double(i); |
---|

38 | Independent(x); |
---|

39 | |
---|

40 | CPPAD_TEST_VECTOR< ADDdouble > y(1); |
---|

41 | y[0] = ADDdouble(j); |
---|

42 | Independent(y); |
---|

43 | |
---|

44 | CPPAD_TEST_VECTOR< ADDdouble > z(2); |
---|

45 | z[0] = x[0] * y[0]; |
---|

46 | z[1] = y[0] * x[0]; |
---|

47 | z[1] *= x[0]; |
---|

48 | |
---|

49 | // f(y) = z = { x * y , y * x * x } |
---|

50 | ADFun< ADdouble > f(y, z); |
---|

51 | CPPAD_TEST_VECTOR< ADdouble > u( f.Domain() ); |
---|

52 | CPPAD_TEST_VECTOR< ADdouble > v( f.Range() ); |
---|

53 | |
---|

54 | // v = f'(y) |
---|

55 | u[0] = ADdouble(1.); |
---|

56 | v = f.Forward(1, u); |
---|

57 | |
---|

58 | // check derivatives of f |
---|

59 | ok &= v[0] == x[0]; |
---|

60 | ok &= v[1] == x[0] * x[0]; |
---|

61 | |
---|

62 | // g(x) = f'(y) = {x , x * x} |
---|

63 | ADFun<double> g(x, v); |
---|

64 | CPPAD_TEST_VECTOR< double > a( g.Domain() ); |
---|

65 | CPPAD_TEST_VECTOR< double > b( g.Range() ); |
---|

66 | |
---|

67 | // b = g'(x) |
---|

68 | a[0] = 1.; |
---|

69 | b = g.Forward(1, a); |
---|

70 | |
---|

71 | // check derivatives of g |
---|

72 | ok &= (b[0] == 1.); |
---|

73 | ok &= (b[1] == 2. * x[0]); |
---|

74 | } |
---|

75 | } |
---|

76 | |
---|

77 | return ok; |
---|

78 | } |
---|