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."));
175 for (
const auto &entry : substitution_map)
178 Assert(SymEngine::is_a<SymEngine::Symbol>(*(symbol.
get_RCP())),
179 ExcMessage(
"Key entry in map is not a symbol."));
185 substitution_map.end());
190 template <
typename ReturnType>
193 const SymEngine::map_basic_basic &substitution_map)
201 template <
typename ReturnType>
208 "Cannot register symbols once the optimizer is finalized."));
210 for (
const auto &symbol : symbols)
222 template <
typename ReturnType>
225 const SymEngine::vec_basic &symbols)
233 template <
typename ReturnType>
242 template <
typename ReturnType>
251 template <
typename ReturnType>
257 "Cannot register functions once the optimizer is finalized."));
264 template <
typename ReturnType>
271 "Cannot register functions once the optimizer is finalized."));
278 template <
typename ReturnType>
281 const SymEngine::vec_basic &functions)
289 template <
typename ReturnType>
298 template <
typename ReturnType>
315 template <
typename ReturnType>
320 ExcMessage(
"Cannot call optimize() more than once."));
358# ifdef HAVE_SYMENGINE_LLVM
359 else if (
typename internal::LLVMOptimizer<ReturnType>::OptimizerType
360 *opt =
dynamic_cast<typename internal::LLVMOptimizer<
366 internal::LLVMOptimizer<ReturnType>>::
392 template <
typename ReturnType>
400 "The optimizer is not configured to perform substitution. "
401 "This action can only performed after optimize() has been called."));
411 Assert(symbol_sub_vec.size() == symbol_vec.size(),
413 for (
unsigned int i = 0; i < symbol_sub_vec.size(); ++i)
417 "The input substitution map is either incomplete, or does "
418 "not match that used in the register_symbols() call."));
424 const std::vector<ReturnType> values =
431 template <
typename ReturnType>
434 const SymEngine::map_basic_basic &substitution_map)
const
442 template <
typename ReturnType>
446 const std::vector<ReturnType> &values)
const
456 template <
typename ReturnType>
459 const SymEngine::vec_basic &symbols,
460 const std::vector<ReturnType> &values)
const
469 template <
typename ReturnType>
472 const std::vector<ReturnType> &substitution_values)
const
477 "The optimizer is not configured to perform substitution. "
478 "This action can only performed after optimize() has been called."));
504# ifdef HAVE_SYMENGINE_LLVM
505 else if (
typename internal::LLVMOptimizer<ReturnType>::OptimizerType
506 *opt =
dynamic_cast<typename internal::LLVMOptimizer<
512 internal::LLVMOptimizer<ReturnType>>
::
526 template <
typename ReturnType>
527 const std::vector<ReturnType> &
533 "The optimizer is not configured to perform evaluation. "
534 "This action can only performed after substitute() has been called."));
541 template <
typename ReturnType>
545 const std::vector<ReturnType> &cached_evaluation)
const
554 const typename map_dependent_expression_to_vector_entry_t::const_iterator
573 auto serialize_and_deserialize_expression =
575 std::ostringstream oss;
577 boost::archive::text_oarchive oa(oss,
578 boost::archive::no_header);
584 std::istringstream iss(oss.str());
585 boost::archive::text_iarchive ia(iss,
586 boost::archive::no_header);
595 serialize_and_deserialize_expression(func);
605 serialize_and_deserialize_expression(e.first);
615 return extract(func, cached_evaluation);
622 "Still cannot find map entry, and there's no hope to recover from this situation."));
626 ExcMessage(
"Function has not been registered."));
629 return cached_evaluation[it->second];
634 template <
typename ReturnType>
641 "The optimizer is not configured to perform evaluation. "
642 "This action can only performed after substitute() has been called."));
649 template <
typename ReturnType>
650 std::vector<ReturnType>
652 const std::vector<Expression> &funcs,
653 const std::vector<ReturnType> &cached_evaluation)
const
655 std::vector<ReturnType> out;
656 out.reserve(funcs.size());
658 for (
const auto &func : funcs)
659 out.emplace_back(
extract(func, cached_evaluation));
666 template <
typename ReturnType>
667 std::vector<ReturnType>
669 const std::vector<Expression> &funcs)
const
674 "The optimizer is not configured to perform evaluation. "
675 "This action can only performed after substitute() has been called."));
681 template <
typename ReturnType>
691 template <
typename ReturnType>
694 const SymEngine::RCP<const SymEngine::Basic> &func)
const
700 if (SymEngine::is_a<SymEngine::Constant>(*func))
702 if (&*func == &*SymEngine::zero)
704 if (&*func == &*SymEngine::one)
706 if (&*func == &*SymEngine::minus_one)
708 if (&*func == &*SymEngine::I)
710 if (&*func == &*SymEngine::Inf)
712 if (&*func == &*SymEngine::NegInf)
714 if (&*func == &*SymEngine::ComplexInf)
716 if (&*func == &*SymEngine::Nan)
724 template <
typename ReturnType>
732 "Cannot register function as the optimizer has already been finalized."));
734 const bool entry_registered =
737 if (entry_registered ==
true &&
740 ExcMessage(
"Function has already been registered."));
742 if (entry_registered ==
false)
752 template <
typename ReturnType>
760 "Cannot register function as the optimizer has already been finalized."));
765 for (
const auto &func : funcs)
767 const bool entry_registered =
770 if (entry_registered ==
true &&
773 ExcMessage(
"Function has already been registered."));
775 if (entry_registered ==
false)
786 template <
typename ReturnType>
789 std::unique_ptr<SymEngine::Visitor> &
optimizer)
808# ifdef HAVE_SYMENGINE_LLVM
809 if (internal::LLVMOptimizer<ReturnType>::supported_by_LLVM)
812 typename internal::LLVMOptimizer<ReturnType>::OptimizerType;
834# include "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
#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)