17#ifdef DEAL_II_WITH_SYMENGINE
22# include <boost/archive/text_iarchive.hpp>
23# include <boost/archive/text_oarchive.hpp>
34 template <
typename ReturnType>
44 template <
typename ReturnType>
55 template <
typename ReturnType>
70 template <
typename ReturnType>
87 template <
typename ReturnType>
96 "Cannot call set_optimization_method() once the optimizer is finalized."));
98# ifndef HAVE_SYMENGINE_LLVM
110 template <
typename ReturnType>
119 template <
typename ReturnType>
128 template <
typename ReturnType>
137 template <
typename ReturnType>
154 template <
typename ReturnType>
163 template <
typename ReturnType>
170 "Cannot register symbols once the optimizer is finalized."));
176 for (
const auto &entry : substitution_map)
179 Assert(SymEngine::is_a<SymEngine::Symbol>(*(symbol.
get_RCP())),
180 ExcMessage(
"Key entry in map is not a symbol."));
186 substitution_map.end());
191 template <
typename ReturnType>
194 const SymEngine::map_basic_basic &substitution_map)
202 template <
typename ReturnType>
209 "Cannot register symbols once the optimizer is finalized."));
211 for (
const auto &symbol : symbols)
223 template <
typename ReturnType>
226 const SymEngine::vec_basic &symbols)
234 template <
typename ReturnType>
243 template <
typename ReturnType>
252 template <
typename ReturnType>
258 "Cannot register functions once the optimizer is finalized."));
265 template <
typename ReturnType>
272 "Cannot register functions once the optimizer is finalized."));
279 template <
typename ReturnType>
282 const SymEngine::vec_basic &functions)
290 template <
typename ReturnType>
299 template <
typename ReturnType>
316 template <
typename ReturnType>
321 ExcMessage(
"Cannot call optimize() more than once."));
359# ifdef HAVE_SYMENGINE_LLVM
360 else if (
typename internal::LLVMOptimizer<ReturnType>::OptimizerType
361 *opt =
dynamic_cast<typename internal::LLVMOptimizer<
367 internal::LLVMOptimizer<ReturnType>>::
393 template <
typename ReturnType>
401 "The optimizer is not configured to perform substitution. "
402 "This action can only performed after optimize() has been called."));
413 Assert(symbol_sub_vec.size() == symbol_vec.size(),
416 for (
unsigned int i = 0; i < symbol_sub_vec.size(); ++i)
421 "The input substitution map is either incomplete, or does "
422 "not match that used in the register_symbols() call."));
428 const std::vector<ReturnType> values =
435 template <
typename ReturnType>
438 const SymEngine::map_basic_basic &substitution_map)
const
446 template <
typename ReturnType>
450 const std::vector<ReturnType> &values)
const
460 template <
typename ReturnType>
463 const SymEngine::vec_basic &symbols,
464 const std::vector<ReturnType> &values)
const
473 template <
typename ReturnType>
476 const std::vector<ReturnType> &substitution_values)
const
481 "The optimizer is not configured to perform substitution. "
482 "This action can only performed after optimize() has been called."));
508# ifdef HAVE_SYMENGINE_LLVM
509 else if (
typename internal::LLVMOptimizer<ReturnType>::OptimizerType
510 *opt =
dynamic_cast<typename internal::LLVMOptimizer<
516 internal::LLVMOptimizer<ReturnType>>
::
530 template <
typename ReturnType>
531 const std::vector<ReturnType> &
537 "The optimizer is not configured to perform evaluation. "
538 "This action can only performed after substitute() has been called."));
545 template <
typename ReturnType>
549 const std::vector<ReturnType> &cached_evaluation)
const
558 const typename map_dependent_expression_to_vector_entry_t::const_iterator
577 auto serialize_and_deserialize_expression =
579 std::ostringstream oss;
581 boost::archive::text_oarchive oa(oss,
582 boost::archive::no_header);
588 std::istringstream iss(oss.str());
589 boost::archive::text_iarchive ia(iss,
590 boost::archive::no_header);
599 serialize_and_deserialize_expression(func);
609 serialize_and_deserialize_expression(e.first);
619 return extract(func, cached_evaluation);
626 "Still cannot find map entry, and there's no hope to recover from this situation."));
630 ExcMessage(
"Function has not been registered."));
633 return cached_evaluation[it->second];
638 template <
typename ReturnType>
645 "The optimizer is not configured to perform evaluation. "
646 "This action can only performed after substitute() has been called."));
653 template <
typename ReturnType>
654 std::vector<ReturnType>
656 const std::vector<Expression> &funcs,
657 const std::vector<ReturnType> &cached_evaluation)
const
659 std::vector<ReturnType> out;
660 out.reserve(funcs.size());
662 for (
const auto &func : funcs)
663 out.emplace_back(
extract(func, cached_evaluation));
670 template <
typename ReturnType>
671 std::vector<ReturnType>
673 const std::vector<Expression> &funcs)
const
678 "The optimizer is not configured to perform evaluation. "
679 "This action can only performed after substitute() has been called."));
685 template <
typename ReturnType>
695 template <
typename ReturnType>
698 const SymEngine::RCP<const SymEngine::Basic> &func)
const
704 if (SymEngine::is_a<SymEngine::Constant>(*func))
706 if (&*func == &*SymEngine::zero)
708 if (&*func == &*SymEngine::one)
710 if (&*func == &*SymEngine::minus_one)
712 if (&*func == &*SymEngine::I)
714 if (&*func == &*SymEngine::Inf)
716 if (&*func == &*SymEngine::NegInf)
718 if (&*func == &*SymEngine::ComplexInf)
720 if (&*func == &*SymEngine::Nan)
728 template <
typename ReturnType>
736 "Cannot register function as the optimizer has already been finalized."));
738 const bool entry_registered =
742 if (entry_registered ==
true &&
745 ExcMessage(
"Function has already been registered."));
747 if (entry_registered ==
false)
757 template <
typename ReturnType>
765 "Cannot register function as the optimizer has already been finalized."));
770 for (
const auto &func : funcs)
772 const bool entry_registered =
776 if (entry_registered ==
true &&
779 ExcMessage(
"Function has already been registered."));
781 if (entry_registered ==
false)
792 template <
typename ReturnType>
795 std::unique_ptr<SymEngine::Visitor> &
optimizer)
814# ifdef HAVE_SYMENGINE_LLVM
815 if (internal::LLVMOptimizer<ReturnType>::supported_by_LLVM)
818 typename internal::LLVMOptimizer<ReturnType>::OptimizerType;
840# include "differentiation/sd/symengine_optimizer.inst"
bool use_symbolic_CSE() const
types::substitution_map independent_variables_symbols
types::symbol_vector dependent_variables_functions
void substitute(const types::substitution_map &substitution_map) const
enum OptimizationFlags flags
void register_scalar_function(const SD::Expression &function)
const types::symbol_vector & get_dependent_functions() const
void create_optimizer(std::unique_ptr< SymEngine::Visitor > &optimizer)
bool ready_for_value_extraction
enum OptimizerType optimization_method() const
void copy_from(const BatchOptimizer &other)
void set_optimization_method(const enum OptimizerType &optimization_method, const enum OptimizationFlags &optimization_flags=OptimizationFlags::optimize_all)
enum OptimizationFlags optimization_flags() const
void register_functions(const types::symbol_vector &functions)
std::vector< ReturnType > dependent_variables_output
enum OptimizerType method
std::size_t n_dependent_variables() const
void register_symbols(const types::substitution_map &substitution_map)
const std::vector< ReturnType > & evaluate() const
void register_function(const Expression &function)
std::unique_ptr< SymEngine::Visitor > optimizer
ReturnType extract(const Expression &func, const std::vector< ReturnType > &cached_evaluation) const
bool is_valid_nonunique_dependent_variable(const SD::Expression &function) const
void register_vector_functions(const types::symbol_vector &functions)
std::size_t n_independent_variables() const
types::symbol_vector get_independent_symbols() const
map_dependent_expression_to_vector_entry_t map_dep_expr_vec_entry
bool values_substituted() const
const SymEngine::RCP< const SymEngine::Basic > & get_RCP() const
const SymEngine::Basic & get_value() const
#define DEAL_II_NAMESPACE_OPEN
constexpr bool running_in_debug_mode()
#define DEAL_II_NAMESPACE_CLOSE
static ::ExceptionBase & ExcNotImplemented()
#define Assert(cond, exc)
static ::ExceptionBase & ExcSymEngineLLVMReturnTypeNotSupported()
static ::ExceptionBase & ExcSymEngineLLVMNotAvailable()
static ::ExceptionBase & ExcInternalError()
static ::ExceptionBase & ExcDimensionMismatch(std::size_t arg1, std::size_t arg2)
static ::ExceptionBase & ExcNotInitialized()
static ::ExceptionBase & ExcMessage(std::string arg1)
#define AssertThrow(cond, exc)
std::vector< NumberType > extract_values(const SD::types::substitution_map &substitution_values)
SD::types::symbol_vector extract_symbols(const SD::types::substitution_map &substitution_values)
SD::types::substitution_map convert_basic_map_to_expression_map(const SymEngine::map_basic_basic &substitution_map)
SD::types::symbol_vector convert_basic_vector_to_expression_vector(const SymEngine::vec_basic &symbol_vector)
SymEngine::vec_basic convert_expression_vector_to_basic_vector(const SD::types::symbol_vector &symbol_vector)
bool use_symbolic_CSE(const enum OptimizationFlags &flags)
std::map< SD::Expression, SD::Expression, internal::ExpressionKeyLess > substitution_map
std::vector< SD::Expression > symbol_vector
Expression substitute(const Expression &expression, const types::substitution_map &substitution_map)
types::substitution_map make_substitution_map(const Expression &symbol, const Expression &value)
constexpr bool values_are_equal(const Number1 &value_1, const Number2 &value_2)