clad
clad copied to clipboard
Simplify generated code by eliminating zero terms to make it more readable
#include "clad/Differentiator/Differentiator.h"
double twox(double x)
{
return 2 * x;
}
int main()
{
auto l_h = clad::differentiate(twox, "x");
l_h.dump();
}
Gives the output:
The code is:
double twox_darg0(double x) {
double _d_x = 1;
return 0 * x + 2 * _d_x;
}
Although zero terms make it easy to see how the chain rule was applied and might be useful for debugging, they also make the output longer and more difficult to read. It would be nice to have an option for dump
or a compile-time flag to do some simplifications on the generated code -- namely, eliminate multiplication by 0
. I know that it's probably optimized out at later compilation stages but maybe something simple could be done in clad too.