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
Changes from 14 commits
cefd428
5389065
7000d9e
3c5ba38
07168de
25a9686
d5abbd7
366f864
00604b1
d374ed5
d277512
dc6cf28
ee126ac
b49c02c
2f7d1de
e1c8ea4
7adde66
cc74cd8
80fd37a
912c0fd
9549f0b
37ba49b
4fbde7b
9771a86
5d3950d
189c659
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2326,7 +2326,23 @@ def count_ops(expr, visual=False): | |
else: # it's Basic not isinstance(expr, Expr): | ||
if not isinstance(expr, Basic): | ||
raise TypeError("Invalid type of expr") | ||
ops = [count_ops(a, visual=visual) for a in expr.args] | ||
else: | ||
true = sympify(True) | ||
false = sympify(False) | ||
ops = [] | ||
args = [expr] | ||
while args: | ||
a = args.pop() | ||
o = C.Symbol(a.func.__name__.upper()) | ||
if (not expr is true and not expr is false): | ||
ops.append(o*(len(a.args)-1)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You don't even need the if blocks. This will be a noop if the args are empty. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think i do, as the args is just a list of expressions for which we are checking ops and the if condition is checking if the expressions which is poped from args have any arguments or not without it all the expressions whether they have any args or not will be appended to args. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh, I am off by one. You are right. But why do you need to check against Basic and Tuple. I don't like that. This if should just be There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sadly i think i have to include it...other wise There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
But I think this is right. In this case, There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The lines
Look really hackish to me. We should not use the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I am going to remove There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think every Basic subclass should be counted as an operation, if it has args. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. okay, i will remove ! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @asmeurer I wish to put one thing in you notice that |
||
args_len = len(a.args) | ||
aargs = list(a.args) | ||
for i in range(args_len): | ||
if (not aargs[i].is_Symbol and | ||
not aargs[i] is true and | ||
not aargs[i] is false): | ||
args.append(aargs[i]) | ||
|
||
if not ops: | ||
if visual: | ||
|
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.
Why these?
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.
Because without these,
True
andFalse
were being treated as logic functions. so i had to add their cases and also i was not able to importtrue
andfalse
fromlogic.boolalg
thus i usedsympify
instead//without
In [1]: count_ops(True)
Out [1]: 1
//with
In [2]: count_ops(True)
Out [2]: 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.
count_ops should just not consider any atomic object (no
.args
) as having 0 operations.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.
Thanks @asmeurer, i have corrected it. I know it was a silly mistake but I am still learning.. please don't mind.