9
9
import os
10
10
import abc
11
11
import numbers
12
+ from typing import Callable
12
13
13
14
import numpy as np
14
15
from . import polyutils as pu
@@ -367,6 +368,14 @@ def _generate_string(self, term_method):
367
368
if linewidth < 1 :
368
369
linewidth = 1
369
370
out = pu .format_float (self .coef [0 ])
371
+
372
+ off , scale = self .mapparms ()
373
+
374
+ scaled_symbol , needs_parens = self ._format_term (pu .format_float ,
375
+ off , scale )
376
+ if needs_parens :
377
+ scaled_symbol = '(' + scaled_symbol + ')'
378
+
370
379
for i , coef in enumerate (self .coef [1 :]):
371
380
out += " "
372
381
power = str (i + 1 )
@@ -376,13 +385,13 @@ def _generate_string(self, term_method):
376
385
# complex). In this case, represent the coefficient as-is.
377
386
try :
378
387
if coef >= 0 :
379
- next_term = f "+ " + pu .format_float (coef , parens = True )
388
+ next_term = "+ " + pu .format_float (coef , parens = True )
380
389
else :
381
- next_term = f "- " + pu .format_float (- coef , parens = True )
390
+ next_term = "- " + pu .format_float (- coef , parens = True )
382
391
except TypeError :
383
392
next_term = f"+ { coef } "
384
393
# Polynomial term
385
- next_term += term_method (power , self . symbol )
394
+ next_term += term_method (power , scaled_symbol )
386
395
# Length of the current line with next term added
387
396
line_len = len (out .split ('\n ' )[- 1 ]) + len (next_term )
388
397
# If not the last term in the polynomial, it will be two
@@ -437,24 +446,30 @@ def _repr_latex_scalar(x, parens=False):
437
446
# exponents in this function
438
447
return r'\text{{{}}}' .format (pu .format_float (x , parens = parens ))
439
448
440
- def _repr_latex_ (self ):
441
- # get the scaled argument string to the basis functions
442
- off , scale = self .mapparms ()
449
+ def _format_term (self , scalar_format : Callable , off : float , scale : float ):
450
+ """ Format a single term in the expansion """
443
451
if off == 0 and scale == 1 :
444
452
term = self .symbol
445
453
needs_parens = False
446
454
elif scale == 1 :
447
- term = f"{ self . _repr_latex_scalar (off )} + { self .symbol } "
455
+ term = f"{ scalar_format (off )} + { self .symbol } "
448
456
needs_parens = True
449
457
elif off == 0 :
450
- term = f"{ self . _repr_latex_scalar (scale )} { self .symbol } "
458
+ term = f"{ scalar_format (scale )} { self .symbol } "
451
459
needs_parens = True
452
460
else :
453
461
term = (
454
- f"{ self . _repr_latex_scalar (off )} + "
455
- f"{ self . _repr_latex_scalar (scale )} { self .symbol } "
462
+ f"{ scalar_format (off )} + "
463
+ f"{ scalar_format (scale )} { self .symbol } "
456
464
)
457
465
needs_parens = True
466
+ return term , needs_parens
467
+
468
+ def _repr_latex_ (self ):
469
+ # get the scaled argument string to the basis functions
470
+ off , scale = self .mapparms ()
471
+ term , needs_parens = self ._format_term (self ._repr_latex_scalar ,
472
+ off , scale )
458
473
459
474
mute = r"\color{{LightGray}}{{{}}}" .format
460
475
0 commit comments