当前位置: 首页>>代码示例>>C++>>正文


C++ CppAD::PrintFor方法代码示例

本文整理汇总了C++中CppAD::PrintFor方法的典型用法代码示例。如果您正苦于以下问题:C++ CppAD::PrintFor方法的具体用法?C++ CppAD::PrintFor怎么用?C++ CppAD::PrintFor使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在CppAD的用法示例。


在下文中一共展示了CppAD::PrintFor方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。

示例1: print_for

bool print_for(void)
{	bool ok = true;
    using CppAD::PrintFor;

    std::stringstream stream_out;   // stream that output is written to
    std::string       string_check; // what we expect the output to be

    // independent variable vector
    size_t n = 1;
    CPPAD_TESTVECTOR(AD<double>) ax(n);
    ax[0] = 1.;
    Independent(ax);

    // print a VecAD<double>::reference object that is a parameter
    CppAD::VecAD<double> av(1);
    AD<double> Zero(0);
    av[Zero] = 0.;
    PrintFor("v[0] = ", av[Zero]);
    string_check += "v[0] = 0"; // v[0] == 0 during Forward(0, x)

    // Print a newline to separate this from previous output,
    // then print an AD<double> object that is a variable.
    PrintFor("\nv[0] + x[0] = ", av[0] + ax[0]);
    string_check += "\nv[0] + x[0] = 2"; // x[0] == 2 during Forward(0, x)

    // A conditional print that will not generate output when x[0] = 2.
    PrintFor(ax[0], "\n  2. + x[0] = ",   2. + ax[0], "\n");

    // A conditional print that will generate output when x[0] = 2.
    PrintFor(ax[0] - 2., "\n  3. + x[0] = ",   3. + ax[0], "\n");
    string_check += "\n  3. + x[0] = 5\n";

    // A log evaluations that will result in an error message when x[0] = 2.
    AD<double> var     = 2. - ax[0];
    AD<double> log_var = check_log(var);
    string_check += "check_log: y == 0 which is <= 0\n";

    // dependent variable vector
    size_t m = 2;
    CPPAD_TESTVECTOR(AD<double>) ay(m);
    ay[0] = av[Zero] + ax[0];

    // define f: x -> y and stop tape recording
    CppAD::ADFun<double> f(ax, ay);

    // zero order forward with x[0] = 2
    CPPAD_TESTVECTOR(double) x(n);
    x[0] = 2.;
    f.Forward(0, x, stream_out);

    std::string string_out = stream_out.str();
    ok &= string_out == string_check;
    return ok;
}
开发者ID:kaskr,项目名称:CppAD,代码行数:54,代码来源:print_for.cpp


注:本文中的CppAD::PrintFor方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。