-
Notifications
You must be signed in to change notification settings - Fork 41
Home
joaoleal edited this page Jun 27, 2012
·
25 revisions
Welcome to the CppADCodeGen wiki!
CppADCodeGen aims to extend the CppAD library in order to perform hybrid automatic differentiation, that is, to use operator overloading and produce source code.
Some of the key features of this library include:
- source code generation for any CppAD method;
- production of human readable source code;
- source code generation for multiple languages;
- elimination of any unneeded operation/variable;
Currently only support for the C language has been developed, however new languages can be easily added. A graph like representation of the operations is generated independently from the source code generation methods. A special template object is used, which supports most common arithmetic operations and math functions, and generates the operation graph during tape evaluation.
A typical program can be written as:
#include <iosfwd>
#include <vector>
#include <cppad_cgoo/cg.hpp>
using namespace CppAD;
int main(void) {
// use a special object for source code generation
typedef CG<double> CGD;
typedef AD<CGD> ADCG;
// independent variable vector
std::vector<ADCG> U(2);
U[0] = 2.;
U[1] = 3.;
Independent(U);
// dependent variable vector
std::vector<ADCG> Z(1);
// the model
ADCG a = U[0] / 1. + U[1] * U[1];
Z[0] = a / 2;
ADFun<CGD> fun(U, Z);
// independent variable vector, indices, values, and declaration
std::vector<double> u(2);
u[0] = 2.;
u[1] = 3.;
/**
* start the special steps for source code generation
* for a jacobian
*/
CodeHandler<double> handler;
std::vector<CGD> indVars(2);
handler.makeVariables(indVars);
std::vector<CGD> jac = fun.SparseJacobian(indVars);
CLanguage<double> langC;
CLangDefaultVariableNameGenerator<double> nameGen;
std::ostringstream code;
handler.generateCode(code, langC, jac, nameGen);
// declare temporary variables
std::cout << langC.generateTemporaryVariableDeclaration("double");
std::cout << code.str();
}