Skip to content

Commit 63b8716

Browse files
Remove extraneous brackets from single unfactored polynomials
Only wrap z-polynomial factors in parentheses when needed for grouping: other factors present, negative sign, or exponent. A lone polynomial like z² + 2(λ-1)z - (λ-1) no longer gets unnecessary outer parens. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent 6b936f3 commit 63b8716

3 files changed

Lines changed: 31 additions & 34 deletions

File tree

webapp/data/algorithms.json

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
"lft": []
2323
},
2424
"catalogOnly": false,
25-
"tf_latex": "\\frac{-\\alpha}{\\left(z -1\\right)}"
25+
"tf_latex": "\\frac{-\\alpha}{z -1}"
2626
},
2727
{
2828
"id": "heavy_ball",
@@ -251,7 +251,7 @@
251251
"lft": []
252252
},
253253
"catalogOnly": false,
254-
"tf_latex": "\\frac{\\alpha}{\\left(z + \\alpha - 1\\right)}"
254+
"tf_latex": "\\frac{\\alpha}{z + \\alpha - 1}"
255255
},
256256
{
257257
"id": "douglas_rachford",
@@ -284,7 +284,7 @@
284284
"lft": []
285285
},
286286
"catalogOnly": false,
287-
"tf_latex": "\\begin{bmatrix} \\frac{-\\alpha}{\\left(z -1\\right)} & \\frac{\\alpha}{\\left(z -1\\right)} \\\\ \\frac{\\left(2 z + \\alpha - 2\\right)}{\\left(z -1\\right)} & \\frac{-\\alpha}{\\left(z -1\\right)} \\end{bmatrix}"
287+
"tf_latex": "\\begin{bmatrix} \\frac{-\\alpha}{z -1} & \\frac{\\alpha}{z -1} \\\\ \\frac{2 z + \\alpha - 2}{z -1} & \\frac{-\\alpha}{z -1} \\end{bmatrix}"
288288
},
289289
{
290290
"id": "admm",
@@ -319,7 +319,7 @@
319319
"lft": []
320320
},
321321
"catalogOnly": false,
322-
"tf_latex": "\\begin{bmatrix} \\frac{-\\rho}{\\left(z -1\\right)} & \\frac{\\left(\\left(\\rho + 1\\right) z -1\\right)}{z \\left(z -1\\right)} \\\\ \\frac{\\left(z + \\rho - 1\\right)}{\\left(z -1\\right)} & \\frac{-\\rho}{\\left(z -1\\right)} \\end{bmatrix}"
322+
"tf_latex": "\\begin{bmatrix} \\frac{-\\rho}{z -1} & \\frac{\\left(\\rho + 1\\right) z -1}{z \\left(z -1\\right)} \\\\ \\frac{z + \\rho - 1}{z -1} & \\frac{-\\rho}{z -1} \\end{bmatrix}"
323323
},
324324
{
325325
"id": "peaceman_rachford",
@@ -347,7 +347,7 @@
347347
"lft": []
348348
},
349349
"catalogOnly": false,
350-
"tf_latex": "\\begin{bmatrix} 0 & \\frac{2}{\\left(z + 1\\right)} \\\\ 2 & \\frac{-2}{\\left(z + 1\\right)} \\end{bmatrix}"
350+
"tf_latex": "\\begin{bmatrix} 0 & \\frac{2}{z + 1} \\\\ 2 & \\frac{-2}{z + 1} \\end{bmatrix}"
351351
},
352352
{
353353
"id": "chambolle_pock",
@@ -440,7 +440,7 @@
440440
]
441441
},
442442
"catalogOnly": false,
443-
"tf_latex": "\\begin{bmatrix} \\frac{-t}{\\left(z -1\\right)} & \\frac{-t}{\\left(z -1\\right)} \\\\ \\frac{-z}{\\left(z -1\\right)} & \\frac{-1}{\\left(z -1\\right)} \\end{bmatrix}"
443+
"tf_latex": "\\begin{bmatrix} \\frac{-t}{z -1} & \\frac{-t}{z -1} \\\\ \\frac{-z}{z -1} & \\frac{-1}{z -1} \\end{bmatrix}"
444444
},
445445
{
446446
"id": "davis_yin",
@@ -476,7 +476,7 @@
476476
"lft": []
477477
},
478478
"catalogOnly": false,
479-
"tf_latex": "\\begin{bmatrix} \\frac{-t}{\\left(z -1\\right)} & 0 & \\frac{t}{\\left(z -1\\right)} \\\\ 1 & 0 & 0 \\\\ \\frac{\\left(2 z + t - 2\\right)}{\\left(z -1\\right)} & -\\gamma & \\frac{-t}{\\left(z -1\\right)} \\end{bmatrix}"
479+
"tf_latex": "\\begin{bmatrix} \\frac{-t}{z -1} & 0 & \\frac{t}{z -1} \\\\ 1 & 0 & 0 \\\\ \\frac{2 z + t - 2}{z -1} & -\\gamma & \\frac{-t}{z -1} \\end{bmatrix}"
480480
},
481481
{
482482
"id": "extragradient",
@@ -503,7 +503,7 @@
503503
"lft": []
504504
},
505505
"catalogOnly": false,
506-
"tf_latex": "\\begin{bmatrix} 0 & \\frac{-t}{\\left(z -1\\right)} \\\\ -t & \\frac{-t}{\\left(z -1\\right)} \\end{bmatrix}"
506+
"tf_latex": "\\begin{bmatrix} 0 & \\frac{-t}{z -1} \\\\ -t & \\frac{-t}{z -1} \\end{bmatrix}"
507507
},
508508
{
509509
"id": "extragradient_korpelevich",
@@ -604,7 +604,7 @@
604604
"lft": []
605605
},
606606
"catalogOnly": false,
607-
"tf_latex": "\\begin{bmatrix} 0 & \\frac{\\left(2 z -1\\right)}{z^{2}} \\\\ -t & \\frac{1}{z} \\end{bmatrix}"
607+
"tf_latex": "\\begin{bmatrix} 0 & \\frac{2 z -1}{z^{2}} \\\\ -t & \\frac{1}{z} \\end{bmatrix}"
608608
},
609609
{
610610
"id": "distributed_gradient_descent",
@@ -636,7 +636,7 @@
636636
"lft": []
637637
},
638638
"catalogOnly": false,
639-
"tf_latex": "\\frac{-\\alpha}{\\left(z + \\lambda - 1\\right)}"
639+
"tf_latex": "\\frac{-\\alpha}{z + \\lambda - 1}"
640640
},
641641
{
642642
"id": "nids",
@@ -667,7 +667,7 @@
667667
"lft": []
668668
},
669669
"catalogOnly": false,
670-
"tf_latex": "\\frac{\\alpha \\left(\\lambda - 1\\right) \\left(z -1\\right)}{\\left(z^{2} + 2 \\left(\\lambda - 1\\right) z -\\left(\\lambda - 1\\right)\\right)}"
670+
"tf_latex": "\\frac{\\alpha \\left(\\lambda - 1\\right) \\left(z -1\\right)}{z^{2} + 2 \\left(\\lambda - 1\\right) z -\\left(\\lambda - 1\\right)}"
671671
},
672672
{
673673
"id": "exact_diffusion",
@@ -700,7 +700,7 @@
700700
"lft": []
701701
},
702702
"catalogOnly": false,
703-
"tf_latex": "\\frac{\\alpha \\left(\\lambda - 1\\right) \\left(z -1\\right)}{\\left(z^{2} + 2 \\left(\\lambda - 1\\right) z -\\left(\\lambda - 1\\right)\\right)}"
703+
"tf_latex": "\\frac{\\alpha \\left(\\lambda - 1\\right) \\left(z -1\\right)}{z^{2} + 2 \\left(\\lambda - 1\\right) z -\\left(\\lambda - 1\\right)}"
704704
},
705705
{
706706
"id": "extra_distributed",
@@ -731,7 +731,7 @@
731731
"lft": []
732732
},
733733
"catalogOnly": false,
734-
"tf_latex": "\\frac{-2 \\alpha \\left(z -1\\right)}{\\left(2 z^{2} + 2 \\left(\\lambda - 2\\right) z -\\left(\\lambda - 2\\right)\\right)}"
734+
"tf_latex": "\\frac{-2 \\alpha \\left(z -1\\right)}{2 z^{2} + 2 \\left(\\lambda - 2\\right) z -\\left(\\lambda - 2\\right)}"
735735
},
736736
{
737737
"id": "diging",
@@ -830,7 +830,7 @@
830830
"lft": []
831831
},
832832
"catalogOnly": false,
833-
"tf_latex": "\\frac{\\alpha \\left(\\delta \\lambda - 1\\right) \\left(z -1\\right)}{\\left(z^{2} + \\left(\\gamma \\lambda - 2\\right) z + \\beta \\lambda - \\gamma \\lambda + 1\\right)}"
833+
"tf_latex": "\\frac{\\alpha \\left(\\delta \\lambda - 1\\right) \\left(z -1\\right)}{z^{2} + \\left(\\gamma \\lambda - 2\\right) z + \\beta \\lambda - \\gamma \\lambda + 1}"
834834
},
835835
{
836836
"id": "unified_extra",
@@ -864,7 +864,7 @@
864864
"lft": []
865865
},
866866
"catalogOnly": false,
867-
"tf_latex": "\\frac{-\\alpha \\left(z -1\\right)}{\\left(z^{2} + 2 \\left(\\lambda \\mu - 1\\right) z - \\left(\\lambda \\mu - 1\\right) \\left(\\alpha l \\lambda \\mu - \\lambda \\mu + 1\\right)\\right)}"
867+
"tf_latex": "\\frac{-\\alpha \\left(z -1\\right)}{z^{2} + 2 \\left(\\lambda \\mu - 1\\right) z - \\left(\\lambda \\mu - 1\\right) \\left(\\alpha l \\lambda \\mu - \\lambda \\mu + 1\\right)}"
868868
},
869869
{
870870
"id": "unified_diging",
@@ -898,6 +898,6 @@
898898
"lft": []
899899
},
900900
"catalogOnly": false,
901-
"tf_latex": "\\frac{-\\alpha \\left(z -1\\right)}{\\left(z^{2} + 2 \\left(\\lambda \\mu - 1\\right) z + \\alpha l \\lambda \\mu + \\lambda^{2} \\mu^{2} - 2 \\lambda \\mu + 1\\right)}"
901+
"tf_latex": "\\frac{-\\alpha \\left(z -1\\right)}{z^{2} + 2 \\left(\\lambda \\mu - 1\\right) z + \\alpha l \\lambda \\mu + \\lambda^{2} \\mu^{2} - 2 \\lambda \\mu + 1}"
902902
}
903903
]

