Spaces:
Sleeping
Sleeping
Commit
·
d1f1f2c
1
Parent(s):
118c5f6
Fix issue with precision of float printing
Browse files- pysr/export_latex.py +7 -4
pysr/export_latex.py
CHANGED
|
@@ -6,21 +6,24 @@ from sympy.printing.latex import LatexPrinter
|
|
| 6 |
class PreciseLatexPrinter(LatexPrinter):
|
| 7 |
"""Modified SymPy printer with custom float precision."""
|
| 8 |
|
| 9 |
-
def __init__(self, settings=None, prec=3):
|
| 10 |
super().__init__(settings)
|
| 11 |
self.prec = prec
|
|
|
|
| 12 |
|
| 13 |
def _print_Float(self, expr):
|
| 14 |
# Reduce precision of float:
|
| 15 |
-
reduced_float = sympy.Float(expr, self.prec)
|
| 16 |
return super()._print_Float(reduced_float)
|
| 17 |
|
| 18 |
|
| 19 |
-
def to_latex(expr, prec=3, **settings):
|
| 20 |
"""Convert sympy expression to LaTeX with custom precision."""
|
| 21 |
if len(settings) == 0:
|
| 22 |
settings = None
|
| 23 |
-
printer = PreciseLatexPrinter(
|
|
|
|
|
|
|
| 24 |
return printer.doprint(expr)
|
| 25 |
|
| 26 |
|
|
|
|
| 6 |
class PreciseLatexPrinter(LatexPrinter):
|
| 7 |
"""Modified SymPy printer with custom float precision."""
|
| 8 |
|
| 9 |
+
def __init__(self, settings=None, prec=3, full_prec=True):
|
| 10 |
super().__init__(settings)
|
| 11 |
self.prec = prec
|
| 12 |
+
self.full_prec = full_prec
|
| 13 |
|
| 14 |
def _print_Float(self, expr):
|
| 15 |
# Reduce precision of float:
|
| 16 |
+
reduced_float = sympy.Float(expr, self.prec, full_prec=self.full_prec)
|
| 17 |
return super()._print_Float(reduced_float)
|
| 18 |
|
| 19 |
|
| 20 |
+
def to_latex(expr, prec=3, full_prec=True, **settings):
|
| 21 |
"""Convert sympy expression to LaTeX with custom precision."""
|
| 22 |
if len(settings) == 0:
|
| 23 |
settings = None
|
| 24 |
+
printer = PreciseLatexPrinter(
|
| 25 |
+
settings=settings, prec=prec, full_prec=full_prec
|
| 26 |
+
)
|
| 27 |
return printer.doprint(expr)
|
| 28 |
|
| 29 |
|