Addition / Subtraction Simplification for the stop parameter in sum / prod
Overview
This PR addresses issue #74.
Details
Implementation
First check the stop parameter of range_info is ast.BinOp and represents an addition or subtraction. If so, the minus one is applied to BinOp.right and a new ast.BinOp node is created. Then the existing visit function is leveraged to create the needed Latex.
Note the special cases + 1 - 1 and - (-1) - 1 for which only BinOp.left is kept.
Added test cases to regression_test.py. Please let me know if these are more appropriate elsewhere (e.g. function_codegen_test.py)
Caveats
-
n+1-1can be reduced tonbut1+n-1cannot. This is becauserange(1+n)is unlikely and unusual. Support for this can be easily added if desired - chained additions and subtractions cannot be fully reduced.
n + 3+4-1will becomen+3+3notn+6. This is in line with how other chained BinOps are currently handled and may be addressed in a future issue
Questions
The implementation is slightly verbose and perhaps should be written as a separate function. Might other future PRs need this similar functionality?
References
Issue #74
Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).
View this failed invocation of the CLA check for more information.
For the most up to date status, view the checks section at the bottom of the pull request.
@Casper-Guo Could you resolve the conflicts introduced by recent changes on main?
@Casper-Guo It is not easy to select only your stuff. Try git merge main to remove duplicated changes.
@Casper-Guo It is not easy to select only your stuff. Try
git merge mainto remove duplicated changes.
I rebased on main before pushing the new changes which should conceal all the new commits... Not sure why it didn't turn out that way. I will try to figure out a way to tidy it up.
Edit: Should be all set for your review now. Sorry for the confusion
Looks great, thanks!