15#ifndef dealii_differentiation_sd_symengine_scalar_operations_h
16#define dealii_differentiation_sd_symengine_scalar_operations_h
20#ifdef DEAL_II_WITH_SYMENGINE
27# include <symengine/basic.h>
28# include <symengine/symengine_rcp.h>
31# include <type_traits>
161 const SymEngine::RCP<const SymEngine::Basic> &symbol,
162 const SymEngine::RCP<const SymEngine::Basic> &value);
185 template <
bool ignore_invalid_symbols =
false,
186 typename ValueType = double,
187 typename SymbolicType>
226 template <
bool ignore_invalid_symbols =
false,
227 typename ValueType = double,
228 typename SymbolicType,
269 template <
bool ignore_inval
id_symbols = false,
typename ValueType =
double>
302 template <
bool ignore_invalid_symbols =
false,
303 typename ValueType = double,
304 typename SymbolicType,
305 typename T = std::enable_if_t<
306 !std::is_base_of_v<Expression, SymbolicType> &&
309 const SymEngine::RCP<const SymEngine::Basic> &>::value>>
312 const SymbolicType &symbol);
339 template <
bool ignore_invalid_symbols =
false,
340 typename ValueType = double,
341 typename SymbolicType>
344 const std::vector<SymbolicType> &symbols);
365 template <
bool ignore_inval
id_symbols = false,
typename ValueType =
double>
412 template <
bool ignore_invalid_symbols =
false,
413 typename ValueType = double,
414 typename SymbolicType,
418 const SymbolicType &symbol,
419 const Args &...other_symbols);
460 template <
typename SymbolicType,
462 typename T = std::enable_if_t<
465 const SymEngine::RCP<const SymEngine::Basic> &>::value &&
466 std::is_constructible_v<SymbolicType, ValueType>>>
469 const SymbolicType &symbol,
470 const ValueType &value);
491 template <
typename SymbolicType,
typename ValueType>
494 const std::vector<SymbolicType> &symbols,
495 const std::vector<ValueType> &values);
518 template <
typename SymbolicType,
typename ValueType>
522 const std::pair<SymbolicType, ValueType> &symbol_value);
548 template <
typename SymbolicType,
typename ValueType,
typename... Args>
552 const std::pair<SymbolicType, ValueType> &symbol_value,
553 const Args &...other_symbol_values);
576 template <
typename SymbolicType,
typename ValueType>
580 const std::vector<std::pair<SymbolicType, ValueType>> &symbol_values);
649 template <
typename ExpressionType,
651 typename T = std::enable_if_t<
654 const SymEngine::RCP<const SymEngine::Basic> &>::value &&
655 std::is_constructible_v<ExpressionType, ValueType>>>
680 template <
typename ExpressionType,
typename ValueType>
683 const std::vector<ValueType> &values);
706 template <
typename ExpressionType,
typename ValueType>
709 const std::pair<ExpressionType, ValueType> &symbol_value);
733 template <
typename ExpressionType,
typename ValueType>
736 const std::vector<std::pair<ExpressionType, ValueType>> &symbol_values);
778 template <
typename ExpressionType,
typename ValueType,
typename... Args>
781 const std::pair<ExpressionType, ValueType> &symbol_value,
782 const Args &...other_symbol_values);
815 template <
bool ignore_inval
id_symbols = false>
819 const SymEngine::RCP<const SymEngine::Basic> &symbol,
820 const SymEngine::RCP<const SymEngine::Basic> &value);
853 template <
bool ignore_inval
id_symbols = false>
891 template <
bool ignore_invalid_symbols =
false,
892 typename ExpressionType,
894 typename = std::enable_if_t<
897 const SymEngine::RCP<const SymEngine::Basic> &>::value &&
898 std::is_constructible_v<ExpressionType, ValueType>>>
901 const ExpressionType &symbol,
902 const ValueType &value);
937 template <
bool ignore_invalid_symbols =
false,
938 typename ExpressionType,
940 typename = std::enable_if_t<
943 const SymEngine::RCP<const SymEngine::Basic> &>::value &&
944 std::is_constructible_v<ExpressionType, ValueType>>>
947 const std::vector<ExpressionType> &symbols,
948 const std::vector<ValueType> &values);
966 template <
bool ignore_inval
id_symbols = false>
1011 template <
bool ignore_invalid_symbols =
false,
1012 typename ExpressionType,
1017 const std::pair<ExpressionType, ValueType> &symbol_value);
1063 template <
bool ignore_invalid_symbols =
false,
1064 typename ExpressionType,
1069 const std::vector<std::pair<ExpressionType, ValueType>> &symbol_values);
1115 template <
bool ignore_invalid_symbols =
false,
1116 typename ExpressionType,
1122 const std::pair<ExpressionType, ValueType> &symbol_value,
1123 const Args &...other_symbol_values);
1146 template <
typename... Args>
1150 const Args &...other_substitution_maps_in);
1159 template <
typename... Args>
1162 const Args &...other_substitution_maps_in);
1212 const bool force_cyclic_dependency_resolution =
false);
1244 template <
typename ExpressionType,
typename ValueType>
1247 const std::vector<std::pair<ExpressionType, ValueType>> &symbol_values,
1248 const bool force_cyclic_dependency_resolution =
false);
1321 template <
typename ValueType>
1325 const ValueType &value);
1355 template <
typename ExpressionType,
typename... Args>
1357 substitute(
const ExpressionType &expression,
const Args &...symbol_values);
1384 template <
typename ValueType>
1420 template <
typename ValueType,
typename... Args>
1423 const Args &...symbol_values);
1444 template <
bool ignore_invalid_symbols,
1446 typename SymbolicType>
1451 add_to_symbol_map<ignore_invalid_symbols, ValueType>(symbol_map, symbol);
1456 template <
bool ignore_invalid_symbols,
1458 typename SymbolicType,
1461 make_symbol_map(
const SymbolicType &symbol,
const Args &...other_symbols)
1464 add_to_symbol_map<ignore_invalid_symbols, ValueType>(symbol_map,
1471 template <
bool ignore_inval
id_symbols,
typename ValueType>
1474 const Expression &symbol)
1477 add_to_substitution_map<ignore_invalid_symbols>(
1484 template <
bool ignore_invalid_symbols,
1486 typename SymbolicType,
1490 const SymbolicType &symbol)
1493 using SE_RCP_Basic =
const SymEngine::RCP<const SymEngine::Basic> &;
1494 add_to_substitution_map<ignore_invalid_symbols>(
1496 static_cast<SE_RCP_Basic
>(symbol),
1501 template <
bool ignore_invalid_symbols,
1503 typename SymbolicType>
1506 const std::vector<SymbolicType> &symbols)
1508 for (
const auto &symbol : symbols)
1514 template <
bool ignore_inval
id_symbols,
typename ValueType>
1524 for (types::substitution_map::const_iterator it = other_symbols.begin();
1525 it != other_symbols.end();
1528 Assert(symbol_map.find(it->first) == symbol_map.end(),
1529 ExcMessage(
"Entry already exists in symbol map"));
1530 add_to_symbol_map<ignore_invalid_symbols, ValueType>(
1531 symbol_map, Expression(it->first));
1536 template <
bool ignore_invalid_symbols,
1538 typename SymbolicType,
1542 const SymbolicType &symbol,
1543 const Args &...other_symbols)
1545 add_to_symbol_map<ignore_invalid_symbols, ValueType>(symbol_map, symbol);
1546 add_to_symbol_map<ignore_invalid_symbols, ValueType>(symbol_map,
1551 template <
typename SymbolicType,
typename ValueType,
typename>
1554 const SymbolicType &symbol,
1555 const ValueType &value)
1558 using SE_RCP_Basic =
const SymEngine::RCP<const SymEngine::Basic> &;
1560 static_cast<SE_RCP_Basic
>(symbol),
1561 static_cast<SE_RCP_Basic
>(
1562 SymbolicType(value)));
1566 template <
typename SymbolicType,
typename ValueType>
1569 const std::vector<SymbolicType> &symbols,
1570 const std::vector<ValueType> &values)
1575 typename std::vector<SymbolicType>::const_iterator it_symb =
1577 typename std::vector<ValueType>::const_iterator it_val =
values.begin();
1578 for (; it_symb != symbols.end(); ++it_symb, ++it_val)
1586 template <
typename SymbolicType,
typename ValueType>
1590 const std::pair<SymbolicType, ValueType> &symbol_value)
1594 symbol_value.second);
1598 template <
typename SymbolicType,
typename ValueType,
typename... Args>
1602 const std::pair<SymbolicType, ValueType> &symbol_value,
1603 const Args &...other_symbol_values)
1610 template <
typename SymbolicType,
typename ValueType>
1614 const std::vector<std::pair<SymbolicType, ValueType>> &symbol_values)
1617 for (
const auto &entry : symbol_values)
1625 template <
typename ExpressionType,
typename ValueType,
typename>
1635 template <
typename ExpressionType,
typename ValueType>
1638 const std::vector<ValueType> &values)
1646 template <
typename ExpressionType,
typename ValueType>
1649 const std::pair<ExpressionType, ValueType> &symbol_value)
1657 template <
typename ExpressionType,
typename ValueType>
1660 const std::vector<std::pair<ExpressionType, ValueType>> &symbol_values)
1668 template <
typename ExpressionType,
typename ValueType,
typename... Args>
1671 const std::pair<ExpressionType, ValueType> &symbol_value,
1672 const Args &...other_symbol_values)
1677 other_symbol_values...);
1697 template <
bool ignore_inval
id_symbols>
1701 const SymEngine::RCP<const SymEngine::Basic> &symbol,
1702 const SymEngine::RCP<const SymEngine::Basic> &value)
1704 if (ignore_invalid_symbols ==
false)
1709 "Substitution with a number that does not represent a symbol or symbolic derivative"));
1711 ExcMessage(
"This symbol is already in the map."));
1719 ExcMessage(
"This symbol is already in the map."));
1728 template <
bool ignore_inval
id_symbols>
1731 const Expression &symbol,
1732 const Expression &value)
1734 internal::add_to_substitution_map<ignore_invalid_symbols>(
1735 substitution_map, symbol.get_RCP(),
value.get_RCP());
1739 template <
bool ignore_invalid_symbols,
1740 typename ExpressionType,
1745 const ExpressionType &symbol,
1746 const ValueType &value)
1748 using SE_RCP_Basic =
const SymEngine::RCP<const SymEngine::Basic> &;
1749 internal::add_to_substitution_map<ignore_invalid_symbols>(
1751 static_cast<SE_RCP_Basic
>(symbol),
1752 static_cast<SE_RCP_Basic
>(ExpressionType(value)));
1756 template <
bool ignore_invalid_symbols,
1757 typename ExpressionType,
1762 const std::vector<ExpressionType> &symbols,
1763 const std::vector<ValueType> &values)
1767 "Vector of symbols and values must be of equal length."));
1769 typename types::symbol_vector::const_iterator it_s = symbols.begin();
1770 typename std::vector<ValueType>::const_iterator it_v =
values.begin();
1771 using SE_RCP_Basic =
const SymEngine::RCP<const SymEngine::Basic> &;
1772 for (; it_s != symbols.end(); ++it_s, ++it_v)
1775 internal::add_to_substitution_map<ignore_invalid_symbols>(
1777 static_cast<SE_RCP_Basic
>(*it_s),
1778 static_cast<SE_RCP_Basic
>(ExpressionType(*it_v)));
1783 template <
bool ignore_invalid_symbols,
1784 typename ExpressionType,
1789 const std::pair<ExpressionType, ValueType> &symbol_value)
1791 add_to_substitution_map<ignore_invalid_symbols>(substitution_map,
1793 symbol_value.second);
1797 template <
bool ignore_invalid_symbols,
1798 typename ExpressionType,
1803 const std::vector<std::pair<ExpressionType, ValueType>> &symbol_values)
1805 for (
const auto &entry : symbol_values)
1807 add_to_substitution_map<ignore_invalid_symbols>(substitution_map,
1813 template <
bool ignore_inval
id_symbols>
1818 for (
const auto &entry : symbol_values)
1820 const SymEngine::RCP<const SymEngine::Basic> &symbol = entry.first;
1821 const SymEngine::RCP<const SymEngine::Basic> &
value = entry.second;
1822 internal::add_to_substitution_map<ignore_invalid_symbols>(
1823 substitution_map, symbol, value);
1828 template <
bool ignore_invalid_symbols,
1829 typename ExpressionType,
1835 const std::pair<ExpressionType, ValueType> &symbol_value,
1836 const Args &...other_symbol_values)
1838 add_to_substitution_map<ignore_invalid_symbols>(substitution_map,
1840 add_to_substitution_map<ignore_invalid_symbols>(substitution_map,
1841 other_symbol_values...);
1845 template <
typename... Args>
1849 const Args &...other_substitution_maps_in)
1853 other_substitution_maps_in...);
1854 return substitution_map_out;
1858 template <
typename... Args>
1863 const Args &...other_substitution_maps_in)
1867 other_substitution_maps_in...);
1874 template <
typename ExpressionType,
typename ValueType>
1877 const std::vector<std::pair<ExpressionType, ValueType>> &symbol_values,
1878 const bool force_cyclic_dependency_resolution)
1881 force_cyclic_dependency_resolution);
1885 template <
typename ValueType>
1888 const Expression &symbol,
1889 const ValueType &value)
1895 template <
typename ExpressionType,
typename... Args>
1897 substitute(
const ExpressionType &expression,
const Args &...symbol_values)
1904 template <
typename ValueType>
1913 template <
typename ValueType,
typename... Args>
1916 const Args &...symbol_values)
1919 return substitute_and_evaluate<ValueType>(
Expression substitute(const types::substitution_map &substitution_values) const
#define DEAL_II_NAMESPACE_OPEN
#define DEAL_II_NAMESPACE_CLOSE
#define Assert(cond, exc)
static ::ExceptionBase & ExcInternalError()
static ::ExceptionBase & ExcDimensionMismatch(std::size_t arg1, std::size_t arg2)
static ::ExceptionBase & ExcMessage(std::string arg1)
void add_to_substitution_map(types::substitution_map &substitution_map, const SymEngine::RCP< const SymEngine::Basic > &symbol, const SymEngine::RCP< const SymEngine::Basic > &value)
bool is_valid_substitution_symbol(const SymEngine::Basic &entry)
void set_value_in_symbol_map(types::substitution_map &substitution_map, const SymEngine::RCP< const SymEngine::Basic > &symbol, const SymEngine::RCP< const SymEngine::Basic > &value)
std::vector< SD::Expression > symbol_vector
std::map< SD::Expression, SD::Expression, internal::ExpressionKeyLess > substitution_map
void merge_substitution_maps(types::substitution_map &substitution_map_out, const types::substitution_map &substitution_map_in)
Expression differentiate(const Expression &f, const Expression &x)
ValueType substitute_and_evaluate(const Expression &expression, const types::substitution_map &substitution_map)
types::substitution_map make_symbol_map(const SymbolicType &symbol)
Expression make_symbolic_function(const std::string &symbol, const types::symbol_vector &arguments)
void set_value_in_symbol_map(types::substitution_map &substitution_map, const Expression &symbol, const Expression &value)
void add_to_symbol_map(types::substitution_map &symbol_map, const Expression &symbol)
void add_to_substitution_map(types::substitution_map &substitution_map, const Expression &symbol, const Expression &value)
types::substitution_map resolve_explicit_dependencies(const types::substitution_map &substitution_map, const bool force_cyclic_dependency_resolution=false)
Expression substitute(const Expression &expression, const types::substitution_map &substitution_map)
types::substitution_map make_substitution_map(const Expression &symbol, const Expression &value)
Expression make_symbol(const std::string &symbol)