| 1 |
// ------------------------------------------------------------------------ |
| 2 |
// |
| 3 |
// SPDX-License-Identifier: LGPL-2.1-or-later |
| 4 |
// Copyright (C) 2020 - 2024 by the deal.II authors |
| 5 |
// |
| 6 |
// This file is part of the deal.II library. |
| 7 |
// |
| 8 |
// Part of the source code is dual licensed under Apache-2.0 WITH |
| 9 |
// LLVM-exception OR LGPL-2.1-or-later. Detailed license information |
| 10 |
// governing the source code and code contributions can be found in |
| 11 |
// LICENSE.md and CONTRIBUTING.md at the top level directory of deal.II. |
| 12 |
// |
| 13 |
// ------------------------------------------------------------------------ |
| 14 |
|
| 15 |
#ifndef dealii_differentiation_sd_symengine_number_visitor_internal_h |
| 16 |
#define dealii_differentiation_sd_symengine_number_visitor_internal_h |
| 17 |
|
| 18 |
#include <deal.II/base/config.h> |
| 19 |
|
| 20 |
#ifdef DEAL_II_WITH_SYMENGINE |
| 21 |
|
| 22 |
# include <deal.II/base/exceptions.h> |
| 23 |
# include <deal.II/base/numbers.h> |
| 24 |
|
| 25 |
# include <deal.II/differentiation/sd/symengine_number_types.h> |
| 26 |
# include <deal.II/differentiation/sd/symengine_utilities.h> |
| 27 |
|
| 28 |
# include <boost/serialization/split_member.hpp> |
| 29 |
|
| 30 |
# include <symengine/basic.h> |
| 31 |
# include <symengine/dict.h> |
| 32 |
# include <symengine/symengine_exception.h> |
| 33 |
# include <symengine/symengine_rcp.h> |
| 34 |
# include <symengine/visitor.h> |
| 35 |
|
| 36 |
|
| 37 |
DEAL_II_NAMESPACE_OPEN |
| 38 |
|
| 39 |
|
| 40 |
namespace Differentiation |
| 41 |
{ |
| 42 |
namespace SD |
| 43 |
{ |
| 44 |
namespace internal |
| 45 |
{ |
| 46 |
/** |
| 47 |
* A class that implements common subexpression elimination |
| 48 |
* for dictionary visitor classes. |
| 49 |
* |
| 50 |
* It is intended that this class only be used in conjunction |
| 51 |
* with the DictionarySubstitutionVisitor. |
| 52 |
*/ |
| 53 |
template <typename ReturnType, typename ExpressionType> |
| 54 |
class CSEDictionaryVisitor |
| 55 |
{ |
| 56 |
using symbol_vector_pair = |
| 57 |
std::vector<std::pair<SD::Expression, SD::Expression>>; |
| 58 |
|
| 59 |
public: |
| 60 |
/* |
| 61 |
* Constructor. |
| 62 |
*/ |
| 63 |
CSEDictionaryVisitor() = default; |
| 64 |
|
| 65 |
/* |
| 66 |
* Destructor. |
| 67 |
*/ |
| 68 |
virtual ~CSEDictionaryVisitor() = default; |
| 69 |
|
| 70 |
/** |
| 71 |
* Initialize and perform common subexpression elimination. |
| 72 |
* |
| 73 |
* Here we build the reduced expressions for the @p dependent_functions |
| 74 |
* as well as a list of intermediate, repeating symbolic expressions |
| 75 |
* that are extracted @p dependent_functions. This operation leads to |
| 76 |
* the elimination of repeated expressions, so they only have to be |
| 77 |
* evaluated once. |
| 78 |
* |
| 79 |
* @param dependent_functions A vector of expressions that represent |
| 80 |
* the dependent functions that CSE will be performed on. |
| 81 |
*/ |
| 82 |
void |
| 83 |
init(const types::symbol_vector &dependent_functions); |
| 84 |
|
| 85 |
/** |
| 86 |
* Evaluate the (reduced) dependent functions using the common |
| 87 |
* subexpressions generated during the call to init(). |
| 88 |
* |
| 89 |
* The @p output_values are the numerical result of substituting |
| 90 |
* each of the @p substitution_values for their corresponding entry |
| 91 |
* of the @p independent_symbols. Specifically, we first calculate |
| 92 |
* the numerical values of each reduced subexpression, and feed those |
| 93 |
* into a reduced equivalent of the dependent variables. The result of |
| 94 |
* this is then written into the @p output_values array. |
| 95 |
* |
| 96 |
* @param[out] output_values A pointer to the first element in an array |
| 97 |
* of type @p ReturnType. After this call, the underlying array will |
| 98 |
* hold the numerical result of substituting the @p substitution_values |
| 99 |
* into the pre-registered dependent functions. |
| 100 |
* @param[in] independent_symbols A vector of symbols that represent |
| 101 |
* the independent variables that are arguments to the previously |
| 102 |
* defined dependent functions. |
| 103 |
* @param[in] substitution_values A pointer to the first element in an |
| 104 |
* array of type @p ReturnType. Each entry in this array stores the |
| 105 |
* numerical value that an independent variable is to take for the |
| 106 |
* purpose of value substitution. |
| 107 |
* |
| 108 |
* @note It is expected that both the @p output_values and |
| 109 |
* @p substitution_values arrays be correctly dimensioned, as there is |
| 110 |
* no range checking performed on these data structures. |
| 111 |
* The @p output_values array should have the same number of elements as |
| 112 |
* the dependent functions first passed to this class in the call to |
| 113 |
* init(). Similarly, the @p substitution_values array should have the |
| 114 |
* same number of elements as the @p independent_symbols vector has. |
| 115 |
* |
| 116 |
* @note It is expected that there be a 1-1 correspondence between the |
| 117 |
* entries in @p independent_symbols and @p substitution_values. This |
| 118 |
* is not checked within this function, so it is up to the user to |
| 119 |
* ensure that the relationships between the independent variables and |
| 120 |
* their numerical value be correctly set up and maintained. |
| 121 |
*/ |
| 122 |
void |
| 123 |
call(ReturnType *output_values, |
| 124 |
const types::symbol_vector &independent_symbols, |
| 125 |
const ReturnType *substitution_values); |
| 126 |
|
| 127 |
/** |
| 128 |
* Write the data of this object to a stream for the purpose |
| 129 |
* of serialization using the [BOOST serialization |
| 130 |
* library](https://www.boost.org/doc/libs/1_74_0/libs/serialization/doc/index.html). |
| 131 |
*/ |
| 132 |
template <class Archive> |
| 133 |
void |
| 134 |
save(Archive &archive, const unsigned int version) const; |
| 135 |
|
| 136 |
/** |
| 137 |
* Read the data for this object from a stream for the purpose |
| 138 |
* of serialization using the [BOOST serialization |
| 139 |
* library](https://www.boost.org/doc/libs/1_74_0/libs/serialization/doc/index.html). |
| 140 |
*/ |
| 141 |
template <class Archive> |
| 142 |
void |
| 143 |
load(Archive &archive, const unsigned int version); |
| 144 |
|
| 145 |
# ifdef DOXYGEN |
| 146 |
/** |
| 147 |
* Write and read the data of this object from a stream for the purpose |
| 148 |
* of serialization using the [BOOST serialization |
| 149 |
* library](https://www.boost.org/doc/libs/1_74_0/libs/serialization/doc/index.html). |
| 150 |
*/ |
| 151 |
template <class Archive> |
| 152 |
void |
| 153 |
serialize(Archive &archive, const unsigned int version); |
| 154 |
# else |
| 155 |
// This macro defines the serialize() method that is compatible with |
| 156 |
// the templated save() and load() method that have been implemented. |
| 157 |
BOOST_SERIALIZATION_SPLIT_MEMBER() |
| 158 |
# endif |
| 159 |
|
| 160 |
/** |
| 161 |
* Print all of the intermediate reduced expressions, as well as |
| 162 |
* the final reduced expressions for dependent variables to the |
| 163 |
* @p stream. |
| 164 |
*/ |
| 165 |
template <typename StreamType> |
| 166 |
void |
| 167 |
print(StreamType &stream) const; |
| 168 |
|
| 169 |
/** |
| 170 |
* Return a flag stating whether we've performed CSE or not. |
| 171 |
*/ |
| 172 |
bool |
| 173 |
executed() const; |
| 174 |
|
| 175 |
/** |
| 176 |
* The number of intermediate expressions that must be evaluated as |
| 177 |
* part of the collection of common subexpressions. |
| 178 |
*/ |
| 179 |
unsigned int |
| 180 |
n_intermediate_expressions() const; |
| 181 |
|
| 182 |
/** |
| 183 |
* The size of the final set of reduced expressions. |
| 184 |
*/ |
| 185 |
unsigned int |
| 186 |
n_reduced_expressions() const; |
| 187 |
|
| 188 |
protected: |
| 189 |
/** |
| 190 |
* Initialize and perform common subexpression elimination. |
| 191 |
* |
| 192 |
* This function performs the same action as the other init() function, |
| 193 |
* except that it works with native SymEngine data types; |
| 194 |
* the single argument is a vector of |
| 195 |
* `SymEngine::RCP<const SymEngine::Basic>`. |
| 196 |
* |
| 197 |
* @param dependent_functions A vector of expressions that represent |
| 198 |
* the dependent functions that CSE will be performed on. |
| 199 |
*/ |
| 200 |
void |
| 201 |
init(const SymEngine::vec_basic &dependent_functions); |
| 202 |
|
| 203 |
/** |
| 204 |
* Evaluate the (reduced) dependent functions using the common |
| 205 |
* subexpressions generated during the call to init(). |
| 206 |
* |
| 207 |
* This function performs the same action as the other init() function, |
| 208 |
* except that it works with native SymEngine data types. |
| 209 |
* The @p independent_symbols is represented by a vector of |
| 210 |
* `SymEngine::RCP<const SymEngine::Basic>`. |
| 211 |
* |
| 212 |
* @param[out] output_values A pointer to the first element in an array |
| 213 |
* of type @p ReturnType. After this call, the underlying array will |
| 214 |
* hold the numerical result of substituting the @p substitution_values |
| 215 |
* into the pre-registered dependent functions. |
| 216 |
* @param[in] independent_symbols A vector of symbols that represent |
| 217 |
* the independent variables that are arguments to the previously |
| 218 |
* defined dependent functions. |
| 219 |
* @param[in] substitution_values A pointer to the first element in an |
| 220 |
* array of type @p ReturnType. Each entry in this array stores the |
| 221 |
* numerical value that an independent variable is to take for the |
| 222 |
* purpose of value substitution. |
| 223 |
* |
| 224 |
* @note The caveats described in the documentation of the other call() |
| 225 |
* function apply here as well. |
| 226 |
*/ |
| 227 |
void |
| 228 |
call(ReturnType *output_values, |
| 229 |
const SymEngine::vec_basic &independent_symbols, |
| 230 |
const ReturnType *substitution_values); |
| 231 |
|
| 232 |
private: |
| 233 |
// Note: It would be more efficient to store this data in native |
| 234 |
// SymEngine types, as it would prevent some copying of the data |
| 235 |
// structures. However, this makes serialization more difficult, |
| 236 |
// so we use our own serializable types instead, and lose a bit |
| 237 |
// of efficiency. |
| 238 |
|
| 239 |
/** |
| 240 |
* Intermediate symbols and their definition. |
| 241 |
*/ |
| 242 |
symbol_vector_pair intermediate_symbols_exprs; |
| 243 |
|
| 244 |
/** |
| 245 |
* Final reduced expressions. |
| 246 |
*/ |
| 247 |
types::symbol_vector reduced_exprs; |
| 248 |
}; |
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
/** |
| 253 |
* A class to perform dictionary-based substitution as if it were an |
| 254 |
* optimizer of the "lambda" or "LLVM" variety. |
| 255 |
* |
| 256 |
* This class is only really useful to assist in the easy switching |
| 257 |
* between different optimizers and, more importantly, for integrating |
| 258 |
* CSE into a dictionary substitution scheme. It is therefore only |
| 259 |
* intended to be created and used by a BatchOptimizer. |
| 260 |
*/ |
| 261 |
template <typename ReturnType, typename ExpressionType> |
| 262 |
class DictionarySubstitutionVisitor |
| 263 |
: public SymEngine::BaseVisitor< |
| 264 |
DictionarySubstitutionVisitor<ReturnType, ExpressionType>> |
| 265 |
{ |
| 266 |
public: |
| 267 |
/* |
| 268 |
* Constructor. |
| 269 |
*/ |
| 270 |
DictionarySubstitutionVisitor() = default; |
| 271 |
|
| 272 |
/* |
| 273 |
* Destructor. |
| 274 |
*/ |
| 275 |
virtual ~DictionarySubstitutionVisitor() override = default; |
| 276 |
|
| 277 |
/** |
| 278 |
* Initialization, and registration of the independent and dependent |
| 279 |
* variables. |
| 280 |
* |
| 281 |
* This variation of the initialization function registers a single |
| 282 |
* dependent expression. If the @p use_cse is set to `true`, then common |
| 283 |
* subexpression elimination is also performed at the time of |
| 284 |
* initialization. |
| 285 |
* |
| 286 |
* @param independent_symbols A vector of symbols that represent |
| 287 |
* the independent variables that are arguments to the |
| 288 |
* @p dependent_function. |
| 289 |
* @param dependent_function A single symbolic expression that |
| 290 |
* represents a dependent variable. |
| 291 |
* @param use_cse A flag to indicate whether or not to use common |
| 292 |
* subexpression elimination. |
| 293 |
* |
| 294 |
* @note After this function is called, no further registration of |
| 295 |
* dependent functions can be performed. If it is desired that multiple |
| 296 |
* dependent expressions be registered, then the other variant of this |
| 297 |
* function that takes in a vector of dependent expressions should be |
| 298 |
* used. |
| 299 |
*/ |
| 300 |
void |
| 301 |
init(const types::symbol_vector &independent_symbols, |
| 302 |
const Expression &dependent_function, |
| 303 |
const bool use_cse = false); |
| 304 |
|
| 305 |
/** |
| 306 |
* Initialization, and registration of the independent and dependent |
| 307 |
* variables. |
| 308 |
* |
| 309 |
* This function performs the same action as the other init() function, |
| 310 |
* described above, except that it works with native SymEngine data |
| 311 |
* types. The @p independent_symbols are represented by a vector of |
| 312 |
* `SymEngine::RCP<const SymEngine::Basic>`, and the |
| 313 |
* @p dependent_function is of type |
| 314 |
* `SymEngine::RCP<const SymEngine::Basic>`. |
| 315 |
* |
| 316 |
* @param independent_symbols A vector of symbols that represent |
| 317 |
* the independent variables that are arguments to the |
| 318 |
* @p dependent_function. |
| 319 |
* @param dependent_function A single symbolic expression that |
| 320 |
* represents a dependent variable. |
| 321 |
* @param use_cse A flag to indicate whether or not to use common |
| 322 |
* subexpression elimination. |
| 323 |
* |
| 324 |
* @note The caveats described in the documentation of the other init() |
| 325 |
* function apply here as well. |
| 326 |
*/ |
| 327 |
// The following definition is required due to base class CRTP. |
| 328 |
void |
| 329 |
init(const SymEngine::vec_basic &independent_symbols, |
| 330 |
const SymEngine::Basic &dependent_function, |
| 331 |
const bool use_cse = false); |
| 332 |
|
| 333 |
/** |
| 334 |
* Initialization, and registration of the independent and dependent |
| 335 |
* variables. |
| 336 |
* |
| 337 |
* This variation of the initialization function registers a vector of |
| 338 |
* dependent expressions. If the @p use_cse is set to `true`, then |
| 339 |
* common subexpression elimination is also performed at the time of |
| 340 |
* initialization. |
| 341 |
* |
| 342 |
* @param independent_symbols A vector of symbols that represent |
| 343 |
* the independent variables that are arguments to the |
| 344 |
* @p dependent_functions. |
| 345 |
* @param dependent_functions A vector of symbolic expressions that |
| 346 |
* represent the dependent variables. |
| 347 |
* @param use_cse A flag to indicate whether or not to use common |
| 348 |
* subexpression elimination. |
| 349 |
* |
| 350 |
* @note After this function is called, no further registration of |
| 351 |
* dependent functions can be performed. |
| 352 |
*/ |
| 353 |
void |
| 354 |
init(const types::symbol_vector &independent_symbols, |
| 355 |
const types::symbol_vector &dependent_functions, |
| 356 |
const bool use_cse = false); |
| 357 |
|
| 358 |
|
| 359 |
/** |
| 360 |
* Initialization, and registration of the independent and dependent |
| 361 |
* variables. |
| 362 |
* |
| 363 |
* This function performs the same action as the other init() function, |
| 364 |
* described above, except that it works with native SymEngine data |
| 365 |
* types. Both the @p independent_symbols and @p dependent_functions |
| 366 |
* are represented by a vector of |
| 367 |
* `SymEngine::RCP<const SymEngine::Basic>`. |
| 368 |
* |
| 369 |
* @param independent_symbols A vector of symbols that represent |
| 370 |
* the independent variables that are arguments to the |
| 371 |
* @p dependent_functions. |
| 372 |
* @param dependent_functions A vector of symbolic expressions that |
| 373 |
* represent the dependent variables. |
| 374 |
* @param use_cse A flag to indicate whether or not to use common |
| 375 |
* subexpression elimination. |
| 376 |
* |
| 377 |
* @note The caveats described in the documentation of the other init() |
| 378 |
* function apply here as well. |
| 379 |
*/ |
| 380 |
// The following definition is required due to base class CRTP. |
| 381 |
void |
| 382 |
init(const SymEngine::vec_basic &independent_symbols, |
| 383 |
const SymEngine::vec_basic &dependent_functions, |
| 384 |
const bool use_cse = false); |
| 385 |
|
| 386 |
/** |
| 387 |
* Evaluate the dependent functions that were registered at |
| 388 |
* initialization time. |
| 389 |
* |
| 390 |
* The @p output_values are the numerical result of substituting |
| 391 |
* each of the @p substitution_values for their corresponding entry |
| 392 |
* of the pre-registered independent variables. |
| 393 |
* |
| 394 |
* @param[out] output_values A pointer to the first element in an array |
| 395 |
* of type @p ReturnType. After this call, the underlying array will |
| 396 |
* hold the numerical result of substituting the @p substitution_values |
| 397 |
* into the pre-registered dependent functions. |
| 398 |
* @param[in] substitution_values A pointer to the first element in an |
| 399 |
* array of type @p ReturnType. Each entry in this array stores the |
| 400 |
* numerical value that an independent variable is to take for the |
| 401 |
* purpose of value substitution. |
| 402 |
* |
| 403 |
* @note It is expected that both the @p output_values and |
| 404 |
* @p substitution_values arrays be correctly dimensioned, as there is |
| 405 |
* no range checking performed on these data structures. |
| 406 |
* The @p output_values array should have the same number of elements as |
| 407 |
* the dependent functions first passed to this class in the call to |
| 408 |
* init(). Similarly, the @p substitution_values array should have the |
| 409 |
* same number of elements as the there were independent variables |
| 410 |
* that were registered at the time of initialization. |
| 411 |
* |
| 412 |
* @note It is expected that there be a 1-1 correspondence between the |
| 413 |
* entries in independent variables and @p substitution_values. This |
| 414 |
* is not checked within this function, so it is up to the user to |
| 415 |
* ensure that the relationships between the independent variables and |
| 416 |
* their numerical value be correctly set up and maintained. |
| 417 |
*/ |
| 418 |
void |
| 419 |
call(ReturnType *output_values, const ReturnType *substitution_values); |
| 420 |
|
| 421 |
/** |
| 422 |
* Evaluate the dependent function that were registered at |
| 423 |
* initialization time. |
| 424 |
* |
| 425 |
* The purpose of this function is the same as the other call() |
| 426 |
* functions, but |
| 427 |
* |
| 428 |
* @param substitution_values A vector that stores the numerical values |
| 429 |
* that each independent variable that was registered with the class |
| 430 |
* instance is to take for the purpose of value substitution. |
| 431 |
* @return ReturnType The numerical value associated with the single |
| 432 |
* dependent function that is registered with this class. |
| 433 |
* |
| 434 |
* @note It is expected that there be a 1-1 correspondence between the |
| 435 |
* entries in independent variables and @p substitution_values. |
| 436 |
*/ |
| 437 |
// The following definition is required due to base class CRTP. |
| 438 |
ReturnType |
| 439 |
call(const std::vector<ReturnType> &substitution_values); |
| 440 |
|
| 441 |
/** |
| 442 |
* Write the data of this object to a stream for the purpose |
| 443 |
* of serialization using the [BOOST serialization |
| 444 |
* library](https://www.boost.org/doc/libs/1_74_0/libs/serialization/doc/index.html). |
| 445 |
*/ |
| 446 |
template <class Archive> |
| 447 |
void |
| 448 |
save(Archive &archive, const unsigned int version) const; |
| 449 |
|
| 450 |
/** |
| 451 |
* Read the data for this object from a stream for the purpose |
| 452 |
* of serialization using the [BOOST serialization |
| 453 |
* library](https://www.boost.org/doc/libs/1_74_0/libs/serialization/doc/index.html). |
| 454 |
*/ |
| 455 |
template <class Archive> |
| 456 |
void |
| 457 |
load(Archive &archive, const unsigned int version); |
| 458 |
|
| 459 |
# ifdef DOXYGEN |
| 460 |
/** |
| 461 |
* Write and read the data of this object from a stream for the purpose |
| 462 |
* of serialization using the [BOOST serialization |
| 463 |
* library](https://www.boost.org/doc/libs/1_74_0/libs/serialization/doc/index.html). |
| 464 |
*/ |
| 465 |
template <class Archive> |
| 466 |
void |
| 467 |
serialize(Archive &archive, const unsigned int version); |
| 468 |
# else |
| 469 |
// This macro defines the serialize() method that is compatible with |
| 470 |
// the templated save() and load() method that have been implemented. |
| 471 |
BOOST_SERIALIZATION_SPLIT_MEMBER() |
| 472 |
# endif |
| 473 |
|
| 474 |
/** |
| 475 |
* Print some information on state of the internal data |
| 476 |
* structures stored in the class. |
| 477 |
* |
| 478 |
* @tparam Stream The type for the output stream. |
| 479 |
* @param stream The output stream to print to. |
| 480 |
* @param print_independent_symbols A flag to indicate if the independent |
| 481 |
* variables should be outputted to the @p stream. |
| 482 |
* @param print_dependent_functions A flag to indicate if the dependent |
| 483 |
* expressions should be outputted to the @p stream. |
| 484 |
* @param print_cse_reductions A flag to indicate whether or not all |
| 485 |
* common subexpressions should be printed to the @p stream. |
| 486 |
*/ |
| 487 |
template <typename StreamType> |
| 488 |
void |
| 489 |
print(StreamType &stream, |
| 490 |
const bool print_independent_symbols = false, |
| 491 |
const bool print_dependent_functions = false, |
| 492 |
const bool print_cse_reductions = false) const; |
| 493 |
|
| 494 |
# ifndef DOXYGEN |
| 495 |
// The following definitions are required due to base class CRTP. |
| 496 |
// Since these are not used, and therefore not important to |
| 497 |
// understand, we'll define them in the most concise manner possible. |
| 498 |
// We also won't bother to document their existence, since they cannot |
| 499 |
// be used. |
| 500 |
# define IMPLEMENT_DSV_BVISIT(Argument) \ |
| 501 |
void bvisit(const Argument &) \ |
| 502 |
{ \ |
| 503 |
AssertThrow(false, ExcNotImplemented()); \ |
| 504 |
} |
| 505 |
|
| 506 |
IMPLEMENT_DSV_BVISIT(SymEngine::Basic) |
| 507 |
IMPLEMENT_DSV_BVISIT(SymEngine::Symbol) |
| 508 |
IMPLEMENT_DSV_BVISIT(SymEngine::Constant) |
| 509 |
IMPLEMENT_DSV_BVISIT(SymEngine::Integer) |
| 510 |
IMPLEMENT_DSV_BVISIT(SymEngine::Rational) |
| 511 |
IMPLEMENT_DSV_BVISIT(SymEngine::RealDouble) |
| 512 |
IMPLEMENT_DSV_BVISIT(SymEngine::ComplexDouble) |
| 513 |
IMPLEMENT_DSV_BVISIT(SymEngine::Add) |
| 514 |
IMPLEMENT_DSV_BVISIT(SymEngine::Mul) |
| 515 |
IMPLEMENT_DSV_BVISIT(SymEngine::Pow) |
| 516 |
IMPLEMENT_DSV_BVISIT(SymEngine::Log) |
| 517 |
IMPLEMENT_DSV_BVISIT(SymEngine::Sin) |
| 518 |
IMPLEMENT_DSV_BVISIT(SymEngine::Cos) |
| 519 |
IMPLEMENT_DSV_BVISIT(SymEngine::Tan) |
| 520 |
IMPLEMENT_DSV_BVISIT(SymEngine::Csc) |
| 521 |
IMPLEMENT_DSV_BVISIT(SymEngine::Sec) |
| 522 |
IMPLEMENT_DSV_BVISIT(SymEngine::Cot) |
| 523 |
IMPLEMENT_DSV_BVISIT(SymEngine::ASin) |
| 524 |
IMPLEMENT_DSV_BVISIT(SymEngine::ACos) |
| 525 |
IMPLEMENT_DSV_BVISIT(SymEngine::ATan) |
| 526 |
IMPLEMENT_DSV_BVISIT(SymEngine::ATan2) |
| 527 |
IMPLEMENT_DSV_BVISIT(SymEngine::ACsc) |
| 528 |
IMPLEMENT_DSV_BVISIT(SymEngine::ASec) |
| 529 |
IMPLEMENT_DSV_BVISIT(SymEngine::ACot) |
| 530 |
IMPLEMENT_DSV_BVISIT(SymEngine::Sinh) |
| 531 |
IMPLEMENT_DSV_BVISIT(SymEngine::Cosh) |
| 532 |
IMPLEMENT_DSV_BVISIT(SymEngine::Tanh) |
| 533 |
IMPLEMENT_DSV_BVISIT(SymEngine::Csch) |
| 534 |
IMPLEMENT_DSV_BVISIT(SymEngine::Sech) |
| 535 |
IMPLEMENT_DSV_BVISIT(SymEngine::Coth) |
| 536 |
IMPLEMENT_DSV_BVISIT(SymEngine::ASinh) |
| 537 |
IMPLEMENT_DSV_BVISIT(SymEngine::ACosh) |
| 538 |
IMPLEMENT_DSV_BVISIT(SymEngine::ATanh) |
| 539 |
IMPLEMENT_DSV_BVISIT(SymEngine::ACsch) |
| 540 |
IMPLEMENT_DSV_BVISIT(SymEngine::ACoth) |
| 541 |
IMPLEMENT_DSV_BVISIT(SymEngine::ASech) |
| 542 |
IMPLEMENT_DSV_BVISIT(SymEngine::Abs) |
| 543 |
IMPLEMENT_DSV_BVISIT(SymEngine::Gamma) |
| 544 |
IMPLEMENT_DSV_BVISIT(SymEngine::LogGamma) |
| 545 |
IMPLEMENT_DSV_BVISIT(SymEngine::Erf) |
| 546 |
IMPLEMENT_DSV_BVISIT(SymEngine::Erfc) |
| 547 |
IMPLEMENT_DSV_BVISIT(SymEngine::Max) |
| 548 |
IMPLEMENT_DSV_BVISIT(SymEngine::Min) |
| 549 |
|
| 550 |
# undef IMPLEMENT_DSV_BVISIT |
| 551 |
# endif // DOXYGEN |
| 552 |
|
| 553 |
private: |
| 554 |
// Note: It would be more efficient to store this data in native |
| 555 |
// SymEngine types, as it would prevent some copying of the data |
| 556 |
// structures. However, this makes serialization more difficult, |
| 557 |
// so we use our own serializable types instead, and lose a bit |
| 558 |
// of efficiency. |
| 559 |
|
| 560 |
/** |
| 561 |
* A vector of symbols that represent the independent variables. |
| 562 |
*/ |
| 563 |
SD::types::symbol_vector independent_symbols; |
| 564 |
|
| 565 |
/** |
| 566 |
* A vector of expressions that represent dependent functions. |
| 567 |
*/ |
| 568 |
SD::types::symbol_vector dependent_functions; |
| 569 |
|
| 570 |
/** |
| 571 |
* A data structure that may be used to invoke common subexpression |
| 572 |
* elimination on the dependent functions, with the aim to decrease |
| 573 |
* the time taken to evaluate them. |
| 574 |
*/ |
| 575 |
CSEDictionaryVisitor<ReturnType, ExpressionType> cse; |
| 576 |
}; |
| 577 |
|
| 578 |
|
| 579 |
|
| 580 |
/* ------------------ inline and template functions ------------------ */ |
| 581 |
|
| 582 |
|
| 583 |
# ifndef DOXYGEN |
| 584 |
|
| 585 |
/* -------------- CommonSubexpressionEliminationVisitor -------------- */ |
| 586 |
|
| 587 |
|
| 588 |
template <typename ReturnType, typename ExpressionType> |
| 589 |
void |
| 590 |
CSEDictionaryVisitor<ReturnType, ExpressionType>::init( |
| 591 |
const SD::types::symbol_vector &dependent_functions) |
| 592 |
{ |
| 593 |
init(Utilities::convert_expression_vector_to_basic_vector( |
| 594 |
dependent_functions)); |
| 595 |
} |
| 596 |
|
| 597 |
|
| 598 |
|
| 599 |
template <typename ReturnType, typename ExpressionType> |
| 600 |
void |
| 601 |
CSEDictionaryVisitor<ReturnType, ExpressionType>::init( |
| 602 |
const SymEngine::vec_basic &dependent_functions) |
| 603 |
{ |
| 604 |
// After the next call, the data stored in replacements is structured |
| 605 |
// as follows: |
| 606 |
// |
| 607 |
// replacements[i] := [f, f(x)] |
| 608 |
// replacements[i].first = intermediate function label "f" |
| 609 |
// replacements[i].second = intermediate function definition "f(x)" |
| 610 |
// |
| 611 |
// It is to be evaluated top down (i.e. index 0 to |
| 612 |
// replacements.size()), with the results going back into the |
| 613 |
// substitution map for the next levels. So for each "i", "x" are the |
| 614 |
// superset of the input values and the previously evaluated [f_0(x), |
| 615 |
// f_1(x), ..., f_{i-1}(x)]. |
| 616 |
// |
| 617 |
// The final result is a set of reduced expressions |
| 618 |
// that must be computed after the replacement |
| 619 |
// values have been computed. |
| 620 |
SymEngine::vec_pair se_replacements; |
| 621 |
SymEngine::vec_basic se_reduced_exprs; |
| 622 |
SymEngine::cse(se_replacements, se_reduced_exprs, dependent_functions); |
| 623 |
|
| 624 |
intermediate_symbols_exprs = |
| 625 |
Utilities::convert_basic_pair_vector_to_expression_pair_vector( |
| 626 |
se_replacements); |
| 627 |
reduced_exprs = Utilities::convert_basic_vector_to_expression_vector( |
| 628 |
se_reduced_exprs); |
| 629 |
} |
| 630 |
|
| 631 |
|
| 632 |
|
| 633 |
template <typename ReturnType, typename ExpressionType> |
| 634 |
void |
| 635 |
CSEDictionaryVisitor<ReturnType, ExpressionType>::call( |
| 636 |
ReturnType *output_values, |
| 637 |
const SD::types::symbol_vector &independent_symbols, |
| 638 |
const ReturnType *substitution_values) |
| 639 |
{ |
| 640 |
call(output_values, |
| 641 |
Utilities::convert_expression_vector_to_basic_vector( |
| 642 |
independent_symbols), |
| 643 |
substitution_values); |
| 644 |
} |
| 645 |
|
| 646 |
|
| 647 |
|
| 648 |
template <typename ReturnType, typename ExpressionType> |
| 649 |
void |
| 650 |
CSEDictionaryVisitor<ReturnType, ExpressionType>::call( |
| 651 |
ReturnType *output_values, |
| 652 |
const SymEngine::vec_basic &independent_symbols, |
| 653 |
const ReturnType *substitution_values) |
| 654 |
{ |
| 655 |
Assert(n_reduced_expressions() > 0, ExcInternalError()); |
| 656 |
|
| 657 |
// First we add the input values into the substitution map... |
| 658 |
SymEngine::map_basic_basic substitution_value_map; |
| 659 |
for (unsigned i = 0; i < independent_symbols.size(); ++i) |
| 660 |
substitution_value_map[independent_symbols[i]] = |
| 661 |
static_cast<const SymEngine::RCP<const SymEngine::Basic> &>( |
| 662 |
ExpressionType(substitution_values[i])); |
| 663 |
|
| 664 |
// ... followed by any intermediate evaluations due to the application |
| 665 |
// of CSE. These are fed directly back into the substitution map... |
| 666 |
for (const auto &expression : intermediate_symbols_exprs) |
| 667 |
{ |
| 668 |
const SymEngine::RCP<const SymEngine::Basic> &cse_symbol = |
| 669 |
expression.first; |
| 670 |
const SymEngine::RCP<const SymEngine::Basic> &cse_expr = |
| 671 |
expression.second; |
| 672 |
Assert(substitution_value_map.find(cse_symbol) == |
| 673 |
substitution_value_map.end(), |
| 674 |
ExcMessage( |
| 675 |
"Reduced symbol already appears in substitution map. " |
| 676 |
"Is there a clash between the reduced symbol name and " |
| 677 |
"the symbol used for an independent variable?")); |
| 678 |
substitution_value_map[cse_symbol] = |
| 679 |
static_cast<const SymEngine::RCP<const SymEngine::Basic> &>( |
| 680 |
ExpressionType(ExpressionType(cse_expr) |
| 681 |
.template substitute_and_evaluate<ReturnType>( |
| 682 |
substitution_value_map))); |
| 683 |
} |
| 684 |
|
| 685 |
// ... followed by the final reduced expressions |
| 686 |
for (unsigned i = 0; i < reduced_exprs.size(); ++i) |
| 687 |
output_values[i] = ExpressionType(reduced_exprs[i]) |
| 688 |
.template substitute_and_evaluate<ReturnType>( |
| 689 |
substitution_value_map); |
| 690 |
} |
| 691 |
|
| 692 |
|
| 693 |
|
| 694 |
template <typename ReturnType, typename ExpressionType> |
| 695 |
template <class Archive> |
| 696 |
void |
| 697 |
CSEDictionaryVisitor<ReturnType, ExpressionType>::save( |
| 698 |
Archive &ar, |
| 699 |
const unsigned int /*version*/) const |
| 700 |
{ |
| 701 |
// The reduced expressions depend on the intermediate expressions, |
| 702 |
// so we serialize the latter before the former. |
| 703 |
ar &intermediate_symbols_exprs; |
| 704 |
ar &reduced_exprs; |
| 705 |
} |
| 706 |
|
| 707 |
|
| 708 |
|
| 709 |
template <typename ReturnType, typename ExpressionType> |
| 710 |
template <class Archive> |
| 711 |
void |
| 712 |
CSEDictionaryVisitor<ReturnType, ExpressionType>::load( |
| 713 |
Archive &ar, |
| 714 |
const unsigned int /*version*/) |
| 715 |
{ |
| 716 |
Assert(intermediate_symbols_exprs.empty(), ExcInternalError()); |
| 717 |
Assert(reduced_exprs.empty(), ExcInternalError()); |
| 718 |
|
| 719 |
// The reduced expressions depend on the intermediate expressions, |
| 720 |
// so we deserialize the latter before the former. |
| 721 |
ar &intermediate_symbols_exprs; |
| 722 |
ar &reduced_exprs; |
| 723 |
} |
| 724 |
|
| 725 |
|
| 726 |
|
| 727 |
template <typename ReturnType, typename ExpressionType> |
| 728 |
template <typename StreamType> |
| 729 |
void |
| 730 |
CSEDictionaryVisitor<ReturnType, ExpressionType>::print( |
| 731 |
StreamType &stream) const |
| 732 |
{ |
| 733 |
stream << "Common subexpression elimination: \n"; |
| 734 |
stream << " Intermediate reduced expressions: \n"; |
| 735 |
for (unsigned i = 0; i < intermediate_symbols_exprs.size(); ++i) |
| 736 |
{ |
| 737 |
const SymEngine::RCP<const SymEngine::Basic> &cse_symbol = |
| 738 |
intermediate_symbols_exprs[i].first; |
| 739 |
const SymEngine::RCP<const SymEngine::Basic> &cse_expr = |
| 740 |
intermediate_symbols_exprs[i].second; |
| 741 |
stream << " " << i << ": " << cse_symbol << " = " << cse_expr |
| 742 |
<< '\n'; |
| 743 |
} |
| 744 |
|
| 745 |
stream << " Final reduced expressions for dependent variables: \n"; |
| 746 |
for (unsigned i = 0; i < reduced_exprs.size(); ++i) |
| 747 |
stream << " " << i << ": " << reduced_exprs[i] << '\n'; |
| 748 |
|
| 749 |
stream << std::flush; |
| 750 |
} |
| 751 |
|
| 752 |
|
| 753 |
|
| 754 |
template <typename ReturnType, typename ExpressionType> |
| 755 |
bool |
| 756 |
CSEDictionaryVisitor<ReturnType, ExpressionType>::executed() const |
| 757 |
{ |
| 758 |
// For dictionary substitution, the CSE algorithm moves |
| 759 |
// ownership of the dependent function expression definition |
| 760 |
// to the entries in reduced_exprs. So its size thus determines |
| 761 |
// whether CSE has been executed or not. |
| 762 |
return (n_reduced_expressions() > 0) || |
| 763 |
(n_intermediate_expressions() > 0); |
| 764 |
} |
| 765 |
|
| 766 |
|
| 767 |
|
| 768 |
template <typename ReturnType, typename ExpressionType> |
| 769 |
unsigned int |
| 770 |
CSEDictionaryVisitor<ReturnType, |
| 771 |
ExpressionType>::n_intermediate_expressions() const |
| 772 |
{ |
| 773 |
return intermediate_symbols_exprs.size(); |
| 774 |
} |
| 775 |
|
| 776 |
|
| 777 |
|
| 778 |
template <typename ReturnType, typename ExpressionType> |
| 779 |
unsigned int |
| 780 |
CSEDictionaryVisitor<ReturnType, ExpressionType>::n_reduced_expressions() |
| 781 |
const |
| 782 |
{ |
| 783 |
return reduced_exprs.size(); |
| 784 |
} |
| 785 |
|
| 786 |
|
| 787 |
|
| 788 |
/* ------------------ DictionarySubstitutionVisitor ------------------ */ |
| 789 |
|
| 790 |
|
| 791 |
template <typename ReturnType, typename ExpressionType> |
| 792 |
void |
| 793 |
DictionarySubstitutionVisitor<ReturnType, ExpressionType>::init( |
| 794 |
const types::symbol_vector &inputs, |
| 795 |
const SD::Expression &output, |
| 796 |
const bool use_cse) |
| 797 |
{ |
| 798 |
init(inputs, types::symbol_vector{output}, use_cse); |
| 799 |
} |
| 800 |
|
| 801 |
|
| 802 |
|
| 803 |
template <typename ReturnType, typename ExpressionType> |
| 804 |
void |
| 805 |
DictionarySubstitutionVisitor<ReturnType, ExpressionType>::init( |
| 806 |
const SymEngine::vec_basic &inputs, |
| 807 |
const SymEngine::Basic &output, |
| 808 |
const bool use_cse) |
| 809 |
{ |
| 810 |
init(Utilities::convert_basic_vector_to_expression_vector(inputs), |
| 811 |
SD::Expression(output.rcp_from_this()), |
| 812 |
use_cse); |
| 813 |
} |
| 814 |
|
| 815 |
|
| 816 |
|
| 817 |
template <typename ReturnType, typename ExpressionType> |
| 818 |
void |
| 819 |
DictionarySubstitutionVisitor<ReturnType, ExpressionType>::init( |
| 820 |
const SymEngine::vec_basic &inputs, |
| 821 |
const SymEngine::vec_basic &outputs, |
| 822 |
const bool use_cse) |
| 823 |
{ |
| 824 |
init(Utilities::convert_basic_vector_to_expression_vector(inputs), |
| 825 |
Utilities::convert_basic_vector_to_expression_vector(outputs), |
| 826 |
use_cse); |
| 827 |
} |
| 828 |
|
| 829 |
|
| 830 |
|
| 831 |
template <typename ReturnType, typename ExpressionType> |
| 832 |
void |
| 833 |
DictionarySubstitutionVisitor<ReturnType, ExpressionType>::init( |
| 834 |
const types::symbol_vector &inputs, |
| 835 |
const types::symbol_vector &outputs, |
| 836 |
const bool use_cse) |
| 837 |
{ |
| 838 |
independent_symbols.clear(); |
| 839 |
dependent_functions.clear(); |
| 840 |
|
| 841 |
independent_symbols = inputs; |
| 842 |
|
| 843 |
// Perform common subexpression elimination if requested |
| 844 |
// Note: After this is done, the results produced by |
| 845 |
// dependent_functions and cse.reduced_exprs should be |
| 846 |
// the same. We could keep the former so that we can print |
| 847 |
// out the original expressions if we wish to do so. |
| 848 |
if (use_cse == false) |
| 849 |
dependent_functions = outputs; |
| 850 |
else |
| 851 |
{ |
| 852 |
cse.init(outputs); |
| 853 |
} |
| 854 |
} |
| 855 |
|
| 856 |
|
| 857 |
|
| 858 |
template <typename ReturnType, typename ExpressionType> |
| 859 |
ReturnType |
| 860 |
DictionarySubstitutionVisitor<ReturnType, ExpressionType>::call( |
| 861 |
const std::vector<ReturnType> &substitution_values) |
| 862 |
{ |
| 863 |
Assert( |
| 864 |
dependent_functions.size() == 1, |
| 865 |
ExcMessage( |
| 866 |
"Cannot use this call function when more than one symbolic expression is to be evaluated.")); |
| 867 |
Assert( |
| 868 |
substitution_values.size() == independent_symbols.size(), |
| 869 |
ExcMessage( |
| 870 |
"Input substitution vector does not match size of symbol vector.")); |
| 871 |
|
| 872 |
ReturnType out = dealii::internal::NumberType<ReturnType>::value(0.0); |
| 873 |
call(&out, substitution_values.data()); |
| 874 |
return out; |
| 875 |
} |
| 876 |
|
| 877 |
|
| 878 |
|
| 879 |
template <typename ReturnType, typename ExpressionType> |
| 880 |
void |
| 881 |
DictionarySubstitutionVisitor<ReturnType, ExpressionType>::call( |
| 882 |
ReturnType *output_values, |
| 883 |
const ReturnType *substitution_values) |
| 884 |
{ |
| 885 |
// Check to see if CSE has been performed |
| 886 |
if (cse.executed()) |
| 887 |
{ |
| 888 |
cse.call(output_values, independent_symbols, substitution_values); |
| 889 |
} |
| 890 |
else |
| 891 |
{ |
| 892 |
// Build a substitution map. |
| 893 |
SymEngine::map_basic_basic substitution_value_map; |
| 894 |
for (unsigned i = 0; i < independent_symbols.size(); ++i) |
| 895 |
substitution_value_map[independent_symbols[i]] = |
| 896 |
static_cast<const SymEngine::RCP<const SymEngine::Basic> &>( |
| 897 |
ExpressionType(substitution_values[i])); |
| 898 |
|
| 899 |
// Since we don't know how to definitively evaluate the |
| 900 |
// input number type, we create a generic Expression |
| 901 |
// with the given symbolic expression and ask it to perform |
| 902 |
// substitution and evaluation for us. |
| 903 |
Assert(dependent_functions.size() > 0, ExcInternalError()); |
| 904 |
for (unsigned i = 0; i < dependent_functions.size(); ++i) |
| 905 |
output_values[i] = |
| 906 |
ExpressionType(dependent_functions[i]) |
| 907 |
.template substitute_and_evaluate<ReturnType>( |
| 908 |
substitution_value_map); |
| 909 |
} |
| 910 |
} |
| 911 |
|
| 912 |
|
| 913 |
|
| 914 |
template <typename ReturnType, typename ExpressionType> |
| 915 |
template <class Archive> |
| 916 |
void |
| 917 |
DictionarySubstitutionVisitor<ReturnType, ExpressionType>::save( |
| 918 |
Archive &ar, |
| 919 |
const unsigned int version) const |
| 920 |
{ |
| 921 |
// Add some dynamic information to determine if CSE has been used, |
| 922 |
// without relying on the CSE class when deserializing. |
| 923 |
// const bool used_cse = cse.executed(); |
| 924 |
// ar &used_cse; |
| 925 |
|
| 926 |
// CSE and dependent variables both require the independent |
| 927 |
// symbols, so we serialize them first. The dependent variables |
| 928 |
// might depend on the outcome of CSE, so we have to serialize |
| 929 |
// them last. |
| 930 |
ar &independent_symbols; |
| 931 |
cse.save(ar, version); |
| 932 |
ar &dependent_functions; |
| 933 |
} |
| 934 |
|
| 935 |
|
| 936 |
|
| 937 |
template <typename ReturnType, typename ExpressionType> |
| 938 |
template <class Archive> |
| 939 |
void |
| 940 |
DictionarySubstitutionVisitor<ReturnType, ExpressionType>::load( |
| 941 |
Archive &ar, |
| 942 |
const unsigned int version) |
| 943 |
{ |
| 944 |
Assert(cse.executed() == false, ExcInternalError()); |
| 945 |
Assert(cse.n_intermediate_expressions() == 0, ExcInternalError()); |
| 946 |
Assert(cse.n_reduced_expressions() == 0, ExcInternalError()); |
| 947 |
|
| 948 |
// CSE and dependent variables both require the independent |
| 949 |
// symbols, so we deserialize them first. The dependent variables |
| 950 |
// might depend on the outcome of CSE, so we have to deserialize |
| 951 |
// them last. |
| 952 |
ar &independent_symbols; |
| 953 |
cse.load(ar, version); |
| 954 |
ar &dependent_functions; |
| 955 |
} |
| 956 |
|
| 957 |
|
| 958 |
|
| 959 |
template <typename ReturnType, typename ExpressionType> |
| 960 |
template <typename StreamType> |
| 961 |
void |
| 962 |
DictionarySubstitutionVisitor<ReturnType, ExpressionType>::print( |
| 963 |
StreamType &stream, |
| 964 |
const bool print_independent_symbols, |
| 965 |
const bool print_dependent_functions, |
| 966 |
const bool print_cse_reductions) const |
| 967 |
{ |
| 968 |
if (print_independent_symbols) |
| 969 |
{ |
| 970 |
stream << "Independent variables: \n"; |
| 971 |
for (unsigned i = 0; i < independent_symbols.size(); ++i) |
| 972 |
stream << " " << i << ": " << independent_symbols[i] << '\n'; |
| 973 |
|
| 974 |
stream << std::flush; |
| 975 |
} |
| 976 |
|
| 977 |
// Check to see if CSE has been performed |
| 978 |
if (print_cse_reductions && cse.executed()) |
| 979 |
{ |
| 980 |
cse.print(stream); |
| 981 |
} |
| 982 |
else |
| 983 |
{ |
| 984 |
Assert(dependent_functions.size() > 0, ExcInternalError()); |
| 985 |
|
| 986 |
if (print_dependent_functions) |
| 987 |
{ |
| 988 |
stream << "Dependent variables: \n"; |
| 989 |
for (unsigned i = 0; i < dependent_functions.size(); ++i) |
| 990 |
stream << " " << i << dependent_functions[i] << '\n'; |
| 991 |
|
| 992 |
stream << std::flush; |
| 993 |
} |
| 994 |
} |
| 995 |
} |
| 996 |
|
| 997 |
# endif // DOXYGEN |
| 998 |
|
| 999 |
} // namespace internal |
| 1000 |
} // namespace SD |
| 1001 |
} // namespace Differentiation |
| 1002 |
|
| 1003 |
|
| 1004 |
DEAL_II_NAMESPACE_CLOSE |
| 1005 |
|
| 1006 |
#else |
| 1007 |
|
| 1008 |
// Make sure the scripts that create the C++20 module input files have |
| 1009 |
// something to latch on if the preprocessor #ifdef above would |
| 1010 |
// otherwise lead to an empty content of the file. |
| 1011 |
DEAL_II_NAMESPACE_OPEN |
| 1012 |
DEAL_II_NAMESPACE_CLOSE |
| 1013 |
|
| 1014 |
#endif // DEAL_II_WITH_SYMENGINE |
| 1015 |
|
| 1016 |
#endif // dealii_differentiation_sd_symengine_number_visitor_internal_h |