-
Notifications
You must be signed in to change notification settings - Fork 36
Home
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, there is support for the C language, Latex, and MathML, however additional languages can be easily added in the future. A graph like representation of operations is generated independently from the source code generation methods which allows to produce source code for different languages.
There are some additional classes that help generate and compile source code for Linux. Please see the Linux page for an example.
A generic program used to generate source code can be written as:
#include <iosfwd>
#include <vector>
#include <cppad/cg.hpp>
using namespace CppAD;
int main(void) {
// use a special object for source code generation
typedef CG<double> CGD;
typedef AD<CGD> ADCG;
/***************************************************************************
* the model
**************************************************************************/
// independent variable vector
CppAD::vector<ADCG> x(2);
x[0] = 2.;
x[1] = 3.;
Independent(x);
// dependent variable vector
CppAD::vector<ADCG> y(1);
// the model
ADCG a = x[0] / 1. + x[1] * x[1];
y[0] = a / 2;
ADFun<CGD> fun(x, y); // the model tape
/***************************************************************************
* Generate the C source code
**************************************************************************/
/**
* start the special steps for source code generation for a Jacobian
*/
CodeHandler<double> handler;
CppAD::vector<CGD> indVars(2);
handler.makeVariables(indVars);
CppAD::vector<CGD> jac = fun.SparseJacobian(indVars);
LanguageC<double> langC("double");
LangCDefaultVariableNameGenerator<double> nameGen;
std::ostringstream code;
handler.generateCode(code, langC, jac, nameGen);
std::cout << code.str();
}
The output of this function is:
y[1] = 0.5 * x[1] + 0.5 * x[1];
// dependent variables without operations
y[0] = 0.5;