Skip to content

Refactor is_linear_expression function of expr.py#187

Open
alisa-kirkinskaia wants to merge 4 commits into
masterfrom
fix_check_linearity
Open

Refactor is_linear_expression function of expr.py#187
alisa-kirkinskaia wants to merge 4 commits into
masterfrom
fix_check_linearity

Conversation

@alisa-kirkinskaia

@alisa-kirkinskaia alisa-kirkinskaia commented May 3, 2026

Copy link
Copy Markdown

1. Fix #121:

Fixed a bug in is_linear_expression related to expand() being very slow for some expressions. The bug is caused by the following line:

if not( (a-b).expand() == 0 or a.expand() == b.expand()):

In the fix we avoid comparing the values of a.expand() and b.expand() if (a-b).expand() is 0, as it already means the expression is linear.

2. Refactor is_linear_expression for better readability:

  • Remove unused argument integral
  • Rename variables
  • Add docstring
  1. Update library patch version to 0.19.3

@alisa-kirkinskaia alisa-kirkinskaia requested a review from a team May 22, 2026 07:15

@yguclu yguclu left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you please update the library version to 0.19.3 in the file sympde/pyproject.toml?

@yguclu yguclu left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good job @alisa-kirkinskaia! I have a couple of minor comments, mostly about formatting.

Comment thread sympde/expr/expr.py
Comment on lines +751 to +752
expr
Symbolic expression to test.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread sympde/expr/expr.py
Comment on lines +760 to +761
bool
True if the expression is linear with respect to all given arguments, False otherwise.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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?

Comment thread sympde/expr/expr.py
Comment on lines +789 to +790
expr_at_sum = expr.subs(zip(args, summed_args)) # f(x + y)
expected_sum = expr_at_x + expr_at_y # = f(x) + f (y)

@yguclu yguclu Jul 14, 2026

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor formatting improvements:

Suggested change
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)

Comment thread sympde/expr/expr.py
if expr1 != expr2:
if debug:
print('Failed to assert addition property')
print('{} != {}'.format(expr1, expr2))

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For long expressions, this error message will be quite difficult to read. Hence, I suggest expanding it across multiple lines:

Suggested change
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

Comment thread sympde/expr/expr.py
if expr1 != expr2:
if debug:
print('Failed to assert multiplication property')
print('{} != {}'.format(expr1, expr2))

@yguclu yguclu Jul 14, 2026

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here I also suggest splitting the error message across multiple lines, as in my previous commentLOL.

Comment thread sympde/expr/expr.py
Comment on lines +753 to +754
args : iterable
Each argument must be ScalarFunction or VectorFunction.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

@yguclu

yguclu commented Jul 14, 2026

Copy link
Copy Markdown
Member

Can you please update the library version to 0.19.3 in the file sympde/pyproject.toml?

Thanks for commit 85fba96!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Stuck or very Slow check is_linear_expression in LinearForm and BilinearForm

2 participants