version GIT relicensing-7278-g091132d5eb 2026-12-02 03:10: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.cc
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#include <deal.II/base/config.h>
14
15#ifdef DEAL_II_WITH_SYMENGINE
16
18
22
23# include <symengine/complex_double.h>
24# include <symengine/logic.h>
25# include <symengine/number.h>
26# include <symengine/parser.h>
27# include <symengine/real_double.h>
28# include <symengine/symbol.h>
29# include <symengine/symengine_exception.h>
30
31# include <string>
32
33
34#endif // DEAL_II_WITH_SYMENGINE
35
37
38#ifdef DEAL_II_WITH_SYMENGINE
39
40namespace Differentiation
41{
42 namespace SD
43 {
44 namespace SE = ::SymEngine;
45
46
47 /* ---------------------------- Constructors -------------------------- */
48
49
51 : expression()
52 {}
53
54
55 Expression::Expression(const bool value)
56 : expression(SE::boolean(value))
57 {}
58
59
60 Expression::Expression(const SymEngine::integer_class &value)
61 : expression(value)
62 {}
63
64
65 Expression::Expression(const SymEngine::rational_class &value)
66 : expression(value)
67 {}
68
69
71 const Expression &expression_if_true,
72 const Expression &expression_if_false)
73 {
74 Assert(SE::is_a_Boolean(condition.get_value()),
76 "The conditional expression must return a boolean type."));
77
78 const SE::RCP<const SE::Boolean> condition_rcp =
79 SE::rcp_static_cast<const SE::Boolean>(condition.get_RCP());
81 SE::piecewise({{expression_if_true.get_RCP(), condition_rcp},
82 {expression_if_false.get_RCP(), SE::boolTrue}});
83 }
84
85
86 Expression::Expression(const std::vector<std::pair<Expression, Expression>>
87 &condition_expression,
88 const Expression &expression_otherwise)
89 {
90 SE::PiecewiseVec piecewise_function;
91 piecewise_function.reserve(condition_expression.size() + 1);
92
93 // Add tested conditional entries
94 for (const auto &entry : condition_expression)
95 {
96 Assert(SE::is_a_Boolean(entry.first.get_value()),
98 "The conditional expression must return a boolean type."));
99 piecewise_function.emplace_back(
100 entry.second.get_RCP(),
101 SE::rcp_static_cast<const SE::Boolean>(entry.first.get_RCP()));
102 }
103
104 // Add default value
105 piecewise_function.emplace_back(expression_otherwise.get_RCP(),
106 SE::boolTrue);
107
108 // Initialize. Note that we need to use a std::move() here for
109 // compatibility with older compilers.
110 expression = SE::piecewise(std::move(piecewise_function)); // NOLINT
111 }
112
113
114 Expression::Expression(const std::vector<std::pair<Expression, Expression>>
115 &condition_expression)
116 {
117 // Use the other constructor with a fatal termination point
118 // ensuring that an error is thrown if none of the conditions
119 // are met.
120 *this = Expression(condition_expression,
121 Expression(numbers::signaling_nan<double>()));
122 }
123
124
125 Expression::Expression(const char *symbol)
126 : expression(SE::symbol(symbol))
127 {}
128
129
130 Expression::Expression(const std::string &str,
131 const bool parse_as_expression)
132 {
133 try
134 {
135 expression = (parse_as_expression ?
136 SE::parse(str) // The string is a symbolic "name"
137 :
138 SE::rcp_static_cast<const SE::Basic>(SE::symbol(
139 str))); // The string is a symbolic "expression"
140 }
141 catch (...)
142 {
144 }
145 }
146
147
148 Expression::Expression(const std::string &symbol_func,
149 const types::symbol_vector &arguments)
150 : expression(SE::function_symbol(
151 symbol_func,
152 Utilities::convert_expression_vector_to_basic_vector(arguments)))
153 {}
154
155
156 Expression::Expression(const SymEngine::Expression &rhs)
157 : expression(rhs)
158 {}
159
160
161 Expression::Expression(const SymEngine::RCP<const SymEngine::Basic> &rhs)
162 : expression(rhs)
163 {}
164
165
166 Expression::Expression(SymEngine::RCP<const SymEngine::Basic> &&rhs)
167 : expression(rhs)
168 {}
169
170
171 /* ------------------------------ Utilities ---------------------------- */
172
173
174 Expression &
175 Expression::parse(const std::string &expression)
176 {
177 *this = Expression(expression, true /*parse_as_expression*/);
178 return *this;
179 }
180
181
182 std::ostream &
183 Expression::print(std::ostream &os) const
184 {
185 os << *this;
186 return os;
187 }
188
189
190 void
191 Expression::save(std::ostream &os) const
192 {
193 // We write each expression on a new line.
194 // Note: SymEngine outputs a non-terminating string
195 os << *this;
196 os << std::endl;
197 }
198
199
200 void
201 Expression::load(std::istream &is)
202 {
203 // Need to make sure that we read the entire line in,
204 // and then subsequently parse it.
205 std::string expr;
206 std::getline(is, expr);
207 Assert(!is.bad(), ExcIO());
208 parse(expr);
209 }
210
211
212 /* ------------------------------- Values ----------------------------- */
213
214
215 const SE::Expression &
217 {
218 return expression;
219 }
220
221
222 SE::Expression &
224 {
225 return expression;
226 }
227
228
229 const SE::Basic &
231 {
232 return *get_RCP();
233 }
234
235
236 const SE::RCP<const SE::Basic> &
238 {
239 return get_expression().get_basic();
240 }
241
242
243 /* --------------------------- Differentiation ------------------------- */
244
245
248 const SymEngine::RCP<const SymEngine::Symbol> &symbol) const
249 {
250 return Expression(SE::diff(get_RCP(), symbol));
251 }
252
253
256 const SymEngine::RCP<const SymEngine::Basic> &symbol) const
257 {
258 // Potential symbol
259 return Expression(SE::sdiff(get_RCP(), symbol));
260 }
261
262
265 {
266 return differentiate(symbol.get_RCP());
267 }
268
269
270 /* ------------- Conversion operators ------------------------- */
271
272
273 Expression::
274 operator const SymEngine::Expression &() const
275 {
276 return get_expression();
277 }
278
279
280 Expression::
281 operator const SymEngine::RCP<const SymEngine::Basic> &() const
282 {
283 return get_expression().get_basic();
284 }
285
286
287 /* ------------- Dictionary-based substitution ------------------------- */
288
289
290 Expression
292 const SymEngine::map_basic_basic &substitution_values) const
293 {
294 return Expression(get_expression().subs(substitution_values));
295 }
296
297
300 const types::substitution_map &substitution_values) const
301 {
302 return substitute(
304 }
305
306
309 const Expression &value) const
310 {
311 Assert(SE::is_a<SE::Symbol>(symbol.get_value()),
313 "Substitution with a number that does not represent a symbol."));
314
316 sub_vals[symbol] = value;
317 return substitute(sub_vals);
318 }
319
320
321 /* -------------------- Math and relational operators ------------------ */
322
323
324 Expression &
326 {
327 if (this != &rhs)
328 this->expression = rhs.get_expression();
329
330 return *this;
331 }
332
333
334 Expression &
336 {
337 if (this != &rhs)
338 this->expression = std::move(rhs.expression);
339
340 return *this;
341 }
342
343
346 {
347 return Expression(-get_expression());
348 }
349
350
351 Expression &
353 {
354 this->expression += rhs.get_expression();
355 return *this;
356 }
357
358
359 Expression &
361 {
362 this->expression -= rhs.get_expression();
363 return *this;
364 }
365
366
367 Expression &
369 {
370 this->expression *= rhs.get_expression();
371 return *this;
372 }
373
374
375 Expression &
377 {
378 this->expression /= rhs.get_expression();
379 return *this;
380 }
381
382
383 std::ostream &
384 operator<<(std::ostream &stream, const Expression &expr)
385 {
386 stream << expr.get_expression();
387 return stream;
388 }
389
390
391 std::istream &
392 operator>>(std::istream &stream, Expression &expr)
393 {
394 std::string str;
395 stream >> str;
396 expr.parse(str);
397 return stream;
398 }
399
400
401 Expression
402 operator==(const Expression &lhs, const Expression &rhs)
403 {
404 return Expression(SE::Eq(lhs.get_RCP(), rhs.get_RCP()));
405 }
406
407
408 Expression
409 operator!=(const Expression &lhs, const Expression &rhs)
410 {
411 return Expression(SE::Ne(lhs.get_RCP(), rhs.get_RCP()));
412 }
413
414
416 operator<(const Expression &lhs, const Expression &rhs)
417 {
418 return Expression(SE::Lt(lhs.get_RCP(), rhs.get_RCP()));
419 }
420
421
422 Expression
423 operator>(const Expression &lhs, const Expression &rhs)
424 {
425 return Expression(SE::Gt(lhs.get_RCP(), rhs.get_RCP()));
426 }
427
428
430 operator<=(const Expression &lhs, const Expression &rhs)
431 {
432 return Expression(SE::Le(lhs.get_RCP(), rhs.get_RCP()));
433 }
434
435
436 Expression
437 operator>=(const Expression &lhs, const Expression &rhs)
438 {
439 return Expression(SE::Ge(lhs.get_RCP(), rhs.get_RCP()));
440 }
441
442
443 Expression
444 operator!(const Expression &expression)
445 {
446 Assert(SE::is_a_Boolean(expression.get_value()),
447 ExcMessage("The expression must return a boolean type."));
448
449 const SE::RCP<const SE::Boolean> expression_rcp =
450 SE::rcp_static_cast<const SE::Boolean>(expression.get_RCP());
451
452 return Expression(SE::logical_not(expression_rcp));
453 }
454
455
456 Expression
457 operator&(const Expression &lhs, const Expression &rhs)
458 {
459 Assert(SE::is_a_Boolean(lhs.get_value()),
460 ExcMessage("The lhs expression must return a boolean type."));
461 Assert(SE::is_a_Boolean(rhs.get_value()),
462 ExcMessage("The rhs expression must return a boolean type."));
463
464 const SE::RCP<const SE::Boolean> lhs_rcp =
465 SE::rcp_static_cast<const SE::Boolean>(lhs.get_RCP());
466 const SE::RCP<const SE::Boolean> rhs_rcp =
467 SE::rcp_static_cast<const SE::Boolean>(rhs.get_RCP());
468
469 return Expression(SE::logical_and({lhs_rcp, rhs_rcp}));
470 }
471
472
473 Expression
474 operator|(const Expression &lhs, const Expression &rhs)
475 {
476 Assert(SE::is_a_Boolean(lhs.get_value()),
477 ExcMessage("The lhs expression must return a boolean type."));
478 Assert(SE::is_a_Boolean(rhs.get_value()),
479 ExcMessage("The rhs expression must return a boolean type."));
480
481 const SE::RCP<const SE::Boolean> lhs_rcp =
482 SE::rcp_static_cast<const SE::Boolean>(lhs.get_RCP());
483 const SE::RCP<const SE::Boolean> rhs_rcp =
484 SE::rcp_static_cast<const SE::Boolean>(rhs.get_RCP());
485
486 return Expression(SE::logical_or({lhs_rcp, rhs_rcp}));
487 }
488
489
490 Expression
491 operator^(const Expression &lhs, const Expression &rhs)
492 {
493 Assert(SE::is_a_Boolean(lhs.get_value()),
494 ExcMessage("The lhs expression must return a boolean type."));
495 Assert(SE::is_a_Boolean(rhs.get_value()),
496 ExcMessage("The rhs expression must return a boolean type."));
497
498 const SE::RCP<const SE::Boolean> lhs_rcp =
499 SE::rcp_static_cast<const SE::Boolean>(lhs.get_RCP());
500 const SE::RCP<const SE::Boolean> rhs_rcp =
501 SE::rcp_static_cast<const SE::Boolean>(rhs.get_RCP());
502
503 return Expression(SE::logical_xor({lhs_rcp, rhs_rcp}));
504 }
505
506
507 Expression
508 operator&&(const Expression &lhs, const Expression &rhs)
509 {
510 return lhs & rhs;
511 }
512
513
514 Expression
515 operator||(const Expression &lhs, const Expression &rhs)
516 {
517 return lhs | rhs;
518 }
519
520
521 Expression
523 {
524 lhs += rhs;
525 return lhs;
526 }
527
528
529 Expression
531 {
532 lhs -= rhs;
533 return lhs;
534 }
535
536
537 Expression
539 {
540 lhs *= rhs;
541 return lhs;
542 }
543
544
545 Expression
547 {
548 lhs /= rhs;
549 return lhs;
550 }
551
552
553 } // namespace SD
554} // namespace Differentiation
555
556
557
558#endif // DEAL_II_WITH_SYMENGINE
*  *  reference operator*() const
Expression & operator/=(const Expression &rhs)
Expression & parse(const std::string &expression)
Expression & operator=(const Expression &rhs)
const SymEngine::RCP< const SymEngine::Basic > & get_RCP() const
Expression & operator*=(const Expression &rhs)
std::ostream & print(std::ostream &stream) const
Expression substitute(const types::substitution_map &substitution_values) const
void save(std::ostream &stream) const
Expression & operator-=(const Expression &rhs)
const SymEngine::Basic & get_value() const
const SymEngine::Expression & get_expression() const
Expression & operator+=(const Expression &rhs)
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 & ExcIO()
static ::ExceptionBase & ExcSymEngineParserError(std::string arg1)
#define Assert(cond, exc)
static ::ExceptionBase & ExcMessage(std::string arg1)
#define AssertThrow(cond, exc)
SymEngine::map_basic_basic convert_expression_map_to_basic_map(const SD::types::substitution_map &substitution_map)
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)
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)