Prev Next ad_output.cpp Headings

@(@\newcommand{\W}[1]{ \; #1 \; } \newcommand{\R}[1]{ {\rm #1} } \newcommand{\B}[1]{ {\bf #1} } \newcommand{\D}[2]{ \frac{\partial #1}{\partial #2} } \newcommand{\DD}[3]{ \frac{\partial^2 #1}{\partial #2 \partial #3} } \newcommand{\Dpow}[2]{ \frac{\partial^{#1}}{\partial {#2}^{#1}} } \newcommand{\dpow}[2]{ \frac{ {\rm d}^{#1}}{{\rm d}\, {#2}^{#1}} }@)@This is cppad-20221105 documentation. Here is a link to its current documentation .
AD Output Operator: Example and Test

# include <cppad/cppad.hpp>

# include <sstream>  // std::ostringstream
# include <string>   // std::string
# include <iomanip>  // std::setprecision, setw, setfill, right

namespace {
    template <class S>
    void set_ostream(S &os)
    {   os
        << std::setprecision(4) // 4 digits of precision
        << std::setw(6)         // 6 characters per field
        << std::setfill(' ')    // fill with spaces
        << std::right;          // adjust value to the right
    }
}

bool ad_output(void)
{   bool ok = true;

    // This output stream is an ostringstream for testing purposes.
    // You can use << with other types of streams; i.e., std::cout.
    std::ostringstream stream;

    // ouput an AD<double> object
    CppAD::AD<double>  pi = 4. * atan(1.); // 3.1415926536
    set_ostream(stream);
    stream << pi;

    // ouput a VecAD<double>::reference object
    CppAD::VecAD<double> v(1);
    CppAD::AD<double> zero(0);
    v[zero]   = exp(1.);                  // 2.7182818285
    set_ostream(stream);
    stream << v[zero];

    // convert output from stream to string
    std::string str = stream.str();

    // check the output
    ok      &= (str == " 3.142 2.718");

    return ok;
}

Input File: example/general/ad_output.cpp