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
Make count_ops() work with logic operations by creating a binary tree. #7303
Conversation
…. Bug was that Logic functions are not the instance of sympy.core.expr.Expr due to which logic operation were actually skipping to "else" case. Fixes #7010
args = [expr] | ||
while args: | ||
a = args.pop() | ||
o = C.Symbol(a.func.__name__.upper()) |
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 may not wonder if args.pop()
will return whole the expr
you import regardless how larger that is. Something like a = expr.args()
would do the trick.
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.
Its for the condition when two or more args of the expr
are also logic functions( something like binary tree), in that case they will be appended to args
and args.pop()
will run the loop for each of them.
yes! you are right, initially it will return the whole expression regardless of how large it is.
Please add tests. |
I have done all the test locally on my system. but i don't know how to add tests here (Travis one's) |
@sahilshekhawat There's a file called .travis.yml in the repository. It contains information related to the test files. You just add tests for the particular function you added in the corresponding test file and the github will take care of the rest. |
But i have not added any functions i have just added some lines to an existing function. |
At least, you should add tests from the referenced issue. |
I got it! Thanks |
assert count(Implies(x,y)) == IMPLIES | ||
assert count(Equivalent(x,y)) == EQUIVALENT | ||
assert count(ITE(x,y,z)) == 2*AND + OR | ||
|
||
# XXX: These are a bit surprising, only Expr-compatible ops are counted. | ||
assert count(And(x, y, z)) == 0 |
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.
This test is causing an error should i remove it. because the bug is now fixed
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.
Yes.
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.
okay, Thanks 👍
…h every other function's ops which is Basic but not isinstance(expr,Expr)
I mean, your algorithm is general. It can work on anything, not just Boolean. So you should replace |
My algorithm is same as that used above in the case of |
But your algorithm actually counts all operations. I'd rather have that, even for Basic ops. |
so to separate the symbols from the Basic ops should i use |
|
@skirpichev can you please help me? |
Sorry for the extra commit for trailing white spaces..i forgot to run |
I'm slightly off this discussion. What help do you need? |
Sorry for that mention, Aaron just cleared my doubt on gitter. I think it will work for any equation now, even those who are the instance of |
@@ -69,7 +79,6 @@ def count(val): | |||
|
|||
assert count(Derivative(x, x)) == D | |||
assert count(Integral(x, x) + 2*x/(1 + x)) == G + DIV + MUL + 2*ADD | |||
assert count(Basic()) is S.Zero |
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.
Don't remove the test. Replace it with the new value. And add some more tests for more complicated expressions like Basic(Basic(), Basic())
or Basic(x + y)
and so on.
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.
Okay! I am adding more complex tests for other logic functions and Basic()
too
…ed a better algo for parsing args.
@asmeurer I have done just as you suggested. Please review it! |
…as suggested by aaron.
@asmeurer Thanks for the help..i have corrected it..i know it was a silly mistake but i am still learning ...please don't mind.. |
@asmeurer on my system the same code |
Thank you @asmeurer for pointing that mistake, now as it has passed all the tests, can we merge it now? |
assert count(Implies(x,y)) == IMPLIES | ||
assert count(Equivalent(x,y)) == EQUIVALENT | ||
assert count(ITE(x,y,z)) == 2*AND + OR | ||
assert count(And((((x,y+z))))) == ADD |
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.
This doesn't even use And
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.
This is because the simpify(And((((x,y+z))))) == (x, y+z)
and thats why it is returning ADD
.
This is due to the fact that Add(x) == x
when there is only one argument instead of two in the Add()
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.
This is actually a special case thats why i have included it.
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.
You can at least remove the redundant parentheses.
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.
You should construct expressions that don't make sense, like And of a tuple.
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.
ya, it doesn't make sense to include so many parenthesis. :)
Okay, i am writing them..Thanks
Is everything okay now? |
@asmeurer please have a look, i have added the tests. |
assert count(Basic()) == 0 | ||
assert count(Basic(Basic(),Basic(x,x+y))) == ADD + 2*BASIC | ||
assert count(Basic(x, x + y)) == ADD + BASIC | ||
assert count(Basic(Basic(), Basic(x,x+y))) == ADD + 2*BASIC |
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.
You have this test twice.
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.
okay, i am removing this.
|
||
# The test given below checks the results which comes when logical | ||
# functions are given less number of arguments than required and | ||
# don't raises an exception. They don't make much sense. |
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 would just remove these tests. They are all either things that don't make sense or operations that return a different object than what is started with. I think these tests are just confusing, especially if any of these things are changed.
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.
okay! i am commiting after removing these.
I have just that one comment. The rest looks good. |
I have removed those tests... |
Yes. |
Thank you Aaron, for your help. |
Make count_ops() work with logic operations by creating a binary tree. Fixes #7010
Bug was that Logic functions are not the instance of sympy.core.expr.Expr due to which logic operation were actually skipping to "else" case.
Fixes #7010