CppADCodeGen 2.4.3
A C++ Algorithmic Differentiation Package with Source Code Generation
Loading...
Searching...
No Matches
lang_c_util.hpp
1#ifndef CPPAD_CG_LANG_C_UTIL_INCLUDED
2#define CPPAD_CG_LANG_C_UTIL_INCLUDED
3/* --------------------------------------------------------------------------
4 * CppADCodeGen: C++ Algorithmic Differentiation with Source Code Generation:
5 * Copyright (C) 2016 Ciengis
6 * Copyright (C) 2019 Joao Leal
7 *
8 * CppADCodeGen is distributed under multiple licenses:
9 *
10 * - Eclipse Public License Version 1.0 (EPL1), and
11 * - GNU General Public License Version 3 (GPL3).
12 *
13 * EPL1 terms and conditions can be found in the file "epl-v10.txt", while
14 * terms and conditions for the GPL3 can be found in the file "gpl3.txt".
15 * ----------------------------------------------------------------------------
16 * Author: Joao Leal
17 */
18
19namespace CppAD {
20namespace cg {
21
27template<class Base>
28inline void printModel(ADFun<CG<Base> >& fun) {
29 std::vector<std::string> depNames;
30 std::vector<std::string> indepNames;
31 printModel(fun, depNames, indepNames);
32}
33
41template<class Base>
42inline void printModel(ADFun<CG<Base> >& fun,
43 const std::vector<std::string>& depNames,
44 const std::vector<std::string>& indepNames) {
45 CPPADCG_ASSERT_UNKNOWN(depNames.size() <= fun.Range())
46 CPPADCG_ASSERT_UNKNOWN(indepNames.size() <= fun.Domain())
47
48 CodeHandler<Base> handler;
49
50 std::vector<CG<Base> > indep0(fun.Domain());
51 handler.makeVariables(indep0);
52
53 std::vector<CG<Base> > dep0 = fun.Forward(0, indep0);
54
55 LanguageC<Base> langC("double");
56
60 LangCCustomVariableNameGenerator<Base> nameGen(depNames, indepNames,
61 "y", "x", "z", "array");
62
63 std::ostringstream code;
64 handler.generateCode(code, langC, dep0, nameGen);
65 std::cout << "\n" << code.str() << std::endl;
66}
67
71template<class Base>
72inline void printExpression(const CG<Base>& dep,
73 std::ostream& out = std::cout) {
74 if(dep.getOperationNode() != nullptr) {
75 if(dep.getOperationNode()->getCodeHandler() == nullptr) {
76 throw CGException("Unable to print expression: found an operation node without a CodeHandler!");
77 }
78
79 CodeHandler<Base>& handler = *dep.getOperationNode()->getCodeHandler();
80 LanguageC<double> langC("double");
81 LangCDefaultVariableNameGenerator<double> nameGen;
82
83 std::vector<CG<Base> > depv(1);
84 depv[0] = dep;
85
86 std::ostringstream code;
87 handler.generateCode(code, langC, depv, nameGen);
88 out << code.str();
89 } else {
90 out << "y[0] = " << dep.getValue() << ";" << std::endl;
91 }
92}
93
94template<class Base>
95inline void printExpression(OperationNode<Base>& dep,
96 std::ostream& out = std::cout) {
97 printExpression(CG<Base>(dep), out);
98}
99
100} // END cg namespace
101} // END CppAD namespace
102
103#endif