1 | /* $Id: print_for.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 | // modified version of test that used to be in ../print_for/print_for.cpp |
---|
14 | # include <cppad/cppad.hpp> |
---|
15 | |
---|
16 | namespace { |
---|
17 | using std::endl; |
---|
18 | using CppAD::AD; |
---|
19 | |
---|
20 | // use of PrintFor to check for invalid function arguments |
---|
21 | AD<double> check_log(const AD<double>& y) |
---|
22 | { // check during recording |
---|
23 | CPPAD_ASSERT_UNKNOWN( y > 0. ); |
---|
24 | |
---|
25 | // check during zero order forward calculation |
---|
26 | PrintFor(y, "check_log: y == ", y , " which is <= 0\n"); |
---|
27 | |
---|
28 | return log(y); |
---|
29 | } |
---|
30 | } |
---|
31 | |
---|
32 | bool print_for(void) |
---|
33 | { bool ok = true; |
---|
34 | using CppAD::PrintFor; |
---|
35 | |
---|
36 | std::stringstream stream_out; // stream that output is written to |
---|
37 | std::string string_check; // what we expect the output to be |
---|
38 | |
---|
39 | // independent variable vector |
---|
40 | size_t n = 1; |
---|
41 | CPPAD_TESTVECTOR(AD<double>) ax(n); |
---|
42 | ax[0] = 1.; |
---|
43 | Independent(ax); |
---|
44 | |
---|
45 | // print a VecAD<double>::reference object that is a parameter |
---|
46 | CppAD::VecAD<double> av(1); |
---|
47 | AD<double> Zero(0); |
---|
48 | av[Zero] = 0.; |
---|
49 | PrintFor("v[0] = ", av[Zero]); |
---|
50 | string_check += "v[0] = 0"; // v[0] == 0 during Forward(0, x) |
---|
51 | |
---|
52 | // Print a newline to separate this from previous output, |
---|
53 | // then print an AD<double> object that is a variable. |
---|
54 | PrintFor("\nv[0] + x[0] = ", av[0] + ax[0]); |
---|
55 | string_check += "\nv[0] + x[0] = 2"; // x[0] == 2 during Forward(0, x) |
---|
56 | |
---|
57 | // A conditional print that will not generate output when x[0] = 2. |
---|
58 | PrintFor(ax[0], "\n 2. + x[0] = ", 2. + ax[0], "\n"); |
---|
59 | |
---|
60 | // A conditional print that will generate output when x[0] = 2. |
---|
61 | PrintFor(ax[0] - 2., "\n 3. + x[0] = ", 3. + ax[0], "\n"); |
---|
62 | string_check += "\n 3. + x[0] = 5\n"; |
---|
63 | |
---|
64 | // A log evaluations that will result in an error message when x[0] = 2. |
---|
65 | AD<double> var = 2. - ax[0]; |
---|
66 | AD<double> log_var = check_log(var); |
---|
67 | string_check += "check_log: y == 0 which is <= 0\n"; |
---|
68 | |
---|
69 | // dependent variable vector |
---|
70 | size_t m = 2; |
---|
71 | CPPAD_TESTVECTOR(AD<double>) ay(m); |
---|
72 | ay[0] = av[Zero] + ax[0]; |
---|
73 | |
---|
74 | // define f: x -> y and stop tape recording |
---|
75 | CppAD::ADFun<double> f(ax, ay); |
---|
76 | |
---|
77 | // zero order forward with x[0] = 2 |
---|
78 | CPPAD_TESTVECTOR(double) x(n); |
---|
79 | x[0] = 2.; |
---|
80 | f.Forward(0, x, stream_out); |
---|
81 | |
---|
82 | std::string string_out = stream_out.str(); |
---|
83 | ok &= string_out == string_check; |
---|
84 | return ok; |
---|
85 | } |
---|