diff --git a/include/clad/Differentiator/BuiltinDerivatives.h b/include/clad/Differentiator/BuiltinDerivatives.h index 0221dbe28..fefb5b66e 100644 --- a/include/clad/Differentiator/BuiltinDerivatives.h +++ b/include/clad/Differentiator/BuiltinDerivatives.h @@ -207,6 +207,11 @@ CUDA_HOST_DEVICE ValueAndPushforward abs_pushforward(T x, dT d_x) { return {-x, -d_x}; } +template +CUDA_HOST_DEVICE ValueAndPushforward fabs_pushforward(T x, dT d_x) { + return abs_pushforward(x, d_x); +} + template CUDA_HOST_DEVICE ValueAndPushforward exp_pushforward(T x, dT d_x) { return {::std::exp(x), ::std::exp(x) * d_x}; @@ -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; diff --git a/test/FirstDerivative/BuiltinDerivatives.C b/test/FirstDerivative/BuiltinDerivatives.C index 95c658204..1d0807176 100644 --- a/test/FirstDerivative/BuiltinDerivatives.C +++ b/test/FirstDerivative/BuiltinDerivatives.C @@ -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]; @@ -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; }