From c74e7b6cede8a01f94ab4ac1b7002b5961b66303 Mon Sep 17 00:00:00 2001 From: Jean-Paul Pelteret Date: Sun, 14 Apr 2019 13:09:07 +0200 Subject: [PATCH] Add some SD-related convenience functions for scalar operations. Specifically, a non-member function for scalar differentiation of an Expression has been added. --- .../sd/symengine_scalar_operations.h | 21 +++++++++ .../sd/symengine_scalar_operations.cc | 9 ++++ .../symengine_scalar_operations_02.cc | 44 +++++++++++++++++++ .../symengine_scalar_operations_02.output | 5 +++ 4 files changed, 79 insertions(+) create mode 100644 tests/symengine/symengine_scalar_operations_02.cc create mode 100644 tests/symengine/symengine_scalar_operations_02.output diff --git a/include/deal.II/differentiation/sd/symengine_scalar_operations.h b/include/deal.II/differentiation/sd/symengine_scalar_operations.h index c920cd7de1..15b9dc7529 100644 --- a/include/deal.II/differentiation/sd/symengine_scalar_operations.h +++ b/include/deal.II/differentiation/sd/symengine_scalar_operations.h @@ -109,6 +109,27 @@ namespace Differentiation //@} + /** + * @name Symbolic differentiation + */ + //@{ + + /** + * Return the symbolic result of computing the partial derivative of the + * scalar @p f with respect to the scalar @p x. + * In most use cases the function or variable @f would be called the + * dependent variable, and @p x the independent variable. + * + * @param[in] f A scalar symbolic function or (dependent) variable. + * @param[in] x A scalar symbolic (independent) variable. + * @return The symbolic function or variable representing the result + * $\frac{\partial f}{\partial x}$. + */ + Expression + differentiate(const Expression &f, const Expression &x); + + //@} + } // namespace SD } // namespace Differentiation diff --git a/source/differentiation/sd/symengine_scalar_operations.cc b/source/differentiation/sd/symengine_scalar_operations.cc index 1757f0dca1..7df4e22cc0 100644 --- a/source/differentiation/sd/symengine_scalar_operations.cc +++ b/source/differentiation/sd/symengine_scalar_operations.cc @@ -56,6 +56,15 @@ namespace Differentiation } + /* --------------------------- Differentiation ------------------------- */ + + + Expression + differentiate(const Expression &func, const Expression &op) + { + return func.differentiate(op); + } + } // namespace SD } // namespace Differentiation diff --git a/tests/symengine/symengine_scalar_operations_02.cc b/tests/symengine/symengine_scalar_operations_02.cc new file mode 100644 index 0000000000..db2e61af51 --- /dev/null +++ b/tests/symengine/symengine_scalar_operations_02.cc @@ -0,0 +1,44 @@ +// --------------------------------------------------------------------- +// +// Copyright (C) 2019 by the deal.II authors +// +// This file is part of the deal.II library. +// +// The deal.II library is free software; you can use it, redistribute +// it, and/or modify it under the terms of the GNU Lesser General +// Public License as published by the Free Software Foundation; either +// version 2.1 of the License, or (at your option) any later version. +// The full text of the license can be found in the file LICENSE at +// the top level of the deal.II distribution. +// +// --------------------------------------------------------------------- + + +// Check that scalar differentiation can be performed using the utility +// functions + +#include + +#include "../tests.h" + +using namespace dealii; +namespace SD = Differentiation::SD; + + +int +main() +{ + initlog(); + + using SD_number_t = SD::Expression; + + const SD_number_t x = SD::make_symbol("x"); + const SD_number_t f = 2 * x * x; + const SD_number_t df_dx = SD::differentiate(f, x); + + deallog << "x: " << x << std::endl; + deallog << "f: " << f << std::endl; + deallog << "df_dx: " << df_dx << std::endl; + + deallog << "OK" << std::endl; +} diff --git a/tests/symengine/symengine_scalar_operations_02.output b/tests/symengine/symengine_scalar_operations_02.output new file mode 100644 index 0000000000..91cad5cafa --- /dev/null +++ b/tests/symengine/symengine_scalar_operations_02.output @@ -0,0 +1,5 @@ + +DEAL::x: x +DEAL::f: 2*x**2 +DEAL::df_dx: 4*x +DEAL::OK -- 2.39.5