16#ifndef dealii_differentiation_sd_symengine_scalar_operations_h
17#define dealii_differentiation_sd_symengine_scalar_operations_h
21#ifdef DEAL_II_WITH_SYMENGINE
28# include <symengine/basic.h>
29# include <symengine/symengine_rcp.h>
32# include <type_traits>
162 const SymEngine::RCP<const SymEngine::Basic> &symbol,
163 const SymEngine::RCP<const SymEngine::Basic> &value);
186 template <
bool ignore_invalid_symbols =
false,
187 typename ValueType = double,
188 typename SymbolicType>
227 template <
bool ignore_invalid_symbols =
false,
228 typename ValueType = double,
229 typename SymbolicType,
270 template <
bool ignore_inval
id_symbols = false,
typename ValueType =
double>
303 template <
bool ignore_invalid_symbols =
false,
304 typename ValueType = double,
305 typename SymbolicType,
306 typename T =
typename std::enable_if<
307 !std::is_base_of<Expression, SymbolicType>::value &&
310 const SymEngine::RCP<const SymEngine::Basic> &>::value>::type>
313 const SymbolicType & symbol);
340 template <
bool ignore_invalid_symbols =
false,
341 typename ValueType = double,
342 typename SymbolicType>
345 const std::vector<SymbolicType> &symbols);
366 template <
bool ignore_inval
id_symbols = false,
typename ValueType =
double>
413 template <
bool ignore_invalid_symbols =
false,
414 typename ValueType = double,
415 typename SymbolicType,
419 const SymbolicType & symbol,
420 const Args &...other_symbols);
461 template <
typename SymbolicType,
463 typename T =
typename std::enable_if<
466 const SymEngine::RCP<const SymEngine::Basic> &>::value &&
467 std::is_constructible<SymbolicType, ValueType>::value>::type>
470 const SymbolicType & symbol,
471 const ValueType & value);
492 template <
typename SymbolicType,
typename ValueType>
495 const std::vector<SymbolicType> &symbols,
496 const std::vector<ValueType> & values);
519 template <
typename SymbolicType,
typename ValueType>
523 const std::pair<SymbolicType, ValueType> &symbol_value);
549 template <
typename SymbolicType,
typename ValueType,
typename... Args>
553 const std::pair<SymbolicType, ValueType> &symbol_value,
554 const Args &...other_symbol_values);
577 template <
typename SymbolicType,
typename ValueType>
581 const std::vector<std::pair<SymbolicType, ValueType>> &symbol_values);
650 template <
typename ExpressionType,
652 typename T =
typename std::enable_if<
655 const SymEngine::RCP<const SymEngine::Basic> &>::value &&
656 std::is_constructible<ExpressionType, ValueType>::value>::type>
681 template <
typename ExpressionType,
typename ValueType>
684 const std::vector<ValueType> & values);
707 template <
typename ExpressionType,
typename ValueType>
710 const std::pair<ExpressionType, ValueType> &symbol_value);
734 template <
typename ExpressionType,
typename ValueType>
737 const std::vector<std::pair<ExpressionType, ValueType>> &symbol_values);
779 template <
typename ExpressionType,
typename ValueType,
typename... Args>
782 const std::pair<ExpressionType, ValueType> &symbol_value,
783 const Args &...other_symbol_values);
816 template <
bool ignore_inval
id_symbols = false>
820 const SymEngine::RCP<const SymEngine::Basic> &symbol,
821 const SymEngine::RCP<const SymEngine::Basic> &value);
854 template <
bool ignore_inval
id_symbols = false>
892 template <
bool ignore_invalid_symbols =
false,
893 typename ExpressionType,
895 typename =
typename std::enable_if<
898 const SymEngine::RCP<const SymEngine::Basic> &>::value &&
899 std::is_constructible<ExpressionType, ValueType>::value>::type>
902 const ExpressionType & symbol,
903 const ValueType & value);
938 template <
bool ignore_invalid_symbols =
false,
939 typename ExpressionType,
941 typename =
typename std::enable_if<
944 const SymEngine::RCP<const SymEngine::Basic> &>::value &&
945 std::is_constructible<ExpressionType, ValueType>::value>::type>
948 const std::vector<ExpressionType> &symbols,
949 const std::vector<ValueType> & values);
967 template <
bool ignore_inval
id_symbols = false>
1012 template <
bool ignore_invalid_symbols =
false,
1013 typename ExpressionType,
1018 const std::pair<ExpressionType, ValueType> &symbol_value);
1064 template <
bool ignore_invalid_symbols =
false,
1065 typename ExpressionType,
1070 const std::vector<std::pair<ExpressionType, ValueType>> &symbol_values);
1116 template <
bool ignore_invalid_symbols =
false,
1117 typename ExpressionType,
1123 const std::pair<ExpressionType, ValueType> &symbol_value,
1124 const Args &...other_symbol_values);
1147 template <
typename... Args>
1151 const Args &...other_substitution_maps_in);
1160 template <
typename... Args>
1163 const Args &...other_substitution_maps_in);
1213 const bool force_cyclic_dependency_resolution =
false);
1245 template <
typename ExpressionType,
typename ValueType>
1248 const std::vector<std::pair<ExpressionType, ValueType>> &symbol_values,
1249 const bool force_cyclic_dependency_resolution =
false);
1322 template <
typename ValueType>
1326 const ValueType & value);
1356 template <
typename ExpressionType,
typename... Args>
1358 substitute(
const ExpressionType &expression,
const Args &...symbol_values);
1385 template <
typename ValueType>
1421 template <
typename ValueType,
typename... Args>
1424 const Args &...symbol_values);
1445 template <
bool ignore_invalid_symbols,
1447 typename SymbolicType>
1452 add_to_symbol_map<ignore_invalid_symbols, ValueType>(symbol_map, symbol);
1457 template <
bool ignore_invalid_symbols,
1459 typename SymbolicType,
1462 make_symbol_map(
const SymbolicType &symbol,
const Args &...other_symbols)
1465 add_to_symbol_map<ignore_invalid_symbols, ValueType>(symbol_map,
1472 template <
bool ignore_inval
id_symbols,
typename ValueType>
1475 const Expression & symbol)
1478 add_to_substitution_map<ignore_invalid_symbols>(
1485 template <
bool ignore_invalid_symbols,
1487 typename SymbolicType,
1491 const SymbolicType & symbol)
1494 using SE_RCP_Basic =
const SymEngine::RCP<const SymEngine::Basic> &;
1495 add_to_substitution_map<ignore_invalid_symbols>(
1497 static_cast<SE_RCP_Basic
>(symbol),
1502 template <
bool ignore_invalid_symbols,
1504 typename SymbolicType>
1507 const std::vector<SymbolicType> &symbols)
1509 for (
const auto &symbol : symbols)
1510 add_to_symbol_map<ignore_invalid_symbols, ValueType>(symbol_map,
1515 template <
bool ignore_inval
id_symbols,
typename ValueType>
1525 for (types::substitution_map::const_iterator it = other_symbols.begin();
1526 it != other_symbols.end();
1529 Assert(symbol_map.find(it->first) == symbol_map.end(),
1530 ExcMessage(
"Entry already exists in symbol map"));
1531 add_to_symbol_map<ignore_invalid_symbols, ValueType>(
1532 symbol_map, Expression(it->first));
1537 template <
bool ignore_invalid_symbols,
1539 typename SymbolicType,
1543 const SymbolicType & symbol,
1544 const Args &...other_symbols)
1546 add_to_symbol_map<ignore_invalid_symbols, ValueType>(symbol_map, symbol);
1547 add_to_symbol_map<ignore_invalid_symbols, ValueType>(symbol_map,
1552 template <
typename SymbolicType,
typename ValueType,
typename>
1555 const SymbolicType & symbol,
1556 const ValueType & value)
1559 using SE_RCP_Basic =
const SymEngine::RCP<const SymEngine::Basic> &;
1561 static_cast<SE_RCP_Basic
>(symbol),
1562 static_cast<SE_RCP_Basic
>(
1563 SymbolicType(value)));
1567 template <
typename SymbolicType,
typename ValueType>
1570 const std::vector<SymbolicType> &symbols,
1571 const std::vector<ValueType> & values)
1576 typename std::vector<SymbolicType>::const_iterator it_symb =
1578 typename std::vector<ValueType>::const_iterator it_val =
values.begin();
1579 for (; it_symb != symbols.end(); ++it_symb, ++it_val)
1587 template <
typename SymbolicType,
typename ValueType>
1591 const std::pair<SymbolicType, ValueType> &symbol_value)
1595 symbol_value.second);
1599 template <
typename SymbolicType,
typename ValueType,
typename... Args>
1603 const std::pair<SymbolicType, ValueType> &symbol_value,
1604 const Args &...other_symbol_values)
1611 template <
typename SymbolicType,
typename ValueType>
1615 const std::vector<std::pair<SymbolicType, ValueType>> &symbol_values)
1618 for (
const auto &entry : symbol_values)
1626 template <
typename ExpressionType,
typename ValueType,
typename>
1636 template <
typename ExpressionType,
typename ValueType>
1639 const std::vector<ValueType> & values)
1647 template <
typename ExpressionType,
typename ValueType>
1650 const std::pair<ExpressionType, ValueType> &symbol_value)
1658 template <
typename ExpressionType,
typename ValueType>
1661 const std::vector<std::pair<ExpressionType, ValueType>> &symbol_values)
1669 template <
typename ExpressionType,
typename ValueType,
typename... Args>
1672 const std::pair<ExpressionType, ValueType> &symbol_value,
1673 const Args &...other_symbol_values)
1678 other_symbol_values...);
1698 template <
bool ignore_inval
id_symbols>
1702 const SymEngine::RCP<const SymEngine::Basic> &symbol,
1703 const SymEngine::RCP<const SymEngine::Basic> &value)
1705 if (ignore_invalid_symbols ==
false)
1710 "Substitution with a number that does not represent a symbol or symbolic derivative"));
1712 ExcMessage(
"This symbol is already in the map."));
1720 ExcMessage(
"This symbol is already in the map."));
1729 template <
bool ignore_inval
id_symbols>
1732 const Expression & symbol,
1733 const Expression & value)
1735 internal::add_to_substitution_map<ignore_invalid_symbols>(
1736 substitution_map, symbol.get_RCP(),
value.get_RCP());
1740 template <
bool ignore_invalid_symbols,
1741 typename ExpressionType,
1746 const ExpressionType & symbol,
1747 const ValueType & value)
1749 using SE_RCP_Basic =
const SymEngine::RCP<const SymEngine::Basic> &;
1750 internal::add_to_substitution_map<ignore_invalid_symbols>(
1752 static_cast<SE_RCP_Basic
>(symbol),
1753 static_cast<SE_RCP_Basic
>(ExpressionType(value)));
1757 template <
bool ignore_invalid_symbols,
1758 typename ExpressionType,
1763 const std::vector<ExpressionType> &symbols,
1764 const std::vector<ValueType> & values)
1768 "Vector of symbols and values must be of equal length."));
1770 typename types::symbol_vector::const_iterator it_s = symbols.begin();
1771 typename std::vector<ValueType>::const_iterator it_v =
values.begin();
1772 using SE_RCP_Basic =
const SymEngine::RCP<const SymEngine::Basic> &;
1773 for (; it_s != symbols.end(); ++it_s, ++it_v)
1776 internal::add_to_substitution_map<ignore_invalid_symbols>(
1778 static_cast<SE_RCP_Basic
>(*it_s),
1779 static_cast<SE_RCP_Basic
>(ExpressionType(*it_v)));
1784 template <
bool ignore_invalid_symbols,
1785 typename ExpressionType,
1790 const std::pair<ExpressionType, ValueType> &symbol_value)
1792 add_to_substitution_map<ignore_invalid_symbols>(substitution_map,
1794 symbol_value.second);
1798 template <
bool ignore_invalid_symbols,
1799 typename ExpressionType,
1804 const std::vector<std::pair<ExpressionType, ValueType>> &symbol_values)
1806 for (
const auto &entry : symbol_values)
1808 add_to_substitution_map<ignore_invalid_symbols>(substitution_map,
1814 template <
bool ignore_inval
id_symbols>
1819 for (
const auto &entry : symbol_values)
1821 const SymEngine::RCP<const SymEngine::Basic> &symbol = entry.first;
1822 const SymEngine::RCP<const SymEngine::Basic> &
value = entry.second;
1823 internal::add_to_substitution_map<ignore_invalid_symbols>(
1824 substitution_map, symbol, value);
1829 template <
bool ignore_invalid_symbols,
1830 typename ExpressionType,
1836 const std::pair<ExpressionType, ValueType> &symbol_value,
1837 const Args &...other_symbol_values)
1839 add_to_substitution_map<ignore_invalid_symbols>(substitution_map,
1841 add_to_substitution_map<ignore_invalid_symbols>(substitution_map,
1842 other_symbol_values...);
1846 template <
typename... Args>
1850 const Args &...other_substitution_maps_in)
1854 other_substitution_maps_in...);
1855 return substitution_map_out;
1859 template <
typename... Args>
1864 const Args &...other_substitution_maps_in)
1868 other_substitution_maps_in...);
1875 template <
typename ExpressionType,
typename ValueType>
1878 const std::vector<std::pair<ExpressionType, ValueType>> &symbol_values,
1879 const bool force_cyclic_dependency_resolution)
1882 force_cyclic_dependency_resolution);
1886 template <
typename ValueType>
1889 const Expression &symbol,
1890 const ValueType & value)
1896 template <
typename ExpressionType,
typename... Args>
1898 substitute(
const ExpressionType &expression,
const Args &...symbol_values)
1905 template <
typename ValueType>
1914 template <
typename ValueType,
typename... Args>
1917 const Args &...symbol_values)
1920 return substitute_and_evaluate<ValueType>(
#define DEAL_II_NAMESPACE_OPEN
#define DEAL_II_NAMESPACE_CLOSE
#define Assert(cond, exc)
Expression substitute(const types::substitution_map &substitution_values) const
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)