Loading...
Searching...
No Matches
SymEngine::RebuildVisitor Class Reference
+ Inheritance diagram for SymEngine::RebuildVisitor:
+ Collaboration diagram for SymEngine::RebuildVisitor:

Public Member Functions

 RebuildVisitor (umap_basic_basic &subs_, umap_basic_basic &opt_subs_, set_basic &to_eliminate_, set_basic &excluded_symbols_, vec_pair &replacements_)
 
virtual RCP< const Basicapply (const RCP< const Basic > &orig_expr)
 
RCP< const Basicnext_symbol ()
 
void bvisit (const FunctionSymbol &x)
 
void bvisit (const Basic &x)
 
void bvisit (const Add &x)
 
void bvisit (const Mul &x)
 
void bvisit (const Pow &x)
 
void bvisit (const OneArgFunction &x)
 
template<class T >
void bvisit (const TwoArgBasic< T > &x)
 
void bvisit (const MultiArgFunction &x)
 
- Public Member Functions inherited from SymEngine::TransformVisitor
virtual RCP< const Basicapply (const RCP< const Basic > &x)
 
void bvisit (const Basic &x)
 
void bvisit (const Add &x)
 
void bvisit (const Mul &x)
 
void bvisit (const Pow &x)
 
void bvisit (const OneArgFunction &x)
 
template<class T >
void bvisit (const TwoArgBasic< T > &x)
 
void bvisit (const MultiArgFunction &x)
 

Data Fields

RCP< const Basicresult_
 

Private Attributes

umap_basic_basicsubs
 
umap_basic_basicopt_subs
 
set_basicto_eliminate
 
set_basicexcluded_symbols
 
vec_pairreplacements
 
unsigned next_symbol_index = 0
 

Additional Inherited Members

- Protected Attributes inherited from SymEngine::TransformVisitor
RCP< const Basicresult_
 

Detailed Description

Definition at line 420 of file cse.cpp.

Constructor & Destructor Documentation

◆ RebuildVisitor()

SymEngine::RebuildVisitor::RebuildVisitor ( umap_basic_basic subs_,
umap_basic_basic opt_subs_,
set_basic to_eliminate_,
set_basic excluded_symbols_,
vec_pair replacements_ 
)
inline

Definition at line 433 of file cse.cpp.

436 : subs(subs_), opt_subs(opt_subs_), to_eliminate(to_eliminate_),
437 excluded_symbols(excluded_symbols_), replacements(replacements_)
438 {
439 }

Member Function Documentation

◆ apply()

virtual RCP< const Basic > SymEngine::RebuildVisitor::apply ( const RCP< const Basic > &  orig_expr)
inlinevirtual

Reimplemented from SymEngine::TransformVisitor.

Definition at line 440 of file cse.cpp.

441 {
442 RCP<const Basic> expr = orig_expr;
443 if (is_a_Atom(*expr)) {
444 return expr;
445 }
446
447 auto iter = subs.find(expr);
448 if (iter != subs.end()) {
449 return iter->second;
450 }
451 auto iter2 = opt_subs.find(expr);
452 if (iter2 != opt_subs.end()) {
453 expr = iter2->second;
454 }
455 expr->accept(*this);
456 auto new_expr = result_;
457 if (to_eliminate.find(orig_expr) != to_eliminate.end()) {
458 auto sym = next_symbol();
459 subs[orig_expr] = sym;
460 replacements.push_back(
461 std::pair<RCP<const Basic>, RCP<const Basic>>(sym, new_expr));
462 return sym;
463 }
464 return new_expr;
465 }
T end(T... args)
T find(T... args)
bool is_a_Atom(const Basic &b)
Returns true if b is an atom. i.e. b.get_args returns an empty vector.
Definition: basic.cpp:95
T push_back(T... args)

◆ bvisit() [1/8]

void SymEngine::TransformVisitor::bvisit ( const Add x)

Definition at line 246 of file visitor.cpp.

154{
155 vec_basic newargs;
156 for (const auto &a : x.get_args()) {
157 newargs.push_back(apply(a));
158 }
159 result_ = add(newargs);
160}
RCP< const Basic > add(const RCP< const Basic > &a, const RCP< const Basic > &b)
Adds two objects (safely).
Definition: add.cpp:425

◆ bvisit() [2/8]

void SymEngine::TransformVisitor::bvisit ( const Basic x)

Definition at line 245 of file visitor.cpp.

149{
150 result_ = x.rcp_from_this();
151}

◆ bvisit() [3/8]

void SymEngine::RebuildVisitor::bvisit ( const FunctionSymbol x)
inline

Definition at line 476 of file cse.cpp.

