1 | initial version |
With all the answers I received before, I was able to build this code, with a small improvement.
def signe(valeur):
if valeur < 0:
return(fr"-{latex(-valeur)}")
else:
return(fr"+{latex(valeur)}")
def cform(a, alpha, beta): # cform means canonical form a (x - alpha)^2 + beta
if a == 1:
fc_str = fr"\displaystyle \left(~{latex(x-alpha)}~\right)^2 {signe(beta)}"
elif a == -1:
fc_str = fr"\displaystyle -\left(~{latex(x-alpha)}~\right)^2 {signe(beta)}"
else:
fc_str = fr"\displaystyle {latex(a)}\left(~{latex(x-alpha)}~\right)^2 {signe(beta)}"
return fc_str
def fform(a, x1, x2): # fform means factored form a (x - x1) (x - x2)
if a==1:
ff_str = fr"\displaystyle \left(~{latex(x-x1)}~\right)\left(~{latex(x-x2)}~\right)"
elif a==-1:
ff_str = fr"\displaystyle -\left(~{latex(x-x1)}~\right)\left(~{latex(x-x2)}~\right)"
else:
ff_str = fr"\displaystyle {latex(a)}\left(~{latex(x-x1)}~\right)\left(~{latex(x-x2)}~\right)"
return ff_str
The improvment is to write \left(~{latex(x-alpha)}~\right)^2
instead of \left(~{latex((x-alpha)^2)}~\right)
which involves a calculation. Placing x-alpha
in latex{ }
prevents this calculation and we remove the sign problem for alpha (I noticed this when I built the fform function and I applied it for function cform).
For the sign of beta, I imagined the function signe
that it is possible to use in other places.
I'm sure this can be improved.
Thanks for all the answers that were very helpfull, I've learnd a lot.