New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
DON'T MERGE: Example matching decision trees for sample patterns #1482
Open
Upabjojr
wants to merge
11
commits into
symengine:master
Choose a base branch
from
Upabjojr:example_generator
base: master
Could not load branches
Branch not found: {{ refName }}
Could not load tags
Nothing to show
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 3 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
1dfb9b5
example matching decision trees for sample patterns
Upabjojr d74f017
Add a CMakeLists.txt
certik 0994363
Add example_yield also
certik b2e2b64
Using C++ functional and abstract class for generators
Upabjojr 828da47
testing coroutine.h library
Upabjojr 70925a9
added example with setjmp
Upabjojr 9b1d2eb
formatting example setjmp
Upabjojr c81be71
applying formatting rules
Upabjojr 4d229a6
removing redundancies
Upabjojr 8ea8da6
Fix memory leaks
isuruf 828d775
Remove debug statement and format
isuruf File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
project(matchpygen) | ||
|
||
include_directories(BEFORE ${symengine_SOURCE_DIR}) | ||
include_directories(BEFORE ${symengine_BINARY_DIR}) | ||
include_directories(BEFORE ${teuchos_SOURCE_DIR}) | ||
include_directories(BEFORE ${teuchos_BINARY_DIR}) | ||
|
||
add_executable(example example.cpp) | ||
target_link_libraries(example symengine) | ||
|
||
add_executable(example_yield example_yield.cpp) | ||
target_link_libraries(example_yield symengine) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,132 @@ | ||
#include <iostream> | ||
#include <tuple> | ||
#include <map> | ||
#include <string> | ||
#include <deque> | ||
|
||
#include <symengine/basic.h> | ||
#include <symengine/pow.h> | ||
|
||
using namespace std; | ||
using namespace SymEngine; | ||
|
||
typedef map<string, RCP<const Basic>> Substitution2; | ||
|
||
int try_add_variable(Substitution2 &subst, string variable_name, | ||
RCP<const Basic> &replacement) | ||
{ | ||
if (subst.find(variable_name) == subst.end()) { | ||
subst[variable_name] = replacement; | ||
// this[variable_name] = replacement; //.copy() if | ||
// isinstance(replacement, Multiset) else replacement | ||
} else { | ||
// Node &existing_value = *(this->find(variable_name)); | ||
/* | ||
if (isinstance(existing_value, tuple)) { | ||
if (isinstance(replacement, Multiset)) { | ||
if (Multiset(existing_value) != replacement){ | ||
return -1; | ||
} | ||
} else if (replacement != existing_value) { | ||
return -1; | ||
} | ||
else if (isinstance(existing_value, Multiset)) { | ||
if (!isinstance(replacement, (tuple, list, Multiset))) { | ||
return -1; | ||
} | ||
auto &compare_value = Multiset(replacement); | ||
if (existing_value == compare_value){ | ||
if (! isinstance(replacement, Multiset)) { | ||
this->insert(make_pair(variable_name, replacement)); | ||
} else { | ||
return -1; | ||
} | ||
} else if (replacement != existing_value){ | ||
return -1; | ||
} | ||
} | ||
} | ||
*/ | ||
} | ||
return 0; | ||
} | ||
|
||
typedef deque<RCP<const Basic>> Deque; | ||
|
||
Deque get_deque(RCP<const Basic> expr) | ||
{ | ||
Deque d; | ||
for (RCP<const Basic> i : expr->get_args()) { | ||
d.push_back(i); | ||
} | ||
return d; | ||
} | ||
|
||
tuple<int, Substitution2> match_root(RCP<const Basic> subject) | ||
{ | ||
; | ||
Deque subjects; | ||
subjects.push_back(subject); | ||
Substitution2 subst0; | ||
// state 2200; | ||
if (subjects.size() >= 1 && is_a<Pow>(*subjects[0])) { | ||
RCP<const Basic> tmp1 = subjects.front(); | ||
subjects.pop_front(); | ||
Deque subjects2 = get_deque(tmp1); | ||
// state 2201; | ||
if (subjects2.size() >= 1 && subjects2[0]->__eq__(*symbol("x"))) { | ||
RCP<const Basic> tmp3 = subjects2.front(); | ||
subjects2.pop_front(); | ||
// state 2202; | ||
if (subjects2.size() >= 1) { | ||
auto tmp4 = subjects2.front(); | ||
subjects2.pop_front(); | ||
Substitution2 subst1(subst0); | ||
if (!try_add_variable(subst1, "i2", tmp4)) { | ||
// state 2203; | ||
if (subjects2.size() == 0) { | ||
// state 2204; | ||
if (subjects.size() == 0) { | ||
Substitution2 tmp_subst; | ||
tmp_subst["w"] = subst1["i2"]; | ||
// 0: x**w; | ||
return make_tuple(0, tmp_subst); | ||
} | ||
} | ||
} | ||
subjects2.push_front(tmp4); | ||
} | ||
subjects2.push_front(tmp3); | ||
} | ||
subjects.push_front(tmp1); | ||
} | ||
if (subjects.size() >= 1 && subjects[0]->__eq__(*symbol("y"))) { | ||
auto &tmp6 = subjects.front(); | ||
subjects.pop_front(); | ||
// state 2205; | ||
if (subjects.size() == 0) { | ||
// 1: y; | ||
return make_tuple(1, subst0); | ||
} | ||
subjects.push_front(tmp6); | ||
} | ||
return make_tuple(-1, subst0); | ||
}; | ||
|
||
int main(int argc, char *argv[]) | ||
{ | ||
// Allowed patterns: x^w, y. w is a wildcard. | ||
RCP<const Basic> expr; | ||
RCP<const Basic> x = symbol("x"); | ||
expr = pow(x, integer(3)); | ||
auto result = match_root(expr); | ||
RCP<const Basic> y = symbol("y"); | ||
std::cout << "Result for " << expr->__str__() << " is " << get<0>(result) | ||
<< std::endl; | ||
result = match_root(y); | ||
std::cout << "Result for " << y->__str__() << " is " << get<0>(result) | ||
<< std::endl; | ||
result = match_root(x); | ||
std::cout << "Result for " << x->__str__() << " is " << get<0>(result) | ||
<< std::endl; | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it's ok to use
std::variant
here, if that's what is intended.For older compilers, we can provide a compatible implementation, say using Boost variant, i.e., by having
SymEngine::variant
, that will be equal tostd::variant
for compilers that support it, andboost::variant
for older compilers. For now just usestd::variant
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Well,
std::variant
was meant to be used in the other PR.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The return type is a tuple, the integer stands for the rule number (e.g. RUBI rules are numbered), while
Substitution2
is a map of wildcards to expressions to be substituted.Substitution2
is then used in a replacement pattern to return the result of the integration.