source: trunk/test_more/print_for.cpp @ 2354

Last change on this file since 2354 was 2057, checked in by bradbell, 9 years ago

merge branches/base_require into trunk, see bin/svn_merge.sh

  • Property svn:keywords set to Id
File size: 2.6 KB
Line 
1/* $Id: print_for.cpp 2057 2011-08-11 14:07:11Z bradbell $ */
2/* --------------------------------------------------------------------------
3CppAD: C++ Algorithmic Differentiation: Copyright (C) 2003-11 Bradley M. Bell
4
5CppAD is distributed under multiple licenses. This distribution is under
6the terms of the
7                    Common Public License Version 1.0.
8
9A copy of this license is included in the COPYING file of this distribution.
10Please 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
16namespace { 
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
32bool 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_TEST_VECTOR< 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_TEST_VECTOR< 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_TEST_VECTOR<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}
Note: See TracBrowser for help on using the repository browser.