deal.II version GIT relicensing-7105-gf306696fdd 2026-11-19 22:00:01+00:00
\(\newcommand{\dealvcentcolon}{\mathrel{\mathop{:}}}\) \(\newcommand{\dealcoloneq}{\dealvcentcolon\mathrel{\mkern-1.2mu}=}\) \(\newcommand{\jump}[1]{\left[\!\left[ #1 \right]\!\right]}\) \(\newcommand{\average}[1]{\left\{\!\left\{ #1 \right\}\!\right\}}\)
Loading...
Searching...
No Matches
symengine_number_types.h
Go to the documentation of this file.
1// -----------------------------------------------------------------------------
2//
3// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception OR LGPL-2.1-or-later
4// Copyright (C) 2019 - 2026 by the deal.II authors
5//
6// This file is part of the deal.II library.
7//
8// Detailed license information governing the source code and contributions
9// can be found in LICENSE.md and CONTRIBUTING.md at the top level directory.
10//
11// -----------------------------------------------------------------------------
12
13#ifndef dealii_differentiation_sd_symengine_number_types_h
14#define dealii_differentiation_sd_symengine_number_types_h
15
16#include <deal.II/base/config.h>
17
18#ifdef DEAL_II_WITH_SYMENGINE
19
20// Low level
21# include <symengine/basic.h>
22# include <symengine/dict.h>
23# include <symengine/symengine_exception.h>
24# include <symengine/symengine_rcp.h>
25
26// Number types
27# include <symengine/expression.h>
28# include <symengine/integer.h>
29# include <symengine/logic.h>
30# include <symengine/number.h>
31# include <symengine/rational.h>
32
33// Number operations
34# include <symengine/add.h>
35# include <symengine/functions.h>
36# include <symengine/mul.h>
37# include <symengine/pow.h>
38
39// Evaluation
40# include <symengine/eval.h>
41# include <symengine/eval_arb.h>
42# include <symengine/eval_double.h>
43
44// Differentiation
46# include <deal.II/base/numbers.h>
47
50
51# include <boost/serialization/split_member.hpp>
52
53# include <symengine/derivative.h>
54
55# include <algorithm>
56# include <memory>
57# include <sstream>
58# include <type_traits>
59# include <utility>
60# include <vector>
61
62#endif // DEAL_II_WITH_SYMENGINE
63
65
66#ifdef DEAL_II_WITH_SYMENGINE
67namespace Differentiation
68{
69 namespace SD
70 {
82 std::string,
83 << "The string '" << arg1
84 << "' could not be parsed successfully. Are you sure that (1) it "
85 << "consists of legitimate operations and syntax, and (2) you've "
86 << "previously declared all symbolic variables that are present "
87 << "in the expression?");
88
178 {
179 public:
188 Expression();
189
197 explicit Expression(const bool value);
198
206 template <typename NumberType,
207 typename = std::enable_if_t<std::is_arithmetic_v<NumberType>>>
208 explicit Expression(const NumberType &value);
209
217 template <typename NumberType,
218 typename = std::enable_if_t<std::is_arithmetic_v<NumberType>>>
219 explicit Expression(const std::complex<NumberType> &value);
220
224 Expression(const SymEngine::integer_class &value);
225
232 template <typename NumberType,
233 typename = std::enable_if_t<std::is_integral_v<NumberType>>>
234 Expression(const NumberType &numerator, const NumberType &denominator);
235
239 Expression(const SymEngine::rational_class &value);
240
277 Expression(const Expression &condition,
278 const Expression &expression_if_true,
279 const Expression &expression_if_false);
280
302 Expression(const std::vector<std::pair<Expression, Expression>>
303 &condition_expression,
304 const Expression &expression_otherwise);
305
325 Expression(const std::vector<std::pair<Expression, Expression>>
326 &condition_expression);
327
328
335 Expression(const char *symbol);
336
349 Expression(const std::string &symb_expr,
350 const bool parse_as_expression = false);
351
358 Expression(const std::string &symbol_func,
359 const types::symbol_vector &arguments);
360
364 Expression(const Expression &rhs) = default;
365
373 explicit Expression(const SymEngine::Expression &rhs);
374
383 Expression(const SymEngine::RCP<const SymEngine::Basic> &rhs);
384
388 Expression(Expression &&rhs) = default;
389
398 Expression(SymEngine::RCP<const SymEngine::Basic> &&rhs);
399
403 virtual ~Expression() = default;
404
417 Expression &
418 parse(const std::string &expression);
419
426 std::ostream &
427 print(std::ostream &stream) const;
428
434 void
435 save(std::ostream &stream) const;
436
448 void
449 load(std::istream &stream);
450
459 template <class Archive>
460 void
461 save(Archive &archive, const unsigned int version) const;
462
475 template <class Archive>
476 void
477 load(Archive &archive, const unsigned int version);
478
479# ifdef DOXYGEN
494 template <class Archive>
495 void
496 serialize(Archive &archive, const unsigned int version);
497# else
498 // This macro defines the serialize() method that is compatible with
499 // the templated save() and load() method that have been implemented.
500 BOOST_SERIALIZATION_SPLIT_MEMBER()
501# endif
502
513 const SymEngine::Expression &
514 get_expression() const;
515
520 const SymEngine::Basic &
521 get_value() const;
522
527 const SymEngine::RCP<const SymEngine::Basic> &
528 get_RCP() const;
529
543 Expression &
544 operator=(const Expression &rhs);
545
552 Expression &
553 operator=(Expression &&rhs) noexcept;
554
561 Expression &
562 operator+=(const Expression &rhs);
563
570 Expression &
571 operator-=(const Expression &rhs);
572
579 Expression &
580 operator*=(const Expression &rhs);
581
588 Expression &
589 operator/=(const Expression &rhs);
590
604 template <typename NumberType>
605 Expression &
606 operator=(const NumberType &rhs);
607
617 operator-() const;
618
625 template <typename NumberType>
626 Expression &
627 operator+=(const NumberType &rhs);
628
635 template <typename NumberType>
636 Expression &
637 operator-=(const NumberType &rhs);
638
645 template <typename NumberType>
646 Expression &
647 operator*=(const NumberType &rhs);
648
655 template <typename NumberType>
656 Expression &
657 operator/=(const NumberType &rhs);
658
671 differentiate(const Expression &symbol) const;
672
679 const SymEngine::RCP<const SymEngine::Symbol> &symbol) const;
680
686 differentiate(const SymEngine::RCP<const SymEngine::Basic> &symbol) const;
687
708 substitute(const types::substitution_map &substitution_values) const;
709
727 substitute(const SymEngine::map_basic_basic &substitution_values) const;
728
739 substitute(const Expression &symbol, const Expression &value) const;
740
750 template <typename NumberType>
752 substitute(const Expression &symbol, const NumberType &value) const;
753
761 template <typename ReturnType>
762 ReturnType
764 const types::substitution_map &substitution_values) const;
765
777 template <typename ReturnType>
778 ReturnType
780 const SymEngine::map_basic_basic &substitution_values) const;
781
839 template <typename ResultType>
840 explicit
841 operator ResultType() const;
842
847 explicit
848 operator const SymEngine::Expression &() const;
849
854 operator const SymEngine::RCP<const SymEngine::Basic> &() const;
855
858 protected:
862 SymEngine::Expression &
864
865 private:
870 SymEngine::Expression expression;
871 };
872
883 template <>
884 struct is_symengine_number<Expression> : std::true_type
885 {};
886
887
893 template <>
894 struct is_sd_number<Expression> : std::true_type
895 {};
896
909 std::ostream &
910 operator<<(std::ostream &stream, const Expression &expression);
911
917 std::istream &
918 operator>>(std::istream &stream, Expression &expression);
919
933 operator==(const Expression &lhs, const Expression &rhs);
934
941 operator!=(const Expression &lhs, const Expression &rhs);
942
949 operator<(const Expression &lhs, const Expression &rhs);
950
957 operator>(const Expression &lhs, const Expression &rhs);
958
965 operator<=(const Expression &lhs, const Expression &rhs);
966
973 operator>=(const Expression &lhs, const Expression &rhs);
974
989 operator!(const Expression &expression);
990
998 operator&(const Expression &lhs, const Expression &rhs);
999
1007 operator|(const Expression &lhs, const Expression &rhs);
1008
1016 operator^(const Expression &lhs, const Expression &rhs);
1017
1026 operator&&(const Expression &lhs, const Expression &rhs);
1027
1036 operator||(const Expression &lhs, const Expression &rhs);
1037
1051 operator+(Expression lhs, const Expression &rhs);
1052
1059 operator-(Expression lhs, const Expression &rhs);
1060
1067 operator*(Expression lhs, const Expression &rhs);
1068
1075 operator/(Expression lhs, const Expression &rhs);
1076
1085 template <typename NumberType,
1086 typename = std::enable_if_t<
1087 std::is_constructible_v<Expression, NumberType>>>
1088 inline Expression
1089 operator+(const NumberType &lhs, const Expression &rhs)
1090 {
1091 return Expression(lhs) + rhs;
1092 }
1093
1102 template <typename NumberType,
1103 typename = std::enable_if_t<
1104 std::is_constructible_v<Expression, NumberType>>>
1105 inline Expression
1106 operator+(const Expression &lhs, const NumberType &rhs)
1107 {
1108 return lhs + Expression(rhs);
1109 }
1110
1119 template <typename NumberType,
1120 typename = std::enable_if_t<
1121 std::is_constructible_v<Expression, NumberType>>>
1122 inline Expression
1123 operator-(const NumberType &lhs, const Expression &rhs)
1124 {
1125 return Expression(lhs) - rhs;
1126 }
1127
1136 template <typename NumberType,
1137 typename = std::enable_if_t<
1138 std::is_constructible_v<Expression, NumberType>>>
1139 inline Expression
1140 operator-(const Expression &lhs, const NumberType &rhs)
1141 {
1142 return lhs - Expression(rhs);
1143 }
1144
1153 template <typename NumberType,
1154 typename = std::enable_if_t<
1155 std::is_constructible_v<Expression, NumberType>>>
1156 inline Expression
1157 operator*(const NumberType &lhs, const Expression &rhs)
1158 {
1159 return Expression(lhs) * rhs;
1160 }
1161
1170 template <typename NumberType,
1171 typename = std::enable_if_t<
1172 std::is_constructible_v<Expression, NumberType>>>
1173 inline Expression
1174 operator*(const Expression &lhs, const NumberType &rhs)
1175 {
1176 return lhs * Expression(rhs);
1177 }
1178
1187 template <typename NumberType,
1188 typename = std::enable_if_t<
1189 std::is_constructible_v<Expression, NumberType>>>
1190 inline Expression
1191 operator/(const NumberType &lhs, const Expression &rhs)
1192 {
1193 return Expression(lhs) / rhs;
1194 }
1195
1204 template <typename NumberType,
1205 typename = std::enable_if_t<
1206 std::is_constructible_v<Expression, NumberType>>>
1207 inline Expression
1208 operator/(const Expression &lhs, const NumberType &rhs)
1209 {
1210 return lhs / Expression(rhs);
1211 }
1212
1215 } // namespace SD
1216} // namespace Differentiation
1217
1218
1219/* ---------------------- inline and template functions -------------------- */
1220
1221
1222# ifndef DOXYGEN
1223
1224
1225namespace Differentiation
1226{
1227 namespace SD
1228 {
1229 template <typename NumberType, typename>
1230 Expression::Expression(const NumberType &value)
1231 : expression(value)
1232 {}
1233
1234
1235 template <typename NumberType, typename>
1236 Expression::Expression(const std::complex<NumberType> &value)
1237 : expression(value)
1238 {}
1239
1240
1241 template <typename NumberType, typename>
1242 Expression::Expression(const NumberType &numerator,
1243 const NumberType &denominator)
1244 : expression(
1245 SymEngine::Rational::from_two_ints(*SymEngine::integer(numerator),
1246 *SymEngine::integer(denominator)))
1247 {}
1248
1249
1250 template <class Archive>
1251 void
1252 Expression::save(Archive &ar, const unsigned int /*version*/) const
1253 {
1254 std::stringstream sstream;
1255 sstream << *this;
1256 const std::string expr = sstream.str();
1257 ar & expr;
1258 }
1259
1260
1261 template <class Archive>
1262 void
1263 Expression::load(Archive &ar, const unsigned int /*version*/)
1264 {
1265 std::string expr;
1266 ar & expr;
1267 parse(expr);
1268 }
1269
1270
1271 template <typename NumberType>
1272 Expression
1273 Expression::substitute(const Expression &symbol,
1274 const NumberType &value) const
1275 {
1276 Assert(SymEngine::is_a<SymEngine::Symbol>(symbol.get_value()),
1277 ExcMessage(
1278 "Substitution with a number that does not represent a symbol."));
1279
1280 types::substitution_map sub_vals;
1281 sub_vals[symbol] = Expression(value);
1282 return substitute(sub_vals);
1283 }
1284
1285
1286 template <typename ReturnType>
1287 ReturnType
1288 Expression::substitute_and_evaluate(
1289 const types::substitution_map &substitution_values) const
1290 {
1291 return static_cast<ReturnType>(substitute(substitution_values));
1292 }
1293
1294
1295 template <typename ReturnType>
1296 ReturnType
1297 Expression::substitute_and_evaluate(
1298 const SymEngine::map_basic_basic &substitution_values) const
1299 {
1300 return static_cast<ReturnType>(substitute(substitution_values));
1301 }
1302
1303
1304 template <typename NumberType>
1305 Expression &
1306 Expression::operator=(const NumberType &rhs)
1307 {
1308 *this = Expression(rhs);
1309 return *this;
1310 }
1311
1312
1313 template <typename NumberType>
1314 Expression &
1315 Expression::operator+=(const NumberType &rhs)
1316 {
1317 *this = Expression(SymEngine::add(get_RCP(), Expression(rhs).get_RCP()));
1318 return *this;
1319 }
1320
1321
1322 template <typename NumberType>
1323 Expression &
1324 Expression::operator-=(const NumberType &rhs)
1325 {
1326 *this = Expression(SymEngine::sub(get_RCP(), Expression(rhs).get_RCP()));
1327 return *this;
1328 }
1329
1330
1331 template <typename NumberType>
1332 Expression &
1333 Expression::operator*=(const NumberType &rhs)
1334 {
1335 *this = Expression(SymEngine::mul(get_RCP(), Expression(rhs).get_RCP()));
1336 return *this;
1337 }
1338
1339
1340 template <typename NumberType>
1341 Expression &
1342 Expression::operator/=(const NumberType &rhs)
1343 {
1344 *this = Expression(SymEngine::div(get_RCP(), Expression(rhs).get_RCP()));
1345 return *this;
1346 }
1347
1348
1349 template <typename ResultType>
1350 Expression::
1351 operator ResultType() const
1352 {
1353 return static_cast<ResultType>(get_expression());
1354 }
1355
1356 } // namespace SD
1357} // namespace Differentiation
1358
1359
1360// Specializations from numbers.h
1361// These are required in order to make the SD::Expression class a compatible
1362// number for the Tensor and SymmetricTensor classes
1363
1364// Forward declarations:
1365template <int rank_, int dim, typename Number>
1366class Tensor;
1367
1368namespace internal
1369{
1370 template <>
1371 struct NumberType<Differentiation::SD::Expression>
1372 {
1373 static const Differentiation::SD::Expression &
1375 {
1376 return t;
1377 }
1378
1379 template <typename T, typename = std::enable_if_t<std::is_arithmetic_v<T>>>
1381 value(const T &t)
1382 {
1384 }
1385
1386 template <typename T, typename = std::enable_if_t<std::is_arithmetic_v<T>>>
1388 value(T &&t)
1389 {
1391 }
1392
1393 template <typename T, typename = std::enable_if_t<std::is_arithmetic_v<T>>>
1395 value(const std::complex<T> &t)
1396 {
1398 }
1399
1400 template <typename T, int dim>
1402 value(const Tensor<0, dim, T> &t)
1403 {
1404 return Differentiation::SD::Expression(static_cast<T>(t));
1405 }
1406
1407 template <typename T, int dim>
1409 value(const Tensor<0, dim, std::complex<T>> &t)
1410 {
1411 return Differentiation::SD::Expression(static_cast<std::complex<T>>(t));
1412 }
1413 };
1414} // namespace internal
1415
1416
1417namespace numbers
1418{
1419 template <>
1420 inline bool
1422 {
1423 if (SymEngine::is_a_Number(value.get_value()))
1424 {
1425 const SymEngine::RCP<const SymEngine::Number> number_rcp =
1426 SymEngine::rcp_static_cast<const SymEngine::Number>(value.get_RCP());
1427 return number_rcp->is_zero();
1428 }
1429
1430 return false;
1431 }
1432
1433 template <>
1434 inline bool
1436 const Differentiation::SD::Expression &value_2)
1437 {
1438 return (value_1.get_value().__cmp__(value_2.get_value()) == 0);
1439 }
1440
1441 template <>
1442 inline bool
1444 const Differentiation::SD::Expression &value_2)
1445 {
1446 return (value_1.get_value().__cmp__(value_2.get_value()) == -1);
1447 }
1448} // namespace numbers
1449
1450
1451# endif // DOXYGEN
1452
1453#endif // DEAL_II_WITH_SYMENGINE
1454
1456
1457#endif // dealii_differentiation_sd_symengine_number_types_h
*  *  reference operator*() const
Expression & operator/=(const Expression &rhs)
Expression & parse(const std::string &expression)
Expression & operator-=(const NumberType &rhs)
Expression substitute(const Expression &symbol, const NumberType &value) const
Expression & operator=(const Expression &rhs)
Expression(const NumberType &value)
const SymEngine::RCP< const SymEngine::Basic > & get_RCP() const
ReturnType substitute_and_evaluate(const types::substitution_map &substitution_values) const
Expression & operator/=(const NumberType &rhs)
Expression(const NumberType &numerator, const NumberType &denominator)
Expression(Expression &&rhs)=default
Expression & operator*=(const Expression &rhs)
void serialize(Archive &archive, const unsigned int version)
Expression & operator+=(const NumberType &rhs)
std::ostream & print(std::ostream &stream) const
Expression substitute(const types::substitution_map &substitution_values) const
Expression & operator=(const NumberType &rhs)
void save(Archive &archive, const unsigned int version) const
Expression(const Expression &rhs)=default
void save(std::ostream &stream) const
Expression & operator*=(const NumberType &rhs)
ReturnType substitute_and_evaluate(const SymEngine::map_basic_basic &substitution_values) const
Expression & operator-=(const Expression &rhs)
const SymEngine::Basic & get_value() const
const SymEngine::Expression & get_expression() const
Expression & operator+=(const Expression &rhs)
void load(Archive &archive, const unsigned int version)
Expression(const std::complex< NumberType > &value)
Expression differentiate(const Expression &symbol) const
#define DEAL_II_NAMESPACE_OPEN
Definition config.h:38
#define DEAL_II_NAMESPACE_CLOSE
Definition config.h:39
static ::ExceptionBase & ExcSymEngineParserError(std::string arg1)
#define Assert(cond, exc)
#define DeclException1(Exception1, type1, outsequence)
static ::ExceptionBase & ExcMessage(std::string arg1)
std::vector< SD::Expression > symbol_vector
std::map< SD::Expression, SD::Expression, internal::ExpressionKeyLess > substitution_map
Expression operator<(const Expression &lhs, const Expression &rhs)
Expression operator-(Expression lhs, const Expression &rhs)
Expression operator+(Expression lhs, const Expression &rhs)
Expression operator!(const Expression &expression)
Expression operator||(const Expression &lhs, const Expression &rhs)
Expression operator^(const Expression &lhs, const Expression &rhs)
Expression operator>=(const Expression &lhs, const Expression &rhs)
Expression operator!=(const Expression &lhs, const Expression &rhs)
Expression operator|(const Expression &lhs, const Expression &rhs)
Expression operator>(const Expression &lhs, const Expression &rhs)
Expression operator&(const Expression &lhs, const Expression &rhs)
Expression operator/(Expression lhs, const Expression &rhs)
Expression operator<=(const Expression &lhs, const Expression &rhs)
Expression operator&&(const Expression &lhs, const Expression &rhs)
Expression substitute(const Expression &expression, const types::substitution_map &substitution_map)
std::ostream & operator<<(std::ostream &stream, const Expression &expression)
Expression operator==(const Expression &lhs, const Expression &rhs)
std::istream & operator>>(std::istream &stream, Expression &expression)
constexpr char T
constexpr bool value_is_zero(const Number &value)
Definition numbers.h:874
constexpr bool values_are_equal(const Number1 &value_1, const Number2 &value_2)
Definition numbers.h:858
bool value_is_less_than(const Number1 &value_1, const Number2 &value_2)
Definition numbers.h:882