| 1 |
% \iffalse |
| 2 |
% |
| 3 |
%% File l3fp-symbolic.dtx (C) Copyright 2012-2025 The LaTeX Project |
| 4 |
% |
| 5 |
% It may be distributed and/or modified under the conditions of the |
| 6 |
% LaTeX Project Public License (LPPL), either version 1.3c of this |
| 7 |
% license or (at your option) any later version. The latest version |
| 8 |
% of this license is in the file |
| 9 |
% |
| 10 |
% http://www.latex-project.org/lppl.txt |
| 11 |
% |
| 12 |
% This file is part of the "l3kernel bundle" (The Work in LPPL) |
| 13 |
% and all files in that bundle must be distributed together. |
| 14 |
% |
| 15 |
% ----------------------------------------------------------------------- |
| 16 |
% |
| 17 |
% The development version of the bundle can be found at |
| 18 |
% |
| 19 |
% https://github.com/latex3/latex3 |
| 20 |
% |
| 21 |
% for those people who are interested. |
| 22 |
% |
| 23 |
%<*driver> |
| 24 |
\documentclass[full]{l3doc} |
| 25 |
\usepackage{amsmath} |
| 26 |
\begin{document} |
| 27 |
\DocInput{\jobname.dtx} |
| 28 |
\end{document} |
| 29 |
%</driver> |
| 30 |
% \fi |
| 31 |
% |
| 32 |
% \title{^^A |
| 33 |
% The \pkg{l3fp-symbolic} module\\ Symbolic expressions^^A |
| 34 |
% } |
| 35 |
% |
| 36 |
% \author{^^A |
| 37 |
% The \LaTeX{} Project\thanks |
| 38 |
% {^^A |
| 39 |
% E-mail: |
| 40 |
% \href{mailto:[email protected]} |
| 41 |
% {[email protected]}^^A |
| 42 |
% }^^A |
| 43 |
% } |
| 44 |
% |
| 45 |
% \date{Released 2026-05-15} |
| 46 |
% |
| 47 |
% \maketitle |
| 48 |
% |
| 49 |
% \begin{documentation} |
| 50 |
% |
| 51 |
% \end{documentation} |
| 52 |
% |
| 53 |
% \begin{implementation} |
| 54 |
% |
| 55 |
% \section{\pkg{l3fp-symbolic} implementation} |
| 56 |
% |
| 57 |
% \begin{macrocode} |
| 58 |
%<*code> |
| 59 |
% \end{macrocode} |
| 60 |
% |
| 61 |
% \begin{macrocode} |
| 62 |
%<@@=fp> |
| 63 |
% \end{macrocode} |
| 64 |
% |
| 65 |
% \subsection{Misc} |
| 66 |
% |
| 67 |
% \begin{variable}{\l_@@_symbolic_fp} |
| 68 |
% Scratch floating point. |
| 69 |
% \begin{macrocode} |
| 70 |
\fp_new:N \l_@@_symbolic_fp |
| 71 |
% \end{macrocode} |
| 72 |
% \end{variable} |
| 73 |
% |
| 74 |
% \subsection{Building blocks for expressions} |
| 75 |
% |
| 76 |
% Every symbolic expression has the form \cs{s_@@_symbolic} |
| 77 |
% \cs{@@_symbolic_chk:w} \meta{operation} |,| \Arg{operands} \meta{junk} |
| 78 |
% \cs{@@_sep:} where the \meta{operation} is a list of \texttt{N}-type tokens, |
| 79 |
% the \meta{operands} is an array of floating point objects, and the |
| 80 |
% \meta{junk} (typically empty) is to be discarded. If the outermost operator |
| 81 |
% (last to be evaluated) is unary, the expression has the form |
| 82 |
% \begin{quote} |
| 83 |
% \cs{s_@@_symbolic} \cs{@@_symbolic_chk:w} \\ |
| 84 |
% \cs{@@_types_unary:NNw} |\__fp_|\meta{op}|_o:w| \meta{token} |,| \\ |
| 85 |
% |{| \meta{operand} |}| \meta{junk} \cs{@@_sep:} |
| 86 |
% \end{quote} |
| 87 |
% where the \meta{op} is a unary operation (|set_sign|, |cos|, |
| 88 |
% \ldots{}), and the \meta{token} and \meta{operand} are used as |
| 89 |
% arguments for \cs{@@_\meta{op}_o:w} (or the type-specific analog of |
| 90 |
% this function). If the outermost operator is binary, the expression |
| 91 |
% has the form |
| 92 |
% \begin{quote} |
| 93 |
% \cs{s_@@_symbolic} \cs{@@_symbolic_chk:w} \\ |
| 94 |
% \cs{@@_types_binary:Nww} |\__fp_|\meta{op}|_o:ww| |,| \\ |
| 95 |
% |{| \meta{operand_1} \meta{operand_2} |}| \meta{junk} \cs{@@_sep:} |
| 96 |
% \end{quote} |
| 97 |
% where the \meta{op} is an operation (|+|, |&|, \ldots{}), and |
| 98 |
% |\__fp_|\meta{op}|_o:ww| receives the \meta{operands} as arguments. |
| 99 |
% If the expression is a user-defined function applied to some arguments |
| 100 |
% it is stored as |
| 101 |
% \begin{quote} |
| 102 |
% \cs{s_@@_symbolic} \cs{@@_symbolic_chk:w} \\ |
| 103 |
% \cs{@@_function_o:w} \cs[no-index]{@@_\meta{identifier}_o:w} |,| \\ |
| 104 |
% |{| \meta{operands} |}| \meta{junk} \cs{@@_sep:} |
| 105 |
% \end{quote} |
| 106 |
% If the expression consists of a single variable, it is stored as |
| 107 |
% \begin{quote} |
| 108 |
% \cs{s_@@_symbolic} \cs{@@_symbolic_chk:w} \\ |
| 109 |
% \cs{@@_variable_o:w} \meta{identifier} |,| \\ |
| 110 |
% |{| |}| \meta{junk} \cs{@@_sep:} |
| 111 |
% \end{quote} |
| 112 |
% |
| 113 |
% Symbolic expressions are stored in a prefix form. When encountering a |
| 114 |
% symbolic expression in a floating point computation, we attempt to |
| 115 |
% evaluate the operands as much as possible, and if that yields floating |
| 116 |
% point numbers rather than expressions, we apply the operator which |
| 117 |
% follows (if the function is known). |
| 118 |
% |
| 119 |
% For instance, the expression |a + b * sin(c)| is stored as |
| 120 |
% \begin{verbatim} |
| 121 |
% \s__fp_symbolic \__fp_symbolic_chk:w |
| 122 |
% \__fp_types_binary:Nww \__fp_+_o:ww , |
| 123 |
% { |
| 124 |
% \s__fp_symbolic \__fp_symbolic_chk:w |
| 125 |
% \__fp_variable_o:w a , { } \__fp_sep: |
| 126 |
% \s__fp_symbolic \__fp_symbolic_chk:w |
| 127 |
% \__fp_types_binary:Nww \__fp_*_o:ww , |
| 128 |
% { |
| 129 |
% \s__fp_symbolic \__fp_symbolic_chk:w |
| 130 |
% \__fp_variable_o:w b , { } \__fp_sep: |
| 131 |
% \s__fp_symbolic \__fp_symbolic_chk:w |
| 132 |
% \__fp_types_unary:NNw \__fp_sin_o:w \use_i:nn , |
| 133 |
% { |
| 134 |
% \s__fp_symbolic \__fp_symbolic_chk:w |
| 135 |
% \__fp_variable_o:w c , { } \__fp_sep: |
| 136 |
% } \__fp_sep: |
| 137 |
% } \__fp_sep: |
| 138 |
% } \__fp_sep: |
| 139 |
% \end{verbatim} |
| 140 |
% |
| 141 |
% \begin{variable}{\s_@@_symbolic} |
| 142 |
% Scan mark indicating the start of a symbolic expression. |
| 143 |
% \begin{macrocode} |
| 144 |
\scan_new:N \s_@@_symbolic |
| 145 |
% \end{macrocode} |
| 146 |
% \end{variable} |
| 147 |
% |
| 148 |
% \begin{macro}{\@@_symbolic_chk:w} |
| 149 |
% Analog of \cs{@@_chk:w} for symbolic expressions. |
| 150 |
% \begin{macrocode} |
| 151 |
\cs_new_protected:Npn \@@_symbolic_chk:w #1,#2#3\@@_sep: |
| 152 |
{ |
| 153 |
\msg_error:nne { fp } { misused-fp } |
| 154 |
{ |
| 155 |
\@@_to_tl_dispatch:w |
| 156 |
\s_@@_symbolic \@@_symbolic_chk:w #1,{#2}\@@_sep: |
| 157 |
} |
| 158 |
} |
| 159 |
% \end{macrocode} |
| 160 |
% \end{macro} |
| 161 |
% |
| 162 |
% \subsection{Expanding after a symbolic expression} |
| 163 |
% |
| 164 |
% \begin{macro}[EXP] |
| 165 |
% {\@@_if_has_symbolic:nTF, \@@_if_has_symbolic_aux:w} |
| 166 |
% Tests if |#1| contains \cs{s_@@_symbolic} at top-level. This test |
| 167 |
% should be precise enough to determine if a given array contains a |
| 168 |
% symbolic expression or only consists of floating points. Used in |
| 169 |
% \cs{@@_exp_after_symbolic_f:nw}. |
| 170 |
% \begin{macrocode} |
| 171 |
\cs_new:Npn \@@_if_has_symbolic:nTF #1 |
| 172 |
{ |
| 173 |
\@@_if_has_symbolic_aux:w |
| 174 |
#1 \s_@@_mark \use_i:nn |
| 175 |
\s_@@_symbolic \s_@@_mark \use_ii:nn |
| 176 |
\s_@@_stop |
| 177 |
} |
| 178 |
\cs_new:Npn \@@_if_has_symbolic_aux:w |
| 179 |
#1 \s_@@_symbolic #2 \s_@@_mark #3#4 \s_@@_stop { #3 } |
| 180 |
% \end{macrocode} |
| 181 |
% \end{macro} |
| 182 |
% |
| 183 |
% \begin{macro}[EXP]{\@@_exp_after_symbolic_f:nw} |
| 184 |
% \begin{macro}[EXP] |
| 185 |
% {\@@_exp_after_symbolic_aux:w, \@@_exp_after_symbolic_loop:N} |
| 186 |
% This function does two things: trigger an \texttt{f}-expansion of |
| 187 |
% the argument~|#1| after the following symbolic expression, and |
| 188 |
% evaluate all pieces of the expression which can be evaluated. |
| 189 |
% \begin{macrocode} |
| 190 |
\cs_new:Npn \@@_exp_after_symbolic_f:nw |
| 191 |
#1 \s_@@_symbolic \@@_symbolic_chk:w #2, #3#4\@@_sep: |
| 192 |
{ |
| 193 |
\exp_after:wN \@@_exp_after_symbolic_aux:w |
| 194 |
\exp:w |
| 195 |
\@@_exp_after_symbolic_loop:N #2 |
| 196 |
{ , \exp:w \use_none:nn } |
| 197 |
\exp_after:wN \exp_end: \exp_after:wN |
| 198 |
{ |
| 199 |
\exp:w \exp_end_continue_f:w |
| 200 |
\@@_exp_after_array_f:w #3 \s_@@_expr_stop |
| 201 |
\exp_after:wN |
| 202 |
} |
| 203 |
\exp_after:wN \@@_sep: |
| 204 |
\exp:w \exp_end_continue_f:w #1 |
| 205 |
} |
| 206 |
\cs_new:Npn \@@_exp_after_symbolic_aux:w #1, #2\@@_sep: |
| 207 |
{ |
| 208 |
\@@_if_has_symbolic:nTF {#2} |
| 209 |
{ \s_@@_symbolic \@@_symbolic_chk:w #1, {#2} \@@_sep: } |
| 210 |
{ #1 #2 @ \prg_do_nothing: } |
| 211 |
} |
| 212 |
\cs_new:Npn \@@_exp_after_symbolic_loop:N #1 |
| 213 |
{ |
| 214 |
\exp_after:wN \exp_end: |
| 215 |
\exp_after:wN #1 |
| 216 |
\exp:w |
| 217 |
\@@_exp_after_symbolic_loop:N |
| 218 |
} |
| 219 |
% \end{macrocode} |
| 220 |
% \end{macro} |
| 221 |
% \end{macro} |
| 222 |
% |
| 223 |
% \subsection{Applying infix operators to expressions} |
| 224 |
% |
| 225 |
% \begin{macro}[EXP]{\@@_symbolic_binary_o:Nww} |
| 226 |
% Used when applying infix operators to expressions. |
| 227 |
% \begin{macrocode} |
| 228 |
\cs_new:Npn \@@_symbolic_binary_o:Nww #1 #2\@@_sep: #3\@@_sep: |
| 229 |
{ |
| 230 |
\@@_exp_after_symbolic_f:nw { \exp_after:wN \exp_stop_f: } |
| 231 |
\s_@@_symbolic \@@_symbolic_chk:w |
| 232 |
\@@_types_binary:Nww #1 , { #2\@@_sep: #3\@@_sep: } \@@_sep: |
| 233 |
} |
| 234 |
% \end{macrocode} |
| 235 |
% \end{macro} |
| 236 |
% |
| 237 |
% \begin{@makeother}{^} %^^A Hack! |
| 238 |
% \begin{@makeother}{|} %^^A Hack! |
| 239 |
% \begin{@makeother}{&} %^^A Hack! |
| 240 |
% \begin{macro}[EXP] |
| 241 |
% { |
| 242 |
% \@@_symbolic_+_symbolic_o:ww, |
| 243 |
% \@@_symbolic_+_o:ww, |
| 244 |
% \@@_+_symbolic_o:ww, |
| 245 |
% \@@_symbolic_-_symbolic_o:ww, |
| 246 |
% \@@_symbolic_-_o:ww, |
| 247 |
% \@@_-_symbolic_o:ww, |
| 248 |
% \@@_symbolic_*_symbolic_o:ww, |
| 249 |
% \@@_symbolic_*_o:ww, |
| 250 |
% \@@_*_symbolic_o:ww, |
| 251 |
% \@@_symbolic_/_symbolic_o:ww, |
| 252 |
% \@@_symbolic_/_o:ww, |
| 253 |
% \@@_/_symbolic_o:ww, |
| 254 |
% \@@_symbolic_^_symbolic_o:ww, |
| 255 |
% \@@_symbolic_^_o:ww, |
| 256 |
% \@@_^_symbolic_o:ww, |
| 257 |
% \@@_symbolic_|_symbolic_o:ww, |
| 258 |
% \@@_symbolic_|_o:ww, |
| 259 |
% \@@_|_symbolic_o:ww, |
| 260 |
% \@@_symbolic_&_symbolic_o:ww, |
| 261 |
% \@@_symbolic_&_o:ww, |
| 262 |
% \@@_&_symbolic_o:ww, |
| 263 |
% } |
| 264 |
% \begin{macrocode} |
| 265 |
\cs_set_protected:Npn \@@_tmp:w #1#2 |
| 266 |
{ |
| 267 |
\cs_new:cpn |
| 268 |
{ @@_symbolic_#2_symbolic_o:ww } |
| 269 |
{ \@@_symbolic_binary_o:Nww #1 } |
| 270 |
\cs_new_eq:cc |
| 271 |
{ @@_symbolic_#2 _o:ww } |
| 272 |
{ @@_symbolic_#2_symbolic_o:ww } |
| 273 |
\cs_new_eq:cc |
| 274 |
{ @@ _#2_symbolic_o:ww } |
| 275 |
{ @@_symbolic_#2_symbolic_o:ww } |
| 276 |
} |
| 277 |
\tl_map_inline:nn { + - * / ^ & | } % | |
| 278 |
{ \exp_args:Nc \@@_tmp:w { @@_#1_o:ww } {#1} } |
| 279 |
% \end{macrocode} |
| 280 |
% \end{macro} |
| 281 |
% \end{@makeother} |
| 282 |
% \end{@makeother} |
| 283 |
% \end{@makeother} |
| 284 |
% |
| 285 |
% \subsection{Applying prefix functions to expressions} |
| 286 |
% |
| 287 |
% \begin{macro}[EXP]{\@@_symbolic_unary_o:NNw} |
| 288 |
% Used when applying infix operators to expressions. |
| 289 |
% \begin{macrocode} |
| 290 |
\cs_new:Npn \@@_symbolic_unary_o:NNw #1#2#3\@@_sep: @ |
| 291 |
{ |
| 292 |
\@@_exp_after_symbolic_f:nw { \exp_after:wN \exp_stop_f: } |
| 293 |
\s_@@_symbolic \@@_symbolic_chk:w |
| 294 |
\@@_types_unary:NNw #1#2 , { #3\@@_sep: } \@@_sep: |
| 295 |
} |
| 296 |
% \end{macrocode} |
| 297 |
% \end{macro} |
| 298 |
% |
| 299 |
% \begin{macro}[EXP] |
| 300 |
% { |
| 301 |
% \@@_symbolic_acos_o:w , |
| 302 |
% \@@_symbolic_acsc_o:w , |
| 303 |
% \@@_symbolic_asec_o:w , |
| 304 |
% \@@_symbolic_asin_o:w , |
| 305 |
% \@@_symbolic_cos_o:w , |
| 306 |
% \@@_symbolic_cot_o:w , |
| 307 |
% \@@_symbolic_csc_o:w , |
| 308 |
% \@@_symbolic_exp_o:w , |
| 309 |
% \@@_symbolic_fact_o:w , |
| 310 |
% \@@_symbolic_ln_o:w , |
| 311 |
% \@@_symbolic_not_o:w , |
| 312 |
% \@@_symbolic_sec_o:w , |
| 313 |
% \@@_symbolic_set_sign_o:w , |
| 314 |
% \@@_symbolic_sign_o:w , |
| 315 |
% \@@_symbolic_sin_o:w , |
| 316 |
% \@@_symbolic_tan_o:w , |
| 317 |
% } |
| 318 |
% \begin{macrocode} |
| 319 |
\tl_map_inline:nn |
| 320 |
{ |
| 321 |
{acos} {acsc} {asec} {asin} {cos} {cot} {csc} {exp} {fact} {ln} |
| 322 |
{not} {sec} {set_sign} {sin} {sign} {sqrt} {tan} |
| 323 |
} |
| 324 |
{ |
| 325 |
\cs_new:cpe { @@_symbolic_#1_o:w } |
| 326 |
{ |
| 327 |
\exp_not:N \@@_symbolic_unary_o:NNw |
| 328 |
\exp_not:c { @@_#1_o:w } |
| 329 |
} |
| 330 |
} |
| 331 |
% \end{macrocode} |
| 332 |
% \end{macro} |
| 333 |
% |
| 334 |
% \subsection{Conversions} |
| 335 |
% |
| 336 |
% \begin{macro}[EXP] |
| 337 |
% { |
| 338 |
% \@@_symbolic_to_decimal:w, |
| 339 |
% \@@_symbolic_to_int:w, |
| 340 |
% \@@_symbolic_to_scientific:w |
| 341 |
% } |
| 342 |
% \begin{macro}[EXP]{\@@_symbolic_convert:wnnN} |
| 343 |
% Symbolic expressions cannot be converted to decimal, integer, or |
| 344 |
% scientific notation unless they can be evaluated all the way. |
| 345 |
% \begin{macrocode} |
| 346 |
\cs_set_protected:Npn \@@_tmp:w #1#2#3 |
| 347 |
{ |
| 348 |
\cs_new:cpn { @@_symbolic_to_#1:w } |
| 349 |
{ |
| 350 |
\exp_after:wN \@@_symbolic_convert:wnnN |
| 351 |
\exp:w \exp_end_continue_f:w |
| 352 |
\@@_exp_after_symbolic_f:nw { { #2 } { fp_to_#1 } #3 } |
| 353 |
} |
| 354 |
} |
| 355 |
\@@_tmp:w { decimal } { 0 } \@@_to_decimal_dispatch:w |
| 356 |
\@@_tmp:w { int } { 0 } \@@_to_int_dispatch:w |
| 357 |
\@@_tmp:w { scientific } { nan } \@@_to_scientific_dispatch:w |
| 358 |
\cs_new:Npn \@@_symbolic_convert:wnnN #1#2\@@_sep: #3#4#5 |
| 359 |
{ |
| 360 |
\str_if_eq:nnTF {#1} { \s_@@_symbolic } |
| 361 |
{ \@@_invalid_operation:nnw {#3} {#4} #1#2\@@_sep: } |
| 362 |
{ #5 #1#2\@@_sep: } |
| 363 |
} |
| 364 |
% \end{macrocode} |
| 365 |
% \end{macro} |
| 366 |
% \end{macro} |
| 367 |
% |
| 368 |
% \begin{macro}[EXP] |
| 369 |
% {\@@_symbolic_cs_arg_to_fn:NN, \@@_symbolic_op_arg_to_fn:nN} |
| 370 |
% \begin{macrocode} |
| 371 |
\cs_new:Npn \@@_symbolic_cs_arg_to_fn:NN #1 |
| 372 |
{ |
| 373 |
\exp_args:Nf \@@_symbolic_op_arg_to_fn:nN |
| 374 |
{ \@@_types_cs_to_op:N #1 } |
| 375 |
} |
| 376 |
\cs_new:Npn \@@_symbolic_op_arg_to_fn:nN #1#2 |
| 377 |
{ |
| 378 |
\str_case:nnF { #1 #2 } |
| 379 |
{ |
| 380 |
{ not ? } { ! } |
| 381 |
{ set_sign 0 } { abs } |
| 382 |
{ set_sign 2 } { - } |
| 383 |
} |
| 384 |
{ |
| 385 |
\token_if_eq_meaning:NNTF #2 \use_ii:nn |
| 386 |
{ #1 d } {#1} |
| 387 |
} |
| 388 |
} |
| 389 |
% \end{macrocode} |
| 390 |
% \end{macro} |
| 391 |
% |
| 392 |
% \begin{macro}[EXP]{\@@_symbolic_to_tl:w} |
| 393 |
% \begin{macro}[rEXP] |
| 394 |
% { |
| 395 |
% \@@_symbolic_unary_to_tl:NNw, |
| 396 |
% \@@_symbolic_binary_to_tl:Nww, |
| 397 |
% \@@_symbolic_function_to_tl:Nw |
| 398 |
% } |
| 399 |
% Converting a symbolic expression to a token list is possible. |
| 400 |
% \begin{macrocode} |
| 401 |
\cs_new:Npn \@@_symbolic_to_tl:w |
| 402 |
\s_@@_symbolic \@@_symbolic_chk:w #1#2, #3#4\@@_sep: |
| 403 |
{ |
| 404 |
\str_case:nnTF {#1} |
| 405 |
{ |
| 406 |
{ \@@_types_unary:NNw } { \@@_symbolic_unary_to_tl:NNw } |
| 407 |
{ \@@_types_binary:Nww } { \@@_symbolic_binary_to_tl:Nww } |
| 408 |
{ \@@_function_o:w } { \@@_symbolic_function_to_tl:Nw } |
| 409 |
} |
| 410 |
{ #2, #3 @ } |
| 411 |
{ \tl_to_str:n {#2} } |
| 412 |
} |
| 413 |
\cs_new:Npn \@@_symbolic_unary_to_tl:NNw #1#2 , #3 @ |
| 414 |
{ |
| 415 |
\use:e |
| 416 |
{ |
| 417 |
\@@_symbolic_cs_arg_to_fn:NN #1#2 |
| 418 |
( \@@_to_tl_dispatch:w #3 ) |
| 419 |
} |
| 420 |
} |
| 421 |
\cs_new:Npn \@@_symbolic_binary_to_tl:Nww #1, #2\@@_sep: #3\@@_sep: @ |
| 422 |
{ |
| 423 |
\use:e |
| 424 |
{ |
| 425 |
( \@@_to_tl_dispatch:w #2\@@_sep: ) |
| 426 |
\@@_types_cs_to_op:N #1 |
| 427 |
( \@@_to_tl_dispatch:w #3\@@_sep: ) |
| 428 |
} |
| 429 |
} |
| 430 |
\cs_new:Npn \@@_symbolic_function_to_tl:Nw #1, #2@ |
| 431 |
{ |
| 432 |
\use:e |
| 433 |
{ |
| 434 |
\@@_types_cs_to_op:N #1 |
| 435 |
( \@@_array_to_clist:n {#2} ) |
| 436 |
} |
| 437 |
} |
| 438 |
% \end{macrocode} |
| 439 |
% \end{macro} |
| 440 |
% \end{macro} |
| 441 |
% |
| 442 |
% \subsection{Identifiers} |
| 443 |
% |
| 444 |
% Functions defined here are not necessarily tied to symbolic |
| 445 |
% expressions. |
| 446 |
% |
| 447 |
% \begin{macro}[TF]{\@@_id_if_invalid:n} |
| 448 |
% \begin{macro}[EXP]{\@@_id_if_invalid_aux:N} |
| 449 |
% If |#1| contains a space, it is not a valid identifier. Otherwise, |
| 450 |
% loop through letters in |#1|: if it is not a letter, break the loop |
| 451 |
% and return \texttt{true}. If the end of the loop is reached |
| 452 |
% without finding any non-letter, return \texttt{false}. |
| 453 |
% Note |#1| must be a str (i.e., resulted from \cs{tl_to_str:n}). |
| 454 |
% \begin{macrocode} |
| 455 |
\prg_new_protected_conditional:Npnn |
| 456 |
\@@_id_if_invalid:n #1 { T , F , TF } |
| 457 |
{ |
| 458 |
\tl_if_empty:nTF {#1} |
| 459 |
{ \prg_return_true: } |
| 460 |
{ |
| 461 |
\tl_if_in:nnTF { #1 } { ~ } |
| 462 |
{ \prg_return_true: } |
| 463 |
{ |
| 464 |
\@@_id_if_invalid_aux:N #1 |
| 465 |
{ ? \prg_break:n \prg_return_false: } |
| 466 |
\prg_break_point: |
| 467 |
} |
| 468 |
} |
| 469 |
} |
| 470 |
\cs_new:Npn \@@_id_if_invalid_aux:N #1 |
| 471 |
{ |
| 472 |
\use_none:n #1 |
| 473 |
\int_compare:nF { `a <= `#1 <= `z } |
| 474 |
{ |
| 475 |
\int_compare:nF { `A <= `#1 <= `Z } |
| 476 |
{ \prg_break:n \prg_return_true: } |
| 477 |
} |
| 478 |
\@@_id_if_invalid_aux:N |
| 479 |
} |
| 480 |
% \end{macrocode} |
| 481 |
% \end{macro} |
| 482 |
% \end{macro} |
| 483 |
% |
| 484 |
% \subsection{Declaring variables and assigning values} |
| 485 |
% |
| 486 |
% \begin{macro}[EXP]{\@@_variable_o:w} |
| 487 |
% We do not use \cs{exp_last_unbraced:Nv} to extract the value of |
| 488 |
% |\l__fp_variable_#1_fp| because in \cs{fp_set_variable:nn} we define |
| 489 |
% this \texttt{fp} variable to be something which \texttt{f}-expands |
| 490 |
% to an actual floating point, rather than a genuine floating point. |
| 491 |
% \begin{macrocode} |
| 492 |
\cs_new:Npn \@@_variable_o:w #1 @ #2 |
| 493 |
{ |
| 494 |
\fp_if_exist:cTF { l_@@_variable_#1_fp } |
| 495 |
{ |
| 496 |
\exp_last_unbraced:Nf \@@_exp_after_array_f:w |
| 497 |
{ \use:c { l_@@_variable_#1_fp } } \s_@@_expr_stop |
| 498 |
\exp_after:wN \exp_stop_f: #2 |
| 499 |
} |
| 500 |
{ |
| 501 |
\token_if_eq_meaning:NNTF #2 \prg_do_nothing: |
| 502 |
{ |
| 503 |
\s_@@_symbolic \@@_symbolic_chk:w |
| 504 |
\@@_variable_o:w #1 , { } \@@_sep: |
| 505 |
} |
| 506 |
{ |
| 507 |
\exp_after:wN \s_@@_symbolic |
| 508 |
\exp_after:wN \@@_symbolic_chk:w |
| 509 |
\exp_after:wN \@@_variable_o:w |
| 510 |
\exp:w |
| 511 |
\@@_exp_after_symbolic_loop:N #1 |
| 512 |
{ , \exp:w \use_none:nn } |
| 513 |
\exp_after:wN \exp_end: |
| 514 |
\exp_after:wN { \exp_after:wN } \exp_after:wN \@@_sep: |
| 515 |
#2 |
| 516 |
} |
| 517 |
} |
| 518 |
} |
| 519 |
% \end{macrocode} |
| 520 |
% \end{macro} |
| 521 |
% |
| 522 |
% \begin{macro} |
| 523 |
% {\@@_variable_set_parsing:Nn, \@@_variable_set_parsing_aux:NNn} |
| 524 |
% \begin{macrocode} |
| 525 |
\cs_new_protected:Npn \@@_variable_set_parsing:Nn #1#2 |
| 526 |
{ |
| 527 |
\cs_set:Npn \@@_tmp:w |
| 528 |
{ |
| 529 |
\@@_exp_after_symbolic_f:nw { \@@_parse_infix:NN } |
| 530 |
\s_@@_symbolic \@@_symbolic_chk:w |
| 531 |
\@@_variable_o:w #2 , { } \@@_sep: |
| 532 |
} |
| 533 |
\exp_args:NNc \@@_variable_set_parsing_aux:NNn #1 |
| 534 |
{ @@_parse_word_#2:N } {#2} |
| 535 |
} |
| 536 |
\cs_new_protected:Npn \@@_variable_set_parsing_aux:NNn #1#2#3 |
| 537 |
{ |
| 538 |
\cs_if_eq:NNF #2 \@@_tmp:w |
| 539 |
{ |
| 540 |
\cs_if_exist:NTF #2 |
| 541 |
{ |
| 542 |
\msg_warning:nnnn |
| 543 |
{ fp } { id-used-elsewhere } {#3} { variable } |
| 544 |
#1 #2 \@@_tmp:w |
| 545 |
} |
| 546 |
{ |
| 547 |
\cs_new_eq:NN #2 \scan_stop: % to declare the function |
| 548 |
#1 #2 \@@_tmp:w |
| 549 |
} |
| 550 |
} |
| 551 |
} |
| 552 |
% \end{macrocode} |
| 553 |
% \end{macro} |
| 554 |
% |
| 555 |
% \begin{macro} |
| 556 |
% { |
| 557 |
% \fp_clear_variable:n, |
| 558 |
% \@@_clear_variable:n, \@@_clear_variable_aux:n |
| 559 |
% } |
| 560 |
% We need local undefining, so have to do it low-level. |
| 561 |
% \cs{@@_clear_variable_aux:n} is needed by \cs{@@_set_function:Nnnn} |
| 562 |
% to skip \cs{@@_id_if_invalid:nTF}. |
| 563 |
% \begin{macrocode} |
| 564 |
\cs_new_protected:Npn \fp_clear_variable:n #1 |
| 565 |
{ |
| 566 |
\exp_args:No \@@_clear_variable:n { \tl_to_str:n {#1} } |
| 567 |
} |
| 568 |
\cs_new_protected:Npn \@@_clear_variable:n #1 |
| 569 |
{ |
| 570 |
\@@_id_if_invalid:nTF {#1} |
| 571 |
{ \msg_error:nnn { fp } { id-invalid } {#1} } |
| 572 |
{ \@@_clear_variable_aux:n {#1} } |
| 573 |
} |
| 574 |
\cs_new_protected:Npn \@@_clear_variable_aux:n #1 |
| 575 |
{ |
| 576 |
\cs_set_eq:cN { l_@@_variable_#1_fp } \tex_undefined:D |
| 577 |
\@@_variable_set_parsing:Nn \cs_set_eq:NN {#1} |
| 578 |
} |
| 579 |
% \end{macrocode} |
| 580 |
% \end{macro} |
| 581 |
% |
| 582 |
% \begin{macro}{\fp_new_variable:n} |
| 583 |
% \begin{macro}{\@@_new_variable:n} |
| 584 |
% Check that |#1| is a valid identifier. If the identifier is already |
| 585 |
% in use, complain. Then set |\__fp_parse_word_#1:N| to use |
| 586 |
% |\__fp_variable_o:w|. |
| 587 |
% \begin{macrocode} |
| 588 |
\cs_new_protected:Npn \fp_new_variable:n #1 |
| 589 |
{ |
| 590 |
\exp_args:No \@@_new_variable:n { \tl_to_str:n {#1} } |
| 591 |
} |
| 592 |
\cs_new_protected:Npn \@@_new_variable:n #1 |
| 593 |
{ |
| 594 |
\@@_id_if_invalid:nTF {#1} |
| 595 |
{ \msg_error:nnn { fp } { id-invalid } {#1} } |
| 596 |
{ |
| 597 |
\cs_if_exist:cT { @@_parse_word_#1:N } |
| 598 |
{ |
| 599 |
\msg_error:nnn |
| 600 |
{ fp } { id-already-defined } {#1} |
| 601 |
\cs_undefine:c { @@_parse_word_#1:N } |
| 602 |
\cs_set_eq:cN { l_@@_variable_#1_fp } \tex_undefined:D |
| 603 |
} |
| 604 |
\@@_variable_set_parsing:Nn \cs_gset_eq:NN {#1} |
| 605 |
} |
| 606 |
} |
| 607 |
% \end{macrocode} |
| 608 |
% \end{macro} |
| 609 |
% \end{macro} |
| 610 |
% |
| 611 |
% \begin{variable}{\l_@@_symbolic_flag} |
| 612 |
% \begin{macro}{\fp_set_variable:nn, \@@_set_variable:nn} |
| 613 |
% Refuse invalid identifiers. If the variable does not exist yet, |
| 614 |
% raise an error and define it just as in \cs{fp_new_variable:n} |
| 615 |
% (but without unnecessary checks). |
| 616 |
% Then evaluate~|#2|. If the result contains the |
| 617 |
% identifier~|#1|, we would later get a loop in cases such as |
| 618 |
% \begin{quote} |
| 619 |
% \cs{fp_set_variable:nn} |{A}| |{A}|\\ |
| 620 |
% \cs{fp_show:n} |{A}| |
| 621 |
% \end{quote} |
| 622 |
% To detect this, define |\l__fp_variable_#1_fp| to raise an |
| 623 |
% internal flag and evaluate to \texttt{nan}. Then re-evaluate |
| 624 |
% \cs{l_@@_symbolic_fp}, and store the result in~|#1|. If the flag is |
| 625 |
% raised, |#1|~was present in \cs{l_@@_symbolic_fp}. In all cases, |
| 626 |
% the |#1|-free result ends up in |\l__fp_variable_#1_fp|. |
| 627 |
% \begin{macrocode} |
| 628 |
\flag_new:N \l_@@_symbolic_flag |
| 629 |
\cs_new_protected:Npn \fp_set_variable:nn #1 |
| 630 |
{ |
| 631 |
\exp_args:No \@@_set_variable:nn { \tl_to_str:n {#1} } |
| 632 |
} |
| 633 |
\cs_new_protected:Npn \@@_set_variable:nn #1#2 |
| 634 |
{ |
| 635 |
\@@_id_if_invalid:nTF {#1} |
| 636 |
{ \msg_error:nnn { fp } { id-invalid } {#1} } |
| 637 |
{ |
| 638 |
\cs_if_exist:cF { @@_parse_word_#1:N } |
| 639 |
{ |
| 640 |
\msg_error:nnn {fp} { id-undefined } {#1} |
| 641 |
\@@_variable_set_parsing:Nn \cs_set_eq:NN {#1} |
| 642 |
} |
| 643 |
\fp_set:Nn \l_@@_symbolic_fp {#2} |
| 644 |
\cs_set_nopar:cpn { l_@@_variable_#1_fp } |
| 645 |
{ \flag_ensure_raised:N \l_@@_symbolic_flag \c_nan_fp } |
| 646 |
\flag_clear:N \l_@@_symbolic_flag |
| 647 |
\fp_set:cn { l_@@_variable_#1_fp } { \l_@@_symbolic_fp } |
| 648 |
\flag_if_raised:NT \l_@@_symbolic_flag |
| 649 |
{ |
| 650 |
\msg_error:nneee { fp } { id-loop } |
| 651 |
{ #1 } |
| 652 |
{ \tl_to_str:n {#2} } |
| 653 |
{ \fp_to_tl:N \l_@@_symbolic_fp } |
| 654 |
} |
| 655 |
} |
| 656 |
} |
| 657 |
% \end{macrocode} |
| 658 |
% \end{macro} |
| 659 |
% \end{variable} |
| 660 |
% |
| 661 |
% \subsection{Messages} |
| 662 |
% |
| 663 |
% \begin{macrocode} |
| 664 |
\msg_new:nnnn { fp } { id-invalid } |
| 665 |
{ Floating~point~identifier~'#1'~invalid. } |
| 666 |
{ |
| 667 |
LaTeX~has~been~asked~to~create~a~new~floating~point~identifier~'#1'~ |
| 668 |
but~this~may~only~contain~ASCII~letters. |
| 669 |
} |
| 670 |
\msg_new:nnnn { fp } { id-already-defined } |
| 671 |
{ Floating~point~identifier~'#1'~already~defined. } |
| 672 |
{ |
| 673 |
LaTeX~has~been~asked~to~create~a~new~floating~point~identifier~'#1'~ |
| 674 |
but~this~name~has~already~been~used~elsewhere. |
| 675 |
} |
| 676 |
\msg_new:nnnn { fp } { id-undefined } |
| 677 |
{ Floating~point~identifier~'#1'~is~undefined. } |
| 678 |
{ |
| 679 |
LaTeX~has~been~asked~to~set~a~floating~point~identifier~'#1'~ |
| 680 |
but~this~name~has~not~been~declared. |
| 681 |
} |
| 682 |
\msg_new:nnnn { fp } { id-used-elsewhere } |
| 683 |
{ Floating~point~identifier~'#1'~already~used~for~something~else. } |
| 684 |
{ |
| 685 |
LaTeX~has~been~asked~to~create~a~new~floating~point~identifier~'#1'~ |
| 686 |
but~this~name~is~used,~and~is~not~a~user-defined~#2. |
| 687 |
} |
| 688 |
\msg_new:nnnn { fp } { id-loop } |
| 689 |
{ Variable~'#1'~used~in~the~definition~of~'#1'. } |
| 690 |
{ |
| 691 |
LaTeX~has~been~asked~to~set~the~floating~point~identifier~'#1'~ |
| 692 |
to~the~expression~'#2'.~Evaluating~this~expression~yields~'#3',~ |
| 693 |
which~contains~'#1'~itself. |
| 694 |
} |
| 695 |
% \end{macrocode} |
| 696 |
% |
| 697 |
% \subsection{Road-map} |
| 698 |
% |
| 699 |
% The following functions are not implemented: |min|, |max|, |?:|, |
| 700 |
% comparisons, |round|, |atan|, |acot|. |
| 701 |
% |
| 702 |
% \begin{macrocode} |
| 703 |
%</code> |
| 704 |
% \end{macrocode} |
| 705 |
% |
| 706 |
% \end{implementation} |
| 707 |
% |
| 708 |
% \PrintIndex |