477 {
478 auto &fargs = x.get_vec();
479 vec_basic newargs;
480 for (const auto &a : fargs) {
481 newargs.push_back(apply(a));
482 }
483 if (x.get_name() == "add") {
484 result_ = add(newargs);
485 } else if (x.get_name() == "mul") {
486 result_ = mul(newargs);
487 } else if (x.get_name() == "pow") {
488 result_ = pow(newargs[0], newargs[1]);
489 } else {
490 result_ = x.create(newargs);
491 }
492 }
RCP< const Basic > mul(const RCP< const Basic > &a, const RCP< const Basic > &b)
Multiplication.
Definition: mul.cpp:352
T pow(T... args)

◆ bvisit() [4/8]

void SymEngine::TransformVisitor::bvisit ( const Mul x)

Definition at line 247 of file visitor.cpp.

163{
164 vec_basic newargs;
165 for (const auto &a : x.get_args()) {
166 newargs.push_back(apply(a));
167 }
168 result_ = mul(newargs);
169}

◆ bvisit() [5/8]

void SymEngine::TransformVisitor::bvisit ( const MultiArgFunction x)

Definition at line 263 of file visitor.cpp.

194{
195 auto fargs = x.get_args();
196 vec_basic newargs;
197 for (const auto &a : fargs) {
198 newargs.push_back(apply(a));
199 }
200 auto nbarg = x.create(newargs);
201 result_ = nbarg;
202}

◆ bvisit() [6/8]

void SymEngine::TransformVisitor::bvisit ( const OneArgFunction x)

Definition at line 249 of file visitor.cpp.

183{
184 auto farg = x.get_arg();
185 auto newarg = apply(farg);
186 if (eq(*newarg, *farg)) {
187 result_ = x.rcp_from_this();
188 } else {
189 result_ = x.create(newarg);
190 }
191}
bool eq(const Basic &a, const Basic &b)
Checks equality for a and b
Definition: basic-inl.h:21

◆ bvisit() [7/8]

void SymEngine::TransformVisitor::bvisit ( const Pow x)

Definition at line 248 of file visitor.cpp.

172{
173 auto base_ = x.get_base(), exp_ = x.get_exp();
174 auto newarg1 = apply(base_), newarg2 = apply(exp_);
175 if (base_ != newarg1 or exp_ != newarg2) {
176 result_ = pow(newarg1, newarg2);
177 } else {
178 result_ = x.rcp_from_this();
179 }
180}

◆ bvisit() [8/8]

template<class T >
void SymEngine::TransformVisitor::bvisit ( const TwoArgBasic< T > &  x)
inline

Definition at line 252 of file visitor.h.

253 {
254 auto farg1 = x.get_arg1(), farg2 = x.get_arg2();
255 auto newarg1 = apply(farg1), newarg2 = apply(farg2);
256 if (farg1 != newarg1 or farg2 != newarg2) {
257 result_ = x.create(newarg1, newarg2);
258 } else {
259 result_ = x.rcp_from_this();
260 }
261 }

◆ next_symbol()

RCP< const Basic > SymEngine::RebuildVisitor::next_symbol ( )
inline

Definition at line 466 of file cse.cpp.

467 {
468 RCP<const Basic> sym = symbol("x" + to_string(next_symbol_index));
469 next_symbol_index++;
470 if (excluded_symbols.find(sym) == excluded_symbols.end()) {
471 return sym;
472 } else {
473 return next_symbol();
474 }
475 };
RCP< const Symbol > symbol(const std::string &name)
inline version to return Symbol
Definition: symbol.h:82
std::string to_string(const T &value)
workaround for MinGW bug
Definition: basic-inl.h:162

Field Documentation

◆ excluded_symbols

set_basic& SymEngine::RebuildVisitor::excluded_symbols
private

Definition at line 426 of file cse.cpp.

◆ next_symbol_index

unsigned SymEngine::RebuildVisitor::next_symbol_index = 0
private

Definition at line 428 of file cse.cpp.

◆ opt_subs

umap_basic_basic& SymEngine::RebuildVisitor::opt_subs
private

Definition at line 424 of file cse.cpp.

◆ replacements

vec_pair& SymEngine::RebuildVisitor::replacements
private

Definition at line 427 of file cse.cpp.

◆ result_

RCP<const Basic> SymEngine::TransformVisitor::result_

Definition at line 238 of file visitor.h.

◆ subs

umap_basic_basic& SymEngine::RebuildVisitor::subs
private

Definition at line 423 of file cse.cpp.

◆ to_eliminate

set_basic& SymEngine::RebuildVisitor::to_eliminate
private

Definition at line 425 of file cse.cpp.


The documentation for this class was generated from the following file: