From fb9f42f092ffcf8f60ed76dc1ee2003976015be4 Mon Sep 17 00:00:00 2001
From: Jean-Paul Pelteret <jppelteret@gmail.com>
Date: Thu, 2 Apr 2020 11:13:25 +0200
Subject: [PATCH] Add more utilities to convert from SymEngine types

---
 .../differentiation/sd/symengine_utilities.h  | 24 ++++++++-
 .../differentiation/sd/symengine_utilities.cc | 49 +++++++++++++++++--
 2 files changed, 68 insertions(+), 5 deletions(-)

diff --git a/include/deal.II/differentiation/sd/symengine_utilities.h b/include/deal.II/differentiation/sd/symengine_utilities.h
index 0725503f58..a2e211c41c 100644
--- a/include/deal.II/differentiation/sd/symengine_utilities.h
+++ b/include/deal.II/differentiation/sd/symengine_utilities.h
@@ -1,6 +1,6 @@
 // ---------------------------------------------------------------------
 //
-// Copyright (C) 2019 by the deal.II authors
+// Copyright (C) 2019-2020 by the deal.II authors
 //
 // This file is part of the deal.II library.
 //
@@ -41,6 +41,13 @@ namespace Differentiation
       convert_expression_map_to_basic_map(
         const SD::types::substitution_map &substitution_map);
 
+      /**
+       * Convert to a map of Expressions from its SymEngine counterpart.
+       */
+      SD::types::substitution_map
+      convert_basic_map_to_expression_map(
+        const SymEngine::map_basic_basic &substitution_map);
+
       /**
        * Convert a vector of Expressions to its SymEngine counterpart.
        */
@@ -48,6 +55,21 @@ namespace Differentiation
       convert_expression_vector_to_basic_vector(
         const SD::types::symbol_vector &symbol_vector);
 
+      /**
+       * Convert to a vector of Expressions from its SymEngine counterpart.
+       */
+      SD::types::symbol_vector
+      convert_basic_vector_to_expression_vector(
+        const SymEngine::vec_basic &symbol_vector);
+
+      /**
+       * Convert to a vector of pairs of Expressions from its SymEngine
+       * counterpart.
+       */
+      std::vector<std::pair<Expression, Expression>>
+      convert_basic_pair_vector_to_expression_pair_vector(
+        const SymEngine::vec_pair &symbol_value_vector);
+
       /**
        * Extract the symbols (key entries) from a substitution map.
        *
diff --git a/source/differentiation/sd/symengine_utilities.cc b/source/differentiation/sd/symengine_utilities.cc
index 9bf3798608..f47a4a2715 100644
--- a/source/differentiation/sd/symengine_utilities.cc
+++ b/source/differentiation/sd/symengine_utilities.cc
@@ -1,6 +1,6 @@
 // ---------------------------------------------------------------------
 //
-// Copyright (C) 2019 by the deal.II authors
+// Copyright (C) 2019-2020 by the deal.II authors
 //
 // This file is part of the deal.II library.
 //
@@ -31,6 +31,7 @@ namespace Differentiation
     namespace Utilities
     {
 #  ifndef DOXYGEN
+
       SE::map_basic_basic
       convert_expression_map_to_basic_map(
         const SD::types::substitution_map &substitution_map)
@@ -40,7 +41,19 @@ namespace Differentiation
           sub_map[entry.first.get_RCP()] = entry.second.get_RCP();
         return sub_map;
       }
-#  endif
+
+
+
+      SD::types::substitution_map
+      convert_basic_map_to_expression_map(
+        const SymEngine::map_basic_basic &substitution_map)
+      {
+        SD::types::substitution_map sub_map;
+        for (const auto &entry : substitution_map)
+          sub_map[Expression(entry.first)] = SD::Expression(entry.second);
+        return sub_map;
+      }
+
 
 
       SE::vec_basic
@@ -55,7 +68,20 @@ namespace Differentiation
       }
 
 
-#  ifndef DOXYGEN
+
+      SD::types::symbol_vector
+      convert_basic_vector_to_expression_vector(
+        const SE::vec_basic &symbol_vector)
+      {
+        SD::types::symbol_vector symb_vec;
+        symb_vec.reserve(symbol_vector.size());
+        for (const auto &entry : symbol_vector)
+          symb_vec.push_back(SD::Expression(entry));
+        return symb_vec;
+      }
+
+
+
       SD::types::symbol_vector
       extract_symbols(const SD::types::substitution_map &substitution_values)
       {
@@ -67,7 +93,22 @@ namespace Differentiation
 
         return symbols;
       }
-#  endif
+
+
+
+      std::vector<std::pair<SD::Expression, SD::Expression>>
+      convert_basic_pair_vector_to_expression_pair_vector(
+        const SymEngine::vec_pair &symbol_value_vector)
+      {
+        std::vector<std::pair<SD::Expression, SD::Expression>> symb_val_vec;
+        symb_val_vec.reserve(symbol_value_vector.size());
+        for (const auto &entry : symbol_value_vector)
+          symb_val_vec.push_back(std::make_pair(SD::Expression(entry.first),
+                                                SD::Expression(entry.second)));
+        return symb_val_vec;
+      }
+
+#  endif // DOXYGEN
 
     } // namespace Utilities
 
-- 
2.39.5