| 1 |
%% filename: polexprexpr.tex |
| 2 |
%% Part of the polexpr package (0.8.7a, 2022/05/19) |
| 3 |
%% |
| 4 |
%% Polynomial extensions to the \xintexpr syntax: |
| 5 |
%% |
| 6 |
%% 1. Authorize ' in variable and function names |
| 7 |
%% This partially breaks infix operators 'and', 'or', 'xor', 'mod' |
| 8 |
%% which must be replaced by &&, ||, /: and xor() syntax |
| 9 |
%% if immediately following a variable name |
| 10 |
%% |
| 11 |
%% 2. Map infix operators to the polexprcore macros |
| 12 |
%% |
| 13 |
%% Overloading of infix operators must be done even outside of \poldef's |
| 14 |
%% scope else functions declared via \xintdeffunc would not be usable in |
| 15 |
%% \poldef as they would be using the xintfrac macros unaware of polynomials |
| 16 |
%% |
| 17 |
%% The overloading of // and /: is experimental. |
| 18 |
%% |
| 19 |
%% 3. Support for the polynomial functions to work in \xintdeffunc |
| 20 |
%% |
| 21 |
%% 4. Support macros for the new functions acting on polynomial variables |
| 22 |
% |
| 23 |
% 1. |
| 24 |
\def\XINT_expr_scanfunc_b #1% |
| 25 |
{% |
| 26 |
\ifcat \relax#1\xint_dothis{\iffalse{\fi}(_#1}\fi |
| 27 |
\if (#1\xint_dothis{\iffalse{\fi}(`}\fi |
| 28 |
\if 1\ifcat a#10\fi |
| 29 |
\ifnum\xint_c_ix<1\string#1 0\fi |
| 30 |
\if @#10\fi |
| 31 |
\if _#10\fi |
| 32 |
\if '#10\fi |
| 33 |
1% |
| 34 |
\xint_dothis{\iffalse{\fi}(_#1}\fi |
| 35 |
\xint_orthat {#1\XINT_expr_scanfunc_a}% |
| 36 |
}% |
| 37 |
% 2. |
| 38 |
% the minus sign as prefix |
| 39 |
\def\POL_tmp #1#2% |
| 40 |
{% |
| 41 |
\expandafter\def\csname XINT_expr_exec_#1\endcsname##1##2##3% \XINT_expr_exec_<op><level> |
| 42 |
{% |
| 43 |
\expandafter ##1\expandafter ##2\expandafter |
| 44 |
{\romannumeral`&&@\XINT:NEhook:f:one:from:one{\romannumeral`&&@#2##3}}% |
| 45 |
}% |
| 46 |
}% |
| 47 |
\POL_tmp{-xii} \xintPolOpp |
| 48 |
\POL_tmp{-xiv} \xintPolOpp |
| 49 |
\POL_tmp{-xvi} \xintPolOpp |
| 50 |
\POL_tmp{-xviii}\xintPolOpp |
| 51 |
% infix operators |
| 52 |
\def\POL_tmp #1#2% |
| 53 |
{% |
| 54 |
\expandafter\def\csname XINT_expr_exec_#1\endcsname##1##2##3##4% \XINT_expr_exec_<op> |
| 55 |
{% |
| 56 |
\expandafter##2\expandafter##3\expandafter |
| 57 |
{\romannumeral`&&@\XINT:NEhook:f:one:from:two{\romannumeral`&&@#2##1##4}}% |
| 58 |
}% |
| 59 |
}% |
| 60 |
\POL_tmp + \xintPolAdd |
| 61 |
\POL_tmp - \xintPolSub |
| 62 |
\POL_tmp * \xintPolMul |
| 63 |
\POL_tmp / \xintPolQuo |
| 64 |
% there is no infix operator mapped to \xintPolRem |
| 65 |
% for lack of notation: perhaps /; ? advices welcome |
| 66 |
\POL_tmp{//}\xintPolDivModQ |
| 67 |
\POL_tmp{/:}\xintPolDivModR |
| 68 |
\POL_tmp ^ \xintPolPow |
| 69 |
\expandafter\let\csname XINT_expr_op_**\expandafter\endcsname |
| 70 |
\csname XINT_expr_op_^\endcsname |
| 71 |
% 3. |
| 72 |
% Matches with "mysterious stuff" section of xintexpr source code |
| 73 |
\let\POL:NEhook:polfunc\expandafter |
| 74 |
\toks0\expandafter{\XINT_expr_redefinemacros}% |
| 75 |
\toks2 {\let\POL:NEhook:polfunc\POL:NE:polfunc}% |
| 76 |
\edef\XINT_expr_redefinemacros{\the\toks0 \the\toks2}% |
| 77 |
\catcode`~ 12 % |
| 78 |
\def\POL@defpolfunc #1#2% |
| 79 |
{% |
| 80 |
\expandafter\POL@defpolfunc_a |
| 81 |
\csname XINT_#2_func_#1\expandafter\endcsname |
| 82 |
\csname XINT_#2_polfunc_#1\endcsname |
| 83 |
}% |
| 84 |
\def\POL@defpolfunc_a #1#2% |
| 85 |
{% |
| 86 |
\XINT_global |
| 87 |
\def#1##1##2##3% |
| 88 |
{% |
| 89 |
\expandafter##1\expandafter##2\expandafter{% |
| 90 |
\romannumeral`&&@\POL:NEhook:polfunc{\romannumeral`&&@#2##3}}% |
| 91 |
}% |
| 92 |
}% |
| 93 |
\def\POL:NE:polfunc #1{% |
| 94 |
\def\POL:NE:polfunc ##1% |
| 95 |
{% |
| 96 |
\if0\XINT:NE:hastilde ##1~!\relax % this ! of catcode 11 |
| 97 |
\XINT:NE:hashash ##1#1!\relax 0\else |
| 98 |
\xint_dothis\POL:NE:polfunc_a\fi |
| 99 |
\xint_orthat\POL:NE:polfunc_b |
| 100 |
##1&&A% |
| 101 |
}}\expandafter\POL:NE:polfunc\string#% |
| 102 |
\def\POL:NE:polfunc_a\romannumeral`&&@#1#2&&A% |
| 103 |
{% |
| 104 |
% If we are here #2 was not braced; \string is done with \escapechar126 |
| 105 |
\expandafter{\expanded{~romannumeral~POL:NE:usepolfunc% |
| 106 |
{\expandafter\xint_gobble_i\string#1}}#2}% |
| 107 |
}% |
| 108 |
\def\POL:NE:polfunc_b#1{% |
| 109 |
\def\POL:NE:polfunc_b\romannumeral`&&@##1##2&&A% |
| 110 |
{% |
| 111 |
\expandafter{% |
| 112 |
\romannumeral`&&@% |
| 113 |
\if0\XINT:NE:hastilde ##2~!\relax |
| 114 |
\XINT:NE:hashash ##2#1!\relax 0\else |
| 115 |
\POL:NE:polfunc_c\fi |
| 116 |
##1{##2}}% |
| 117 |
}}\expandafter\POL:NE:polfunc_b\string#% |
| 118 |
% In this case the \expandafter inserted by \POL:NE:usepolfunc |
| 119 |
% expansion will be superfluous |
| 120 |
\def\POL:NE:polfunc_c#1#2% #1=\fi |
| 121 |
{% |
| 122 |
\expanded{#1~romannumeral~POL:NE:usepolfunc% |
| 123 |
{\expandafter\xint_gobble_i\string#2}}% |
| 124 |
}% |
| 125 |
% This \expandafter is in case there is an \expanded after that due |
| 126 |
% to some slicing constructs |
| 127 |
% Call: \romannumeral\POL:NE:usepolfunc |
| 128 |
\def\POL:NE:usepolfunc#1{`&&@\csname#1\expandafter\endcsname}% |
| 129 |
\catcode`~ 3 % its normal catcode in xint bundle |
| 130 |
% 4. |
| 131 |
\def\POL_tmp #1#2#3% |
| 132 |
{% |
| 133 |
\expandafter\def\csname XINT_expr_func_#1\endcsname##1##2##3% |
| 134 |
{% |
| 135 |
\expandafter ##1\expandafter ##2\expandafter |
| 136 |
{% |
| 137 |
\romannumeral`&&@#2{\romannumeral`&&@#3##3}% |
| 138 |
}% |
| 139 |
}% |
| 140 |
}% |
| 141 |
\POL_tmp {sqr} \XINT:NEhook:f:one:from:one \xintPolSqr |
| 142 |
% 0.8.1 renames 0.8.0 icontent() to cont() and lcoeff() to lc() |
| 143 |
% and adds topol() and lcoeffs() |
| 144 |
\POL_tmp {pol} \XINT:NEhook:f:one:from:one \xintPolPol |
| 145 |
\POL_tmp {lpol} \XINT:NEhook:f:one:from:one \xintPolLPol |
| 146 |
\POL_tmp {deg} \XINT:NEhook:f:one:from:one \xintPolDeg |
| 147 |
\POL_tmp {coeffs} \XINT:NEhook:f:one:from:one \xintPolCoeffs |
| 148 |
\POL_tmp {lcoeffs} \XINT:NEhook:f:one:from:one \xintPolLCoeffs |
| 149 |
\POL_tmp {coeff} \XINT:NEhook:f:one:from:two \xintPolCoeff |
| 150 |
\POL_tmp {lc} \XINT:NEhook:f:one:from:one \xintPolLC |
| 151 |
\POL_tmp {monicpart} \XINT:NEhook:f:one:from:one \xintPolMonicPart |
| 152 |
\POL_tmp {cont} \XINT:NEhook:f:one:from:one \xintPolCont |
| 153 |
\POL_tmp {primpart} \XINT:NEhook:f:one:from:one \xintPolPrimPart |
| 154 |
\POL_tmp {rdcoeffs} \XINT:NEhook:f:one:from:one \xintPolRedCoeffs |
| 155 |
\POL_tmp {rdzcoeffs} \XINT:NEhook:f:one:from:one \xintPolSRedCoeffs |
| 156 |
\POL_tmp {diff1} \XINT:NEhook:f:one:from:one \xintPolDiffOne |
| 157 |
\POL_tmp {diff2} \XINT:NEhook:f:one:from:one \xintPolDiffTwo |
| 158 |
\POL_tmp {diffn} \XINT:NEhook:f:one:from:two \xintPolDiffN |
| 159 |
\POL_tmp {antider} \XINT:NEhook:f:one:from:one \xintPolAntiOne |
| 160 |
\POL_tmp {integral} \XINT:NEhook:f:one:from:two \xintPolIntegral |
| 161 |
\POL_tmp {intfrom} \XINT:NEhook:f:one:from:two \xintPolIntFrom |
| 162 |
\POL_tmp {quorem} \XINT:NEhook:f:one:from:two \xintPolQuoRem |
| 163 |
\POL_tmp {quo} \XINT:NEhook:f:one:from:two \xintPolQuo |
| 164 |
\POL_tmp {rem} \XINT:NEhook:f:one:from:two \xintPolRem |
| 165 |
\POL_tmp {prem} \XINT:NEhook:f:one:from:two \xintPolPRem |
| 166 |
\POL_tmp {divmod} \XINT:NEhook:f:one:from:two \xintPolDivMod |
| 167 |
\POL_tmp {mod} \XINT:NEhook:f:one:from:two \xintPolDivModR |
| 168 |
\POL_tmp {evalp} \XINT:NEhook:f:one:from:two \xintPolEvalAt |
| 169 |
\def\XINT_expr_func_polgcd #1#2#3% |
| 170 |
{% |
| 171 |
\expandafter #1\expandafter #2\expandafter{\expandafter |
| 172 |
{\romannumeral`&&@\XINT:NEhook:f:from:delim:u\XINT_PolGCDof#3^}}% |
| 173 |
}% |
| 174 |
% this is provisory |
| 175 |
\xintdeffunc polpowmod_(P, m, Q) := |
| 176 |
isone(m)? |
| 177 |
% m=1: return P modulo Q |
| 178 |
{ mod(P,Q) } |
| 179 |
% m > 1: test if odd or even and do recursive call |
| 180 |
{ odd(m)? { mod(P*sqr(polpowmod_(P, m//2, Q)), Q) } |
| 181 |
{ mod( sqr(polpowmod_(P, m//2, Q)), Q) } |
| 182 |
} |
| 183 |
;% |
| 184 |
\xintdeffunc polpowmod(P, m, Q) := (m)?{polpowmod_(P, m, Q)}{1};% |
| 185 |
% |
| 186 |
\endinput |