webapp/js/app.js

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -279,17 +279,15 @@ def _factored_product_latex(expr, z):
279279
if isinstance(p, Add):
280280
p_str = '\\\\left(' + p_str + '\\\\right)'
281281
parts.append(p_str)
282+
total_factors = len(numerics) + len(params) + len(z_polys)
282283
for zp in z_polys:
283284
base = zp.base if isinstance(zp, Pow) else zp
284285
exp = zp.exp if isinstance(zp, Pow) else 1
285286
base_str = _poly_latex(base, z)
286-
try:
287-
deg = Poly(base, z).degree()
288-
except Exception:
289-
deg = 0
290-
needs_parens = deg >= 1 and isinstance(cancel(base), Add)
291-
if not needs_parens:
292-
needs_parens = ('+' in base_str or base_str.count('-') > (1 if base_str.startswith('-') else 0))
287+
has_multiple_terms = ('+' in base_str or base_str.count('-') > (1 if base_str.startswith('-') else 0))
288+
needs_parens = has_multiple_terms and (
289+
total_factors > 1 or sign == -1 or exp != 1
290+
)
293291
if needs_parens:
294292
base_str = '\\\\left(' + base_str + '\\\\right)'
295293
if exp != 1:

webapp/python/regen_latex.py

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -139,24 +139,23 @@ def _factored_product_latex(expr):
139139
p_str = r'\left(' + p_str + r'\right)'
140140
parts.append(p_str)
141141

