From ee25dccba5d0448f1e59877231c4805db70b20d4 Mon Sep 17 00:00:00 2001 From: Jean-Paul Pelteret Date: Thu, 25 Apr 2019 08:57:02 +0200 Subject: [PATCH] Implement function to merge substitution maps --- .../sd/symengine_scalar_operations.h | 65 +++++++++++++++++++ .../sd/symengine_scalar_operations.cc | 24 +++++++ 2 files changed, 89 insertions(+) diff --git a/include/deal.II/differentiation/sd/symengine_scalar_operations.h b/include/deal.II/differentiation/sd/symengine_scalar_operations.h index 51417f7d4f..777ee737df 100644 --- a/include/deal.II/differentiation/sd/symengine_scalar_operations.h +++ b/include/deal.II/differentiation/sd/symengine_scalar_operations.h @@ -435,6 +435,45 @@ namespace Differentiation const std::pair &symbol_value, const Args &... other_symbol_values); + /** + * Concatenate two symbolic maps, merging a second map @p substitution_map_in + * in-place into the initial and resultant map @p substitution_map_out. The map + * @p substitution_map_out need not initially be empty. + * + * @note Duplicate symbols (keys) in the maps are permitted, so long as their + * values are equal. If this is not the case then an error will be thrown. + */ + void + merge_substitution_maps(types::substitution_map & substitution_map_out, + const types::substitution_map &substitution_map_in); + + /** + * Concatenate multiple symbolic maps, merging the maps @p substitution_map_in + * and @p other_substitution_maps_in, a collection of other maps, + * in-place into the resultant map @p substitution_map_out. The map + * @p substitution_map_out need not initially be empty. + * + * @note Duplicate symbols (keys) in the maps are permitted, so long as their + * values are equal. If this is not the case then an error will be thrown. + */ + template + void + merge_substitution_maps(types::substitution_map & substitution_map_out, + const types::substitution_map &substitution_map_in, + const Args &... other_substitution_maps_in); + + /** + * Concatenate multiple symbolic maps, merging the maps @p substitution_map_in + * and @p other_substitution_maps_in and returning the result. + * + * @note Duplicate symbols (keys) in the maps are permitted, so long as their + * values are equal. If this is not the case then an error will be thrown. + */ + template + types::substitution_map + merge_substitution_maps(const types::substitution_map &substitution_map_in, + const Args &... other_substitution_maps_in); + //@} } // namespace SD @@ -611,6 +650,32 @@ namespace Differentiation other_symbol_values...); } + + template + types::substitution_map + merge_substitution_maps( + const types::substitution_map &substitution_map_in_1, + const Args &... other_substitution_maps_in) + { + types::substitution_map substitution_map_out = substitution_map_in_1; + merge_substitution_maps(substitution_map_out, + other_substitution_maps_in...); + return substitution_map_out; + } + + + template + void + merge_substitution_maps( + types::substitution_map & substitution_map_out, + const types::substitution_map &substitution_map_in_1, + const Args &... other_substitution_maps_in) + { + merge_substitution_maps(substitution_map_out, substitution_map_in_1); + merge_substitution_maps(substitution_map_out, + other_substitution_maps_in...); + } + } // namespace SD } // namespace Differentiation diff --git a/source/differentiation/sd/symengine_scalar_operations.cc b/source/differentiation/sd/symengine_scalar_operations.cc index b9e898b5ac..ea573e5694 100644 --- a/source/differentiation/sd/symengine_scalar_operations.cc +++ b/source/differentiation/sd/symengine_scalar_operations.cc @@ -120,6 +120,30 @@ namespace Differentiation } // namespace internal + + /* ---------------- Symbolic substitution map enlargement --------------*/ + + + void + merge_substitution_maps(types::substitution_map & symb_map_out, + const types::substitution_map &symb_map_in) + { + // Do this by hand so that we can perform some sanity checks + for (const auto &entry : symb_map_in) + { + const typename types::substitution_map::const_iterator it_other = + symb_map_out.find(entry.first); + if (it_other == symb_map_out.end()) + symb_map_out.insert(std::make_pair(entry.first, entry.second)); + else + { + Assert(SE::eq(*(entry.second.get_RCP()), + *(it_other->second.get_RCP())), + ExcMessage("Key already in map, but values don't match")); + } + } + } + } // namespace SD } // namespace Differentiation -- 2.39.5