Refactor is_linear_expression function of expr.py#187
Refactor is_linear_expression function of expr.py#187alisa-kirkinskaia wants to merge 4 commits into
Conversation
yguclu
left a comment
There was a problem hiding this comment.
Can you please update the library version to 0.19.3 in the file sympde/pyproject.toml?
yguclu
left a comment
There was a problem hiding this comment.
Good job @alisa-kirkinskaia! I have a couple of minor comments, mostly about formatting.
| expr | ||
| Symbolic expression to test. |
There was a problem hiding this comment.
I think expr should be of some basic SymPy type. Could you please try and find out which one? It would be nice to add this information to the docstring, and possibly do a type check, too.
| bool | ||
| True if the expression is linear with respect to all given arguments, False otherwise. |
There was a problem hiding this comment.
We should check that is_linear_expression truly works for expressions with an arbitrary number of arguments. I am not sure if we do that already in any unit tests in folder sympde/expr/, could you please have a look?
| expr_at_sum = expr.subs(zip(args, summed_args)) # f(x + y) | ||
| expected_sum = expr_at_x + expr_at_y # = f(x) + f (y) |
There was a problem hiding this comment.
Minor formatting improvements:
| expr_at_sum = expr.subs(zip(args, summed_args)) # f(x + y) | |
| expected_sum = expr_at_x + expr_at_y # = f(x) + f (y) | |
| expr_at_sum = expr.subs(zip(args, summed_args)) # f(x + y) | |
| expected_sum = expr_at_x + expr_at_y # f(x) + f(y) |
| if expr1 != expr2: | ||
| if debug: | ||
| print('Failed to assert addition property') | ||
| print('{} != {}'.format(expr1, expr2)) |
There was a problem hiding this comment.
For long expressions, this error message will be quite difficult to read. Hence, I suggest expanding it across multiple lines:
| print('{} != {}'.format(expr1, expr2)) | |
| print(r"Failed to assert addition property `f(x + y) = f(x) + f(y)`, where:") | |
| print() | |
| print('f(x + y) =') | |
| print(expr1) | |
| print() | |
| print('f(x) + f(y) =') | |
| print(expr2) |
Please note that I have used a Python raw string in the first line of the suggested code, in order to avoid any issue with the markdown inline quote characters I wanted to print
| if expr1 != expr2: | ||
| if debug: | ||
| print('Failed to assert multiplication property') | ||
| print('{} != {}'.format(expr1, expr2)) |
There was a problem hiding this comment.
Here I also suggest splitting the error message across multiple lines, as in my previous commentLOL.
| args : iterable | ||
| Each argument must be ScalarFunction or VectorFunction. |
There was a problem hiding this comment.
It is probably worth explaining in some detail that x and y at the top of this docstring correspond to different values of args, which is the list of arguments expr depends on
Thanks for commit 85fba96! |
1. Fix #121:
Fixed a bug in
is_linear_expressionrelated toexpand()being very slow for some expressions. The bug is caused by the following line:In the fix we avoid comparing the values of
a.expand()andb.expand()if(a-b).expand()is 0, as it already means the expression is linear.2. Refactor
is_linear_expressionfor better readability:integral