-
Notifications
You must be signed in to change notification settings - Fork 393
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
Implement summation with limits using comprehension #30 #32
Conversation
This supports summation with limits (super/subscripts) written with comprehension with for loop and range function.
Support for summation is pending and will be addressed after the following change. |
…torBase. Still WIP and needs refactoring
latexify/core.py
Outdated
@@ -224,6 +244,27 @@ def visit_If(self, node, action): # pylint: disable=invalid-name | |||
latex += self.visit(node) | |||
return latex + r', & \mathrm{otherwise} \end{array} \right.' | |||
|
|||
def visit_GeneratorExp_sum(self, node): # pylint: disable=invalid-name |
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 could have been implemented with generic visit_GeneratorExp
, which might be useful for FRs in the future (ditto for visit_comprehension
). In that case, we might need some additional checks in decorated_lstr_and_arg
.
You could view the generic implementation in this commit (sorry it's a bit dirty before refactoring).
0c5d67f
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.
What are FRs?
This function can be reused with other operators (e.g., products and integrals have the same syntax). We can use some general action name rather than sum
.
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 think the bare visitor (=visitor with None
action) of every node should return only a LaTeX string. Otherwise every parent node is required to know what kind of objects are returned from children and how to process them to construct LaTeX strings.
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.
What are FRs?
Sorry for not being clear, but I meant feature requests. I thought some operations could use GeneratorExp
and there might not be a reason to limit my function only to summation.
However, your point on bare visitors makes 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.
This function can be reused with other operators (e.g., products and integrals have the same syntax). We can use some general action name rather than sum.
set_bounds
as suggested in the new commit?
latexify/core.py
Outdated
@@ -224,6 +244,27 @@ def visit_If(self, node, action): # pylint: disable=invalid-name | |||
latex += self.visit(node) | |||
return latex + r', & \mathrm{otherwise} \end{array} \right.' | |||
|
|||
def visit_GeneratorExp_sum(self, node): # pylint: disable=invalid-name |
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.
What are FRs?
This function can be reused with other operators (e.g., products and integrals have the same syntax). We can use some general action name rather than sum
.
latexify/core.py
Outdated
@@ -224,6 +244,27 @@ def visit_If(self, node, action): # pylint: disable=invalid-name | |||
latex += self.visit(node) | |||
return latex + r', & \mathrm{otherwise} \end{array} \right.' | |||
|
|||
def visit_GeneratorExp_sum(self, node): # pylint: disable=invalid-name |
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 think the bare visitor (=visitor with None
action) of every node should return only a LaTeX string. Otherwise every parent node is required to know what kind of objects are returned from children and how to process them to construct LaTeX strings.
…ts (or bounds) - Use n - 1 instead of n in test (the second arg in range is exclusive) - Use more general name to support limits operation (sum is not the only operation that could use limits but integral of products are, too)
from typing import NamedTuple | ||
|
||
|
||
class Actions(NamedTuple): |
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 decided to use NamedTuple
here because of two benefits.
- It allows dot access - e.g.
constants.actions.SET_BOUNDS
(vs.constants.actions[key]
with adict
implementation) - Tuples are immutable.
NamedTuple
can be written more simpler than a class.
I could have prepared another file actions.py
to achieve the same interface, but actions
are essentially constants. Thus I preferred to keep it in constants.py
.
If you think otherwise, please let me know.
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 think this change works, thanks!
This supports summation with limits (super/subscripts) written with comprehension with for loop and range function.
Example usage is outlined in
tests/test_io.py
Note:
y_bar[i]
).sum_with_limit
andtest_with_latex_to_str
intests/test_io.py
are impacted by issue Underscores in function names conflict with LaTeX and shown as subscripts #31 , which need to be corrected after its fix.