TEST(CppADCGLatexTest, latex) { // use a special object for source code generation typedef CG<double> CGD; typedef AD<CGD> ADCG; // independent variable vector CppAD::vector<ADCG> x(2); x[0] = 2.; x[1] = 3.; Independent(x); // dependent variable vector CppAD::vector<ADCG> y(2); // the model ADCG a = x[0] / 1. + x[1] * x[1]; ADCG b = a / 2e-6; y[0] = b + 1 / (sign(b)*5 * a); y[1] = x[1]; ADFun<CGD> fun(x, y); // the model tape /** * 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); CppAD::vector<CGD> vals = fun.Forward(0, indVars); LanguageLatex<double> langLatex; LangLatexDefaultVariableNameGenerator<double> nameGen; std::ofstream texfile; texfile.open("algorithm.tex"); handler.generateCode(texfile, langLatex, vals, nameGen); texfile.close(); std::string dir = system::getWorkingDirectory(); ASSERT_NO_THROW(system::callExecutable(PDFLATEX_COMPILER,{"-halt-on-error", "-shell-escape", system::createPath(dir, "latexTemplate.tex")})); }
TEST_F(CppADCGEvaluatorTest, Print) { CodeHandler<double> handler; std::vector<CG<double>> x(3); handler.makeVariables(x); std::vector<CG<double>> y(3); y[0] = makePrintValue("y[0]=", x[0] + x[1] + x[2], "\n"); y[1] = makePrintValue("x[0]=", x[0], "\n") * 2; y[2] = makePrintValue("y[2]=", y[1], "\n") * 2; CppAD::cg::Evaluator<double, double, CppAD::AD<double>> evaluator(handler); std::vector<CppAD::AD<double>> xNew(x.size()); CppAD::Independent(xNew); std::vector<CppAD::AD<double>> yNew(y.size()); std::cout << "Evaluating..."<<std::endl; evaluator.evaluate(xNew, yNew, y); std::cout << "Using tape..."<<std::endl; CppAD::ADFun<double> fun(xNew, yNew); std::vector<double> x2(x.size()); for(size_t i = 0; i< x2.size(); ++i) x2[i] = i + 1; std::vector<double> y2 = fun.Forward(0, x2); // validate results ASSERT_EQ(y2[0], x2[0] + x2[1] + x2[2]); ASSERT_EQ(y2[1], x2[0] * 2); ASSERT_EQ(y2[2], y2[1] * 2); }