Skip to content

Commit

Permalink
Add fabs_pushforward to built-in derivatives
Browse files Browse the repository at this point in the history
Fixes: #1162
  • Loading branch information
gojakuch committed Dec 7, 2024
1 parent 65330b5 commit dc77b3c
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
6 changes: 6 additions & 0 deletions include/clad/Differentiator/BuiltinDerivatives.h
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,11 @@ CUDA_HOST_DEVICE ValueAndPushforward<T, dT> abs_pushforward(T x, dT d_x) {
return {-x, -d_x};
}

template <typename T, typename dT>
CUDA_HOST_DEVICE ValueAndPushforward<T, dT> fabs_pushforward(T x, dT d_x) {
return abs_pushforward(x, d_x);
}

template <typename T, typename dT>
CUDA_HOST_DEVICE ValueAndPushforward<T, dT> exp_pushforward(T x, dT d_x) {
return {::std::exp(x), ::std::exp(x) * d_x};
Expand Down Expand Up @@ -413,6 +418,7 @@ using std::atan2_pushforward;
using std::ceil_pushforward;
using std::cos_pushforward;
using std::exp_pushforward;
using std::fabs_pushforward;
using std::floor_pushforward;
using std::fma_pullback;
using std::fma_pushforward;
Expand Down
9 changes: 9 additions & 0 deletions test/FirstDerivative/BuiltinDerivatives.C
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,11 @@ float f16(float x) {
//CHECK-NEXT: return _t0.pushforward;
//CHECK-NEXT: }

double f17(double x) {
double y = std::fabs(x);
return 2*y;
}

int main () { //expected-no-diagnostics
float f_result[2];
double d_result[2];
Expand Down Expand Up @@ -377,6 +382,10 @@ int main () { //expected-no-diagnostics

auto f16_darg0 = clad::differentiate(f16, 0);
printf("Result is = %f\n", f16_darg0.execute(0.9)); //CHECK-EXEC: Result is = -2.294157

INIT_GRADIENT(f17);

TEST_GRADIENT(f17, /*numOfDerivativeArgs=*/1, -3, &d_result[0]); // CHECK-EXEC: {-2.00}

return 0;
}

0 comments on commit dc77b3c

Please sign in to comment.