21 CGHelpers() =
default;
23 virtual ~CGHelpers() =
default;
49 template <
typename AD_SCALAR,
typename SCALAR =
double>
50 static std::string generateJacobianSource(CppAD::ADFun<AD_SCALAR>& f,
51 SparsityPattern& pattern,
53 size_t& maxTempVarCount,
54 bool useReverse =
false,
55 bool ignoreZero =
true,
56 std::string jacName =
"jac",
57 std::string inputName =
"x_in",
58 std::string tempName =
"v_",
59 std::string scalarName =
"double")
61 CppAD::cg::CodeHandler<SCALAR> codeHandler;
63 size_t n = f.Domain();
65 CppAD::vector<AD_SCALAR> input(n);
66 codeHandler.makeVariables(input);
67 for (
size_t i = 0;
i <
n;
i++)
70 input[
i].setValue(static_cast<double>(rand()) / static_cast<double>(RAND_MAX));
73 CppAD::vector<AD_SCALAR> jac(jacDim);
76 f.SparseJacobianReverse(
77 input, pattern.sparsity(), pattern.row(), pattern.col(), jac, pattern.workJacobian());
79 f.SparseJacobianForward(
80 input, pattern.sparsity(), pattern.row(), pattern.col(), jac, pattern.workJacobian());
82 CppAD::cg::LanguageC<SCALAR> langC(scalarName, 4);
83 langC.setIgnoreZeroDepAssign(ignoreZero);
84 CppAD::cg::LangCDefaultVariableNameGenerator<SCALAR> nameGen(jacName, inputName, tempName);
86 std::ostringstream code;
87 codeHandler.generateCode(code, langC, jac, nameGen);
89 std::cout <<
"temporary variables: " << codeHandler.getTemporaryVariableCount() << std::endl;
90 maxTempVarCount = codeHandler.getTemporaryVariableCount();
113 template <
typename AD_SCALAR>
114 static std::string generateForwardZeroSource(CppAD::ADFun<AD_SCALAR>& f,
115 size_t& maxTempVarCount,
116 bool ignoreZero =
true,
117 std::string jacName =
"forwardZero",
118 std::string inputName =
"x_in",
119 std::string tempName =
"v_")
121 CppAD::cg::CodeHandler<double> codeHandler;
124 size_t n = f.Domain();
126 CppAD::vector<AD_SCALAR> input(n);
128 for (
size_t i = 0;
i < input.size();
i++)
131 input[
i].setValue(static_cast<double>(rand()) / static_cast<double>(RAND_MAX));
136 codeHandler.makeVariables(input);
138 CppAD::vector<AD_SCALAR> forwardZero = f.Forward(0, input);
140 CppAD::cg::LanguageC<double> langC(
"double", 4);
141 langC.setIgnoreZeroDepAssign(ignoreZero);
142 CppAD::cg::LangCDefaultVariableNameGenerator<double> nameGen(jacName, inputName, tempName);
144 std::ostringstream code;
145 codeHandler.generateCode(code, langC, forwardZero, nameGen);
147 std::cout <<
"temporary variables: " << codeHandler.getTemporaryVariableCount() << std::endl;
148 maxTempVarCount = codeHandler.getTemporaryVariableCount();
175 template <
typename AD_SCALAR>
176 static std::string generateHessianSource(CppAD::ADFun<AD_SCALAR>& f,
177 SparsityPattern& pattern,
179 size_t& maxTempVarCount,
180 bool ignoreZero =
true,
181 std::string jacName =
"hes",
182 std::string inputName =
"x_in",
183 std::string tempName =
"v_")
185 CppAD::cg::CodeHandler<double> codeHandler;
187 size_t m = f.Range();
188 size_t n = f.Domain();
190 CppAD::vector<AD_SCALAR> input(n);
191 codeHandler.makeVariables(input);
193 for (
size_t i = 0;
i < input.size();
i++)
196 input[
i].setValue(static_cast<double>(rand()) / static_cast<double>(RAND_MAX));
199 CppAD::vector<AD_SCALAR> weights(m);
200 codeHandler.makeVariables(weights);
202 for (
size_t i = 0;
i < m; ++
i)
205 weights[
i].setValue(static_cast<double>(rand()) / static_cast<double>(RAND_MAX));
209 CppAD::vector<AD_SCALAR> hes(hesDim);
210 f.SparseHessian(input, weights, pattern.sparsity(), pattern.row(), pattern.col(), hes, pattern.workHessian());
213 for (
size_t i = 0;
i <
n;
i++)
214 for (
size_t j = 0; j <
i; j++)
215 hes[i * n + j] = hes[j * n + i];
217 CppAD::cg::LanguageC<double> langC(
"double", 4);
218 langC.setIgnoreZeroDepAssign(ignoreZero);
219 CppAD::cg::LangCDefaultVariableNameGenerator<double> nameGenTmp(jacName, inputName, tempName);
220 CppAD::cg::LangCDefaultHessianVarNameGenerator<double> nameGen(&nameGenTmp,
"w_in", m);
222 std::ostringstream code;
223 codeHandler.generateCode(code, langC, hes, nameGen);
225 std::cout <<
"temporary variables: " << codeHandler.getTemporaryVariableCount() << std::endl;
226 maxTempVarCount = codeHandler.getTemporaryVariableCount();
239 static void replaceAll(std::string& text,
const std::string& placeholder,
const std::string replaceWith)
241 size_t pos = text.find(placeholder);
242 while (pos != std::string::npos)
244 text.replace(pos, placeholder.length(), replaceWith);
245 pos = text.find(placeholder);
256 static void replaceOnce(std::string& text,
const std::string& placeholder,
const std::string replaceWith)
258 text.replace(text.find(placeholder), placeholder.length(), replaceWith);
267 static void writeFile(
const std::string& outputFile,
const std::string& content)
269 std::ofstream out(outputFile);
273 std::cout <<
"Could not open output file for writing " << outputFile << std::endl;
275 throw std::runtime_error(
"Code generation failed. Exiting.");
278 std::cout <<
"Writing generated code to file... " << std::endl;
290 static std::string parseFile(
const std::string& filename)
292 std::ifstream
t(filename);
296 std::cout <<
"Could not open template file " << filename << std::endl;
297 std::cout <<
"Code generation failed. Exiting." << std::endl;
299 throw std::runtime_error(
"Code generation failed. Exiting.");
302 std::stringstream stream;
305 std::string source(stream.str());
320 template <
typename SC =
double>
321 static std::shared_ptr<CppAD::cg::DynamicLib<SC>> loadDynamicLibCppad(
const std::string& libName)
323 #if CPPAD_CG_SYSTEM_LINUX 324 return std::shared_ptr<CppAD::cg::DynamicLib<SC>>(
325 new CppAD::cg::LinuxDynamicLib<SC>(libName + CppAD::cg::system::SystemInfo<>::DYNAMIC_LIB_EXTENSION));
327 throw std::runtime_error(
"Loading dynamic Cppad codegen libraries only supported in Linux.");
clear all close all load ct GNMSLog0 mat reformat t
constexpr size_t n
Definition: MatrixInversionTest.cpp:14