Skip to content

More and more refactoring. #72

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

Merged
merged 15 commits into from
Nov 1, 2022
Merged

More and more refactoring. #72

merged 15 commits into from
Nov 1, 2022

Conversation

odashi
Copy link
Collaborator

@odashi odashi commented Oct 31, 2022

Overview

  • Change the substitution sign: use $=$ rather than $\triangleq$.
  • Separate reduce_assignments behavior to transformers/AssignmentReducer.
  • Refactoring the behavior around "sum(... for x in range(...))".
  • Remove NodeVisitorBase implementation.
  • Move LatexifyVisitor to codegen/FunctionCodegen
  • Some trivial refactoring.

Details

Substitution sign

$\triangleq$ is available only when the amssymb package is loaded, which is not compatible with the bare LaTeX. This behavior often confuses users who try to compile the obtained LaTeX source by themselves.

The sign is replaced to $=$ at this point, because I thought that this sign is the most neutral choice to every user.
We may further need to provide some flags to replace this sign by users.

Other refactoring

This change improves the modulability of the whole library.

The visit_* functions in the new FunctionCodegen visitor returns always str, while ad-hoc processes are moved away to the new AST transformer AssignmentReducer or some private functions.

AssignmentReducer directly modifies the AST by replacing subtrees with substitution. This could reduce some unnecessary parenthesis pairs \left( and \right) caused by ad-hoc processing in the old Visitor.

This change also removes all "actions" from the original Visitor so that we can use the standard ast.NodeVisitor as the base class of the codegen.

References

Blocked by

NA

@odashi odashi added this to the v0.2 milestone Oct 31, 2022
@odashi odashi requested a review from ShigekiKarita October 31, 2022 11:27
@odashi odashi changed the title Assignment reducer More and more refactoring. Oct 31, 2022
if lstr is None:
lstr = r"\mathrm{" + callee_str + r"}\left("
rstr = r"\right)"
if func_str in ("sum", "prod") and isinstance(node.args[0], ast.GeneratorExp):
Copy link
Contributor

Choose a reason for hiding this comment

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

sum / prod handler here LGTM.

Copy link
Collaborator

@ShigekiKarita ShigekiKarita left a comment

Choose a reason for hiding this comment

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

LGTM

r"\mathrm{f}(x) \triangleq "
r"{3}\left( \left( x^{{2}} \right) + \left( x^{{2}} \right) \right)"

def test_reduce_assignments_with_if() -> None:
Copy link
Collaborator

Choose a reason for hiding this comment

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

so nice

@odashi odashi merged commit 5320117 into main Nov 1, 2022
@odashi odashi deleted the assignment-reducer branch November 1, 2022 10:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Lack of equals sign
3 participants