8000 Add sympy names for Eq, Lt, etc and check before casting by isuruf · Pull Request #1930 · symengine/symengine · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Add sympy names for Eq, Lt, etc and check before casting #1930

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

Merged
merged 7 commits into from
Sep 18, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions symengine/parser/parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ RCP<const Basic> Parser::functionify(const std::string &name, vec_basic &params)
const std::function<RCP<const Boolean>(const RCP<const Basic> &)>>
single_arg_boolean_functions = {
{"Eq", (single_arg_boolean_func)Eq},
{"Equality", (single_arg_boolean_func)Eq},
};
const static std::map<
const std::string,
Expand All @@ -154,11 +155,17 @@ RCP<const Basic> Parser::functionify(const std::string &name, vec_basic &params)
const RCP<const Basic> &)>>
double_arg_boolean_functions = {
{"Eq", (double_arg_boolean_func)Eq},
{"Equality", (double_arg_boolean_func)Eq},
{"Ne", Ne},
{"Unequality", Ne},
{"Ge", Ge},
{"GreaterThan", Ge},
{"Gt", Gt},
{"StrictGreaterThan", Gt},
{"Le", Le},
{"LessThan", Le},
{"Lt", Lt},
{"StrictLessThan", Lt},
};

const static std::map<
Expand Down Expand Up @@ -191,6 +198,10 @@ RCP<const Basic> Parser::functionify(const std::string &name, vec_basic &params)
}
auto it3 = single_arg_boolean_boolean_functions.find(name);
if (it3 != single_arg_boolean_boolean_functions.end()) {
if (!is_a_Boolean(*params[0])) {
throw ParseError(
"Boolean function received non-boolean arguments");
}
return it3->second(rcp_static_cast<const Boolean>(params[0]));
}
}
Expand All @@ -215,6 +226,10 @@ RCP<const Basic> Parser::functionify(const std::string &name, vec_basic &params)
if (it2 != multi_arg_vec_boolean_functions.end()) {
vec_boolean p;
for (auto &v : params) {
if (!is_a_Boolean(*v)) {
throw ParseError(
"Boolean function received non-boolean arguments");
}
p.push_back(rcp_static_cast<const Boolean>(v));
}
return it2->second(p);
Expand All @@ -224,6 +239,10 @@ RCP<const Basic> Parser::functionify(const std::string &name, vec_basic &params)
if (it3 != multi_arg_set_boolean_functions.end()) {
set_boolean s;
for (auto &v : params) {
if (!is_a_Boolean(*v)) {
throw ParseError(
"Boolean function received non-boolean arguments");
}
s.insert(rcp_static_cast<const Boolean>(v));
}
return it3->second(s);
Expand Down
8 changes: 8 additions & 0 deletions symengine/tests/basic/test_parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -411,6 +411,11 @@ TEST_CASE("Parsing: functions", "[parser]")
CHECK(eq(*res, *Eq(x, integer(0))));
REQUIRE(eq(*res, *parse(res->__str__())));

s = "And(Equality(x), Unequality(y, 1))";
res = parse(s);
CHECK(eq(*res, *logical_and({Eq(x, integer(0)), Ne(y, integer(1))})));
REQUIRE(eq(*res, *parse(res->__str__())));

s = "Eq(x, y)";
res = parse(s);
CHECK(eq(*res, *Eq(x, y)));
Expand Down Expand Up @@ -932,6 +937,9 @@ TEST_CASE("Parsing: errors", "[parser]")

s = "Piecewise((x, y))";
CHECK_THROWS_AS(parse(s), ParseError);

s = "And(x, y)";
CHECK_THROWS_AS(parse(s), ParseError);
}

TEST_CASE("Parsing: bison stack reallocation", "[parser]")
Expand Down
0