Skip to content

erf(x) support #869

Answered by alexander-penev
kkaryos asked this question in Q&A
Apr 25, 2024 · 1 comments · 4 replies
Discussion options

You must be logged in to vote

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 = c…

Replies: 1 comment 4 replies

Comment options

You must be logged in to vote
4 replies
@kkaryos
Comment options

@alexander-penev
Comment options

@alexander-penev
Comment options

@vgvassilev
Comment options

Answer selected by parth-07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
3 participants