From: Jean-Paul Pelteret Date: Sun, 14 Apr 2019 08:57:52 +0000 (+0200) Subject: Add some SD-related convenience functions for scalar operations. X-Git-Tag: v9.1.0-rc1~195^2~1 X-Git-Url: https://gitweb.dealii.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=1556051a5cf11199aa5000bc62a04b4da7b984fd;p=dealii.git Add some SD-related convenience functions for scalar operations. Specifically, these can be used to create a scalar symbol and symbolic function. --- diff --git a/include/deal.II/differentiation/sd.h b/include/deal.II/differentiation/sd.h index e618da4646..2050e6a509 100644 --- a/include/deal.II/differentiation/sd.h +++ b/include/deal.II/differentiation/sd.h @@ -23,6 +23,7 @@ # include # include # include +# include # include # include diff --git a/include/deal.II/differentiation/sd/symengine_scalar_operations.h b/include/deal.II/differentiation/sd/symengine_scalar_operations.h new file mode 100644 index 0000000000..c920cd7de1 --- /dev/null +++ b/include/deal.II/differentiation/sd/symengine_scalar_operations.h @@ -0,0 +1,119 @@ +// --------------------------------------------------------------------- +// +// 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. +// +// --------------------------------------------------------------------- + +#ifndef dealii_differentiation_sd_symengine_scalar_operations_h +#define dealii_differentiation_sd_symengine_scalar_operations_h + +#include + +#ifdef DEAL_II_WITH_SYMENGINE + +# include + +# include +# include + +# include +# include +# include +# include + +DEAL_II_NAMESPACE_OPEN + +namespace Differentiation +{ + namespace SD + { + /** + * @name Symbolic variable creation + */ + //@{ + + /** + * Return an Expression representing a scalar symbolic variable + * with the identifier specified by @p symbol. + * + * For example, if the @p symbol is the string `"x"` then the scalar + * symbolic variable that is returned represents the scalar $x$. + * + * @param[in] symbol An indentifier (or name) for the returned symbolic + * variable. + * @return A scalar symbolic variable with the name @p symbol. + * + * @warning It is up to the user to ensure that there is no ambiguity in the + * symbols used within a section of code. + */ + Expression + make_symbol(const std::string &symbol); + + /** + * Return an Expression representing a scalar symbolic function + * with the identifier specified by @p symbol. The function's symbolic + * dependencies are specified by the input @p arguments. + * + * For example, if the @p symbol is the string `"f"`, and the + * arguments to the function that is generated are the + * symbolic variable `x` and the symbolic expression `y+z`, then the + * generic symbolic function that is returned represents $f(x, y+z)$. + * + * @param[in] symbol An indentifier (or name) for the returned symbolic + * function. + * @param[in] arguments A vector of input arguments to the returned + * symbolic function. + * @return A generic symbolic function with the identifier @p symbolic and + * the number of input arguments equal to the length of @p arguments. + * + * @warning It is up to the user to ensure that there is no ambiguity in the + * symbols used within a section of code. + */ + Expression + make_symbolic_function(const std::string & symbol, + const types::symbol_vector &arguments); + + /** + * Return an Expression representing a scalar symbolic function + * with the identifier specified by @p symbol. The function's symbolic + * dependencies are specified by the keys to the input @p arguments map; + * the values stored in the map are ignored. + * + * For example, if the @p symbol is the string `"f"`, and the + * arguments to the function that is generated are the + * symbolic variable `x` and the symbolic expression `y+z`, then the + * generic symbolic function that is returned represents $f(x, y+z)$. + * + * @param[in] symbol An indentifier (or name) for the returned symbolic + * function. + * @param[in] arguments A map of input arguments to the returned + * symbolic function. + * @return A generic symbolic function with the identifier @p symbolic and + * the number of input arguments equal to the length of @p arguments. + * + * @warning It is up to the user to ensure that there is no ambiguity in the + * symbols used within a section of code. + */ + Expression + make_symbolic_function(const std::string & symbol, + const types::substitution_map &arguments); + + //@} + + } // namespace SD +} // namespace Differentiation + +DEAL_II_NAMESPACE_CLOSE + +#endif // DEAL_II_WITH_SYMENGINE + +#endif diff --git a/source/differentiation/sd/CMakeLists.txt b/source/differentiation/sd/CMakeLists.txt index 26920f47a9..0cb7b56a5e 100644 --- a/source/differentiation/sd/CMakeLists.txt +++ b/source/differentiation/sd/CMakeLists.txt @@ -18,6 +18,7 @@ INCLUDE_DIRECTORIES(BEFORE ${CMAKE_CURRENT_BINARY_DIR}) SET(_src symengine_math.cc symengine_number_types.cc + symengine_scalar_operations.cc symengine_types.cc symengine_utilities.cc ) diff --git a/source/differentiation/sd/symengine_scalar_operations.cc b/source/differentiation/sd/symengine_scalar_operations.cc new file mode 100644 index 0000000000..1757f0dca1 --- /dev/null +++ b/source/differentiation/sd/symengine_scalar_operations.cc @@ -0,0 +1,64 @@ +// --------------------------------------------------------------------- +// +// 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. +// +// --------------------------------------------------------------------- + + +#include + +#ifdef DEAL_II_WITH_SYMENGINE + +# include +# include +# include +# include + +DEAL_II_NAMESPACE_OPEN + +namespace Differentiation +{ + namespace SD + { + /* ------------------------- Symbol creation -----------------------*/ + + + Expression + make_symbol(const std::string &symbol) + { + return Expression(symbol); + } + + + Expression + make_symbolic_function(const std::string & symbol, + const SD::types::symbol_vector &arguments) + { + return Expression(symbol, arguments); + } + + + Expression + make_symbolic_function(const std::string & symbol, + const SD::types::substitution_map &arguments) + { + return make_symbolic_function(symbol, + SD::Utilities::extract_symbols(arguments)); + } + + + } // namespace SD +} // namespace Differentiation + +DEAL_II_NAMESPACE_CLOSE + +#endif // DEAL_II_WITH_SYMENGINE diff --git a/tests/symengine/symengine_scalar_operations_01.cc b/tests/symengine/symengine_scalar_operations_01.cc new file mode 100644 index 0000000000..666be3079e --- /dev/null +++ b/tests/symengine/symengine_scalar_operations_01.cc @@ -0,0 +1,55 @@ +// --------------------------------------------------------------------- +// +// 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 symbols and symbolic functions can be created 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::types::substitution_map sub_map{ + {SD_number_t("c"), SD_number_t(1.0)}, + {SD_number_t("b"), SD_number_t(2)}, + {SD_number_t("a"), SD_number_t(3.0f)}}; + + const SD::types::symbol_vector symbols{SD_number_t("c"), + SD_number_t("b"), + SD_number_t("a")}; + + const SD_number_t symb = SD::make_symbol("d"); + const SD_number_t symb_func_1 = SD::make_symbolic_function("f", symbols); + const SD_number_t symb_func_2 = SD::make_symbolic_function("g", sub_map); + + deallog << "Symbol: " << symb << std::endl; + deallog << "Symbolic function (vector of arguments): " << symb_func_1 + << std::endl; + deallog << "Symbolic function (map of arguments): " << symb_func_2 + << std::endl; + + deallog << "OK" << std::endl; +} diff --git a/tests/symengine/symengine_scalar_operations_01.output b/tests/symengine/symengine_scalar_operations_01.output new file mode 100644 index 0000000000..b81239fe9a --- /dev/null +++ b/tests/symengine/symengine_scalar_operations_01.output @@ -0,0 +1,5 @@ + +DEAL::Symbol: d +DEAL::Symbolic function (vector of arguments): f(c, b, a) +DEAL::Symbolic function (map of arguments): g(a, b, c) +DEAL::OK