-
Dear clad community, #include "clad/Differentiator/Differentiator.h"
#include <iostream>
#include <string>
#include <cmath>
struct Parameters{
double operator()(const double x) const noexcept { return this->a * std::cos(x); }
// uncomment to trigger the issue
// std::string name = "name";
double a = 0;
};
int main() {
// Call clad to generate the derivative of f wrt x.
auto c = [](const Parameters& p, const double x) { return p(x); };
auto f_dx = clad::differentiate(+c, 1);
// Execute the generated derivative function.
std::cout << f_dx.execute(Parameters{.a = 2}, /*x=*/3) << " "
<< -2 * std::sin(3) << std::endl;
// Dump the generated derivative code to standard output.
f_dx.dump();
} If the Is there an intrinsic limitations of P.S.: Note that this example is mostly a way to circumvent the lack of support of lambda of captures which seems to be on the way of being implemented, which will be super useful. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 6 replies
-
Hi Thomas, Thanks for opening the discussion.
I think you meant the As for the issue itself: is there a reason why you are trying to differentiate |
Beta Was this translation helpful? Give feedback.
I agree that it's valid C++, although it's a confusing notation for sure. a couple of things though:
you don't necessarily need to pass a function pointer to
clad::differentiate
. the examples you've provided would work with just passingc
(not+c
) orCallable::f
(not&Callable::f
) respectively. and while it would work with addresses to some extent, please consider the next point.Clad operates at compile time. that is, we don't really have access to the addresses at all, we can't evaluate that. what is actually passed to the differentiator in the end is the expression you put as the first argument. so even if we provide full support for this sort of expressions (the unary plus), that …