%%%
% Factorisation
%%%
\def\filedateFactorisation{2026/01/21}%
\def\fileversionFactorisation{0.1b}%
\message{-- \filedateFactorisation\space v\fileversionFactorisation}%
%
\setKVdefault[Facto]{Aide=false,AideMul=false,Couleur=Crimson,NomExpression=A,Lettre=x,Details,Formel=false,VariableSup=false,ParenthesesFin=false,Finale=false,Type=0,Litteral={},AutreVariable={},Resultat={},Case=false}%
\defKV[Facto]{%
  Litteral=\ifempty{#1}{}{\setKV[Facto]{Details=false}},%
  AutreVariable=\ifempty{#1}{}{\setKV[Facto]{VariableSup}},%
  Resultat=\ifempty{#1}{}{\setKV[Facto]{Finale}}%
}%

\NewDocumentCommand\PfCEcrireFacteur{mm}{%
  \StrCount{#1}{+}[\PfCCountArgTroisPlus]%
  \StrCount{#1}{-}[\PfCCountArgTroisMoins]%
  \xdef\PfCCountArgTrois{\fpeval{\PfCCountArgTroisPlus+\PfCCountArgTroisMoins}}%
  \ensuremath{%
    \IfDecimal{#1}{%
      \ifboolKV[Facto]{Aide}{%
        \phantom{(}\print{#2}\phantom{)}%
      }{%
        \print{#2}%
      }%
    }{%
      \xintifboolexpr{\PfCCountArgTrois==0}{%
        \ifboolKV[Facto]{Aide}{%
          \phantom{(}\print{#2}\phantom{)}%
        }{%
          \print{#2}%
        }%
      }{%
        (\print{#2})%
      }%
    }%
  }%
}%
 
\NewDocumentCommand\Factorisation{sommm}{%
  \ifluatex
%  \directlua{dofile('poly_render_fix.lua')}
  \useKVdefault[Facto]%
  \setKV[Facto]{#2}%
  \colorlet{Csymbole}{\useKV[Facto]{Couleur}}%
  \IfBooleanTF{#1}{%
    \xdef\PfCFactoSymbole{-}%
  }{%
    \xdef\PfCFactoSymbole{+}%
  }%
  \xdef\PfCFactoNE{\useKV[Facto]{NomExpression}}%
  \ifx\bla#5\bla%
  % Cas a^2-b^2
  \begin{CAS}
    % La variable
    vars('\useKV[Facto]{Lettre}')
    % Les facteurs
    Ka=#3
    Kb=#4
    Kc=(#3)^2
    Kd=(#4)^2
    Ke=(#3)-(#4)
    Kf=(#3)+(#4)
    % Les dérivées pour savoir si ce sont des facteurs littéraux ou pas.
    PfCDDn=DD(Ka,\useKV[Facto]{Lettre}):autosimplify()
    PfCDDm=DD(Kb,\useKV[Facto]{Lettre}):autosimplify()
    PfCDDo=DD(Ke,\useKV[Facto]{Lettre}):autosimplify()
    PfCDDp=DD(Kf,\useKV[Facto]{Lettre}):autosimplify()
    % opposé
    Ko=topoly(-Kb)
    % 
  \end{CAS}
  \setsepchar{,}\ignoreemptyitems%
  \readlist\ListeElementDeriveeA{\fetch{PfCDDn}}%
  \readlist\ListeElementDeriveeB{\fetch{PfCDDm}}%
  \readlist\ListeElementDeriveeE{\fetch{PfCDDo}}%
  \readlist\ListeElementDeriveeF{\fetch{PfCDDp}}%
  \reademptyitems%
  \xdef\TestCASA{\ListeElementDeriveeA[1]}%
  \xdef\TestCASB{\ListeElementDeriveeB[1]}%
  \ifboolKV[Facto]{Details}{%Version monome mais pas deux nombres
    \begin{align*}
      \PfCFactoNE&=\print{Kc:expand()}-\print{Kd:expand()}\\
      \PfCFactoNE&=\ifboolKV[Facto]{Aide}{\xintifboolexpr{\TestCASA>1}{(\,}{}{\underbrace{\Fdash{$\print{Ka}$}}_{A}}\xintifboolexpr{\TestCASA>1}{\,)}{}^{\,2}-\xintifboolexpr{\TestCASB>1}{(\,}{}{\underbrace{\Fdash{$\print{Kb}$}}_{B}}\xintifboolexpr{\TestCASB>1}{\,)}{}^{\,2}}{\IfDecimal{#3}{\print{Ka}^2}{\print{Kc}}-\IfDecimal{#4}{\print{Kb}^2}{\print{Kd}}}\\
      \PfCFactoNE&=(\print{Ke})\times(\print{Kf})%
                   \ifboolKV[Facto]{Formel}{\\\PfCFactoNE&=(\print{Ke})(\print{Kf})}{}%
    \end{align*}
  }{%Avec au moins un des facteurs polynomes
    \begin{align*}
      \ifboolKV[Facto]{Aide}{\IfDecimal{#3}{\PfCFactoNE&=\print{Kc}-\print{Kd}}{\IfDecimal{#4}{\PfCFactoNE&=\print{Kc}-\print{Kd}}{\PfCFactoNE&={\underbrace{\Fdash{$(\print{Ka})$}}_{A}}^{\,2}-{\underbrace{\Fdash{$(\print{Kb})$}}_{B}}^{\,2}}}}{\PfCFactoNE&=\print{Kc}-\print{Kd}}\\
      \IfDecimal{#3}{\PfCFactoNE\uppercase{&}=\ifboolKV[Facto]{Aide}{{\underbrace{\Fdash{$\phantom{(}\print{Ka}\phantom{)}$}}_{A}}^{\,2}}{\print{Ka}^2}-\ifboolKV[Facto]{Aide}{{\underbrace{\Fdash{$(\print{Kb})$}}_{B}}^{\,2}}{\print{Kd}}\\}{\IfDecimal{#4}{\PfCFactoNE\uppercase{&}=\ifboolKV[Facto]{Aide}{{\underbrace{\Fdash{$(\print{Ka})$}}_{A}}^{\,2}}{\print{Kc}}-\ifboolKV[Facto]{Aide}{{\underbrace{\Fdash{$\phantom{(}\print{Kb}\phantom{)}$}}_{B}}^{\,2}}{\print{Kb}^2}\\}{}}
      \PfCFactoNE&=\left[\IfDecimal{#3}{\print{Ka}}{(\print{Ka})}-\IfDecimal{#4}{\print{Kb}}{(\print{Kb})}\right]\times\left[\IfDecimal{#3}{\print{Ka}}{(\print{Ka})}+\IfDecimal{#4}{\print{Kb}}{(\print{Kb})}\right]\\
      \PfCFactoNE&=\left[\print{Ka}\xintifboolexpr{\TestCASB<0}{+}{}\print{Ko}\right]\times\left[\print{Ka}\StrChar{#4}{1}[\PfCTestCaracUn]\StrCompare{\PfCTestCaracUn}{-}[\PfCTestCaracRetour]\xintifboolexpr{\PfCTestCaracRetour==0}{}{+}\print{Kb}\right]\\
      \PfCFactoNE&=\ifboolKV[Facto]{ParenthesesFin}{\left(}{\left[}\print{Ke:expand():topolynomial()}\ifboolKV[Facto]{ParenthesesFin}{\right)}{\right]}\times\ifboolKV[Facto]{ParenthesesFin}{\left(}{\left[}\print{Kf:expand():topolynomial()}\ifboolKV[Facto]{ParenthesesFin}{\right)}{\right]}%
                   \ifboolKV[Facto]{Finale}{\\\PfCFactoNE&=\useKV[Facto]{Resultat}}{\ifboolKV[Facto]{Formel}{\\\PfCFactoNE&=\ifboolKV[Facto]{ParenthesesFin}{\left(}{\left[}\print{Ke:expand():topolynomial()}\ifboolKV[Facto]{ParenthesesFin}{\right)}{\right]}\ifboolKV[Facto]{ParenthesesFin}{\left(}{\left[}\print{Kf:expand():topolynomial()}\ifboolKV[Facto]{ParenthesesFin}{\right)}{\right]}}{}}%
    \end{align*}
  }
\else%k*a+-k*b=k(a+-b)
  % \#3 #3 -- \#4 #4 -- \#5 #5
  \begin{CAS}
    % La variable
    vars('\useKV[Facto]{Lettre}'\ifboolKV[Facto]{VariableSup}{%
      ,'\useKV[Facto]{AutreVariable}'
    }{})
    % Les facteurs
    Kk=#3
    Ka=#4
    Kb=#5
    Kka=Kk*Ka
    Kkb=Kk*Kb
    % Les dérivées pour savoir si ce sont des facteurs littéraux ou pas.
    PfCDDn=DD(Ka,\useKV[Facto]{Lettre}):autosimplify()
    PfCDDm=DD(Kb,\useKV[Facto]{Lettre}):autosimplify()
    % opposé
    Ko=topoly(-Kb)
    Ke=(#4)\PfCFactoSymbole(#5)
  \end{CAS}
  \setsepchar{,}\ignoreemptyitems%
  \readlist\ListeElementDeriveeA{\fetch{PfCDDn}}%
  \readlist\ListeElementDeriveeB{\fetch{PfCDDm}}%
  \reademptyitems%
  \xdef\TestCAS{\ListeElementDeriveeA[1]}%
  \xdef\TestCASB{\ListeElementDeriveeB[1]}%
  \ifboolKV[Facto]{Details}{%Version monome mais pas des nombres
    \begin{align*}
      \PfCFactoNE&=\print{Kka:expand()}\PfCFactoSymbole\print{Kkb:expand()}\\
      \PfCFactoNE&=\ifboolKV[Facto]{Case}{%
                   \underbrace{\Fdash{$\phantom{(\print{Kk})}$}}_{k}\times\underbrace{\Fdash{$\phantom{(\print{Kk})}$}}_{a}\PfCFactoSymbole\underbrace{\Fdash{$\phantom{(\print{Kk})}$}}_{k}\times\underbrace{\Fdash{$\phantom{(\print{Kk})}$}}_{b}
                   }{%
                   \ifboolKV[Facto]{Aide}{\underbrace{\Fdash{$\phantom{(}\print{Kk}\phantom{)}$}}_{k}}{\print{Kk}}\times\xintifboolexpr{\TestCAS<0}{\ifboolKV[Facto]{Aide}{\underbrace{\Fdash{$(\print{Ka}$}}_{a}}{(\print{Ka})}}{\ifboolKV[Facto]{Aide}{\underbrace{\Fdash{$\phantom{(}\print{Ka}\phantom{)}$}}_{a}}{\print{Ka}}}\PfCFactoSymbole\ifboolKV[Facto]{Aide}{\underbrace{\Fdash{$\phantom{(}\print{Kk}\phantom{)}$}}_{k}}{\print{Kk}}\times\xintifboolexpr{\TestCASB<0}{\ifboolKV[Facto]{Aide}{\underbrace{\Fdash{$(\print{Kb}$}}_{b}}{(\print{Kb})}}{\ifboolKV[Facto]{Aide}{\underbrace{\Fdash{$\phantom{(}\print{Kb}\phantom{)}$}}_{b}}{\print{Kb}}}%
      }\\
      \PfCFactoNE&=\ifboolKV[Facto]{Case}{\underbrace{\Fdash{$\phantom{(\print{Kk})}$}}_{k}\times\left(\underbrace{\Fdash{$\phantom{(\print{Kk})}$}}_{a}\PfCFactoSymbole\underbrace{\Fdash{$\phantom{(\print{Kk})}$}}_{b}\right)}{\print{Kk}\times(\print{Ka\PfCFactoSymbole Kb})}%
                   \ifboolKV[Facto]{Formel}{\\\PfCFactoNE&=\print{Kk}(\print{Ka\PfCFactoSymbole Kb})}{}%
    \end{align*}
  }{%Avec au moins un des facteurs polynomes
    \StrCompare{\PfCFactoSymbole}{-}[\PfCRetiensSigneFacteurDeux]%
    \StrChar{#5}{1}[\RetiensPremierCaractere]%en cas de deuxième terme écrit b-ax
    \edef\PfCFacteurUnA{\PfCEcrireFacteur{#3}{Kk}}%
    \edef\PfCFacteurDeuxA{\PfCEcrireFacteur{#4}{Ka}}%
    \edef\PfCFacteurTroisA{\PfCEcrireFacteur{#3}{Kk}}%
    \edef\PfCFacteurQuatreA{\PfCEcrireFacteur{#5}{Kb}}%
    \ifnum\fpeval{\useKV[Facto]{Type}}=1\relax
      \edef\PfCFacteurUn{\PfCEcrireFacteur{#4}{Ka}}%
      \edef\PfCFacteurDeux{\PfCEcrireFacteur{#3}{Kk}}%
      \edef\PfCFacteurTrois{\PfCEcrireFacteur{#3}{Kk}}%
      \edef\PfCFacteurQuatre{\PfCEcrireFacteur{#5}{Kb}}%
      \edef\PfCLettreUn{a}%
      \edef\PfCLettreDeux{k}%
      \edef\PfCLettreTrois{k}%
      \edef\PfCLettreQuatre{b}%
    \else%
      \ifnum\fpeval{\useKV[Facto]{Type}}=3\relax
        \edef\PfCFacteurUn{\PfCEcrireFacteur{#4}{Ka}}%
        \edef\PfCFacteurDeux{\PfCEcrireFacteur{#3}{Kk}}%
        \edef\PfCFacteurTrois{\PfCEcrireFacteur{#5}{Kb}}%
        \edef\PfCFacteurQuatre{\PfCEcrireFacteur{#3}{Kk}}%
        \edef\PfCLettreUn{a}%
        \edef\PfCLettreDeux{k}%
        \edef\PfCLettreTrois{b}%
        \edef\PfCLettreQuatre{k}%
      \else%
        \ifnum\fpeval{\useKV[Facto]{Type}}=2\relax
          \edef\PfCFacteurUn{\PfCEcrireFacteur{#3}{Kk}}%
          \edef\PfCFacteurDeux{\PfCEcrireFacteur{#4}{Ka}}%
          \edef\PfCFacteurTrois{\PfCEcrireFacteur{#5}{Kb}}%
          \edef\PfCFacteurQuatre{\PfCEcrireFacteur{#3}{Kk}}%
          \edef\PfCLettreUn{k}%
          \edef\PfCLettreDeux{a}%
          \edef\PfCLettreTrois{b}%
          \edef\PfCLettreQuatre{k}%
        \else%
          \edef\PfCFacteurUn{\PfCEcrireFacteur{#3}{Kk}}%
          \edef\PfCFacteurDeux{\PfCEcrireFacteur{#4}{Ka}}%
          \edef\PfCFacteurTrois{\PfCEcrireFacteur{#3}{Kk}}%
          \edef\PfCFacteurQuatre{\PfCEcrireFacteur{#5}{Kb}}%
          \edef\PfCLettreUn{k}%
          \edef\PfCLettreDeux{a}%
          \edef\PfCLettreTrois{k}%
          \edef\PfCLettreQuatre{b}%
        \fi%
      \fi%
    \fi%
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 
    \begin{align*}
      \ifboolKV[Facto]{Aide}{\PfCFactoNE&=\setKV[Facto]{Aide=false}\PfCFacteurUn\ifboolKV[Facto]{AideMul}{\times}{}\PfCFacteurDeux\PfCFactoSymbole\PfCFacteurTrois\ifboolKV[Facto]{AideMul}{\times}{}\PfCFacteurQuatre\\\setKV[Facto]{Aide}}{\ifboolKV[Facto]{Case}{\PfCFactoNE&=\PfCFacteurUn\PfCFacteurDeux\PfCFactoSymbole\PfCFacteurTrois\PfCFacteurQuatre\\}{}}%
        \PfCFactoNE&=\ifboolKV[Facto]{Case}{\underbrace{\Fdash{\phantom{\PfCFacteurUnA}}}_{k}\times\underbrace{\Fdash{\phantom{\PfCFacteurDeuxA}}}_{a}\PfCFactoSymbole\underbrace{\Fdash{\phantom{\PfCFacteurTroisA}}}_{k}\times\underbrace{\Fdash{\phantom{\PfCFacteurQuatreA}}}_{b}}{\ifboolKV[Facto]{Aide}{\underbrace{\Fdash{\PfCFacteurUn}}_{\PfCLettreUn}\times\underbrace{\Fdash{\PfCFacteurDeux}}_{\PfCLettreDeux}\PfCFactoSymbole\underbrace{\Fdash{\PfCFacteurTrois}}_{\PfCLettreTrois}\times\underbrace{\Fdash{\PfCFacteurQuatre}}_{\PfCLettreQuatre}}{\PfCFacteurUn\ifboolKV[Facto]{AideMul}{\times}{}\PfCFacteurDeux\PfCFactoSymbole\PfCFacteurTrois\ifboolKV[Facto]{AideMul}{\times}{}\PfCFacteurQuatre}\setKV[Facto]{Aide=false}}\\
      \PfCFactoNE&=\ifboolKV[Facto]{Case}{%
                   \underbrace{\Fdash{\phantom{\PfCFacteurUnA}}}_{k}\times\left[\underbrace{\Fdash{\phantom{\PfCFacteurDeuxA}}}_{a}\PfCFactoSymbole\underbrace{\Fdash{\phantom{\PfCFacteurQuatreA}}}_{b}\right]
                   }{\setKV[Facto]{Aide=false}\PfCEcrireFacteur{#3}{Kk}\times\left[\PfCEcrireFacteur{#4}{Ka}\PfCFactoSymbole\PfCEcrireFacteur{#5}{Kb}\right]}\\
      \PfCFactoNE&=\ifboolKV[Facto]{Case}{
                   \pointilles[150pt]
                   }{\setKV[Facto]{Aide=false}\PfCEcrireFacteur{#3}{Kk}\times\ifboolKV[Facto]{ParenthesesFin}{\left(}{\left[}\print{Ka}\xintifboolexpr{\PfCRetiensSigneFacteurDeux==1}{\xintifboolexpr{\TestCASB<0}{\IfDecimal{\RetiensPremierCaractere}{+}{}}{+}\print{Kb}}{\xintifboolexpr{\TestCASB<0}{+}{}\print{Ko}}\ifboolKV[Facto]{ParenthesesFin}{\right)}{\right]}}\\
      \PfCFactoNE&=\ifboolKV[Facto]{Case}{
                   \pointilles[150pt]
                   }{\setKV[Facto]{Aide=false}\PfCEcrireFacteur{#3}{Kk}\times\ifboolKV[Facto]{ParenthesesFin}{\left(}{\left[}\print{Ke:expand():topolynomial()}\ifboolKV[Facto]{ParenthesesFin}{\right)}{\right]}}%
                     \ifboolKV[Facto]{Finale}{\\\PfCFactoNE&=\useKV[Facto]{Resultat}}{\ifboolKV[Facto]{Formel}{\\\PfCFactoNE&=\setKV[Facto]{Aide=false}\PfCEcrireFacteur{#3}{Kk}\ifboolKV[Facto]{ParenthesesFin}{\left(}{\left[}\print{Ke:expand():topolynomial()}\ifboolKV[Facto]{ParenthesesFin}{\right)}{\right]}}{}}%
      \end{align*}
    }%
  \fi%
  \fi
}%