Debian Codegraph @

texmf-dist/source/latex/randomwalk/randomwalk.dtx

raw
1 % \iffalse
2 %% File: randomwalk.dtx Copyright (C) 2011-2018 Bruno Le Floch
3 %%
4 %% This work may be distributed and/or modified under the
5 %% conditions of the LaTeX Project Public License, either version 1.3c
6 %% of this license or (at your option) any later version.
7 %% The latest version of this license is in
8 %% http://www.latex-project.org/lppl.txt
9 %%
10 %% This work has the LPPL maintenance status 'maintained'
11 %% and the current maintainer is Bruno Le Floch.
12 %%
13 %% This work consists of the files randomwalk.dtx and randomwalk.ins and
14 %% derived files randomwalk.sty and randomwalk.pdf.
15 %% -----------------------------------------------------------------------
16 %
17 %<*driver|package>
18 %</driver|package>
19 %<*driver>
20 \RequirePackage{expl3}
21 \documentclass[full]{l3doc}
22 \usepackage{randomwalk}
23 \AtBeginDocument{\chgrand[seed = 411500]\rand}
24 \usepackage{amsmath}
25 \begin{document}
26 \DocInput{randomwalk.dtx}
27 \end{document}
28 %</driver>
29 % \fi
30 %
31 %^^A The date here also is in \ProvidesExplPackage, and in the copyright.
32 % \def\fileversion{v0.6}
33 % \def\filedate{2018/12/28}
34 %
35 % \title{The \pkg{randomwalk} package: \\
36 % customizable random walks\thanks{This file describes version
37 % \fileversion, last revised \filedate.}}
38 % \author{Bruno Le Floch\thanks{E-mail [email protected]}}
39 % \date{Released on \filedate}
40 %
41 % \maketitle
42 % \tableofcontents
43 %
44 % \begin{documentation}
45 %
46 % \begin{abstract}
47 %
48 % The \pkg{randomwalk} package draws random walks. The
49 % following parameters can be customized:
50 % \begin{itemize}
51 % \item The number of steps, of course.
52 % \item The length of the steps, either a fixed length, or a length
53 % taken uniformly at random from a given list.
54 % \item The angle of each step, either taken uniformly at random
55 % from a given list, or uniformly distributed between $0$~and
56 % $360$ degrees.
57 % \end{itemize}
58 %
59 % \end{abstract}
60 %
61 %
62 % \section{How to use \pkg{randomwalk}}
63 %
64 % \newcommand{\examplei}
65 % {\RandomWalk {number = 200, length = {4pt, 10pt}}}
66 % \newcommand{\exampleii}
67 % {\RandomWalk {number = 100, angles = {0,60,120,180,240,300}, degree}}
68 % \newcommand{\exampleiii}
69 % {\RandomWalk {number = 50, length = 1ex, angles = {0,24,48,-24,-48}, degree, angles-relative}}
70 % \begin{function}{\RandomWalk}
71 % The \pkg{randomwalk} package has a single user command:
72 % \cs{RandomWalk}, which takes a list of key-value pairs as its
73 % argument. A few examples are given in Figures~\ref{examplei}, \ref{exampleii}, and~\ref{exampleiii}:
74 % \begin{quote}\ttfamily
75 % \detokenize\expandafter{\examplei}\\
76 % \detokenize\expandafter{\exampleii}\\
77 % \detokenize\expandafter{\exampleiii}
78 % \end{quote}
79 % Here is a list of all the keys, and their meaning:
80 % \begin{itemize}
81 % \item \texttt{number}: the number of steps (default \(10\))
82 % \item \texttt{length}: the length of each step: either one dimension
83 % (\emph{e.g.}, |1ex|), or a comma-separated list of dimensions
84 % (\emph{e.g.}, |{2pt, 5pt}|), by default |10pt|. The length of each
85 % step is a (uniformly distributed) random element in this set of
86 % possible dimensions.
87 % \item \texttt{angles}: the polar angle for each step: a
88 % comma-separated list of angles, and each step takes a random angle
89 % in the list. If this is not specified, then the angle is
90 % uniformly distributed along the circle.
91 % \item \texttt{degree} or \texttt{degrees}: specify that the angles
92 % are given in degrees (by default, they are in radians).
93 % \item \texttt{angles-relative}: instead of being absolute, the
94 % angles are relative to the direction of the previous step.
95 % \item \texttt{revert-random} (boolean, false by default): revert the
96 % seed of the random number generator to its original value after
97 % the random walk.
98 % \end{itemize}
99 % \end{function}
100 %
101 % \begin{figure}
102 % \begin{center}
103 % \framebox{\examplei}
104 % \caption{\label{examplei}A \(200\) steps long
105 % walk, where each step has one of two lengths:
106 % \texttt{\detokenize\expandafter{\examplei}}}
107 % \end{center}
108 % \end{figure}
109 %
110 % \begin{figure}
111 % \begin{center}
112 % \framebox{\exampleii}
113 % \caption{\label{exampleii}A walk with constrained angles:
114 % \texttt{\detokenize\expandafter{\exampleii}}}
115 % \end{center}
116 % \end{figure}
117 %
118 % \begin{figure}
119 % \begin{center}
120 % \framebox{\exampleiii}
121 % \caption{\label{exampleiii}A last example, with small relative
122 % angles: \texttt{\detokenize\expandafter{\exampleiii}}}
123 % \end{center}
124 % \end{figure}
125 %
126 % \end{documentation}
127 %
128 % \begin{implementation}
129 %
130 % \section{\pkg{randomwalk} implementation}
131 %
132 % \subsection{Packages}
133 %
134 % The \pkg{expl3} bundle is loaded first.
135 %
136 %<*package>
137 % \begin{macrocode}
138 %<@@=randomwalk>
139 % \end{macrocode}
140 %
141 % \begin{macrocode}
142 \RequirePackage{expl3}[2017/11/14]
143 \ProvidesExplPackage
144 {randomwalk} {2018/12/28} {0.6} {Customizable random walks}
145 \RequirePackage{xparse}[2017/11/14]
146 % \end{macrocode}
147 %
148 % Load \pkg{pgfcore} for figures.
149 % \begin{macrocode}
150 \RequirePackage{pgfcore}
151 % \end{macrocode}
152 %
153 % Load \pkg{lcg} for random numbers.
154 % It needs to know the smallest and biggest random numbers that
155 % should be produced, which we take to be $0$ and $\cs{c_@@_lcg_last_int}
156 % = 2^{31}-2$. It will then store them in \cs{c@lcg@rand}: the |\c@| is
157 % there because of how \LaTeXe{} defines counters. To make it clear that
158 % |\c| has a very special meaning here, I do not follow \LaTeX3 naming
159 % conventions. Also of note is that I use \cs{cr@nd} in \cs{@@_walk:}.
160 %
161 % It seems that the \pkg{lcg} package has to be loaded after the
162 % document class, hence we do it \cs{AtBeginDocument}. Also worth noting
163 % is the call to \cs{rand}, which avoids some very odd bug.
164 % \begin{macrocode}
165 \int_const:Nn \c_@@_lcg_last_int { \c_max_int - 1 }
166 \AtBeginDocument
167 {
168 \RequirePackage
169 [
170 first= 0 ,
171 last = \c_@@_lcg_last_int ,
172 counter = lcg@rand
173 ]
174 { lcg }
175 \rand
176 }
177 % \end{macrocode}
178 %
179 % \subsection{Variables}
180 %
181 % \begin{variable}{\l_@@_internal_tl, \l_@@_internal_int}
182 % Used for scratch assignments.
183 % \begin{macrocode}
184 \tl_new:N \l_@@_internal_tl
185 \int_new:N \l_@@_internal_int
186 % \end{macrocode}
187 % \end{variable}
188 %
189 % \begin{variable}{\l_@@_step_number_int}
190 % The number of steps requested by the caller.
191 % \begin{macrocode}
192 \int_new:N \l_@@_step_number_int
193 % \end{macrocode}
194 % \end{variable}
195 %
196 % \begin{variable}{\l_@@_relative_angles_bool, \l_@@_degrees_bool}
197 % Booleans for whether angles are relative (keyval option),
198 % and whether they are in degrees.
199 % \begin{macrocode}
200 \bool_new:N \l_@@_relative_angles_bool
201 \bool_new:N \l_@@_degrees_bool
202 % \end{macrocode}
203 % \end{variable}
204 %
205 % \begin{variable}{\l_@@_revert_random_bool}
206 % Booleans for whether to revert the random seed to its original value
207 % or keep the last value reached at the end of a random path.
208 % \begin{macrocode}
209 \bool_new:N \l_@@_revert_random_bool
210 % \end{macrocode}
211 % \end{variable}
212 %
213 % \begin{macro}{\@@_next_angle:, \@@_next_length:}
214 % Set the \cs{l_@@_angle_fp} and \cs{l_@@_length_fp} of the next step,
215 % most often randomly.
216 % \begin{macrocode}
217 \cs_new_protected:Npn \@@_next_angle: { }
218 \cs_new_protected:Npn \@@_next_length: { }
219 % \end{macrocode}
220 % \end{macro}
221 %
222 % \begin{variable}{\l_@@_angle_fp, \l_@@_length_fp}
223 % Angle and length of the next step.
224 % \begin{macrocode}
225 \fp_new:N \l_@@_angle_fp
226 \fp_new:N \l_@@_length_fp
227 % \end{macrocode}
228 % \end{variable}
229 %
230 % \begin{variable}{\l_@@_x_dim, \l_@@_y_dim}
231 % Current coordinates: each \cs{pgfpathlineto} statement
232 % goes from the previous value of these to the next. See
233 % \cs{@@_walk_step:}.
234 % \begin{macrocode}
235 \dim_new:N \l_@@_x_dim
236 \dim_new:N \l_@@_y_dim
237 % \end{macrocode}
238 % \end{variable}
239 %
240 % \begin{variable}{\l_@@_angles_seq, \l_@@_lengths_seq}
241 % Sequences containing all allowed angles and lengths, as
242 % floating point numbers.
243 % \begin{macrocode}
244 \seq_new:N \l_@@_angles_seq
245 \seq_new:N \l_@@_lengths_seq
246 % \end{macrocode}
247 % \end{variable}
248 %
249 % \subsection{User command and key-value list}
250 %
251 % \begin{macro}{\RandomWalk, \randomwalk:n}
252 % The user command \cs{RandomWalk} is based on the code-level
253 % command \cs{randomwalk:n}, which simply does the setup and calls
254 % the internal macro \cs{@@_walk:}.
255 % \begin{macrocode}
256 \DeclareDocumentCommand \RandomWalk { m }
257 { \randomwalk:n {#1} }
258 \cs_new_protected:Npn \randomwalk:n #1
259 {
260 \@@_setup_defaults:
261 \keys_set:nn { randomwalk } {#1}
262 \@@_walk:
263 }
264 % \end{macrocode}
265 % \end{macro}
266 %
267 % We introduce the keys for the package.
268 % \begin{macrocode}
269 \keys_define:nn { randomwalk }
270 {
271 number .value_required:n = true ,
272 length .value_required:n = true ,
273 angles .value_required:n = true ,
274 number .int_set:N = \l_@@_step_number_int ,
275 length .code:n = { \@@_setup_length:n {#1} } ,
276 angles .code:n = { \@@_setup_angles:n {#1} } ,
277 degree .bool_set:N = \l_@@_degrees_bool ,
278 degrees .bool_set:N = \l_@@_degrees_bool ,
279 angles-relative .bool_set:N = \l_@@_relative_angles_bool ,
280 revert-random .bool_set:N = \l_@@_revert_random_bool ,
281 }
282 % \end{macrocode}
283 %
284 % \subsection{Setup}
285 %
286 % \begin{macro}{\@@_setup_defaults:}
287 % The package treats the length of steps, and the angle,
288 % completely independently. The function \cs{@@_next_length:}
289 % contains the action that decides the length of the next step, while
290 % the function \cs{@@_next_angle:} pertains to the angle.
291 %
292 % \cs{@@_setup_defaults:} sets the default values before processing the
293 % user's key-value input. This also sets initial values of variables
294 % that currently cannot be altered through keys, because it might be
295 % good to provide keys for their initial values too later on.
296 % \begin{macrocode}
297 \cs_new_protected:Npn \@@_setup_defaults:
298 {
299 \int_set:Nn \l_@@_step_number_int {10}
300 \cs_gset_protected:Npn \@@_next_angle:
301 { \@@_fp_set_rand:Nnn \l_@@_angle_fp { 0 } { 360 } }
302 \cs_gset_protected:Npn \@@_next_length:
303 { \fp_set:Nn \l_@@_length_fp {10} }
304 \bool_set_false:N \l_@@_revert_random_bool
305 \bool_set_false:N \l_@@_relative_angles_bool
306 \fp_zero:N \l_@@_angle_fp
307 \fp_zero:N \l_@@_length_fp
308 \dim_zero:N \l_@@_x_dim
309 \dim_zero:N \l_@@_y_dim
310 }
311 % \end{macrocode}
312 % \end{macro}
313 %
314 % \begin{macro}{\@@_setup_length:n}
315 % Convert each item in the comma list into a floating point, then
316 % define \cs{@@_next_length:} to set \cs{l_@@_length_fp} to a random
317 % floating point in the list.
318 % \begin{macrocode}
319 \cs_new_protected:Npn \@@_setup_length:n #1
320 {
321 \seq_set_split:Nnn \l_@@_lengths_seq { , } {#1}
322 \seq_set_map:NNn \l_@@_lengths_seq
323 \l_@@_lengths_seq { \dim_to_fp:n {##1} }
324 \cs_gset_protected:Npn \@@_next_length:
325 {
326 \@@_get_rand_seq_item:NN
327 \l_@@_lengths_seq \l_@@_internal_tl
328 \fp_set:Nn \l_@@_length_fp { \l_@@_internal_tl }
329 }
330 }
331 % \end{macrocode}
332 % \end{macro}
333 %
334 % \begin{macro}{\@@_setup_angles:n}
335 % Two complications compared to \cs{@@_setup_length:n}. First, the
336 % angle can be given in radians rather than degrees: then add |rad|
337 % after the randomly chosen value (in principle it would be better to
338 % convert angles once and for all at the beginning, but that
339 % interacts in a complicated way with the fact that keys can be given
340 % in any order). Second, angles can be relative, in which case we
341 % use \cs{fp_add:Nn} to take the last angle into account.
342 % \begin{macrocode}
343 \cs_new_protected:Npn \@@_setup_angles:n #1
344 {
345 \seq_set_split:Nnn \l_@@_angles_seq { , } {#1}
346 \seq_set_map:NNn \l_@@_angles_seq
347 \l_@@_angles_seq { \fp_to_tl:n {##1} }
348 \cs_gset_protected:Npn \@@_next_angle:
349 {
350 \@@_get_rand_seq_item:NN
351 \l_@@_angles_seq \l_@@_internal_tl
352 \bool_if:NF \l_@@_degrees_bool
353 { \tl_put_right:Nn \l_@@_internal_tl { rad } }
354 \bool_if:NTF \l_@@_relative_angles_bool
355 { \fp_add:Nn } { \fp_set:Nn }
356 \l_@@_angle_fp { \l_@@_internal_tl }
357 }
358 }
359 % \end{macrocode}
360 % \end{macro}
361 %
362 % \subsection{Drawing}
363 %
364 % \begin{macro}{\@@_walk:}
365 % We are ready to define \cs{@@_walk:}, which draws a \pkg{pgf}
366 % picture of a random walk with the parameters set up by the
367 % \texttt{keys}. We reset coordinates to zero originally.
368 % Then draw the relevant \pkg{pgf} picture by repeatedly calling
369 % \cs{@@_walk_step:}.
370 % \begin{macrocode}
371 \cs_new_protected:Npn \@@_walk:
372 {
373 \@@_walk_start:
374 \prg_replicate:nn { \l_@@_step_number_int }
375 { \@@_walk_step: }
376 \bool_if:NF \l_@@_revert_random_bool
377 { \int_gset_eq:NN \cr@nd \cr@nd }
378 \@@_walk_stop:
379 }
380 % \end{macrocode}
381 % \cs{cr@nd} is internal to the lcg package.
382 % \end{macro}
383 %
384 % \begin{macro}{\@@_walk_start:, \@@_walk_line:, \@@_walk_stop:}
385 % These functions encapsulate all of the \pkg{pgf}-related code. The
386 % \texttt{start} function begins the pgfpicture environment and
387 % starts a path at position (x,y). The \texttt{line} function adds
388 % to the path a line from the previous position to the new (x,y).
389 % The \texttt{stop} function draws the path constructed by
390 % \cs{@@_walk_step:} and ends the pgfpicture environment.
391 % \begin{macrocode}
392 \cs_new_protected:Npn \@@_walk_start:
393 {
394 \begin{pgfpicture}
395 \pgfpathmoveto
396 { \pgfpoint { \l_@@_x_dim } { \l_@@_y_dim } }
397 }
398 \cs_new_protected:Npn \@@_walk_line:
399 {
400 \pgfpathlineto
401 { \pgfpoint { \l_@@_x_dim } { \l_@@_y_dim } }
402 }
403 \cs_new_protected:Npn \@@_walk_stop:
404 {
405 \pgfusepath { stroke }
406 \end{pgfpicture}
407 }
408 % \end{macrocode}
409 % \end{macro}
410 %
411 % \begin{macro}{\@@_walk_step:}
412 % \cs{@@_walk_step:} calls \cs{@@_next_length:} and
413 % \cs{@@_next_angle:} to determine the length and angle of the new
414 % step. This is then converted to cartesian coordinates and added to
415 % the previous end-point. Finally, we call \pkg{pgf}'s \cs{pgfpathlineto} to
416 % produce a line to the new point.
417 % \begin{macrocode}
418 \cs_new_protected:Npn \@@_walk_step:
419 {
420 \@@_next_length:
421 \@@_next_angle:
422 \dim_add:Nn \l_@@_x_dim
423 {
424 \fp_to_dim:n
425 { \l_@@_length_fp * cosd ( \l_@@_angle_fp ) }
426 }
427 \dim_add:Nn \l_@@_y_dim
428 {
429 \fp_to_dim:n
430 { \l_@@_length_fp * sind ( \l_@@_angle_fp ) }
431 }
432 \@@_walk_line:
433 }
434 % \end{macrocode}
435 % \end{macro}
436 %
437 % \subsection{On random numbers and items}
438 %
439 % For random numbers, the interface of \pkg{lcg} is not quite enough, so
440 % we provide our own \LaTeX3-y functions. Also, this will allow us to
441 % change quite easily our source of random numbers.
442 %
443 % \begin{macro}{\@@_fp_set_rand:Nnn}
444 % We also need floating point random numbers, assigned
445 % to the variable |#1|.
446 % \begin{macrocode}
447 \cs_new_protected:Npn \@@_fp_set_rand:Nnn #1#2#3
448 {
449 \rand
450 \fp_set:Nn #1
451 { #2 + (#3 - (#2)) * \c@lcg@rand / \c_@@_lcg_last_int }
452 }
453 % \end{macrocode}
454 % \end{macro}
455 %
456 % \begin{macro}{\@@_get_rand_seq_item:NN}
457 % We can now pick an element at random from a sequence. If the
458 % sequence has a single element, no need for randomness.
459 % \begin{macrocode}
460 \cs_new_protected:Npn \@@_get_rand_seq_item:NN #1#2
461 {
462 \int_set:Nn \l_@@_internal_int { \seq_count:N #1 }
463 \int_compare:nTF { \l_@@_internal_int = 1 }
464 { \tl_set:Nx #2 { \seq_item:Nn #1 { 1 } } }
465 {
466 \rand
467 \tl_set:Nx #2
468 {
469 \seq_item:Nn #1
470 {
471 1 +
472 \int_mod:nn { \c@lcg@rand } { \l_@@_internal_int }
473 }
474 }
475 }
476 }
477 % \end{macrocode}
478 % \end{macro}
479 %
480 % \begin{macrocode}
481 %</package>
482 % \end{macrocode}
483 %
484 % \end{implementation}
485 %
486 % \endinput