Skip to content
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

Addition / Subtraction Simplification for the stop parameter in sum / prod #115

Merged
merged 7 commits into from
Nov 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions src/integration_tests/regression_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,28 @@ def sum_with_limit(a, n):
_check_function(sum_with_limit, latex)


def test_sum_with_reducible_limit() -> None:
def sum_with_limit(n):
return sum(i for i in range(n + 1))

latex = (
r"\mathrm{sum_with_limit}(n) = \sum_{i = {0}}^{{n}} "
r"\mathopen{}\left({i}\mathclose{}\right)"
)
_check_function(sum_with_limit, latex)


def test_sum_with_irreducible_limit() -> None:
def sum_with_limit(n):
return sum(i for i in range(n * 3))

latex = (
r"\mathrm{sum_with_limit}(n) = \sum_{i = {0}}^{{n {3} - 1}} "
r"\mathopen{}\left({i}\mathclose{}\right)"
)
_check_function(sum_with_limit, latex)


def test_prod_with_limit_1arg() -> None:
def prod_with_limit(n):
return math.prod(i**2 for i in range(n))
Expand All @@ -128,6 +150,28 @@ def prod_with_limit(a, n):
_check_function(prod_with_limit, latex)


def test_prod_with_reducible_limits() -> None:
def prod_with_limit(n):
return math.prod(i for i in range(n - 1))

latex = (
r"\mathrm{prod_with_limit}(n) = "
r"\prod_{i = {0}}^{{n - {2}}} \mathopen{}\left({i}\mathclose{}\right)"
)
_check_function(prod_with_limit, latex)


def test_prod_with_irreducible_limit() -> None:
def prod_with_limit(n):
return math.prod(i for i in range(n * 3))

latex = (
r"\mathrm{prod_with_limit}(n) = "
r"\prod_{i = {0}}^{{n {3} - 1}} \mathopen{}\left({i}\mathclose{}\right)"
)
_check_function(prod_with_limit, latex)


def test_nested_function() -> None:
def nested(x):
return 3 * x
Expand Down
51 changes: 50 additions & 1 deletion src/latexify/codegen/function_codegen.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from __future__ import annotations

import ast
import sys
import dataclasses
from typing import Any

Expand Down Expand Up @@ -536,6 +537,48 @@ def visit_IfExp(self, node: ast.IfExp) -> str:
latex += self.visit(node)
return latex + r", & \mathrm{otherwise} \end{array} \right."

def _reduce_stop_parameter(self, node: ast.BinOp) -> str:
# ast.Constant class is added in Python 3.8
# ast.Num is the relevant node type in previous versions
if sys.version_info.minor < 8:
if isinstance(node.right, ast.Num):
if isinstance(node.op, ast.Add):
if node.right.n == 1:
upper = "{" + self.visit(node.left) + "}"
else:
reduced_constant = ast.Num(node.right.n - 1)
new_node = ast.BinOp(node.left, node.op, reduced_constant)
upper = "{" + self.visit(new_node) + "}"
else:
if node.right.n == -1:
upper = "{" + self.visit(node.left) + "}"
else:
reduced_constant = ast.Num(node.right.n + 1)
new_node = ast.BinOp(node.left, node.op, reduced_constant)
upper = "{" + self.visit(new_node) + "}"
else:
upper = "{" + self.visit(node) + "}"
else:
if isinstance(node.right, ast.Constant):
if isinstance(node.op, ast.Add):
if node.right.value == 1:
upper = "{" + self.visit(node.left) + "}"
else:
reduced_constant = ast.Constant(node.right.value - 1)
new_node = ast.BinOp(node.left, node.op, reduced_constant)
upper = "{" + self.visit(new_node) + "}"
else:
if node.right.value == -1:
upper = "{" + self.visit(node.left) + "}"
else:
reduced_constant = ast.Constant(node.right.value + 1)
new_node = ast.BinOp(node.left, node.op, reduced_constant)
upper = "{" + self.visit(new_node) + "}"
else:
upper = "{" + self.visit(node) + "}"

return upper

def _get_sum_prod_range(self, node: ast.comprehension) -> tuple[str, str] | None:
"""Helper to process range(...) for sum and prod functions.

Expand Down Expand Up @@ -577,7 +620,13 @@ def _get_sum_prod_range(self, node: ast.comprehension) -> tuple[str, str] | None
lower_rhs = f"{{{range_info.start_int}}}"

if range_info.stop_int is None:
upper = "{" + self.visit(range_info.stop) + " - 1}"
# use special processing if range_info.stop involves addition or subtraction
if isinstance(range_info.stop, ast.BinOp) and isinstance(
range_info.stop.op, (ast.Add, ast.Sub)
):
upper = self._reduce_stop_parameter(range_info.stop)
else:
upper = "{" + self.visit(range_info.stop) + " - 1}"
else:
upper = f"{{{range_info.stop_int - 1}}}"

Expand Down
18 changes: 18 additions & 0 deletions src/latexify/codegen/function_codegen_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,24 @@ def test_visit_call_sum_prod(src_suffix: str, dest_suffix: str) -> None:
r"\prod_{y \in x}^{} \prod_{z \in y}^{} \prod_{i \in z}^{} "
r"\mathopen{}\left({i}\mathclose{}\right)",
),
# reduce stop parameter
(
"sum(i for i in range(n+1))",
r"\sum_{i = {0}}^{{n}} \mathopen{}\left({i}\mathclose{}\right)",
),
(
"math.prod(i for i in range(n-1))",
r"\prod_{i = {0}}^{{n - {2}}} \mathopen{}\left({i}\mathclose{}\right)",
),
# reduce stop parameter
(
"sum(i for i in range(n+1))",
r"\sum_{i = {0}}^{{n}} \mathopen{}\left({i}\mathclose{}\right)",
),
(
"math.prod(i for i in range(n-1))",
r"\prod_{i = {0}}^{{n - {2}}} \mathopen{}\left({i}\mathclose{}\right)",
),
],
)
def test_visit_call_sum_prod_multiple_comprehension(code: str, latex: str) -> None:
Expand Down