erf(x) support #869
-
Can we use clad to differentiate erf(x)? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 4 replies
-
Yes. It is not very well documented yet, but when there is a built-in function (or other) that is not yet supported by clad, we can use the so-called custom_derivatives. This is done as follows: #include "clad/Differentiator/Differentiator.h"
#include <iostream>
#include <math.h>
namespace clad { namespace custom_derivatives {
namespace std {
template <typename T>
ValueAndPushforward<T, T> erf_pushforward(T x, T d_x) {
return {::std::erf(x), M_2_SQRTPI*::std::exp(-::std::pow(x,2.0)) * d_x};
}
}
using std::erf_pushforward; // Required if we use C style function call erf(...) instead C++ std::erf(...)
}}
double f(double x) {
return std::erf(x) + 1;
}
int main() {
auto df = clad::differentiate(f, 0);
df.dump();
std::cout << "f(3)=" << f(3.0) << "; df(3)=" << df.execute(3.0) << std::endl;
return 0;
} If we compile it with
and run the program we will get a working program in which
If this function is not defined, clad generates a warning and uses numeric differentiation for
Of course, in the near future we plan to implement custom derivatives for all built-in functions, as part of a clad, as has already been done for some of the functions in math.h here |
Beta Was this translation helpful? Give feedback.
Yes.
It is not very well documented yet, but when there is a built-in function (or other) that is not yet supported by clad, we can use the so-called custom_derivatives. This is done as follows: