Debian Codegraph @

texmf-dist/source/latex/l3kernel/l3fparray.dtx

raw
1 % \iffalse meta-comment
2 %
3 %% File: l3fparray.dtx
4 %
5 % Copyright (C) 2018-2026 The LaTeX Project
6 %
7 % It may be distributed and/or modified under the conditions of the
8 % LaTeX Project Public License (LPPL), either version 1.3c of this
9 % license or (at your option) any later version. The latest version
10 % of this license is in the file
11 %
12 % https://www.latex-project.org/lppl.txt
13 %
14 % This file is part of the "l3kernel bundle" (The Work in LPPL)
15 % and all files in that bundle must be distributed together.
16 %
17 % -----------------------------------------------------------------------
18 %
19 % The development version of the bundle can be found at
20 %
21 % https://github.com/latex3/latex3
22 %
23 % for those people who are interested.
24 %
25 %<*driver>
26 \documentclass[full,kernel]{l3doc}
27 \begin{document}
28 \DocInput{\jobname.dtx}
29 \end{document}
30 %</driver>
31 % \fi
32 %
33 %
34 % \title{^^A
35 % The \pkg{l3fparray} module\\ Fast global floating point arrays^^A
36 % }
37 %
38 % \author{^^A
39 % The \LaTeX{} Project\thanks
40 % {^^A
41 % E-mail:
42 % \href{mailto:[email protected]}
43 % {[email protected]}^^A
44 % }^^A
45 % }
46 %
47 % \date{Released 2026-05-15}
48 %
49 % \maketitle
50 %
51 % \begin{documentation}
52 %
53 % For applications requiring heavy use of floating points, this module
54 % provides arrays which can be accessed in constant time (contrast
55 % \pkg{l3seq}, where access time is linear). The interface is very
56 % close to that of \pkg{l3intarray}. The size of the array is fixed and
57 % must be given at point of initialization
58 %
59 % \section{Creating and initializing floating point array variables}
60 %
61 % \begin{function}[added = 2018-05-05]{\fparray_new:Nn, \fparray_new:cn}
62 % \begin{syntax}
63 % \cs{fparray_new:Nn} \meta{fparray~var} \Arg{size}
64 % \end{syntax}
65 % Evaluates the integer expression \meta{size} and allocates an
66 % \meta{fparray var} with that number of (zero)
67 % entries. The variable name should start with |\g_| because
68 % assignments are always global.
69 % \end{function}
70 %
71 % \begin{function}[added = 2018-05-05]{\fparray_gzero:N, \fparray_gzero:c}
72 % \begin{syntax}
73 % \cs{fparray_gzero:N} \meta{fparray~var}
74 % \end{syntax}
75 % Sets all entries of the \meta{fparray var} to
76 % $+0$. Assignments are always global.
77 % \end{function}
78 %
79 % \section{Adding data to floating point arrays}
80 %
81 % \begin{function}[added = 2018-05-05]{\fparray_gset:Nnn, \fparray_gset:cnn}
82 % \begin{syntax}
83 % \cs{fparray_gset:Nnn} \meta{fparray~var} \Arg{position} \Arg{value}
84 % \end{syntax}
85 % Stores the result of evaluating the floating point expression
86 % \meta{value} into the \meta{fparray var} at the
87 % (integer expression) \meta{position}. If the \meta{position} is not
88 % between $1$ and the \cs{fparray_count:N}, an error occurs.
89 % Assignments are always global.
90 % \end{function}
91 %
92 % \section{Counting entries in floating point arrays}
93 %
94 % \begin{function}[EXP, added = 2018-05-05]{\fparray_count:N, \fparray_count:c}
95 % \begin{syntax}
96 % \cs{fparray_count:N} \meta{fparray~var}
97 % \end{syntax}
98 % Expands to the number of entries in the \meta{fparray
99 % var}. This is performed in constant time.
100 % \end{function}
101 %
102 % \section{Using a single entry}
103 %
104 % \begin{function}[EXP, added = 2018-05-05]
105 % {
106 % \fparray_item:Nn, \fparray_item:cn,
107 % \fparray_item_to_tl:Nn, \fparray_item_to_tl:cn
108 % }
109 % \begin{syntax}
110 % \cs{fparray_item:Nn} \meta{fparray~var} \Arg{position}
111 % \end{syntax}
112 % Applies \cs{fp_use:N} or \cs{fp_to_tl:N} (respectively) to the
113 % floating point entry stored at the (integer expression)
114 % \meta{position} in the \meta{fparray var}. If the
115 % \meta{position} is not between $1$ and the
116 % \cs{fparray_count:N} \meta{fparray~var}, an error occurs.
117 % \end{function}
118 %
119 % \section{Floating point array conditional}
120 %
121 % \begin{function}[pTF, added = 2024-03-31]
122 % {\fparray_if_exist:N, \fparray_if_exist:c}
123 % \begin{syntax}
124 % \cs{fparray_if_exist_p:N} \meta{fparray~var}
125 % \cs{fparray_if_exist:NTF} \meta{fparray~var} \Arg{true code} \Arg{false code}
126 % \end{syntax}
127 % Tests whether the \meta{fparray~var} is currently defined. This
128 % does not check that the \meta{fparray~var} really is a floating
129 % point array variable.
130 % \end{function}
131 %
132 % \end{documentation}
133 %
134 % \begin{implementation}
135 %
136 % \section{\pkg{l3fparray} implementation}
137 %
138 % \begin{macrocode}
139 %<*code>
140 % \end{macrocode}
141 %
142 % \begin{macrocode}
143 %<@@=fp>
144 % \end{macrocode}
145 %
146 % In analogy to \pkg{l3intarray} it would make sense to have
147 % |<@@=fparray>|, but we need direct access to \cs{@@_parse:n} from
148 % \pkg{l3fp-parse}, and a few other (less crucial) internals of the
149 % \pkg{l3fp} family.
150 %
151 % \subsection{Allocating arrays}
152 %
153 % There are somewhat more than $(2^{31}-1)^2$ floating point numbers so
154 % we store each floating point number as three entries in integer
155 % arrays. To avoid having to multiply indices by three or to add 1 etc,
156 % a floating point array is just a token list consisting of three
157 % tokens: integer arrays of the same size.
158 %
159 % \begin{variable}{\g_@@_array_int}
160 % Used to generate unique names for the three integer arrays.
161 % \begin{macrocode}
162 \int_new:N \g_@@_array_int
163 % \end{macrocode}
164 % \end{variable}
165 %
166 % \begin{variable}{\l_@@_array_loop_int}
167 % Used to loop in \cs{@@_array_gzero:N}.
168 % \begin{macrocode}
169 \int_new:N \l_@@_array_loop_int
170 % \end{macrocode}
171 % \end{variable}
172 %
173 % \begin{macro}{\fparray_new:Nn, \fparray_new:cn}
174 % \begin{macro}{\@@_array_new:nNNN}
175 % Build a three-token token list, then define all three tokens to be
176 % integer arrays of the same size. No need to initialize the data:
177 % the integer arrays start with zeros, and three zeros denote
178 % precisely \cs{c_zero_fp}, as we want.
179 % \begin{macrocode}
180 \cs_new_protected:Npn \fparray_new:Nn #1#2
181 {
182 \tl_new:N #1
183 \prg_replicate:nn { 3 }
184 {
185 \int_gincr:N \g_@@_array_int
186 \exp_args:NNc \tl_gput_right:Nn #1
187 { g_@@_array_ \@@_int_to_roman:w \g_@@_array_int _intarray }
188 }
189 \exp_last_unbraced:Nfo \@@_array_new:nNNNN
190 { \int_eval:n {#2} } #1 #1
191 }
192 \cs_generate_variant:Nn \fparray_new:Nn { c }
193 \cs_new_protected:Npn \@@_array_new:nNNNN #1#2#3#4#5
194 {
195 \int_compare:nNnTF {#1} < 0
196 {
197 \msg_error:nnn { kernel } { negative-array-size } {#1}
198 \cs_undefine:N #1
199 \int_gsub:Nn \g_@@_array_int { 3 }
200 }
201 {
202 \intarray_new:Nn #2 {#1}
203 \intarray_new:Nn #3 {#1}
204 \intarray_new:Nn #4 {#1}
205 }
206 }
207 % \end{macrocode}
208 % \end{macro}
209 % \end{macro}
210 %
211 % \begin{macro}[EXP]{\fparray_count:N, \fparray_count:c}
212 % Size of any of the intarrays, here we pick the third.
213 % \begin{macrocode}
214 \cs_new:Npn \fparray_count:N #1
215 {
216 \exp_after:wN \use_i:nnn
217 \exp_after:wN \intarray_count:N #1
218 }
219 \cs_generate_variant:Nn \fparray_count:N { c }
220 % \end{macrocode}
221 % \end{macro}
222 %
223 % \subsection{Array items}
224 %
225 % \begin{macro}[EXP]{\@@_array_bounds:NNnTF, \@@_array_bounds_error:NNn}
226 % See the \pkg{l3intarray} analogue: only names change.
227 % The functions \cs{fparray_gset:Nnn} and \cs{fparray_item:Nn} share
228 % bounds checking. The |T| branch is used if |#3| is within bounds of
229 % the array |#2|.
230 % \begin{macrocode}
231 \cs_new:Npn \@@_array_bounds:NNnTF #1#2#3#4#5
232 {
233 \if_int_compare:w 1 > #3 \exp_stop_f:
234 \@@_array_bounds_error:NNn #1 #2 {#3}
235 #5
236 \else:
237 \if_int_compare:w #3 > \fparray_count:N #2 \exp_stop_f:
238 \@@_array_bounds_error:NNn #1 #2 {#3}
239 #5
240 \else:
241 #4
242 \fi:
243 \fi:
244 }
245 \cs_new:Npn \@@_array_bounds_error:NNn #1#2#3
246 {
247 #1 { kernel } { out-of-bounds }
248 { \token_to_str:N #2 } {#3} { \fparray_count:N #2 }
249 }
250 % \end{macrocode}
251 % \end{macro}
252 %
253 % \begin{macro}{\fparray_gset:Nnn, \fparray_gset:cnn}
254 % \begin{macro}
255 % {
256 % \@@_array_gset:NNNNww, \@@_array_gset:w,
257 % \@@_array_gset_recover:Nw, \@@_array_gset_special:nnNNN,
258 % \@@_array_gset_normal:w
259 % }
260 % Evaluate, then store exponent in one intarray, sign and $8$ digits
261 % of mantissa in the next, and $8$ trailing digits in the last.
262 % \begin{macrocode}
263 \cs_new_protected:Npn \fparray_gset:Nnn #1#2#3
264 {
265 \exp_after:wN \exp_after:wN
266 \exp_after:wN \@@_array_gset:NNNNww
267 \exp_after:wN #1
268 \exp_after:wN #1
269 \int_value:w \int_eval:n {#2} \exp_after:wN \@@_sep:
270 \exp:w \exp_end_continue_f:w \@@_parse:n {#3}
271 }
272 \cs_generate_variant:Nn \fparray_gset:Nnn { c }
273 \cs_new_protected:Npn \@@_array_gset:NNNNww #1#2#3#4#5 \@@_sep: #6 \@@_sep:
274 {
275 \@@_array_bounds:NNnTF \msg_error:nneee #4 {#5}
276 {
277 \exp_after:wN \@@_change_func_type:NNN
278 \@@_use_i_until_s:nw #6 \@@_sep:
279 \@@_array_gset:w
280 \@@_array_gset_recover:Nw
281 #6 \@@_sep: {#5} #1 #2 #3
282 }
283 { }
284 }
285 \cs_new_protected:Npn \@@_array_gset_recover:Nw #1#2 \@@_sep:
286 {
287 \@@_error:nffn { unknown-type } { \tl_to_str:n { #2 \@@_sep: } } { } { }
288 \exp_after:wN #1 \c_nan_fp
289 }
290 \cs_new_protected:Npn \@@_array_gset:w \s_@@ \@@_chk:w #1#2
291 {
292 \if_case:w #1 \exp_stop_f:
293 \@@_case_return:nw { \@@_array_gset_special:nnNNN {#2} }
294 \or: \exp_after:wN \@@_array_gset_normal:w
295 \or: \@@_case_return:nw { \@@_array_gset_special:nnNNN { #2 3 } }
296 \or: \@@_case_return:nw { \@@_array_gset_special:nnNNN { 1 } }
297 \fi:
298 \s_@@ \@@_chk:w #1 #2
299 }
300 \cs_new_protected:Npn \@@_array_gset_normal:w
301 \s_@@ \@@_chk:w 1 #1 #2 #3#4#5 \@@_sep: #6#7#8#9
302 {
303 \__kernel_intarray_gset:Nnn #7 {#6} {#2}
304 \__kernel_intarray_gset:Nnn #8 {#6}
305 { \if_meaning:w 2 #1 3 \else: 1 \fi: #3#4 }
306 \__kernel_intarray_gset:Nnn #9 {#6} { 1 \use:nn #5 }
307 }
308 \cs_new_protected:Npn \@@_array_gset_special:nnNNN #1#2#3#4#5
309 {
310 \__kernel_intarray_gset:Nnn #3 {#2} {#1}
311 \__kernel_intarray_gset:Nnn #4 {#2} {0}
312 \__kernel_intarray_gset:Nnn #5 {#2} {0}
313 }
314 % \end{macrocode}
315 % \end{macro}
316 % \end{macro}
317 %
318 % \begin{macro}{\fparray_gzero:N, \fparray_gzero:c}
319 % \begin{macrocode}
320 \cs_new_protected:Npn \fparray_gzero:N #1
321 {
322 \int_zero:N \l_@@_array_loop_int
323 \prg_replicate:nn { \fparray_count:N #1 }
324 {
325 \int_incr:N \l_@@_array_loop_int
326 \exp_after:wN \@@_array_gset_special:nnNNN
327 \exp_after:wN 0
328 \exp_after:wN \l_@@_array_loop_int
329 #1
330 }
331 }
332 \cs_generate_variant:Nn \fparray_gzero:N { c }
333 % \end{macrocode}
334 % \end{macro}
335 %
336 % \begin{macro}[EXP]
337 % {
338 % \fparray_item:Nn,
339 % \fparray_item:cn,
340 % \fparray_item_to_tl:Nn,
341 % \fparray_item_to_tl:cn,
342 % }
343 % \begin{macro}[EXP]
344 % {
345 % \@@_array_item:NwN,
346 % \@@_array_item:NNNnN,
347 % \@@_array_item:N,
348 % \@@_array_item:w,
349 % \@@_array_item_special:w,
350 % \@@_array_item_normal:w
351 % }
352 % \begin{macrocode}
353 \cs_new:Npn \fparray_item:Nn #1#2
354 {
355 \exp_after:wN \@@_array_item:NwN
356 \exp_after:wN #1
357 \int_value:w \int_eval:n {#2} \@@_sep:
358 \@@_to_decimal:w
359 }
360 \cs_generate_variant:Nn \fparray_item:Nn { c }
361 \cs_new:Npn \fparray_item_to_tl:Nn #1#2
362 {
363 \exp_after:wN \@@_array_item:NwN
364 \exp_after:wN #1
365 \int_value:w \int_eval:n {#2} \@@_sep:
366 \@@_to_tl:w
367 }
368 \cs_generate_variant:Nn \fparray_item_to_tl:Nn { c }
369 \cs_new:Npn \@@_array_item:NwN #1#2 \@@_sep: #3
370 {
371 \@@_array_bounds:NNnTF \msg_expandable_error:nnfff #1 {#2}
372 { \exp_after:wN \@@_array_item:NNNnN #1 {#2} #3 }
373 { \exp_after:wN #3 \c_nan_fp }
374 }
375 \cs_new:Npn \@@_array_item:NNNnN #1#2#3#4
376 {
377 \exp_after:wN \@@_array_item:N
378 \int_value:w \__kernel_intarray_item:Nn #2 {#4} \exp_after:wN \@@_sep:
379 \int_value:w \__kernel_intarray_item:Nn #3 {#4} \exp_after:wN \@@_sep:
380 \int_value:w \__kernel_intarray_item:Nn #1 {#4} \@@_sep:
381 }
382 \cs_new:Npn \@@_array_item:N #1
383 {
384 \if_meaning:w 0 #1 \exp_after:wN \@@_array_item_special:w \fi:
385 \@@_array_item:w #1
386 }
387 \cs_new:Npn \@@_array_item:w #1 #2#3#4#5 #6 \@@_sep: 1 #7 \@@_sep:
388 {
389 \exp_after:wN \@@_array_item_normal:w
390 \int_value:w \if_meaning:w #1 1 0 \else: 2 \fi: \exp_stop_f:
391 #7 \@@_sep: {#2#3#4#5} {#6} \@@_sep:
392 }
393 \cs_new:Npn \@@_array_item_special:w #1 \@@_sep: #2 \@@_sep: #3 \@@_sep: #4
394 {
395 \exp_after:wN #4
396 \exp:w \exp_end_continue_f:w
397 \if_case:w #3 \exp_stop_f:
398 \exp_after:wN \c_zero_fp
399 \or: \exp_after:wN \c_nan_fp
400 \or: \exp_after:wN \c_minus_zero_fp
401 \or: \exp_after:wN \c_inf_fp
402 \else: \exp_after:wN \c_minus_inf_fp
403 \fi:
404 }
405 \cs_new:Npn \@@_array_item_normal:w
406 #1 #2#3#4#5 #6 \@@_sep: #7 \@@_sep: #8 \@@_sep: #9
407 { #9 \s_@@ \@@_chk:w 1 #1 {#8} #7 {#2#3#4#5} {#6} \@@_sep: }
408 % \end{macrocode}
409 % \end{macro}
410 % \end{macro}
411 %
412 % \begin{macro}[pTF]{\fparray_if_exist:N, \fparray_if_exist:c}
413 % Copies of the \texttt{cs} functions defined in \pkg{l3basics}.
414 % \begin{macrocode}
415 \prg_new_eq_conditional:NNn \fparray_if_exist:N \cs_if_exist:N
416 { TF , T , F , p }
417 \prg_new_eq_conditional:NNn \fparray_if_exist:c \cs_if_exist:c
418 { TF , T , F , p }
419 % \end{macrocode}
420 % \end{macro}
421 %
422 % \begin{macrocode}
423 %</code>
424 % \end{macrocode}
425 %
426 % \end{implementation}
427 %
428 % \PrintIndex