16 #ifndef dealii_differentiation_sd_symengine_scalar_operations_h 17 #define dealii_differentiation_sd_symengine_scalar_operations_h 19 #include <deal.II/base/config.h> 21 #ifdef DEAL_II_WITH_SYMENGINE 23 # include <deal.II/base/numbers.h> 25 # include <deal.II/differentiation/sd/symengine_number_types.h> 26 # include <deal.II/differentiation/sd/symengine_types.h> 28 # include <symengine/basic.h> 29 # include <symengine/symengine_rcp.h> 32 # include <type_traits> 36 DEAL_II_NAMESPACE_OPEN
86 const types::symbol_vector &arguments);
111 const types::substitution_map &arguments);
148 is_valid_substitution_symbol(
const SymEngine::Basic &entry);
161 types::substitution_map & substitution_map,
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>
189 types::substitution_map
227 template <
bool ignore_invalid_symbols =
false,
228 typename ValueType = double,
229 typename SymbolicType,
231 types::substitution_map
232 make_symbol_map(
const SymbolicType &symbol,
const Args &... other_symbols);
270 template <
bool ignore_inval
id_symbols = false,
typename ValueType =
double>
273 const Expression & symbol);
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>
369 const types::substitution_map &other_symbols);
413 template <
bool ignore_invalid_symbols =
false,
414 typename ValueType = double,
415 typename SymbolicType,
419 const SymbolicType & symbol,
420 const Args &... other_symbols);
432 const Expression & symbol,
433 const Expression & value);
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>
522 types::substitution_map & substitution_map,
523 const std::pair<SymbolicType, ValueType> &symbol_value);
549 template <
typename SymbolicType,
typename ValueType,
typename... Args>
552 types::substitution_map & substitution_map,
553 const std::pair<SymbolicType, ValueType> &symbol_value,
554 const Args &... other_symbol_values);
577 template <
typename SymbolicType,
typename ValueType>
580 types::substitution_map & substitution_map,
581 const std::vector<std::pair<SymbolicType, ValueType>> &symbol_values);
595 const types::substitution_map &symbol_values);
616 types::substitution_map
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>
657 types::substitution_map
681 template <
typename ExpressionType,
typename ValueType>
682 types::substitution_map
684 const std::vector<ValueType> & values);
707 template <
typename ExpressionType,
typename ValueType>
708 types::substitution_map
710 const std::pair<ExpressionType, ValueType> &symbol_value);
734 template <
typename ExpressionType,
typename ValueType>
735 types::substitution_map
737 const std::vector<std::pair<ExpressionType, ValueType>> &symbol_values);
779 template <
typename ExpressionType,
typename ValueType,
typename... Args>
780 types::substitution_map
782 const std::pair<ExpressionType, ValueType> &symbol_value,
783 const Args &... other_symbol_values);
816 template <
bool ignore_inval
id_symbols = false>
819 types::substitution_map & substitution_map,
820 const SymEngine::RCP<const SymEngine::Basic> &symbol,
821 const SymEngine::RCP<const SymEngine::Basic> &value);
854 template <
bool ignore_inval
id_symbols = false>
857 const Expression & symbol,
858 const Expression & value);
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>
970 const types::substitution_map &symbol_values);
1012 template <
bool ignore_invalid_symbols =
false,
1013 typename ExpressionType,
1017 types::substitution_map & substitution_map,
1018 const std::pair<ExpressionType, ValueType> &symbol_value);
1064 template <
bool ignore_invalid_symbols =
false,
1065 typename ExpressionType,
1069 types::substitution_map & substitution_map,
1070 const std::vector<std::pair<ExpressionType, ValueType>> &symbol_values);
1116 template <
bool ignore_invalid_symbols =
false,
1117 typename ExpressionType,
1122 types::substitution_map & substitution_map,
1123 const std::pair<ExpressionType, ValueType> &symbol_value,
1124 const Args &... other_symbol_values);
1136 const types::substitution_map &substitution_map_in);
1147 template <
typename... Args>
1150 const types::substitution_map &substitution_map_in,
1151 const Args &... other_substitution_maps_in);
1160 template <
typename... Args>
1161 types::substitution_map
1163 const Args &... other_substitution_maps_in);
1210 types::substitution_map
1212 const types::substitution_map &substitution_map,
1213 const bool force_cyclic_dependency_resolution =
false);
1245 template <
typename ExpressionType,
typename ValueType>
1246 types::substitution_map
1248 const std::vector<std::pair<ExpressionType, ValueType>> &symbol_values,
1249 const bool force_cyclic_dependency_resolution =
false);
1299 const types::substitution_map &substitution_map);
1322 template <
typename ValueType>
1325 const Expression &symbol,
1326 const ValueType & value);
1356 template <
typename ExpressionType,
typename... Args>
1358 substitute(
const ExpressionType &expression,
const Args &... symbol_values);
1385 template <
typename ValueType>
1388 const types::substitution_map &substitution_map);
1421 template <
typename ValueType,
typename... Args>
1424 const Args &... symbol_values);
1445 template <
bool ignore_invalid_symbols,
1447 typename SymbolicType>
1448 types::substitution_map
1451 types::substitution_map symbol_map;
1452 add_to_symbol_map<ignore_invalid_symbols, ValueType>(symbol_map, symbol);
1457 template <
bool ignore_invalid_symbols,
1459 typename SymbolicType,
1461 types::substitution_map
1462 make_symbol_map(
const SymbolicType &symbol,
const Args &... other_symbols)
1464 types::substitution_map symbol_map;
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>
1518 const types::substitution_map &other_symbols)
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> &;
1560 internal::set_value_in_symbol_map(substitution_map,
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)
1573 Assert(symbols.size() == values.size(),
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>
1590 types::substitution_map & substitution_map,
1591 const std::pair<SymbolicType, ValueType> &symbol_value)
1595 symbol_value.second);
1599 template <
typename SymbolicType,
typename ValueType,
typename... Args>
1602 types::substitution_map & substitution_map,
1603 const std::pair<SymbolicType, ValueType> &symbol_value,
1604 const Args &... other_symbol_values)
1611 template <
typename SymbolicType,
typename ValueType>
1614 types::substitution_map & substitution_map,
1615 const std::vector<std::pair<SymbolicType, ValueType>> &symbol_values)
1618 for (
const auto &entry : symbol_values)
1626 template <
typename ExpressionType,
typename ValueType,
typename>
1627 types::substitution_map
1630 types::substitution_map substitution_map;
1632 return substitution_map;
1636 template <
typename ExpressionType,
typename ValueType>
1637 types::substitution_map
1639 const std::vector<ValueType> & values)
1641 types::substitution_map substitution_map;
1643 return substitution_map;
1647 template <
typename ExpressionType,
typename ValueType>
1648 types::substitution_map
1650 const std::pair<ExpressionType, ValueType> &symbol_value)
1652 types::substitution_map substitution_map;
1654 return substitution_map;
1658 template <
typename ExpressionType,
typename ValueType>
1659 types::substitution_map
1661 const std::vector<std::pair<ExpressionType, ValueType>> &symbol_values)
1663 types::substitution_map substitution_map;
1665 return substitution_map;
1669 template <
typename ExpressionType,
typename ValueType,
typename... Args>
1670 types::substitution_map
1672 const std::pair<ExpressionType, ValueType> &symbol_value,
1673 const Args &... other_symbol_values)
1675 types::substitution_map substitution_map;
1678 other_symbol_values...);
1679 return substitution_map;
1698 template <
bool ignore_inval
id_symbols>
1701 types::substitution_map & substitution_map,
1702 const SymEngine::RCP<const SymEngine::Basic> &symbol,
1703 const SymEngine::RCP<const SymEngine::Basic> &value)
1705 if (ignore_invalid_symbols ==
false)
1708 internal::is_valid_substitution_symbol(*symbol),
1710 "Substitution with a number that does not represent a symbol or symbolic derivative"));
1711 Assert(substitution_map.find(symbol) == substitution_map.end(),
1712 ExcMessage(
"This symbol is already in the map."));
1713 substitution_map[Expression(symbol)] = Expression(value);
1717 if (internal::is_valid_substitution_symbol(*symbol))
1719 Assert(substitution_map.find(symbol) == substitution_map.end(),
1720 ExcMessage(
"This symbol is already in the map."));
1721 substitution_map[Expression(symbol)] = Expression(value);
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)
1766 Assert(symbols.size() == values.size(),
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,
1789 types::substitution_map & substitution_map,
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,
1803 types::substitution_map & substitution_map,
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>
1817 const types::substitution_map &symbol_values)
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,
1835 types::substitution_map & substitution_map,
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>
1847 types::substitution_map
1849 const types::substitution_map &substitution_map_in_1,
1850 const Args &... other_substitution_maps_in)
1852 types::substitution_map substitution_map_out = substitution_map_in_1;
1854 other_substitution_maps_in...);
1855 return substitution_map_out;
1859 template <
typename... Args>
1862 types::substitution_map & substitution_map_out,
1863 const types::substitution_map &substitution_map_in_1,
1864 const Args &... other_substitution_maps_in)
1868 other_substitution_maps_in...);
1875 template <
typename ExpressionType,
typename ValueType>
1876 types::substitution_map
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>
1908 const types::substitution_map &substitution_map)
1910 return expression.substitute_and_evaluate<ValueType>(substitution_map);
1914 template <
typename ValueType,
typename... Args>
1917 const Args &... symbol_values)
1920 return substitute_and_evaluate<ValueType>(
1930 DEAL_II_NAMESPACE_CLOSE
1932 #endif // DEAL_II_WITH_SYMENGINE
void set_value_in_symbol_map(types::substitution_map &substitution_map, const Expression &symbol, const Expression &value)
types::substitution_map make_symbol_map(const SymbolicType &symbol)
void merge_substitution_maps(types::substitution_map &substitution_map_out, const types::substitution_map &substitution_map_in)
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
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)
static ::ExceptionBase & ExcDimensionMismatch(std::size_t arg1, std::size_t arg2)
ValueType substitute_and_evaluate(const Expression &expression, const types::substitution_map &substitution_map)
void add_to_symbol_map(types::substitution_map &symbol_map, const Expression &symbol)
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)
static ::ExceptionBase & ExcInternalError()