Error evaluating derivative of abs() function using symbolic differentiation

30 views
Skip to first unread message

Vinayak Vijay

unread,
Jul 17, 2023, 3:51:24 AM7/17/23
to deal.II User Group
Hello,

I am trying to differentiate and then evaluate a function with abs(). As a simple example, consider the function to be f(x) = abs(x). I use the following code to first define the symbolic function and then try to evaluate its derivative:

   double x = -2;
Differentiation::SD::Expression x_sd("x");
const Differentiation::SD::types::substitution_map substitution_map =
Differentiation::SD::make_substitution_map(
std::pair<Differentiation::SD::Expression, double>{x_sd, x});
std::cout << "Evaluate f(x) at x = " << x << std::endl;
Differentiation::SD::Expression f = abs(x_sd);
std::cout << "Function f(x)= " << f << std::endl;
const double computed_f =
f.substitute_and_evaluate<double>(substitution_map);
std::cout << "Computed Function f(x)= " << computed_f << std::endl;

Differentiation::SD::Expression df_dx_sd = f.differentiate(x_sd);
std::cout << "Derivative of f: " << df_dx_sd << std::endl;
const double computed_df_dx =
df_dx_sd.substitute_and_evaluate<double>(substitution_map);

std::cout << "Computed df_dx: " << computed_df_dx << std::endl;

However, I get the output as below with an error:

[ 91%] Built target main
[100%] Run main with Debug configuration
Evaluate f(x) at x = -2
Function f(x)=  abs(x)
Computed Function f(x)=  2
Derivative of f:  Derivative(abs(x), x)
terminate called after throwing an instance of 'SymEngine::NotImplementedError'
  what():  Not Implemented
make[3]: *** [CMakeFiles/run.dir/build.make:71: CMakeFiles/run] Aborted (core dumped)
make[2]: *** [CMakeFiles/Makefile2:116: CMakeFiles/run.dir/all] Error 2
make[1]: *** [CMakeFiles/Makefile2:123: CMakeFiles/run.dir/rule] Error 2
make: *** [Makefile:137: run] Error 2

Can someone help me with the issue here?

Thanks
Vinayak

Bruno Turcksin

unread,
Jul 17, 2023, 8:56:17 AM7/17/23
to deal.II User Group
Vinayak,

The error is pretty explicit, the function is not implemented in SymEngine. Maybe it is fixed in a newer version of SymEngine. If it is not, you will need to change the formulation of your equation.

Best,

Bruno

Vinayak Vijay

unread,
Jul 20, 2023, 6:40:04 AM7/20/23
to deal.II User Group
Thanks, I'll implement the function in a different way then.

Regards
Vinayak
Reply all
Reply to author
Forward
0 new messages