17 #include <deal.II/base/config.h> 19 #ifdef DEAL_II_WITH_SYMENGINE 21 # include <deal.II/differentiation/sd/symengine_number_types.h> 22 # include <deal.II/differentiation/sd/symengine_scalar_operations.h> 23 # include <deal.II/differentiation/sd/symengine_types.h> 24 # include <deal.II/differentiation/sd/symengine_utilities.h> 26 # include <symengine/real_double.h> 28 DEAL_II_NAMESPACE_OPEN
34 namespace SE = ::SymEngine;
49 const SD::types::symbol_vector &arguments)
57 const SD::types::substitution_map &arguments)
60 SD::Utilities::extract_symbols(arguments));
80 is_valid_substitution_symbol(
const SE::Basic &entry)
84 if (SE::is_a<SE::Symbol>(entry))
92 if (SE::is_a<SE::FunctionSymbol>(entry))
98 if (SE::is_a<SE::Derivative>(entry))
105 if (SE::is_a<SE::Mul>(entry))
107 const SE::Mul &entry_mul = SE::down_cast<
const SE::Mul &>(entry);
109 if (SE::eq(*(entry_mul.get_coef()), *SE::real_double(0.5)))
113 const SE::map_basic_basic &entry_mul_dict =
114 entry_mul.get_dict();
115 if (entry_mul_dict.size() == 1 &&
116 SE::is_a<SE::Derivative>(*(entry_mul_dict.begin()->first)))
127 types::substitution_map & substitution_map,
128 const SymEngine::RCP<const SymEngine::Basic> &symbol,
129 const SymEngine::RCP<const SymEngine::Basic> &value)
132 internal::is_valid_substitution_symbol(*symbol),
134 "Substitution with a number that does not represent a symbol or symbolic derivative"));
136 auto it_sym = substitution_map.find(Expression(symbol));
137 Assert(it_sym != substitution_map.end(),
138 ExcMessage(
"Did not find this symbol in the map."));
140 it_sym->second = Expression(value);
151 internal::set_value_in_symbol_map(substitution_map,
159 const types::substitution_map &symbol_values)
161 for (
const auto &entry : symbol_values)
169 types::substitution_map
172 types::substitution_map substitution_map;
174 return substitution_map;
183 const types::substitution_map &symb_map_in)
186 for (
const auto &entry : symb_map_in)
188 const typename types::substitution_map::const_iterator it_other =
189 symb_map_out.find(entry.first);
190 if (it_other == symb_map_out.end())
191 symb_map_out.insert(std::make_pair(entry.first, entry.second));
194 Assert(SE::eq(*(entry.second.get_RCP()),
195 *(it_other->second.get_RCP())),
196 ExcMessage(
"Key already in map, but values don't match"));
205 types::substitution_map
207 const bool force_cyclic_dependency_resolution)
209 types::substitution_map symbol_values_resolved = symbol_values;
210 const std::size_t size = symbol_values.size();
212 for (
auto &entry : symbol_values_resolved)
221 SE::hash_t hash_new = out.
get_RCP()->hash();
222 unsigned int iter = 0;
227 if (force_cyclic_dependency_resolution)
242 hash_old = std::move(hash_new);
243 hash_new = out.
get_RCP()->hash();
247 "Unresolvable cyclic dependency detected in substitution map."));
250 while (hash_new != hash_old);
253 return symbol_values_resolved;
259 const types::substitution_map &substitution_map)
261 return expression.
substitute(substitution_map);
267 DEAL_II_NAMESPACE_CLOSE
269 #endif // DEAL_II_WITH_SYMENGINE Expression differentiate(const Expression &symbol) const
void set_value_in_symbol_map(types::substitution_map &substitution_map, const Expression &symbol, const Expression &value)
void merge_substitution_maps(types::substitution_map &substitution_map_out, const types::substitution_map &substitution_map_in)
#define AssertThrow(cond, exc)
Expression make_symbolic_function(const std::string &symbol, const types::symbol_vector &arguments)
types::substitution_map resolve_explicit_dependencies(const types::substitution_map &substitution_map, const bool force_cyclic_dependency_resolution=false)
Expression differentiate(const Expression &f, const Expression &x)
Expression substitute(const types::substitution_map &substitution_values) const
const SymEngine::RCP< const SymEngine::Basic > & get_RCP() const
void add_to_substitution_map(types::substitution_map &substitution_map, const Expression &symbol, const Expression &value)
static ::ExceptionBase & ExcMessage(std::string arg1)
#define Assert(cond, exc)
types::substitution_map make_substitution_map(const Expression &symbol, const Expression &value)
Expression make_symbol(const std::string &symbol)
Expression substitute(const Expression &expression, const types::substitution_map &substitution_map)