142-
# z-polynomial factors — each wrapped in parens if degree >= 1 and has
143-
# multiple terms (Add), or if it's a Pow
142+
# z-polynomial factors — wrap in parens only when needed for grouping
143+
# (i.e., there are other factors to multiply with, or it has an exponent)
144+
total_factors = len(numerics) + len(params) + len(z_polys)
144145
for zp in z_polys:
145146
base = zp.base if isinstance(zp, Pow) else zp
146147
exp = zp.exp if isinstance(zp, Pow) else 1
147148

148149
# Format the base as polynomial in z
149150
base_str = poly_latex(base, z)
150151

151-
# Wrap in parens if it's a multi-term polynomial
152-
try:
153-
deg = Poly(base, z).degree()
154-
except Exception:
155-
deg = 0
156-
needs_parens = deg >= 1 and isinstance(cancel(base), Add)
157-
if not needs_parens:
158-
# Also check if poly_latex produced multiple terms
159-
needs_parens = ('+' in base_str or base_str.count('-') > (1 if base_str.startswith('-') else 0))
152+
# Only wrap in parens if: (a) there are other factors or sign prefix,
153+
# or (b) the factor has an exponent (e.g., (z+λ-1)²)
154+
has_multiple_terms = ('+' in base_str or
155+
base_str.count('-') > (1 if base_str.startswith('-') else 0))
156+
needs_parens = has_multiple_terms and (
157+
total_factors > 1 or sign == -1 or exp != 1
158+
)
160159

161160
if needs_parens:
162161
base_str = r'\left(' + base_str + r'\right)'

0 commit comments

Comments
 (0)