Debian Codegraph @

texmf-dist/source/generic/zeckendorf/zeckendorf.dtx

raw
1 % -*- coding: utf-8; time-stamp-format: "%02d-%02m-%:y at %02H:%02M:%02S %Z" ; time-stamp-active: nil -*-
2 %<*dtx>
3 \def\zeckdtxtimestampaux #1<#2>{#2}
4 \edef\zeckdtxtimestamp{\zeckdtxtimestampaux Time-stamp: <16-11-2025 at 18:36:26 CET>}
5 %</dtx>
6 %<*drv>
7 %% ---------------------------------------------------------------
8 \def\zeckpackdate{2025/11/16}%
9 \def\zeckversion {0.9d}%
10 %</drv>
11 %<*dtx>
12 \catcode0 0 \catcode`\\ 12
13 ^^@iffalse
14 %</dtx>
15 %<*readme>--------------------------------------------------------
16 | Source: zeckendorf.dtx
17 | Version: 0.9d, 2025/11/16
18 | Author: Jean-François Burnol
19 | Keywords: Zeckendorf representation; Knuth Fibonacci multiplication
20 | License: LPPL 1.3c
21
22 Aim
23 ===
24
25 This package extends `\xinteval`'s syntax:
26 - The four operations are overloaded to do algebra in `Q(phi)`,
27 where phi is a variable standing for the golden ratio.
28 - Functions are available in this syntax to compute:
29 - Fibonacci numbers,
30 - Zeckendorf representations of positive integers (as lists
31 of indices),
32 - Bergman phi-representations of positive elements of `Z[phi]`
33 (as lists of exponents).
34 - The `$` character serves as infix operator computing the Knuth
35 Fibonacci multiplication of positive integers.
36
37 The macro interface also provides conversion of integers to
38 Zeckendorf words and Bergman phi-ary representations.
39
40 The functionalities can be used in three ways:
41 - as a LaTeX package: `\usepackage{zeckendorf}`,
42 - as a Plain e-TeX macro file: `\input zeckendorfcore.tex`,
43 - or interactively on the command line: `etex zeckendorf`. If
44 available use rather `rlwrap etex zeckendorf` for a smoother
45 experience.
46
47 The package is in alpha stage and its user interface may change
48 in backwards incompatible ways.
49 Of course, suggestions are most welcome.
50
51 Installation
52 ============
53
54 Exert `etex zeckendorf.dtx` to extract macro files. To build the
55 documentation run `xelatex` on the extracted `zeckendorf-doc.tex`,
56 or directly on `zeckendorf.dtx`.
57
58 The recommended TDS structure is:
59
60 /tex/generic/zeckendorf/zeckendorfcore.tex
61 /tex/generic/zeckendorf/zeckendorf.tex
62 /tex/generic/zeckendorf/zeckendorf.sty
63 /source/generic/zeckendorf/zeckendorf.dtx
64 /doc/generic/zeckendorf/zeckendorf-doc.pdf
65 /doc/generic/zeckendorf/README.md
66
67 License
68 =======
69
70 Copyright © 2025 Jean-François Burnol
71
72 | This Work may be distributed and/or modified under the
73 | conditions of the LaTeX Project Public License 1.3c.
74 | This version of this license is in
75
76 > <http://www.latex-project.org/lppl/lppl-1-3c.txt>
77
78 | and version 1.3 or later is part of all distributions of
79 | LaTeX version 2005/12/01 or later.
80
81 This Work has the LPPL maintenance status "author-maintained".
82
83 The Author and Maintainer of this Work is Jean-François Burnol.
84
85 This Work consists of the main source file and its derived files
86
87 zeckendorf.dtx,
88 zeckendorfcore.tex, zeckendorf.tex, zeckendorf.sty,
89 README.md, zeckendorf-doc.tex, zeckendorf-doc.pdf
90
91 %</readme>--------------------------------------------------------
92 %<*!readme>
93 %% ---------------------------------------------------------------
94 %% The zeckendorf package: Zeckendorf representation of big integers
95 %% Copyright (C) 2025 Jean-Francois Burnol
96 %%
97 %</!readme>
98 %<*changes>-------------------------------------------------------
99 \item[0.9d (2025/11/16)]
100 \textbf{Breaking changes:}
101 \begin{itemize}[nosep]
102 \item
103 The radix separator
104 used by default by |\PhiBasePhi| on output and expected by
105 |\PhiXfromBasePhi| on input is now a period, not a comma (the comma
106 was used by accident by the author due to patriotism).
107 \item |\ZeckIndices| and |\ZeckWord| both replace a negative argument by its
108 absolute value, rather than returning an empty output as so far.
109 \end{itemize}
110
111 \textbf{New feature:} Macros to support Zeckendorf and Bergman
112 representations using hexadecimal digits. For lack of time, the PDF doc is
113 not updated, please use the interactive interface or check the source code
114 for the macro names. Thanks to \textbf{Laurent Barme} for the feature request.
115
116 \textbf{Bug fix:} After the |0.9c| transition from |\xintiieval| to
117 |\xinteval|, the |$| and |$$| infix operators were broken with operands such
118 as |(2+3)| in place of lone integers.
119
120 \textbf{Improvements:}
121 \begin{itemize}
122 \item The |fibseq(a,b)| function can now be used also with |a<b| and |a=b|.
123 \item Under-the-hood polishing for efficiency, improved code comments and
124 documentation of the main algorithms.
125 \end{itemize}
126 \item[0.9c (2025/10/17)] This adds many new features and has
127 some breaking changes due to renamings, not listed here.
128 \begin{itemize}
129 \item It is not |\xintiieval| but |\xinteval|'s syntax which is
130 now extended.
131 \item Variables |phi| and |psi| are defined and one can do algebra
132 with |+|, |-|, |*|, |/| and |^| on them in $\QQ(\phi)$.
133 \item The Bergman $\phi$-representation is added for elements
134 of $\ZZ[\phi]$ in particular for integers.
135 \item The |$| % $
136 character doing the Knuth Fibonacci multiplication
137 on positive integers now uses the (more efficient) Arnoux formula.
138 The |$$| % $$
139 computes out of deference according to the original Knuth definition.
140 \item The PDF documentation section on the mathematical background has
141 been extended and includes bibliographical references.
142 \item The interactive interface integrates all novelties.
143 \end{itemize}
144 \item[0.9b (2025/10/07)]\mbox{}\newline
145 \textbf{Bug fixes:}
146 \begin{itemize}
147 \item The instructions for interactive use mentioned |1e100| as possible
148 input, but the author had forgotten that this syntax is not legitimate in
149 |\xintiieval| (for example |1+1e10| crashes immediately).\newline
150 \emph{This remark is obsolete as of 0.9c because the interactive mode
151 now uses |\xinteval|, not |\xintiieval|.}
152 \item The code tries at some locations to be compatible with \xintexprname
153 versions earlier than |1.4n|. But these versions did not load
154 \xintbinhexname automatically and the needed |\RequirePackage| or |\input|
155 for Plain \TeX{} was lacking from the \zeckname code.
156 \end{itemize}
157 \textbf{Other changes:} In the interactive interface, the input may now start
158 with an |\xintiieval| function such as |binomial| whose first letter coincides
159 with one of the letter commands without it being needed to for example
160 add some |\empty| control sequence first. On the other hand, it was possible
161 to use the full command names, now only their first letters (lower or uppercase)
162 are recognized as such.
163 \item[0.9alpha (2025/10/06)] Initial release.
164 %</changes>-------------------------------------------------------
165 %<*drv>-----------------------------------------------------------
166 %% latex zeckendorf-doc.tex (thrice) && dvipdfmx zeckendorf-doc.dvi
167 %% to produce zeckendorf-doc.pdf
168 %%
169 %% Doing latexmk -pdf zeckendorf-doc.tex is also possible but
170 %% will produce a bigger file due usage of pdflatex.
171 %%
172 \NeedsTeXFormat{LaTeX2e}[2020/02/02]
173 \ProvidesFile{zeckendorf-doc.tex}%
174 [\zeckpackdate\space v\zeckversion\space driver file for %
175 zeckendorf documentation (JFB)]%
176 \PassOptionsToClass{a4paper,fontsize=11pt}{scrartcl}
177 \RequirePackage{iftex}
178 \chardef\Withdvipdfmx \iftutex0 \else\ifpdf0 \else1 \fi\fi
179 \chardef\NoSourceCode 0 % replace 0 by 1 for not including source code
180 \input zeckendorf.dtx
181 %%% Local Variables:
182 %%% mode: latex
183 %%% End:
184 %</drv>-----------------------------------------------------------
185 %<*dtx>
186 ^^@fi ^^@catcode`\ 0 \catcode0 12
187 \def\hiddenifs{\iftutex0 \else\ifpdf0 \else1 \fi\fi}%
188 \chardef\noetex 0
189 \ifx\numexpr\undefined \chardef\noetex 1 \fi
190 \ifnum\noetex=1 \chardef\extractfiles 0 % extract files, then stop
191 \else
192 \ifx\ProvidesFile\undefined
193 \chardef\extractfiles 0 % no LaTeX2e; etex, ... on zeckendorf.dtx
194 \else % latex/pdflatex on zeckendorf-doc.tex or on zeckendorf.dtx
195 \NeedsTeXFormat{LaTeX2e}[2020/02/02]%
196 \ProvidesFile{zeckendorf.dtx}[bundle source (\zeckpackdate)]%
197 \ifx\Withdvipdfmx\undefined
198 % latex run is on zeckendorf.dtx, we will extract all files
199 \chardef\extractfiles 1 % 1 = extract and typeset, 2=only typeset
200 \chardef\NoSourceCode 0 % 0 = include source code, 1 = do not
201 \PassOptionsToClass{a4paper,fontsize=11pt}{scrartcl}%
202 \RequirePackage{iftex}\relax
203 \chardef\Withdvipdfmx \hiddenifs
204 \else % latex run is on zeckendorf-doc.tex,
205 \chardef\extractfiles 2 % no extractions
206 \fi
207 \fi
208 \fi
209 \ifnum\extractfiles<2 % we extract files
210 \def\MessageDeFin{\newlinechar10 \let\Msg\message
211 \Msg{^^J}%
212 \Msg{********************************************************************^^J}%
213 \Msg{*^^J}%
214 \Msg{* To finish the installation you have to move the following^^J}%
215 \Msg{* files into a directory searched by TeX:^^J}%
216 \Msg{*^^J}%
217 \Msg{*\space\space\space\space zeckendorf.sty^^J}%
218 \Msg{*\space\space\space\space zeckendorf.tex^^J}%
219 \Msg{*\space\space\space\space zeckendorfcore.tex^^J}%
220 \Msg{*^^J}%
221 \Msg{* Happy TeXing!^^J}%
222 \Msg{*^^J}%
223 }%
224 \ifnum\extractfiles=0 % only extraction
225 \expandafter\def\expandafter\MessageDeFin\expandafter{%
226 \MessageDeFin
227 \Msg{* To produce the documentation run latex thrice on zeckendorf-doc.tex^^J}%
228 \Msg{* then dvipdfmx on zeckendorf-doc.dvi.^^J}%
229 \Msg{*^^J}%
230 \Msg{********************************************************************^^J}%
231 }%
232 \else %% *latex run on zeckendorf.dtx
233 \ifnum\Withdvipdfmx=1 % for dvipdfmx
234 \expandafter\def\expandafter\MessageDeFin\expandafter{%
235 \MessageDeFin
236 \Msg{* Please use dvipdfmx once the LaTeX runs are done, and^^J}%
237 \Msg{* consider renaming zeckendorf.pdf to zeckendorf-doc.pdf^^J}%
238 \Msg{********************************************************************^^J}%
239 }%
240 \else % pdflatex, lualatex or xelatex
241 \expandafter\def\expandafter\MessageDeFin\expandafter{%
242 \MessageDeFin
243 \Msg{* Please consider renaming zeckendorf.pdf to zeckendorf-doc.pdf^^J}%
244 \Msg{********************************************************************^^J}%
245 }%
246 \fi
247 \fi
248 % file extraction
249 \begingroup
250 \input docstrip.tex
251 \askforoverwritefalse
252 \generate{\nopreamble\nopostamble
253 \file{README.md}{\from{zeckendorf.dtx}{readme}}
254 \usepreamble\defaultpreamble
255 \usepostamble\defaultpostamble
256 \file{zeckendorfchanges.tex}{\from{zeckendorf.dtx}{changes}}
257 \file{zeckendorf-doc.tex}{\from{zeckendorf.dtx}{drv}}
258 \file{zeckendorfcore.tex}{\from{zeckendorf.dtx}{core}}
259 \file{zeckendorf.tex}{\from{zeckendorf.dtx}{interactive}}
260 \file{zeckendorf.sty}{\from{zeckendorf.dtx}{sty}}}
261 \endgroup
262 \else % *latex on zeckendorf-doc.tex we skipped extraction
263 \ifnum\Withdvipdfmx=1
264 \def\MessageDeFin{\newlinechar10 \let\Msg\message
265 \Msg{^^J}% too lazy to add the multipls spaces to make a frame
266 \Msg{********************************************************************^^J}%
267 \Msg{* Please use dvipdfmx once the LaTeX runs are done^^J}%
268 \Msg{********************************************************************^^J}%
269 }%
270 \fi
271 \fi
272 \ifnum\extractfiles=0
273 % direct tex/etex/xetex on zeckendorf.dtx, files now extracted, stop
274 \MessageDeFin\expandafter\end
275 \fi
276 \ifdefined\MessageDeFin\AtEndDocument{\MessageDeFin}\fi
277 %-------------------------------------------------------------------------------
278 \documentclass {scrartcl}
279 %%% START OF CUSTOM doc.sty LOADING (May 21, 2022)
280 \usepackage{doc}[=v2]
281 % As explained above I was formerly using scrdoc hence ltxdoc indirectly.
282 % Let's emulate here the little I appear to need from ltxdoc.cls and
283 % srcdoc.cls.
284 %
285 \AtBeginDocument{\MakeShortVerb{\|}}
286 \DeclareFontShape{OT1}{cmtt}{bx}{n}{<-> ssub * cmtt/m/n}{}
287 \DeclareFontFamily{OMS}{cmtt}{\skewchar\font 48} % '60
288 \DeclareFontShape{OMS}{cmtt}{m}{n}{<-> ssub * cmsy/m/n}{}
289 \DeclareFontShape{OMS}{cmtt}{bx}{n}{<-> ssub * cmsy/b/n}{}
290 \DeclareFontShape{OT1}{cmss}{m}{it}{<->ssub*cmss/m/sl}{}
291 \ifnum\NoSourceCode=1
292 \OnlyDescription
293 \fi
294 \CodelineNumbered
295 \DisableCrossrefs
296 % \setcounter{StandardModuleDepth}{1} % we don't use this mechanism currently
297 \def\cmd#1{\cs{\expandafter\cmd@to@cs\string#1}}
298 \def\cmd@to@cs#1#2{\char\number`#2\relax}
299 % This is hyperref compatible (contrarily to using \bslash)
300 % I need the \detokenize due to usage in implementation part with names
301 % containing the underscore for example
302 \DeclareRobustCommand\cs[1]{\texttt{\textbackslash\detokenize{#1}}}
303 \providecommand\marg[1]{%
304 {\ttfamily\char`\{}\meta{#1}{\ttfamily\char`\}}}
305 \providecommand\oarg[1]{%
306 {\ttfamily[}\meta{#1}{\ttfamily]}}
307 \providecommand\parg[1]{%
308 {\ttfamily(}\meta{#1}{\ttfamily)}}
309
310 % There is very little we seem to need from the scrdoc extras: page geometry
311 % is set by geometry package and a4paper option from zeckendorf.tex file. So it
312 % seems I only need the hologo loading:
313 \usepackage{hologo}
314 \DeclareRobustCommand*{\eTeX}{\hologo{eTeX}}%
315 %\DeclareRobustCommand*{\LuaTeX}{\hologo{LuaTeX}}%
316 %
317 %%% end of ltxdoc+srcdoc emulation.
318
319 \ifnum\NoSourceCode=1 \OnlyDescription\fi
320 \usepackage{ifpdf}
321 \ifpdf\chardef\Withdvipdfmx 0 \fi
322 \makeatletter
323 \ifnum\Withdvipdfmx=1
324 % I think those are mostly obsolete...
325 \@for\@tempa:=hyperref,bookmark,graphicx,xcolor,pict2e\do
326 {\PassOptionsToPackage{dvipdfmx}\@tempa}
327 %
328 \PassOptionsToPackage{dvipdfm}{geometry}
329 \PassOptionsToPackage{bookmarks=true}{hyperref}
330 \PassOptionsToPackage{dvipdfmx-outline-open}{hyperref}
331 %\PassOptionsToPackage{dvipdfmx-outline-open}{bookmark}
332 %
333 \def\pgfsysdriver{pgfsys-dvipdfm.def}
334 \else
335 \PassOptionsToPackage{bookmarks=true}{hyperref}
336 \fi
337 \makeatother
338
339 \pagestyle{headings}
340
341 \iftutex\else % but newtxtt requires it anyhow
342 \usepackage[T1]{fontenc}
343 \fi
344
345 \usepackage[hscale=0.66,vscale=0.75]{geometry}
346
347 \usepackage{amsmath}
348 \usepackage{amssymb}% for \blacktriangleright utilisé par titled-frame
349 \allowdisplaybreaks
350
351 % requires newtxtt 1.05 or later
352 \usepackage[zerostyle=d,scaled=0.95,straightquotes]{newtxtt}
353 \renewcommand\familydefault\ttdefault
354 \usepackage[noendash]{mathastext}
355 \renewcommand\familydefault\sfdefault
356
357 % pas le temps d'expliquer pourquoi
358 \iftutex
359 \catcode`⊙\active\def⊙{\ensuremath{\odot}}
360 \else
361 \DeclareUnicodeCharacter{2299}{\ensuremath{\odot}}
362 \fi
363
364 \usepackage{graphicx}
365
366 \usepackage[dvipsnames,svgnames]{xcolor}
367
368 \definecolor{joli}{RGB}{225,95,0}
369 \definecolor{JOLI}{RGB}{225,95,0}
370 \definecolor{BLUE}{RGB}{0,0,255}
371 %% \definecolor{niceone}{RGB}{38,128,192}% utilisé avant pour urlcolor
372 %% \colorlet{jfverbcolor}{yellow!5}
373 \colorlet{shortverbcolor}{magenta}% 1.6 release NavyBlue RoyalPurple
374 \colorlet{softwrapcolor}{blue}
375 \colorlet{digitscolor}{olive}% 1.6 {OrangeRed}
376
377 % transféré de xint-manual.tex (maintenant dans xint.dtx)
378 \DeclareFontFamily{U}{MdSymbolC}{}
379 \DeclareFontShape {U}{MdSymbolC}{m}{n}{<-> MdSymbolC-Regular}{}
380
381 \makeatletter
382 \newbox\cdbx@SoftWrapIcon
383 \def\cdbx@SetSoftWrapBox{%
384 \setbox\cdbx@SoftWrapIcon\hb@xt@\z@
385 {\hb@xt@\fontdimen2\font
386 {\hss{\color{softwrapcolor}\usefont{U}{MdSymbolC}{m}{n}\char"97}\hss}%
387 \hss}%
388 }
389 \makeatother
390
391 \usepackage[english]{babel}
392
393 \usepackage{hyperref}
394 \hypersetup{%
395 %linktoc=all,%
396 breaklinks=true,%
397 colorlinks=true,%
398 urlcolor=Cerulean,% 1.6 ProcessBlue,%SkyBlue
399 citecolor=[RGB]{120,29,126},% ACM Purple
400 linkcolor=PineGreen,%
401 pdfauthor={Jean-François Burnol},%
402 pdftitle={The Zeckendorf package},%
403 pdfsubject={Arithmetic with TeX},%
404 pdfkeywords={Phi-expansion, arithmetic, TeX},%
405 pdfstartview=FitH,%
406 pdfpagemode=UseOutlines}
407 \usepackage{bookmark}
408
409 % importé de xint 2021/05/14
410 \newcommand\RaisedLabel[2][6]{%
411 \vspace*{-#1\baselineskip}%
412 \begingroup
413 \let\leavevmode\relax\phantomsection
414 \label{#2}%
415 \endgroup
416 \vspace*{#1\baselineskip}%
417 }
418
419 %---- \verb, and verbatim like `environments'. \MicroFont et \MacroFont
420 \makeatletter
421 \def\MicroFont {\ttfamily\cdbx@SetSoftWrapBox\color{shortverbcolor}}
422 %\def\MicroFont {\ttfamily \color[named]{OrangeRed}\cdbx@SetSoftWrapBox }
423
424 % \MacroFont est utilisé par macrocode, mais sa définition est figée dans
425 % \macro@font au \begin{document}
426
427 \def\MacroFont {\ttfamily \baselineskip12pt\relax }
428
429 %--- November 4, 2014 making quotes straight. (maintenu pour *)
430 %
431 % there is no hook in \macrocode after \dospecials etc. Thus I will need to
432 % take the risk that some future evolution of doc.sty (or perhaps scrdoc)
433 % invalidates the following.
434 %
435 % Actually, I should not at all rely on the doc class, I should do it all by
436 % myself.
437
438 \def\macrocode{\macro@code
439 \frenchspacing \@vobeyspaces
440 \makestarlowast % \makequotesstraight (obsolète)
441 \xmacro@code }
442
443 %--- lower * symbol in text
444
445 \def\lowast{\raisebox{-.25\height}{*}}
446 \catcode`* \active
447 \def\makestarlowast {\let*\lowast\catcode`\*\active}%
448 \catcode`* 12
449
450 \def\verb #1%
451 {%
452 \relax\leavevmode\null
453 \begingroup
454 \MicroFont
455 \def\@makeletter##1{\catcode`##1=11\relax}%
456 % \scantokens will have a result of inserting a space after cs's.
457 % hence the need to have the catcodes of things like _ right.
458 % I also need < for > for code comments
459 % \dospecials at begin document
460 % \do\ \do\\\do\{\do\}\do\$\do\&\do\#\do\^\do\_\do\%\do\~\do\|
461 \odef\dospecials{\dospecials\do\:\do\<\do\>\do\-\do\+}%
462 % naturally won't work in footnotes though.
463 % this code is truly not satisfying, but enough for my needs here.
464 \catcode`\\ 11 \catcode`## 11
465 % I don't do \catcode`\% 11 to avoid surprises in code comments
466 % if my |...| has a linebreak
467 \def\@jfverb ##1#1{\let\do\@makeletter \dospecials
468 % lowering a bit the *
469 \makestarlowast
470 %\let\do\do@noligs \verbatim@nolig@list % not needed here
471 \@vobeyspaces\everyeof{\noexpand}%
472 \expandafter\@@jfverb\scantokens{##1}\relax}%
473 \@jfverb
474 }%
475
476 \def\@@jfverb #1{\ifcat\noexpand#1\relax\endgroup\@\else
477 \nobreak\hskip0pt plus .5pt % added 2025/10/06 for Zeckendorf
478 % only .5pt 2025/10/15
479 \discretionary{\copy\cdbx@SoftWrapIcon}{}{}%
480 #1\expandafter\@@jfverb\fi }
481
482 \makeatother
483
484
485 % everbatim environment
486 % =====================
487
488 % October 13-14, 2014
489 % Verbatim with an \everypar hook, mainly to have background color, followed by
490 % execution of the contents (not limited by a group-scope)
491
492 \makeatletter
493 \colorlet{everbatimfgcolor}{DarkBlue}
494 \colorlet{everbatimbgcolor}{Beige}
495 \colorlet{everbatimxfgcolor}{Maroon}
496
497 \def\everbatim {\s@everbatim\@everbatim}
498 \@namedef{everbatim*}{\s@everbatim\@everbatimx}
499
500 % Note: one can not use everbatim inside itself or everbatim* inside itself
501 \def\s@everbatim {%
502 \everbatimtop % put there size changes
503 \topsep \z@skip
504 \partopsep \z@skip
505 \itemsep \z@skip
506 \parsep \z@skip
507 \parskip \z@skip
508 \lineskip \z@skip
509 \let\do\@makeother \dospecials
510 \let\do\do@noligs \verbatim@nolig@list
511 \makestarlowast
512 \everbatimhook
513 \trivlist
514 \@topsepadd \z@skip
515 \item\relax
516 \leftskip \@totalleftmargin
517 \rightskip \z@skip
518 \parindent \z@
519 \parfillskip\@flushglue
520 \parskip \z@skip
521 \@@par
522 \def\par{\leavevmode\null\@@par\pagebreak[1]}%
523 \everypar\expandafter{\the\everypar \unpenalty
524 \everbatimeverypar
525 \everypar \expandafter{\the\everypar\everbatimeverypar}%
526 }%
527 \obeylines \@vobeyspaces
528 }
529 % 27 mai 2022, plus de \small
530 \def\everbatimtop {\MacroFont }%
531 \let\everbatimhook\empty
532 \def\everbatimeverypar{\strut
533 {\color{everbatimbgcolor}\vrule\@width\linewidth }%
534 \kern-\linewidth
535 \kern\everbatimindent }
536 \def\everbatimindent {\z@}% voir plus loin atbegindocument
537
538 \begingroup
539 \lccode`X 13
540 \catcode`X \active
541 \lccode`Y `* % this is because of \makestarlowast.
542 % I have to think whether this is useful: obviously if I were to provide
543 % everbatim and everbatim* in a package I wouldn't do that.
544 \catcode`Y \active
545 \catcode`| 0 \catcode`[ 1 \catcode`] 2 \catcode`* 12
546 \catcode`{ 12 \catcode`} 12 |catcode`\\ 12
547 |lowercase[|endgroup% both freezes catcodes and converts X to active ^^M
548 |def|@everbatim #1X#2\end{everbatim}%
549 [#2|end[everbatim]|everbatimbottom ]
550 |def|@everbatimx #1X#2\end{everbatimY}]%
551 {#2\end{everbatim*}%
552 % No group here: this allows executed code to make macro
553 % definitions which may reused in later uses of everbatim.
554 % refactored 2022/01/11, rather than passing \newlinechar value
555 % as was done formerly via everbatim* (see above) and fetching it here as #1
556 % it is thus assumed executed contents do not terminate a scope
557 \edef\everbatimrestorenewlinechar{\newlinechar\the\newlinechar\relax}%
558 \newlinechar 13
559 % refactored 2022/01/11 to fix a \parskip issue
560 % attention, \parskip thus set to zero for execution of contents
561 % reason: avoid extra space if everbatim* is in an \item of a list
562 % between verbatim and output of execution, if it starts a paragraph
563 % a \vskip-\parskip approach (cf former \everbatimundoparskip)
564 % would be no good in case contents create a display
565 \edef\everbatimrestoreparskip{\parskip\the\parskip\relax}%
566 \parskip\z@skip
567 % execution of the contents (expected to be LaTeX code...)
568 \everbatimxprehook
569 \scantokens {#2}%
570 \everbatimrestorenewlinechar
571 \everbatimrestoreparskip
572 \everbatimxposthook
573 % input after \end{everbatim*} on same line in source is allowed
574 }%
575 % L'espace venant du endofline final mis par \scantokens sera inhibé si #2 se
576 % termine par un % ou un \x, etc...
577 \let\everbatimbottom\empty
578
579 \def\endeverbatim{\if@newlist \leavevmode\fi\endtrivlist}
580 \@namedef{endeverbatim*}{\endeverbatim}
581 \def\everbatimxprehook {\colorlet{everbsavedcolor}{.}%
582 \color{everbatimxfgcolor}}%
583 \def\everbatimxposthook{\color{everbsavedcolor}}
584 {\sbox0{\color{everbatimxfgcolor}\xdef\@tempa{\current@color}}}
585 \ifpdf
586 \ifluatex
587 \edef\everbatimxprehook
588 {\pdfextension colorstack\noexpand\@pdfcolorstack push{\@tempa}\relax}
589 \def\everbatimxposthook
590 {\pdfextension colorstack\@pdfcolorstack pop\relax}
591 \else
592 \edef\everbatimxprehook
593 {\pdfcolorstack\noexpand\@pdfcolorstack push{\@tempa}\relax}
594 \def\everbatimxposthook{\pdfcolorstack\@pdfcolorstack pop\relax}
595 \fi
596 \else
597 \ifxetex
598 \edef\everbatimxprehook{\special{color push \@tempa}}
599 \def\everbatimxposthook{\special{color pop}}
600 \else
601 \ifnum\Withdvipdfmx=1
602 \edef\everbatimxprehook{\special{color push \@tempa}}
603 \def\everbatimxposthook{\special{color pop}}
604 \fi
605 \fi
606 \fi
607 \makeatother
608
609 % 2025/10/05
610 % Toujours pénible de devoir se battre avec les espaces verticaux de LaTeX.
611 % J'ai dû réexaminer la situation pour Zeckendorf, différent de bnumexpr
612 % où j'utilise \[..\] pour les \bneshoweval etc...
613 \AtBeginDocument{%
614 % \message{ICI below: \the\belowdisplayskip^^J%
615 % belowshort: \the\belowdisplayshortskip^^J%
616 % above: \the\abovedisplayskip^^J%
617 % aboveshor: \the\abovedisplayshortskip^^J%
618 % }%
619 % below: 11.0pt plus 3.0pt minus 6.0pt
620 % belowshort: 6.5pt plus 3.5pt minus 3.0pt
621 % above: 11.0pt plus 3.0pt minus 6.0pt
622 % aboveshort: 0.0pt plus 3.0pt
623 \belowdisplayskip\belowdisplayshortskip
624 %\abovedisplayskip\belowdisplayskip
625 }
626
627 \usepackage{xspace}
628
629 \def\zeckname
630 {\href{http://www.ctan.org/pkg/zeckendorf}{zeckendorf}\xspace }%
631
632 \def\zecknameimp
633 {\texorpdfstring
634 {\hyperref[part:zeckendorfcode]{{\color{shortverbcolor}\ttzfamily zeckendorf}}}
635 {zeckendorf}%
636 \xspace }%
637
638 \def\zecknameuserdoc
639 {\texorpdfstring
640 {\hyperref[titlepage]{{\color{PineGreen}\ttzfamily zeckendorf}}}
641 {zeckendorf}%
642 \xspace }%
643
644 \def\bnumexprname
645 {\href{http://www.ctan.org/pkg/bnumexpr}{bnumexpr}\xspace }%
646 \def\xintkernelname
647 {\href{http://www.ctan.org/pkg/xint}{xintkernel}\xspace }%
648 \def\xintcorename
649 {\href{http://www.ctan.org/pkg/xint}{xintcore}\xspace }%
650 \def\xintname
651 {\href{http://www.ctan.org/pkg/xint}{xint}\xspace }%
652 \def\xinttoolsname
653 {\href{http://www.ctan.org/pkg/xint}{xinttools}\xspace }%
654 \def\xintfracname
655 {\href{http://www.ctan.org/pkg/xint}{xintfrac}\xspace }%
656 \def\xintexprname
657 {\href{http://www.ctan.org/pkg/xintexpr}{xintexpr}\xspace }%
658 \def\xintbinhexname
659 {\href{http://www.ctan.org/pkg/xint}{xintbinhex}\xspace }%
660
661 \DeclareRobustCommand\ctanpackage[1]{\href{https://ctan.org/pkg/#1}{#1}}
662
663 \usepackage{zeckendorf}
664
665 \usepackage[nottoc,notlot,notlof,other]{tocbibind}\tocotherhead{subsection}
666 \usepackage{etoc}
667 \usepackage{centeredline}
668 \usepackage[dvipsnames]{xcolor}
669 \usepackage{colorframed}
670 \colorlet{TFFrameColor}{teal!20}
671 \colorlet{TFTitleColor}{purple}
672 \colorlet{shadecolor}{lightgray!25}
673
674 \makeatletter\let\originalcheck@percent\check@percent
675 \let\check@percent\relax\makeatother
676 \usepackage[autolanguage,np]{numprint}
677 \usepackage{enumitem}
678
679 \newenvironment{TeXnote}{\par\footnotesize\medskip\noindent\textbf{\TeX-nical note:
680 }}{\par\medskip}
681
682 \newcommand\dgts{\textcolor{digitscolor}}
683 \newcommand\mdgts{\mathcolor{digitscolor}}
684 \DeclareMathOperator{\NN}{\mathbf{N}}
685 \DeclareMathOperator{\ZZ}{\mathbf{Z}}
686 \DeclareMathOperator{\QQ}{\mathbf{Q}}
687 \begin{document}
688 \thispagestyle{empty}
689 \ttzfamily
690 \pdfbookmark[1]{Title page}{TOP}
691
692 {%
693 \normalfont\Large\parindent0pt \parfillskip 0pt\relax
694 \leftskip 2cm plus 1fil \rightskip 2cm plus 1fil
695 The \zeckname package\par
696 }
697
698 \RaisedLabel{titlepage}
699
700 {\centering
701 \textsc{Jean-François Burnol}\par
702 \small
703 jfbu (at) free (dot) fr\par
704 Package version: \zeckversion\ (\zeckpackdate)\par
705 {From source file \texttt{zeckendorf.dtx} of \texttt{\zeckdtxtimestamp}}\par
706 }
707
708
709 \begin{titled-frame}{Warning}
710 \small
711 This package is still in alpha stage. Any or all of the user interface may change
712 in backwards incompatible ways at each release.
713
714 Suggestions for new features are most welcome!
715 \end{titled-frame}
716
717 \etocsetnexttocdepth{subsubsection}
718 \etocsettagdepth{code}{section}
719 \etocsettocstyle{}{}
720 \etocdefaultlines
721 \makeatletter
722 \etocsetstyle{subsubsection}
723 {\addpenalty\@M\etocfontthree\vspace{\etocsepthree}%
724 \begingroup\raggedright\noindent
725 \etocskipfirstprefix}
726 {, }%
727 {\etocname}
728 {.%
729 \baselineskip\etocbaselinespreadthree\baselineskip
730 \par\endgroup\addpenalty{-\@highpenalty}}
731 \makeatother
732
733 \tableofcontents
734
735 \section{Mathematical background}
736 \label{sec:math}
737
738 Let us recall that the Fibonacci sequence starts with $F_0=0$,
739 $F_1=1$, and obeys the recurrence $F_n=F_{n-1} + F_{n-2}$ for $n\geq2$. So $F_2=1$,
740 $F_3=2$, $F_4=3$ and by a simple induction $F_k = k-1$. Ahem, not at all!
741 Here are the first few, starting at $F_2=1$:
742 \[
743 \xinteval{*fibseq(2, 18)}\dots
744 \]
745 The ratios of consecutive Fibonacci numbers are the convergents of the golden
746 ratio $\phi$.
747 \[\phi = \xintDigits:=32;\frac{1+sqrt(5)}2\approx
748 \np{\xintfloateval[-2]{(1+sqrt(5))/2}}.\]
749 The Fibonacci recurrence can also be prolungated to negative $n$'s, and it
750 turns out that $F_{-n}=(-1)^{n-1}F_n$.
751
752 Let us a give a few equations which are constantly in use. The first one
753 implies explicitly, in particular, that $\ZZ[\phi]$ (i.e. all polynomial
754 expression in $\phi$ with integer coefficients) is $\ZZ + \ZZ\phi$.
755 \begin{equation}
756 \label{eq:phik}
757 \forall n\in\ZZ\quad \phi^n = F_{n-1} + F_n \phi\,.
758 \end{equation}
759 Applying the $\phi\leftrightarrow\psi=-\phi^{-1}=1-\phi$ automorphism of
760 the ring $\ZZ[\phi]$ and
761 adding we obtain the \textbf{Lucas} numbers:
762 \begin{equation}
763 \label{eq:lucas}
764 L_n = \phi^n + \psi^n = 2 F_{n-1} + F_n = F_{n-1} + F_{n+1}\,.
765 \end{equation}
766 If subtracting, we obtain the \textbf{Binet} formula:
767 \begin{equation}
768 \label{eq:binet}
769 F_n = \frac{\phi^n - \psi^n}{\phi - \psi}\;.
770 \end{equation}
771 Of course one should always keep in mind that $-1<\psi<0$. And perhaps also that
772 $\phi-\psi=\sqrt5$.
773
774 Finally, there is an important formula using $2\times2$-matrices,
775 closely related with equation \eqref{eq:phik} and the recurrence relation of
776 the Fibonacci numbers:
777 \begin{equation}
778 \label{eq:matrix}
779 \forall n\in\ZZ\quad
780 \begin{pmatrix}
781 1&1\\1&0
782 \end{pmatrix}^n =
783 \begin{pmatrix}
784 F_{n+1}&F_n\\F_n&F_{n-1}
785 \end{pmatrix}.
786 \end{equation}
787
788
789
790 \def\testN{100000000}
791 \oodef\testword{\ZeckWord{\testN}}
792 \oodef\testmaxindex{\ZeckMaxK{\testN}}
793 \ifnum\xintNthElt{0}{\testword}=\numexpr\testmaxindex-1\relax\else\ERROR\fi
794
795 \textbf{Zeckendorf}'s Theorem (\textbf{Lekkerkerker}'s \cite{LEKK}
796 in 1952 (preprint 1951) attributes the result to
797 Zeckendorf; Zeckendorf, who was not in academia, published \cite{ZECK} only
798 later in 1972)
799 says that any positive integer has a unique representation as a sum of the
800 Fibonacci numbers $F_n$, $n\geq2$, under the conditions that no two indices
801 differ by one, and that no index is repeated. For example:
802 \begin{align*}
803 \xintFor #1 in {10,100,1000,10000}\do{\xdef\tmpcsv{\ZeckIndices{#1}}%
804 \np{#1} &= {\def\ZeckPrintOne{\ZeckTheFN}\mdgts{\ZeckPrintIndexedSum{\tmpcsv}}}
805 = \mdgts{\ZeckPrintIndexedSum{\tmpcsv}}\\
806 }
807 \xintFor #1 in {100000, 1000000, 10000000}\do{\xdef\tmpcsv{\ZeckIndices{#1}}%
808 \np{#1} &= \def\ZeckPrintOne{\ZeckTheFN}
809 \mdgts{\ZeckPrintIndexedSum{\tmpcsv}}\\ &= \mdgts{\ZeckPrintIndexedSum{\tmpcsv}}\\
810 }
811 \np{\testN} &= \mdgts{\ZeckPrintIndexedSum{\ZeckIndices{\testN}}}
812 \end{align*}
813 This is called the Zeckendorf representation, and it can be given either as
814 above, or as the list of the indices (in decreasing or increasing order), or
815 as a binary word which in the examples above are
816 %
817 \begin{align*}
818 \xintFor #1 in {10,100,1000,10000,100000, 1000000, 10000000}\do{
819 \np{#1} &= \mdgts{\ZeckWord{#1}_{\text{zeck}}}\\
820 }
821 \np{\testN} &= \mdgts{\ZeckWord{\testN}_{\text{zeck}}}\\
822 \np{1000000000} &= \mdgts{\ZeckWord{1000000000}_{\text{zeck}}}
823 \end{align*}
824 %
825 The least significant digit says whether the Zeckendorf representation uses
826 $F_2$ and so on from right to left (one may prefer to put the binary digits in
827 the reverse order, but doing as above is more reminiscent of binary, decimal,
828 or other representations using a given radix).
829
830 % In the next-to-last example the word
831 % length is $\testmaxindex-2+1=\mdgts{\the\numexpr\testmaxindex-1\relax}$, and in
832 % general it is $K-1$ where $K$ is the largest index such that $F_K$ is at most
833 % equal to the given positive integer. For $\np{1000000000}$ this maximal index is
834 % \dgts{\ZeckMaxK{1000000000}} and indeed the associated word has length
835 % \dgts{\expanded{\noexpand\xintLength{\ZeckWord{1000000000}}}}.
836
837 \ifnum\ZeckMaxK{1000000000}=\numexpr
838 \expanded{\noexpand\xintLength{\ZeckWord{1000000000}}}
839 +1\relax\else \ERROR\fi
840
841 In a Zeckendorf binary word the sub-word
842 \dgts{11} never occurs, and this, combined wih the fact that
843 the leading digit is \dgts{1}, characterizes the Zeckendorf words.
844
845
846 \textbf{Donald Knuth} (whose name may ring some bells to \TeX{} users) has
847 defined in 1988 a \textbf{Fibonacci multiplication} (\cite{KNUTHFIBMUL})
848 of positive integers via the formula
849 \begin{equation}\label{eq:kmul}
850 a \circ b = \sum_{i,j} F_{a_i+b_j}\,,
851 \end{equation}
852 where $a = \sum F_{a_i}$ and $b = \sum F_{b_j}$ are the Zeckendorf
853 representations of the positive integers $a$ and $b$. Although it is
854 sometimes true that formula \eqref{eq:kmul} remains valid when using
855 non-Zeckendorf expressions of $a$ and/or $b$ as sums of Fibonacci numbers,
856 this is not a general rule. The next identity by Knuth, which applies
857 whenever three positive integers $a$, $b$, $c$ are expressed via their
858 Zeckendorf representations, is thus non-trivial:
859 \begin{equation}\label{eq:kassoc}
860 (a \circ b) \circ c = \sum_{i,j,k} F_{a_i+b_j+c_k}\,.
861 \end{equation}
862 From it, the associativity of the Fibonacci multiplication
863 follows immediately, the same as
864 commutativity followed immediately from \eqref{eq:kmul}.
865
866 Knuth's proof is combinatorial in nature. \textbf{Pierre Arnoux}
867 (\cite{ARNOUX}) obtained in 1989 a non-combinatorial proof of associativity
868 based upon the identification of a certain subset (or subsets) of the ring
869 $\ZZ[\phi]$, closed under multiplication, and indexed by the positive
870 integers. The circle-product on the indices is mapped to the standard
871 multiplication of these algebraic integers $A_n$: $A_nA_m=A_{n\circ m}$. As
872 by-product of this, he obtained the following remarkable alternative formula
873 for the Knuth product:
874 \begin{equation}
875 \label{eq:arnoux}
876 a \circ b = ab + a\sum_j F_{b_j - 1} + b \sum_i F_{a_i - 1}\;.
877 \end{equation}
878 Again, here we use the Zeckendorf representations of the positive integers $a$
879 and $b$. Clearly formula \eqref{eq:arnoux} is advantageous numerically compared
880 to original definition \eqref{eq:kmul}. Arnoux also re-interpreted a
881 ``star-product'' which had been defined by \textbf{Horacio Porta} and
882 \textbf{Kenneth Stolarsky} (\cite{PORTSTOL}).
883
884 % If we extend the notion of Zeckendorf representation to all non-negative
885 % integers by deciding that $0$ is represented by $F_0$ (chosen in preference to
886 % the empty sum!), then both equations \eqref{eq:kmul} and \eqref{eq:arnoux}
887 % make $0$ act as the identity.
888
889 \textbf{Donald Knuth} (see
890 \cite[7.1.3]{TAOCP4A}) has shown that any relative integer has a unique
891 representation as a sum of the ``NegaFibonacci'' numbers $F_{-n}$, $n\geq1$,
892 again with the condition that no index is repeated and no two indices differ
893 by one. In the special case of zero, the representation is an empty sum. Here
894 is the sequence of these ``NegaFibonacci'' numbers starting at $n=-1$:
895 \[
896 \xinteval{*fibseq(-1,-16)}\dots
897 \]
898
899 In 1957, the twelve-year-old \textbf{George Bergman} (\cite{BERG})
900 introduced the notion of
901 a ``base $\phi$'' number system. This uses
902 $0$ and $1$ as digits but with the ambiguity rule $011\leftrightarrow100$ due
903 to $\phi^2=\phi+1$. He proved that any positive integer can be represented
904 this way finitely, i.e.\@ is a \emph{finite} sum of powers $\phi^k$, with
905 decreasing relative integers as exponents (i.e.\@ each power occurring at most
906 once and it is crucial that negative powers are allowed). For example:
907 \[ 100 =
908 \PhiPrintIndexedSum{\PhiExponents{100}}
909 %\phi^9 + \phi^6 + \phi^3 + \phi + \phi^{-4} + \phi^{-7} + \phi^{-10}
910 =
911 \PhiBasePhi{100}_\phi\,.
912 %1001001010.0001001001_\phi\,.
913 \]
914 %
915 Such a finite ``phi-ary'' representation (it seems ``phi-representation'' is the
916 more commonly used term in academia) is unique if one adds the condition that
917 no two exponents differ by one. This is equivalent to requiring that the
918 number of terms is minimal. The real numbers which can be represented by such
919 finite sums are exactly the positive numbers in $\ZZ[\phi]$, i.e. all
920 combinations $p + q \phi$ with $p$ and $q$ relative integers which turn out to
921 be strictly positive.
922 \[100 - 30\phi = \PhiPrintIndexedSum{\PhiExponents{{100}{-30}}}
923 =
924 \PhiBasePhi{{100}{-30}}_\phi\,.
925 \]
926
927
928 The naive approach to obtain the finite phi-representations, and actually
929 prove that they do exist for all positive integers, is to show how to
930 repeatedly add $1$ (hence also powers of $\phi$). One then only needs to
931 explain how to subtract $1$ (hence also powers of $\phi$) to deduce
932 that all $p+q\phi>0$ are representable. This is actually
933 what Bergman did. If one wants, as we do, to be able to obtain the
934 representations for integers having say more than a few decimal digits, this
935 theoretical approach is simply not feasible as is, one needs a bit more
936 thinking.
937
938 A theoretical way, called the ``greedy'' algorithm, is based upon the fact
939 that for any $x = p+q\phi >0$, the maximal exponent $k\in\ZZ$ in its minimal
940 representation is characterized by $\phi^k\leq x<\phi^{k+1}$. So one only
941 needs to get $k$ and then replace $x$ by $x-\phi^k$. Doing this using
942 floating point number calculations will only be able to handle integers with
943 few enough digits to be exactly representable, and may lead at some point to a
944 negative $x$, hence fail, due to rounding errors. So here again one has to
945 think a bit.
946
947 This has been done by the author, and the resulting algorithm is implemented
948 (expandably) here in \eTeX{}.%
949 %
950 \footnote{As the intrepid reader will see if looking at the code, this uses a
951 little bit floating point logarithms witn mantissas of eight decimal digits.
952 This is because we have arbitrary precision logarithm available from
953 \xintexprname, with the fastest being with eight decimal digits precision,
954 and after all we were not preparing a reference paper for \emph{Mathematics
955 of Computation} but simply aiming at computing for fun as efficiently as
956 we could using tools at our disposal. This shortcut induces a theoretical
957 upper bound on the size of the starting $x$: if it is an integer it must
958 have less than say about one million decimal digits (see
959 \autoref{sssec:maxe} for details). As we can do computations (with \TeX
960 Live 2025 default memory settings) only up to about |13000| decimal digits
961 (and in reasonable time up to less than |1000| digits), this is not a
962 problem to us. And if we were to use logarithms with about 16 decimal
963 digits of precision, the theoretical limit would raise to say inputs of less
964 than about $10^{14}$ decimal digits. Each of our decimal digit occupies one
965 word of computer memory, and even if we were using a programming language
966 manipulating binary numbers, we would need more than $37$ terabytes of
967 computer memory to store the binary representation of (one less than) $10$
968 to the power $10^{14}$, so using double precision floats (which are close to
969 having 16 decimal digits of precision) is largely enough to cover real-life
970 cases. Nevertheless, in the |0.9d| code comments we briefly describe how we
971 could proceed all the way using only integer arithmetic with no theoretical
972 limit on input size. See \autoref{sssec:maxe}. Similar remarks apply to
973 Zeckendorf representations.%
974 }
975 %
976 Of course this is only elementary mathematics
977 and it would be extremely surprising if the algorithm was not in the
978 literature. Inputs of hundreds of digits are successfully handled. The same,
979 implemented in C or other language with a library for big integers, would of
980 course go way beyond and be a thousand times faster.
981
982 An ``integer-only'' algorithm (i.e.\@ an algorithm which can be made to
983 process only integers, but is in fact restricted to them; to compare, the
984 approach described in the previous paragraph is in principle also
985 implementable using integers only, but it applies to all $x=p+q\phi>0$ not
986 only to integers) to obtain the Bergman minimal $\phi$-representation of a
987 positive integer $N$ is explained by \textbf{Donald Knuth} in the solution to
988 Problem 35 of section 1.2.8 from \cite{TAOCP1} (there is a typographical error
989 with a missing negative sign in an exponent there, on page 495; this has been
990 reported to the author). It starts with the position of $N$ with respect to
991 Lucas numbers, the more subtle case being when $N$ follows an odd indexed
992 Lucas number.
993
994 One has to think a bit how to find efficiently the largest Lucas number at
995 most equal to $N$, when $N$ has hundreds of digits. This is about the same as
996 identifying the maximal $k$ such as $\phi^k\leq N$, as $\phi^k + (-1)^k
997 \phi^{-k} = L_k$ is an integer. It is also very similar to finding the
998 Zeckendorf maximal index which essentially means to locate $\sqrt{5}N$ with
999 respect to powers of $\phi$ (as $\phi^k - (-1)^k \phi^{-k}$ for $k\geq1$
1000 belongs to $\sqrt5 \NN$).
1001
1002 For $x=N$ an \textbf{integer} (at least $2$) it can be proven that the smallest
1003 contribution $\phi^{-\ell}$ to the minimal Bergman representation is with
1004 $\ell=k$ if $k$ is even and $\ell=k+1$ is $k$ is odd. Otherwise stated $\ell$
1005 is the smallest even integer at least equal to $k$. (So we can always find
1006 the location of the radix separator if we had lost it).
1007
1008 \textbf{Christiane Frougny} and \textbf{Jacques Sakarovitch} (\cite{FROUSAKA})
1009 showed that there exists a (non explicited) finite two-tape automaton which
1010 converts the Zeckendorf expansion of a positive integer into the Bergman
1011 representation (where the part with negative exponents is ``folded'' across
1012 the radix point to sit on top (or below) the part with positive exponents).
1013 Very recently \textbf{Jeffrey Shallit} (\cite{SHAL}) has revisited this topic and
1014 constructed explicitly a Frougny-Sakarovitch automaton.
1015
1016
1017 \iffalse
1018 \begin{equation}
1019 \label{eq:phipowers}
1020 \forall n\in\ZZ\quad \phi^n = F_{n-1} + F_n \phi\,,
1021 \end{equation}
1022 observe that the above expression of $100$ is equivalent to the two integer
1023 only relations:
1024 \begin{align*}
1025 \xinteval{add(fib(n-1), n = 9,6,3,1,-4,-7,-10)}
1026 &=F_8 + F_5 + F_2 + F_0 + F_{-5} + F_{-8} + F_{-11}\,,
1027 \\
1028 \ZeckNFromIndices{9,6,3,1,-4,-7,-10}
1029 &= F_9 + F_6 + F_3 + F_1 + F_{-4} + F_{-7} + F_{-10}\,.
1030 \end{align*}
1031 Due to \textbf{Binet}'s formulas, the last relation expresses that $\phi^9 +
1032 \phi^6 + \phi^3 + \phi^1 + \phi^{-4} + \phi^{-7} + \phi^{-10}$ is invariant by
1033 the automorphism $\phi\mapsto -\phi^{-1}=1-\phi$ of $\ZZ[\phi]$. This confirms
1034 that it has to be an integer.
1035 \fi
1036 %\bibliographystyle{unsrt}
1037 %\bibliography{zeckendorf}
1038 \begin{thebibliography}{10}
1039
1040 \bibitem{LEKK}
1041 C.~G. Lekkerkerker.
1042 \newblock Voorstelling van natuurlijke getallen door een som van getallen van
1043 {F}ibonacci.
1044 \newblock {\em Simon Stevin}, 29:190--195, 1951-1952.
1045
1046 \bibitem{ZECK}
1047 E.~Zeckendorf.
1048 \newblock Repr\'esentation des nombres naturels par une somme de nombres de
1049 {F}ibonacci ou de nombres de {L}ucas.
1050 \newblock {\em Bull. Soc. Roy. Sci. Li\`ege}, 41:179--182, 1972.
1051
1052 \bibitem{KNUTHFIBMUL}
1053 Donald~E. Knuth.
1054 \newblock Fibonacci multiplication.
1055 \newblock {\em Appl. Math. Lett.}, 1(1):57--60, 1988.
1056
1057 \bibitem{ARNOUX}
1058 Pierre Arnoux.
1059 \newblock Some remarks about {F}ibonacci multiplication.
1060 \newblock {\em Appl. Math. Lett.}, 2(4):319--320, 1989.
1061
1062 \bibitem{PORTSTOL}
1063 H.~Porta and K.~B. Stolarsky.
1064 \newblock The edge of a golden semigroup.
1065 \newblock In {\em Number theory, {V}ol.\ {I} ({B}udapest, 1987)}, volume~51 of
1066 {\em Colloq. Math. Soc. J\'anos Bolyai}, pages 465--471. North-Holland,
1067 Amsterdam, 1990.
1068
1069 \bibitem{TAOCP4A}
1070 Donald~E. Knuth.
1071 \newblock {\em The art of computer programming. {V}ol. 4{A}. {C}ombinatorial
1072 algorithms. {P}art 1}.
1073 \newblock Addison-Wesley, Upper Saddle River, NJ, 2011.
1074
1075 \bibitem{BERG}
1076 George Bergman.
1077 \newblock A number system with an irrational base.
1078 \newblock {\em Math. Mag.}, 31:98--110, 1957/58.
1079
1080 \bibitem{TAOCP1}
1081 Donald~E. Knuth.
1082 \newblock {\em The art of computer programming. {V}ol. 1}.
1083 \newblock Addison-Wesley, Reading, MA, third edition, 1997.
1084 \newblock Fundamental algorithms.
1085
1086 \bibitem{FROUSAKA}
1087 Christiane Frougny and Jacques Sakarovitch.
1088 \newblock Automatic conversion from {F}ibonacci representation to
1089 representation in base {$\phi$}, and a generalization.
1090 \newblock volume~9, pages 351--384. 1999.
1091 \newblock Dedicated to the memory of Marcel-Paul Sch\"utzenberger.
1092
1093 \bibitem{SHAL}
1094 Jeffrey Shallit.
1095 \newblock Proving properties of {$\varphi$}-representations with the
1096 \texttt{{W}alnut} theorem-prover.
1097 \newblock {\em Commun. Math.}, 33(2):Paper No. 3, 33, 2025.
1098
1099 \end{thebibliography}
1100
1101 \part{User manual}
1102
1103 \section{Use on the command line}
1104 \label{sec:interactive}
1105
1106 Open a command line window and execute:
1107 \centeredline{|etex zeckendorf|}
1108 then follow the displayed instructions.
1109
1110 The (\TeX{} Live) |*tex| executables are not linked with the |readline|
1111 library, and this makes interactive use quite painful. If you are on a decent
1112 system, launch the interactive session rather via
1113 \centeredline{|rlwrap etex zeckendorf|}
1114 for a smoother experience.
1115
1116
1117 \section{The core package features}
1118 \label{sec:macros}
1119
1120 \subsection{Algebra in \texorpdfstring{$\QQ(\phi)$}{Q(phi)}, extensions to the
1121 \cs{xinteval} syntax}
1122 \label{ssec:xinteval}
1123
1124 The |\xinteval| syntax is extended in the following manner:
1125 \begin{enumerate}
1126 \item Bracketed pairs |[a, b]| represent $a +
1127 b\phi$, where $\phi$ is the golden ratio, and one can operate on them with
1128 |+|, |-| (also as prefix unary operator), |*|, |/|, and |^| (or |**|)
1129 to do additions, subtractions, multiplications,
1130 divisions and powers with integer exponents.
1131
1132 So |a| and |b| can be rational numbers and are not limited to integers for
1133 these computations.
1134
1135 |phi| stands for |[0,1]| and its conjugate |psi = [1, -1]| is
1136 defined also. One can use on input |a + b phi|, which on output will be
1137 printed as |[a, b]|.
1138
1139 \textit{\textbf{DO NOT USE |\phi| OR |\psi|...} except if redefined to expand
1140 to the letters |phi| and |psi| but this not recommended...}!
1141 \begin{everbatim*}
1142 \xinteval{phi^50, psi^50, phi^50 * psi^50}
1143 \end{everbatim*}
1144 \begin{everbatim*}
1145 \xinteval{(1+phi)(10-7phi)(3+phi)/(2+phi)^3}
1146 \end{everbatim*}
1147 \begin{everbatim*}
1148 \xinteval{add(phi^n, n = -4,-7,-10, 1, 3, 6, 9)}
1149 \end{everbatim*}
1150 \begin{everbatim*}
1151 \xinteval{phi^20 / phi^10}
1152 \end{everbatim*}
1153
1154 \begin{TeXnote}
1155 When dividing, and except if both operands are scalars, the coefficients of
1156 the result are reduced to their smallest terms; but for scalar-only division,
1157 one needs to use the |reduce()| function explicitly.
1158
1159 The |[0, 0]| acts as |0| in operations, but is not automatically replaced
1160 by it, if produced by a subtraction for example. It is not allowed as an
1161 exponent for powers.
1162 \end{TeXnote}
1163 \item The functions |phisign()|, |phiabs()|, |phinorm()|, |phiconj()| do what
1164 one expects.
1165
1166 \begin{snugshade}
1167 \textbf{Attention:} |\xinteval| functions are always used with parentheses, not
1168 with curly braces, contrarily to macros!
1169 \end{snugshade}
1170
1171 \begin{everbatim*}
1172 \xinteval{phisign(10000 - 6180 phi)}
1173 \end{everbatim*}
1174 \begin{everbatim*}
1175 \xinteval{phisign(10000 - 6181 phi)}
1176 \end{everbatim*}
1177 \begin{everbatim*}
1178 \xinteval{phiabs(10000 - 6181 phi)}
1179 \end{everbatim*}
1180 \begin{everbatim*}
1181 \xinteval{phinorm(10000 - 6180 phi)}
1182 \end{everbatim*}
1183 \begin{everbatim*}
1184 \xinteval{(10000 - 6180 phi) * phiconj(10000 - 6180 phi)}
1185 \end{everbatim*}
1186
1187 \item The function |fib()| computes the Fibonacci numbers (also for negative
1188 indices), and |fibseq(a,b)| will compute a consecutive stretch of
1189 them from index |a| to index |b| (one may also have |b=a|, or |b<a|).
1190
1191 \begin{everbatim*}
1192 \xinteval{seq(fib(n), n=-5..5, 10, 20, 100)}
1193 \end{everbatim*}
1194 \begin{everbatim*}
1195 \xinteval{seq(fib(2^n), n=1..7)}
1196 \end{everbatim*}
1197 \begin{TeXnote}
1198 In the next example, |\xintFor| expands only once, but |\xinteval| needs
1199 two expansion steps so we use |\expanded| wrapper. We could have used
1200 |\xintFor*| but then we need |\xintCSVtoList| wrapper. We also could have
1201 used some |\romannumeral-`0| prefix but I figured |\expanded| looked less
1202 scary. For details on |\xintFor/\xintFor*| check the \xinttoolsname
1203 documentation.
1204 \end{TeXnote}
1205 \begin{everbatim*}
1206 \xintFor #1 in {\expanded{\xinteval{*fibseq(100, 110)}}}%
1207 \do{#1\xintifForLast{.\par}{,\newline}}
1208 \end{everbatim*}
1209
1210 In the previous example, note the syntax |*fibseq(100,110)|. Indeed
1211 |fibseq(a,b)| produces a |nutple| (see \xintexprname documentation), i.e.\@
1212 the output will display brackets |[...]| (even if |a=b|):
1213 \begin{everbatim*}
1214 \xinteval{fibseq(20, 25)}
1215 \end{everbatim*}
1216
1217 With the |*| prefix the brackets are removed.
1218
1219 \item The |zeckindices()| function computes the indices needed for the
1220 Zeckendorf representation. The input must be an integer. If negative,
1221 it is replaced by its opposite. The zero input gives an empty output
1222 (i.e. is printed as |[]|).
1223 \begin{everbatim*}
1224 \xinteval{zeckindices(123456789)}
1225 \end{everbatim*}
1226
1227 We use the |*| prefix to not have brackets in the output.
1228 \begin{everbatim*}
1229 \xinteval{*zeckindices(123456789123456789123456789)}
1230 \end{everbatim*}
1231 \begin{everbatim*}
1232 \xinteval{*zeckindices(1e40)}
1233 \end{everbatim*}
1234
1235 It is easy with this syntax to manipulate the indices in various ways. Let's
1236 print them from smallest to largest:
1237 \begin{everbatim*}
1238 \xinteval{*reversed(zeckindices(123456789123456789123456789))}
1239 \end{everbatim*}
1240
1241 The power of |\xinteval|, always eager to prove |A=A|, can be demonstrated:
1242 \begin{everbatim*}
1243 \xinteval{add(fib(n), n = *zeckindices(123456789))}
1244 \end{everbatim*}
1245 \begin{everbatim*}
1246 \xinteval{add(fib(n), n = *zeckindices(123456789123456789123456))}
1247 \end{everbatim*}
1248
1249 \item the |$| %$
1250 is added as infix operator on \emph{positive} integers (it will error if
1251 used with non-positive integers), to compute the Knuth Fibonacci multiplication.
1252 It does it using the Arnoux formula \eqref{eq:arnoux}. The |$$| %$$
1253 does the same but using the original Knuth formula \eqref{eq:kmul}.
1254
1255 For examples see \autoref{sssec:kmul}.
1256 \item The |phiexponents()| function computes the exponents in the Bergman
1257 $\phi$-representation of its input. This input must be either an integer or
1258 a bracketed pair |[a,b]| or equivalently |a + b phi|, standing for $a+b\phi$
1259 with $a$ and $b$ relative integers. It $a + b\phi<0$ it is
1260 replaced by its opposite. The output is the empty nutple |[]| if input is
1261 zero. Non-integer input is truncated to integers.
1262
1263 The |phiexponents()| function produces a bracketed list.
1264 \begin{everbatim*}
1265 \xinteval{phiexponents(100)}\newline
1266 \xinteval{phiexponents(100 - 50phi)}\newline
1267 \xinteval{phiexponents(-100 + 50phi)}\newline
1268 \xinteval{phiexponents(100 - 50psi)}
1269 \end{everbatim*}
1270
1271 We can use |*| prefix as already indicated if we prefer not to see the brackets:
1272 \begin{everbatim*}
1273 \xinteval{*phiexponents(3141592653)}
1274 \end{everbatim*}
1275 \end{enumerate}
1276 %
1277 The added |\xinteval| syntax elements are also sometimes examplified alongside
1278 their respective matching macros. Not all macros defined by the package are
1279 documented, because documentation takes incredible amount of times and induces
1280 costly maintenance. See the commented source code.
1281
1282 \begin{titled-frame}{Important}
1283 The added syntax elements are only defined for |\xinteval|. It is possible
1284 though to access them inside of |\xintfloateval| using the
1285 lower-level |\xintexpr|. Here is an example:
1286 \begin{everbatim*}
1287 \xintfloateval{\xintexpr fib(100) / fib(99)\relax}
1288 \end{everbatim*}
1289
1290 The variables |phi| and |psi| can not be used for operations directly inside
1291 of |\xintfloateval|. And they should not be redefined as floating point variables,
1292 as this would break their usage in |\xinteval|.
1293 %
1294 But one can transfer computations after having defined first an auxiliary
1295 |\xintfloateval| function:
1296 \begin{everbatim*}
1297 \xintdeffloatfunc phi_to_fp(x):= x[0] + (1+sqrt(5))/2 * x[1];
1298 \end{everbatim*}
1299 Then one can use it this way:
1300 \begin{everbatim*}
1301 \xintfloateval{phi_to_fp(\xintexpr (1+phi)(1+2phi)(1-3phi)\relax)}
1302 \end{everbatim*}
1303 \end{titled-frame}
1304
1305 \subsection{Fibonacci numbers}
1306
1307 \subsubsection{\cs{ZeckTheFN}}
1308
1309 This macro computes Fibonacci numbers.
1310 \begin{everbatim*}
1311 \ZeckTheFN{100}
1312 \end{everbatim*}
1313 \begin{everbatim*}
1314 \ZeckTheFN{100 + 15}
1315 \end{everbatim*}
1316
1317 As shown, the argument can be an integer expression (only in the sense of
1318 |\inteval|, not in the one of |\xinteval|, for example you can not have
1319 powers only additions and multiplications). Negative arguments are allowed:
1320 \begin{everbatim*}
1321 \ZeckTheFN{0}, \ZeckTheFN{-1}, \ZeckTheFN{-2}, \ZeckTheFN{-3},
1322 \ZeckTheFN{-4}
1323 \end{everbatim*}
1324
1325 \begin{titled-frame}{|fib()|}
1326 The syntax of |\xinteval| is extended via addition of a |fib()| function,
1327 which gives a convenient interface. See its documentation in
1328 \autoref{ssec:xinteval}.
1329 \end{titled-frame}
1330 \subsubsection{\cs{ZeckTheFSeq}}
1331
1332 This computes not only one but a whole contiguous
1333 series of Fibonacci numbers but its output format is a sequence of braced
1334 numbers, and tools such as those of \xinttoolsname are needed to manipulate
1335 its output. For this reason it is not further documented here.
1336
1337 \begin{titled-frame}{|fibseq()|}
1338 The syntax of |\xinteval| is extended via addition of a |fibseq()|
1339 function, which gives a convenient interface:
1340 \begin{everbatim*}
1341 \xinteval{fibseq(10,20)}
1342 \end{everbatim*}
1343
1344 \noindent Notice the square brackets used on output. In the terminology of
1345 \xintexprname, the function produces a |nutple|. Use the |*| prefix to
1346 remove the brackets:
1347 \begin{everbatim*}
1348 \xinteval{*fibseq(-10,-20)}
1349 \end{everbatim*}
1350 \end{titled-frame}
1351
1352 \subsection{Zeckendorf representation}
1353
1354
1355 \subsubsection{\cs{ZeckIndices}}
1356
1357 This computes the Zeck representation as a comma
1358 separated list of indices. The input is only \emph{f}-expanded, if you need it
1359 to be an expression you must wrap it in |\xinteval|. A negative input will
1360 be replaced by its absolute value. A vanishing input gives an empty output.
1361
1362 The macro is also known as |\ZeckZeck|.
1363
1364 \begin{everbatim*}
1365 \ZeckZeck{123456789123456789123456789}
1366 \end{everbatim*}
1367
1368 \begin{titled-frame}{|zeckindices()|}
1369 The syntax of |\xinteval| is extended via addition of a |zeckindices()|
1370 function, which gives a more convenient interface.
1371 \end{titled-frame}
1372
1373
1374 \subsubsection{\cs{ZeckWord}}
1375
1376 This computes the Zeck representation as a binary word.
1377 The input is only \emph{f}-expanded, if you need it
1378 to be an expression you must wrap it in |\xinteval|.
1379
1380 A zero input gives an empty output and a negative input is replaced by its
1381 absolute value.
1382
1383 \begin{everbatim*}
1384 \ZeckWord{123456789}
1385 \end{everbatim*}
1386 \begin{everbatim*}
1387 \ZeckWord{\xinteval{2^40}}
1388 \end{everbatim*}
1389
1390 As \TeX{} does not by default split long strings of digits at the line ends,
1391 we gave so far only some small examples. See \xintname or \bnumexprname
1392 documentations for a \cs{printnumber} macro able to add linebreaks. Using
1393 such an auxiliary (a bit refined) we can for example obtain this:
1394 \begin{everbatim}
1395 \ZeckWord{\xinteval{2^100}}
1396 \end{everbatim}
1397
1398 \begingroup\colorlet{shortverbcolor}{everbatimxfgcolor}
1399 \expandafter|\expanded{\ZeckWord{\xinteval{2^100}}}|
1400 \endgroup
1401
1402 Compare the above with the list of indices in the Zeckendorf representation:
1403 \textcolor{everbatimxfgcolor}{\ZeckZeck{\xinteval{2^100}}}.
1404
1405 \subsubsection{\cs{ZeckNFromIndices}}
1406
1407 This computes an integer from a list of (comma
1408 separated) indices. These indices do not have to be positive, their order
1409 is indifferent and they can be repeated or differ by only one unit. The
1410 list is allowed to be empty. Contiguous commas (or commas separated only by
1411 space characters) act as a single one, a final comma is tolerated. A new
1412 \emph{f}-expansion is done at each item, they can be (\emph{f}-expandable)
1413 macros.
1414
1415 \begin{everbatim*}
1416 \ZeckNFromIndices{}\newline
1417 \ZeckNFromIndices{100, ,,, 90, 80, 70, 60, 50, 40, 30 , , ,,,}
1418 \end{everbatim*}
1419 \begin{everbatim*}
1420 \ZeckIndices{357128524055170099155}
1421 \end{everbatim*}
1422 \begin{everbatim*}
1423 \ZeckIndices{\ZeckNFromIndices{100, 90, 80, 70, 60, 50, 40, 30}}
1424 \end{everbatim*}
1425 \begin{everbatim*}
1426 \ZeckNFromIndices{3,-1,4,-1,5,-9,2,-6,5,-3}
1427 \end{everbatim*}
1428
1429 \begin{titled-frame}{Emulation inside \cs{xinteval}}
1430 There is no associated |\xinteval| function but the functionality is a
1431 one-liner in its syntax:
1432 \begin{everbatim*}
1433 \xinteval{add(fib(i), i= 100, 90, 80, 70, 60, 50, 40, 30)}
1434 \end{everbatim*}
1435 \begin{everbatim*}
1436 \xinteval{add(fib(i), i= 3, -1, 4, -1, 5, -9, 2, -6, 5, -3)}
1437 \end{everbatim*}
1438
1439 It is even a one-liner to define by oneself the missing function:
1440 \begin{everbatim*}
1441 \xintdeffunc Nfromlist(x):= add(fib(i), i = *x);
1442 \xinteval{Nfromlist([100, 90, 80, 70, 60, 50, 40, 30])}\\
1443 \xinteval{Nfromlist(zeckindices(1e60))}
1444 \end{everbatim*}
1445 \end{titled-frame}
1446
1447
1448 \subsubsection{\cs{ZeckNfromWord}}
1449
1450 This computes a positive integer from a binary word.
1451 The word can be arbitrary apart from not being empty.
1452
1453 \begin{everbatim*}
1454 \ZeckNfromWord{1}, \ZeckNfromWord{11}, \ZeckNfromWord{111},
1455 \ZeckNfromWord{1111}, \ZeckNfromWord{11111}
1456 \end{everbatim*}
1457 \begin{everbatim*}
1458 \ZeckNfromWord{\xintReplicate{30}{10}}
1459 \end{everbatim*}
1460 \begin{everbatim*}
1461 \ZeckWord{4052739537880}
1462 \end{everbatim*}
1463
1464
1465 \subsection{Knuth Fibonacci Multiplication}
1466
1467 \subsubsection{\cs{ZeckKMul}, \cs{ZeckAMul}}
1468 \label{sssec:kmul}
1469
1470 Both compute the Knuth multiplication of its two
1471 \textbf{positive} integer arguments. The former, using formula
1472 \eqref{eq:kmul}, the latter using \eqref{eq:arnoux}.
1473 The two arguments are only
1474 \emph{f}-expanded, you need to wrap each in an |\xinteval| if
1475 it is an expression.
1476
1477 \begin{everbatim*}
1478 \ZeckKMul{100}{200}, \ZeckAMul{100}{200}
1479 \end{everbatim*}
1480 \begin{everbatim*}
1481 \ZeckKMul{\ZeckKMul{100}{200}}{300},
1482 \ZeckAMul{\ZeckKMul{100}{200}}{300}
1483 \end{everbatim*}
1484 \begin{everbatim*}
1485 \ZeckKMul{100}{\ZeckKMul{200}{300}},
1486 \ZeckAMul{100}{\ZeckKMul{200}{300}}
1487 \end{everbatim*}
1488
1489 \begin{titled-frame}{|$|, |$$|}%$$$
1490 The syntax of |\xinteval| is extended via addition of a |$| %$
1491 infix operator computing according to the Arnoux formula
1492 \eqref{eq:arnoux}, and |$$| computing according to the Knuth formula
1493 \eqref{eq:kmul}.% $$
1494 \begin{everbatim*}
1495 \xinteval{(100 $ 200) $ 300, 100 $ (200 $ 300), 100 $$ 200 $$ 300}
1496 \end{everbatim*}
1497 \end{titled-frame}
1498
1499 Let us mention here that we could have
1500 defined a |knuth()| function easily using the powerful |\xinteval| syntax:%
1501 \begin{everbatim*}
1502 \xintNewFunction{knuth}[2]
1503 {add(fib(x), x = flat(ndmap(+, *zeckindices(#1); *zeckindices(#2);)))}
1504 \xinteval{knuth(100,200), knuth(knuth(100,200),300),
1505 knuth(100,knuth(200,300))}
1506 \end{everbatim*}
1507 \begin{TeXnote}
1508 We could not have used \expandafter|\string\xintdeffunc| here to define
1509 |knuth()|, so we used the \expandafter|\string\xintNewFunction| interface.
1510 The sole inconvenient is that when using |knuth()| it is as if we injected by
1511 hand the replacement expression, which will have to be parsed by
1512 \cs{xinteval}.
1513
1514 About using |ndmap()| with |+| as first argument, it is related to
1515 \xintexprname having defined a |`+`| function. So we can also use |*| here,
1516 but not |-| or |/|.
1517 \end{TeXnote}
1518
1519 The advantage is that we have now the means to check the validity of Knuth's
1520 triple product formula \eqref{eq:kassoc}:
1521 \begin{everbatim*}
1522 \xintNewFunction{knuththree}[3]
1523 {add(fib(x), x= flat(ndmap(+, *zeckindices(#1);
1524 *zeckindices(#2);
1525 *zeckindices(#3);)))}
1526 \xinteval{knuththree(100, 200, 300),
1527 100 $ 200 $ 300,
1528 100 $$ 200 $$ 300}
1529 \newline
1530 \xinteval{knuththree(1000, 2000, 3000),
1531 1000 $ 2000 $ 3000,
1532 1000 $$ 2000 $$ 3000}
1533 \end{everbatim*}
1534
1535 \subsubsection{\cs{ZeckSetAsKnuthOp}, \cs{ZeckSetAsArnouxOp}}
1536
1537 This takes as input a
1538 character, or multiple characters, and turns them (as a unit) into an infix
1539 operator computing the Knuth multiplication, respectively according to the
1540 original Knuth definition \eqref{eq:kmul} or to the Arnoux formula
1541 \eqref{eq:arnoux}. The pre-defined meanings of |$| or |$$| for this will not
1542 be canceled. One may use %$
1543 |\ZeckDeleteOperator|\marg{operator} to delete the existing meaning of an
1544 |\xinteval| operator.
1545
1546 \begin{titled-frame}{IMPORTANT}
1547 There is NO WARNING if you override a pre-existing operator from the
1548 |\xinteval| syntax, and not all such operators are user-documented because
1549 some exist for internal purposes only. But if done inside a group or
1550 environment, the former meaning will be recovered on exit.
1551 \end{titled-frame}
1552
1553 There are a few important points to be aware of:
1554 \begin{itemize}[nosep]
1555 \item You can use a letter such as |o| as operator but it then must be used
1556 prefixed by |\string| which is not convenient:
1557 \begin{everbatim*}
1558 \ZeckSetAsArnouxOp{o}
1559 \xinteval{100 \string o 200 \string o 300}
1560 \end{everbatim*}
1561 \item
1562 With a Unicode engine, they are plenty of available characters
1563 that are already of catcode 12. For example:
1564 \begin{everbatim}
1565 \ZeckSetAsArnoux{⊙}
1566 \xinteval{100 ⊙ 200 ⊙ 300}
1567 \end{everbatim}
1568 \begingroup
1569 \iftutex
1570 \catcode`⊙=12
1571 \ZeckSetAsArnouxOp{⊙}
1572 \noindent\textcolor{everbatimxfgcolor}{\xinteval{100 ⊙ 200 ⊙ 300}}
1573 \else
1574 \noindent\textcolor{everbatimxfgcolor}{\xinteval{100 $ 200 $ 300}}
1575 \fi
1576 \endgroup
1577
1578 You can also use letters from Greek or other scripts, but make
1579 sure they have catcode 12.
1580
1581
1582 \item It is not possible to use as operator a control sequence such as
1583 |\odot|. It has to be one or more non-letter characters. It can not be
1584 the period (full stop) which, although not being a predefined operator is
1585 recognized as decimal separator.
1586
1587 \item In case your document is compiled with |pdflatex| or |latex| and uses
1588 Babel, some characters may be catcode active. To make them part of a name
1589 of an operator defined by |\ZeckSetAsKnuthOp|, each such catcode active
1590 character has to be prefixed with |\string| in the argument of
1591 |\ZeckSetAsKnuthOp|. But |\string| is then \emph{unneeded} inside
1592 |\xinteval| (since \xintexprname |1.4n|).
1593 \end{itemize}
1594
1595
1596 \subsection{Bergman phi-representation}
1597
1598
1599 \subsubsection{\cs{PhiExponents}}
1600
1601 It has a unique mandatory argument which can be (or expand too) either an
1602 integer |a|, or two braced integers |{a}{b}|.
1603
1604 It outputs the comma separated list of the exponents from the minimal Bergman
1605 representation of the absolute value of $a + b \phi$. If $a + b\phi<0$, this
1606 list will be prefixed by a period. If $a=b=0$, the output is empty.
1607
1608 \begin{titled-frame}{|phiexponents()|}
1609 The syntax of |\xinteval| is extended via addition of a |phiexponents()|
1610 function, which gives a more convenient interface.
1611
1612 Contrarily to the macro, it loses the information about the sign of the
1613 input and tacitly replaces it with its absolute value.
1614
1615 See \autoref{ssec:xinteval} for examples.
1616 \end{titled-frame}
1617 \begin{everbatim*}
1618 \[100\rightarrow \PhiExponents{100}\]
1619 \end{everbatim*}
1620 \begin{everbatim*}
1621 \[100\phi\rightarrow \PhiExponents{{0}{100}}\]
1622 \end{everbatim*}
1623 \begin{everbatim*}
1624 \[1000000\rightarrow \PhiExponents{1000000}\]
1625 \end{everbatim*}
1626 \begin{everbatim*}
1627 $10^{20}\rightarrow{}$ \PhiExponents{\xinteval{10^20}}.
1628 \end{everbatim*}
1629
1630 We did not use math mode for the longer output, because \TeX{} needs extra
1631 instructions to wrap the line.
1632 But the separator can be customized to this aim:
1633 % 0.5pt ne serait pas suffisant
1634 \begin{everbatim*}
1635 \renewcommand\PhiExponentsSep
1636 {,\allowbreak\hskip0pt plus 1pt\relax}
1637 $10^{50}\rightarrow \PhiExponents{\xinteval{10^50}}$.
1638 \end{everbatim*}
1639
1640 The attentive reader will have noticed though that our math mode does not
1641 differ much from our nice monospace text mode. Maybe look at some other
1642 \LaTeX{} package
1643 by the author to find some clues explaining this top-quality typesetting.
1644
1645 \subsubsection{\cs{PhiBasePhi}}
1646
1647 It has a unique mandatory argument which can be (or expand two) either an
1648 integer |a|, or two braced integers |{a}{b}|.
1649
1650 It computes the Bergman $\phi$-representation of $x=a+b\phi$ if $x$ turns out
1651 to be positive, outputs $0$ if both $a$ and $b$ vanish, and outputs a minus
1652 sign followed with the expansion of the opposite of $x$ if $x<0$.
1653
1654 The output for positive $x$ is a sequence of $1$'s and $0$'s with possibly a
1655 period as radix separator (it can be customized, see next), which either
1656 starts with a leading |1| or with zero followed by the radix separator
1657 |0.|. It always ends with a |1|. No |1| is repeated.
1658
1659 The arguments are \emph{x}-expanded, if you need them
1660 to be expressions you must wrap them using |\xinteval|.
1661
1662 A little stress-test:
1663 \begin{everbatim*}
1664 \begin{multicols}{2}
1665 \xintFor* #1 in {\xintSeq[-1]{20}{-21}}\do{%
1666 $\phi^{#1}=\PhiBasePhi{{\ZeckTheFN{#1-1}}%
1667 {\ZeckTheFN{#1}}}_\phi$\par
1668 }
1669 \end{multicols}
1670 \end{everbatim*}
1671
1672 The radix separator
1673 is customizable as |\PhiBasePhiSep|. It defaults to a period |.|.
1674 If, for example, you use the package \ctanpackage{numprint}, you could
1675 do
1676 \begin{everbatim}
1677 \makeatletter\renewommand{\PhiBasePhiSep}{\nprt@decimal}\makeatother
1678 \end{everbatim}
1679 for the output to use the radix separator as set via |\npdecimalsign|
1680 command. It is easier to simply wrap usage of |\PhiBasePhi| inside of
1681 |\numprint| (or shortcut |\np|) command of that package.
1682 \begin{TeXnote}
1683 For fancy set-ups, for example for a radix separator using color, it is
1684 recommended to use |\RenewDocumentCommand|. Indeed, it turns out that
1685 |\PhiBasePhi| will trigger an expansion context, and the radix separator
1686 has to be compatible with it.
1687 \end{TeXnote}
1688 \begin{everbatim*}
1689 % or \protected\def if not using LaTeX
1690 \RenewDocumentCommand\PhiBasePhiSep{}{{\mathcolor{blue}{.}}}
1691 \begin{gather*}
1692 \xintFor #1 in {1, 10, 100, 1000, 10000, 100000, 1000000}\do{%
1693 #1 = \PhiBasePhi{#1}_\phi \xintifForLast{}{\\}}
1694 \end{gather*}
1695 \end{everbatim*}
1696 \vspace*{-\baselineskip}
1697 \begin{titled-frame}{Tip}
1698 As mentioned elsewhere, \TeX{} and \LaTeX{} will not per default split
1699 long sequences of zeros and ones at ends of lines.
1700 See \xintname or \bnumexprname
1701 documentations for a \cs{printnumber} macro able to add linebreaks. Using
1702 such an auxiliary (a bit refined) we can for example obtain this:
1703 \begin{everbatim}
1704 $10^{50}-10^{50}\phi = {}$\printnumber{%
1705 \PhiBasePhi{{\xinteval{10^50}}{\xinteval{-10^50}}}}${}_\phi$.
1706 \end{everbatim}
1707
1708 \def\PhiBasePhiSep{.}
1709 \noindent
1710 $10^{50}-10^{50}\phi={}$\expandafter
1711 |\expanded{\PhiBasePhi{{\xinteval{10^50}}{\xinteval{-10^50}}}}|${}_\phi$.
1712
1713 The radix separator is somewhere in the middle.
1714 \end{titled-frame}
1715
1716 \begin{TeXnote}
1717 Maybe this |10^{50}| gives opportunity to stress the following: in
1718 |\xinteval|, braces |{{...}}| % attention à mon jfverb
1719 are removed, so for example |2^{10-1}| is same as |2^10-1| and not at all
1720 |2^(10-1)|. It is easy to forget this when doing both \TeX{} typesetting
1721 and |\xinteval| calculations at the same time. By the way, |2^-50| is
1722 accepted syntax in |\xinteval|, parentheses as in |2^(-50)| are not mandatory.
1723 \end{TeXnote}
1724
1725 \subsubsection{\cs{PhiXfromExponents}}
1726
1727 This computes a $\phi$-integer from an arbitrary list of (comma separated)
1728 exponents, not necessarily ordered. The output |{a}{b}| is not destined for
1729 direct typesetting, one needs for this to wrap usage of the macro inside of
1730 |\PhiTypesetX|.
1731
1732 The list input is allowed to be empty. A period upfront the input signals to
1733 change the sign of the output to its opposite.
1734
1735 \begin{TeXnote}
1736 When using the interactive interface, such a leading period in the list of
1737 exponents will be produced on output from an |a+b phi| if $a+b\phi<0$, but
1738 when converting in the other direction, from a list of exponents to some
1739 |a+b phi|, a leading period will cause the first exponent to be replaced
1740 with zero, if it is non-negative, and will cause a crash if the first listed
1741 exponent is negative.
1742 \end{TeXnote}
1743
1744 Contiguous commas (or commas separated only by space characters) act as a
1745 single one, a final comma is tolerated. A new \emph{f}-expansion is done at
1746 each item, they can be (\emph{f}-expandable) macros.
1747
1748 \begin{everbatim*}
1749 $\PhiTypesetX{\PhiXfromExponents{}}$\newline
1750 $\PhiTypesetX{\PhiXfromExponents{100, 49}}$\newline
1751 $\PhiTypesetX{\PhiXfromExponents{15, 13, 10, 5, 3, 1, -6, -11, -16}}$\\
1752 $\PhiTypesetX{\PhiXfromExponents{.16, 14, 11, 6, 4, 2, -5, -10, -15}}$
1753 \end{everbatim*}
1754
1755 For these next two examples:
1756 \begin{everbatim}
1757 $\PhiTypesetX{\PhiXfromExponents{\PhiExponents{1000}}}$\newline
1758 $\PhiTypesetX{\PhiXfromExponents{\PhiExponents{{1000}{-1000}}}}$
1759 \end{everbatim}
1760 We have to be careful that we previously customized |\PhiExponentsSep| like this:
1761 \begin{everbatim}
1762 \renewcommand\PhiExponentsSep
1763 {,\allowbreak\hskip0pt plus 1pt\relax}
1764 \end{everbatim}
1765 But this will break |\PhiXfromExponents| because it really needs its argument,
1766 after expansion, to be a genuine comma separated list (possibly with extra
1767 spaces, they do not matter). So we now reset |\PhiExponentsSep| to its default,
1768 and we can execute successfully the next instructions confirming this package
1769 is excellent at doing nothing:
1770 \begin{everbatim*}
1771 \renewcommand\PhiExponentsSep{, }%
1772 $\PhiTypesetX{\PhiXfromExponents{\PhiExponents{1000}}}$\newline
1773 $\PhiTypesetX{\PhiXfromExponents{\PhiExponents{{1000}{-1000}}}}$
1774 \end{everbatim*}
1775
1776 \begin{titled-frame}{Emulation inside \cs{xinteval}}
1777 There is no associated |\xinteval| function but the functionality is a
1778 one-(or-two)-liner in its syntax:
1779 % même ceci marche
1780 % \xinteval{subs(([add(fib(i-1), i=*L),
1781 % add(fib(i), i=*L)]), L=[100, 50, -40, -80])}
1782 \begin{everbatim*}
1783 \xinteval{[add(fib(i-1), i=100, 50, -40, -80),
1784 add(fib(i), i=100, 50, -40, -80)]}
1785 \end{everbatim*}
1786
1787 Compare with:
1788 \begin{everbatim*}
1789 $\PhiTypesetX{\PhiXfromExponents{100, 50, -40, -80}}$
1790 \end{everbatim*}
1791
1792 It is even a one-or-two-liner to define a function all by oneself!
1793 \begin{everbatim*}
1794 \xintdeffunc ABfromlist(x):=
1795 [add(fib(i-1),i=*x), add(fib(i),i=*x)];
1796 \xinteval{ABfromlist(phiexponents(10^30))}
1797 \end{everbatim*}
1798 \end{titled-frame}
1799
1800
1801 \subsubsection{\cs{PhiXfromBasePhi}}
1802
1803 This computes an element from $\ZZ[\phi]$ from a Bergman
1804 $\phi$-representation. The input is allowed to be empty. If it contains a
1805 radix separator, it must be a period, and that period must be preceded by at
1806 least one |0| or |1|. It is allowed for |1|'s to be consecutive. A leading
1807 minus sign is allowed.
1808
1809 The output |{a}{b}| is not destined for direct typesetting one needs
1810 to wrap it inside of |\PhiTypesetX|.
1811 \begin{everbatim*}
1812 \edef\x{\PhiXfromBasePhi{}, \PhiXfromBasePhi{0}, \PhiXfromBasePhi{-0}}
1813 \meaning\x\newline
1814 $\PhiTypesetX{\PhiXfromBasePhi{10.01}}$\newline
1815 $\PhiTypesetX{\PhiXfromBasePhi{101000100010.001000100001}}$\newline
1816 $\PhiTypesetX{\PhiXfromBasePhi{-101000100010.0010001000011}}$
1817 \end{everbatim*}
1818
1819
1820 \subsection{Typesetting}
1821
1822 \subsubsection{\cs{ZeckPrintIndexedSum}}
1823
1824 This is a typesetting utility which produces (expandably), by default,
1825 |F_a + F_{a'} +| \dots{} from |a, a', ...|.
1826 \begin{everbatim*}
1827 $\ZeckPrintIndexedSum{\ZeckIndices{10000000000000000000}}$.
1828 \end{everbatim*}
1829
1830 The |+| is injected by |\ZeckPrintIndexedSumSep| whose default definition is:
1831 \begin{everbatim}
1832 \def\ZeckPrintIndexedSumSep{+\allowbreak}
1833 \end{everbatim}
1834 Each index from the input list is given as argument to |\ZeckPrintOne| whose
1835 default definition requires math mode:
1836 \begin{everbatim}
1837 \def\ZeckPrintOne#1{F_{#1}}
1838 \end{everbatim}
1839 If one wants explicit Fibonacci numbers, one can do this:
1840 \begin{everbatim*}
1841 $\def\ZeckPrintOne{\ZeckTheFN}
1842 \ZeckPrintIndexedSum{\ZeckIndices{10000000000000000000}}$.
1843 \end{everbatim*}
1844
1845 However, as one can see above and was already mentioned, \TeX{} and \LaTeX{}
1846 do not know out-of-the-box to split strings of digits at line endings. Hence
1847 the first two lines are squeezed, but still overflow, which is not pleasing.
1848
1849 With the help of a \xinttoolsname utility we can redefine |\ZeckPrintOne| to
1850 inject breakpoints in-between consecutive digits:
1851 \begin{everbatim*}
1852 \renewcommand\ZeckPrintOne[1]
1853 {\xintListWithSep{\allowbreak}{\ZeckTheFN{#1}}}
1854 $\ZeckPrintIndexedSum{\ZeckIndices{10000000000000000000}}$.
1855 \end{everbatim*}
1856
1857 \begingroup
1858 \colorlet{shortverbcolor}{everbatimxfgcolor}
1859 \mathsurround0pt
1860 \def\ZeckPrintIndexedSumSep{{}+{}}
1861 \def\ZeckPrintOne#1{$\expandafter|\expanded{\ZeckTheFN{#1}}|$}%
1862 % exit math mode because we use \ttfamily
1863 Expert \LaTeX{} users will know how to achieve a result
1864 such as this one, which pleasantly decorate the linebreaks:
1865
1866 \noindent$\ZeckPrintIndexedSum{\ZeckIndices{10000000000000000000}}$.\par
1867 \endgroup
1868
1869 \subsubsection{\cs{PhiPrintIndexedSum}}
1870
1871 It is actually a clone of |\ZeckPrintIndexedSum| which only differs
1872 from it via separate configuration macros:
1873 \begin{everbatim}
1874 \def\PhiPrintIndexedSumSep{+\allowbreak}% same as \ZeckPrintIndexedSumSep
1875 \def\PhiPrintOne#1{\phi^{#1}}% powers of phi rather than Fibonacci
1876 % numbers.
1877 \end{everbatim}
1878 As for |\ZeckPrintIndexedSum| the default configuration is thus
1879 math mode only.
1880
1881 \begin{everbatim*}
1882 \[2025 = \PhiPrintIndexedSum{\PhiExponents{2025}}\]
1883 \end{everbatim*}% ah la la
1884 It is important in the above example that |\PhiExponentsSep| has its default
1885 definition because |\PhiPrintIndexedSum| needs to see real commas.
1886
1887 \begin{titled-frame}{TODO?}
1888 Maybe let it recognize an upfront period (as produced by |\PhiExponents| if
1889 $x=a+b\phi<0$) in the input, and then use minus signs in the output?
1890 \end{titled-frame}
1891
1892 \subsubsection{\cs{PhiTypesetX}}
1893
1894 This is supposed to receive as (single) argument two braced
1895 relative integers |{a}{b}|.
1896
1897 The default output is math-mode only, as it is of the type $a+b\phi$, with
1898 simplifications for zero or negative coefficients. This is decided by the
1899 two-arguments macro |\PhiTypesetXPrint| which can be redefined.
1900
1901 For examples, see the documentation of |\PhiXfromExponents| and |\PhiXfromBasePhi|.
1902
1903 \section{Use as a \LaTeX{} package}
1904 \label{sec:latex}
1905
1906 As expected, add to the preamble: \centeredline{|\usepackage{zeckendorf}|}
1907 There are no options.
1908
1909 \section{Use with Plain \eTeX{}}
1910 \label{sec:plain}
1911
1912 You will need to input the core code using:
1913 \centeredline{|\input zeckendorfcore.tex|}
1914
1915 \begin{titled-frame}{IMPORTANT}
1916 After this |\input|, the catcode regimen is
1917 a specific one (for example |_|, |:|, and |^| all have catcode letter). So,
1918 you will probably want to emit |\ZECKrestorecatcodes| immediately after this
1919 import, it will reset all modified catcodes to their values as prior to the
1920 import.
1921 \end{titled-frame}
1922
1923 Then you can use the exact same interface as described in the previous
1924 section.
1925
1926 \section{Changes}
1927 \label{sec:changes}
1928
1929 \begin{description}
1930 \input zeckendorfchanges.tex
1931 \end{description}
1932
1933 %\clearpage
1934 \section{License}
1935 \label{sec:license}
1936
1937 \makeatletter
1938 % 2025/10/04
1939 % attention au © qui pose problème avec ts1newtxttz.fd bogué
1940 % j'ai fait le report à MS mais peut-être mon mail il y a plusieurs
1941 % mois a fini dans le spam. Donc ici ne pas oublier le \ttfamily.
1942 %
1943 % 2025/10/06 le © est cassé avec xelatex + newtxtt. Sur un mwe
1944 % si on utilise newtxtext c'est bon, mais pas ici et je n'ai pas
1945 % le temps de m'acharner.
1946 \def\MacroFont{\ttfamily \baselineskip11pt }%
1947 \begin{verbatim}
1948 Copyright (c) 2025 Jean-François Burnol
1949
1950 | This Work may be distributed and/or modified under the
1951 | conditions of the LaTeX Project Public License 1.3c.
1952 | This version of this license is in
1953
1954 > <http://www.latex-project.org/lppl/lppl-1-3c.txt>
1955
1956 | and version 1.3 or later is part of all distributions of
1957 | LaTeX version 2005/12/01 or later.
1958
1959 This Work has the LPPL maintenance status "author-maintained".
1960
1961 The Author and Maintainer of this Work is Jean-François Burnol.
1962
1963 This Work consists of the main source file and its derived files
1964
1965 zeckendorf.dtx,
1966 zeckendorfcore.tex, zeckendorf.tex, zeckendorf.sty,
1967 README.md, zeckendorf-doc.tex, zeckendorf-doc.pdf
1968
1969 \end{verbatim}
1970
1971
1972 \StopEventually{\end{document}\endinput}
1973 \makeatletter\let\check@percent\originalcheck@percent\makeatother
1974
1975 % provisoire
1976 \def\csh{\cs}
1977
1978 % workaround
1979 % https://github.com/latex3/latex2e/issues/563
1980 \makeatletter
1981 \AddToHook{env/macrocode/begin}{\partopsep0pt\relax}
1982 \AddToHook{env/macrocode/after}{\@nobreakfalse}
1983 \makeatother
1984 \newgeometry{hscale=0.75,vscale=0.75}% ATTENTION \newgeometry fait
1985 % un reset de vscale si on ne le
1986 % précise pas ici !!!
1987
1988 \MakePercentIgnore
1989 \AddToHook{env/macrocode/begin}{\bfseries}
1990 %
1991 % \catcode`\<=0 \catcode`\>=11 \catcode`\*=11 \catcode`\/=11
1992 % \let</dtx>\relax
1993 % \def<*core>{\catcode`\<=12 \catcode`\>=12 \catcode`\*=12 \catcode`\/=12 }
1994 %</dtx>
1995 %<*core>
1996 % \etocdepthtag.toc{code}
1997 % \part{Commented source code}
1998 % \localtableofcontents
1999 %
2000 % \label{part:zeckendorfcode}
2001 % \etocmarkbothnouc{\zecknameuserdoc \hyperref[sec:zeckendorfcode]{implementation}}
2002 % \section{Core code}
2003 % \label{sec:code:core}
2004 % \etocignoredepthtags
2005 % \etocsetnexttocdepth{subsubsection}
2006 % \localtableofcontents
2007 %
2008 % Extracts to |zeckendorfcore.tex|.
2009 %
2010 % A general remark is that expandable macros (usually) \emph{f}-expand their
2011 % arguments, and most are \emph{f}-expandable. Usually, the CamelCase macro
2012 % (with neither |@| nor |_| in their names)
2013 % expands to either |\romannumeral0| or |\expanded| followed with a lowercase
2014 % macro. Macros destined to be used in typesetting context usually omit any
2015 % such construct and may require \emph{x}-expansion. They remain fully
2016 % expandable as long as some user level customization (for example for the
2017 % radix separator) has not injected things not compatible with an |\edef|.
2018 %
2019 % For variety we use here sometimes |@| in macro names, whereas \xintname uses
2020 % only |_| (and sometimes some other a priori non-letter characters).
2021 %
2022 % \subsection{Loading \xintexprname and setting catcodes}
2023 % \begin{macrocode}
2024 \input xintexpr.sty
2025 \input xintbinhex.sty
2026 \wlog{Package: zeckendorfcore 2025/11/16 v0.9d (JFB)}%
2027 \edef\ZECKrestorecatcodes{\XINTrestorecatcodes}%
2028 \def\ZECKrestorecatcodesendinput{\ZECKrestorecatcodes\endinput}%
2029 \XINTsetcatcodes%
2030 % \end{macrocode}
2031 % Small helpers related to \cs{expanded}-based methods. But the package only
2032 % has a few macros and these helpers are used only once or twice, most macros
2033 % using own custom terminators adapted to their own optimizations.
2034 % \begin{macrocode}
2035 \def\zeck_done#1\xint:{\iffalse{\fi}}%
2036 \let\zeck_abort\zeck_done
2037 % \end{macrocode}
2038 % \subsection{Fibonacci numbers}
2039 % \subsubsection{\csh{ZeckTheFN}}
2040 % The multiplicative algorithm is as in the \bnumexprname manual (at
2041 % |1.7b|), or since about ten years in the \xintname manual (at |1.4o| or
2042 % earlier) but termination is different and simply leaves |{F_n}{F_{n-1}}|
2043 % in input stream. We do not use
2044 % |\csname...\endcsname| branching here, for variety. Also, we replaced
2045 % usage of chained expressions handled via |\xintiiexpro| with direct usage of
2046 % the \xintcorename macros, for optimized efficiency, and taking into account
2047 % that |\expanded| now helps doing this without intermediate step.
2048 %
2049 % |\Zeck@FPair| and |\Zeck@@FPair| are not public interface. The former is
2050 % allows a negative or zero argument, the latter is positive only.
2051 % \begin{macrocode}
2052 \def\Zeck@FPair#1{\expandafter\zeck@fpair\the\numexpr #1.}%
2053 \def\zeck@fpair #1{%
2054 \xint_UDzerominusfork
2055 #1-\zeck@fpair_n
2056 0#1\zeck@fpair_n
2057 0-\zeck@fpair_p
2058 \krof #1%
2059 }%
2060 \def\zeck@fpair_p #1.{\Zeck@@FPair{#1}}%
2061 \def\zeck@fpair_n #1.{%
2062 \ifodd#1 \expandafter\zeck@fpair_ei\else\expandafter\zeck@fpair_eii\fi
2063 \romannumeral`&&@\Zeck@@FPair{1-#1}%
2064 }%
2065 \def\zeck@fpair_ei{\expandafter\zeck@fpair_fi}%
2066 \def\zeck@fpair_eii{\expandafter\zeck@fpair_fii}%
2067 \def\zeck@fpair_fi#1#2{\expanded{{#2}{\XINT_Opp#1}}}%
2068 \def\zeck@fpair_fii#1#2{\expanded{{\XINT_Opp#2}{#1}}}%
2069 \def\Zeck@@FPair#1{%
2070 \expandafter\zeck@@fpair@start
2071 \romannumeral0\xintdectobin{\the\numexpr#1\relax};%
2072 }%
2073 % \end{macrocode}
2074 % Inlining here at start the |\zeck@@fpair@again| because we don't
2075 % want the |\expandafter|'s here, due to current |\XINTfstop| definition.
2076 % \begin{macrocode}
2077 \def\zeck@@fpair@start 1#1{%
2078 \xint_gob_til_sc#1\zeck@@fpair@done;%
2079 \xint_UDzerofork
2080 #1\zeck@@fpair@zero
2081 0\zeck@@fpair@one
2082 \krof
2083 {1}{0}%
2084 }%
2085 % \end{macrocode}
2086 % Prior to |0.9d| we were using coding like this, as it has been easier to use
2087 % expressions (the \xintname documentation had such code for more than ten
2088 % years, precisely to illustrate chaining of expressions, and at a time when
2089 % |\expanded| was not available, and of course we simply took it over initially).
2090 %\begin{verbatim}
2091 % \romannumeral0\xintiiexpro (#1+2*#2)*#1\expandafter\relax\expandafter;%
2092 % \romannumeral0\xintiiexpro #1*#1+#2*#2\relax;%
2093 %\end{verbatim}
2094 % or
2095 %\begin{verbatim}
2096 % \romannumeral0\xintiiexpro 2*(#1+#2)*#1+#2*#2\expandafter\relax\expandafter;%
2097 % \romannumeral0\xintiiexpro (#1+2*#2)*#1\relax;%
2098 %\end{verbatim}
2099 % At |0.9d| we go directly to the \xintcorename core macros for optimal
2100 % efficiency. This required a few adjustments elsewhere and the removal from
2101 % code comments of some technical discusions about |\xintthe|. The also
2102 % dropped semi-colons as we are already using braces.
2103 % \begin{macrocode}
2104 \def\zeck@@fpair@zero #1#2#3{%
2105 \zeck@@fpair@again#3%
2106 \expanded{%
2107 {\xintiiMul{#1}{\xintiiAdd{#1}{\xintDouble{#2}}}}%
2108 {\xintiiAdd{\xintiiSqr{#1}}{\xintiiSqr{#2}}}%
2109 }%
2110 }%
2111 \def\zeck@@fpair@one #1#2#3{%
2112 \zeck@@fpair@again#3%
2113 \expanded{%
2114 {\xintiiAdd{\xintDouble{\xintiiMul{\xintiiAdd{#1}{#2}}{#1}}}%
2115 {\xintiiSqr{#2}}}%
2116 {\xintiiMul{#1}{\xintiiAdd{#1}{\xintDouble{#2}}}}%
2117 }%
2118 }%
2119 \def\zeck@@fpair@again#1{%
2120 \xint_gob_til_sc#1\zeck@@fpair@done;%
2121 \xint_UDzerofork
2122 #1{\expandafter\zeck@@fpair@zero}%
2123 0{\expandafter\zeck@@fpair@one}%
2124 \krof
2125 }%
2126 \def\zeck@@fpair@done#1\krof{}%
2127 % \end{macrocode}
2128 % For individual Fibonacci numbers, we have non public |\Zeck@@FN| which only
2129 % works on positive input and has a braced output. We also have non-public
2130 % |\Zeck@FN| and |\Zeck@FNminusOne| which accept negative input, and whose
2131 % output is also braced. And we have public |\ZeckTheFN| which accepts
2132 % negative input and whose output is not braced.
2133 %
2134 % The reason for strange name |\ZeckTheFN| is that originally |\Zeck@FPair|
2135 % produced its output using a special \xintexprname format, which needs to be
2136 % prefixed with |\xintthe| to get resolved into only digits. We have now
2137 % modified the structure by-pass this but the name sticks.
2138 % \begin{macrocode}
2139 \def\zeck@bracedfirstoftwo #1#2{{#1}}%
2140 \def\zeck@bracedsecondoftwo #1#2{{#2}}%
2141 \def\Zeck@FN{%
2142 \expandafter\zeck@bracedfirstoftwo\romannumeral`&&@\Zeck@FPair
2143 }%
2144 \def\Zeck@FNminusOne{%
2145 \expandafter\zeck@bracedsecondoftwo\romannumeral`&&@\Zeck@FPair
2146 }%
2147 \def\ZeckTheFN{\expandafter\xint_firstoftwo\romannumeral`&&@\Zeck@FPair}%
2148 \def\Zeck@@FN{\expandafter\zeck@bracedfirstoftwo\romannumeral`&&@\Zeck@@FPair}%
2149 % \end{macrocode}
2150 % \subsubsection{\csh{ZeckTheFSeq}}
2151 % The computation of stretches of Fibonacci numbers is not needed for the
2152 % package, but is provided for user convenience. This is lifted from the
2153 % development version of the |\xintname| user manual, which refactored a bit
2154 % the code which has been there for over ten years.
2155 %
2156 % The two arguments may be negative, and since |0.9d| they do not have to be
2157 % ordered.
2158 % \begin{macrocode}
2159 \def\ZeckTheFSeq#1#2{%
2160 \expanded\bgroup\expandafter\zeckthefseq_a
2161 \the\numexpr #1\expandafter.\the\numexpr #2.%
2162 }%
2163 \def\zeckthefseq_a#1.#2.{\expandafter\zeckthefseq_b\the\numexpr#2-#1.#1.}%
2164 \def\zeckthefseq_b #1{%
2165 \xint_UDzerominusfork
2166 0#1\zeckthefseq_n
2167 #1-\zeckthefseq_one
2168 0-\zeckthefseq_p
2169 \krof #1%
2170 }%
2171 \def\zeckthefseq_one0.#1.{{\ZeckTheFN{#1}}\iffalse{\fi}}%
2172 % \end{macrocode}
2173 % The |#1+1| is because |\Zeck@FPair{N}| expands to |{F_{N}}{F_{N-1}}|, so
2174 % here we will have |F_{A+1};F_{A};| as starting point. We want up to
2175 % |F_B|. If |B=A+1| there will be nothing more to do.
2176 % \begin{macrocode}
2177 \def\zeckthefseq_p #1.#2.{%
2178 \expandafter\zeckthefseq_loop
2179 \the\numexpr #1-1\expandafter.%
2180 \romannumeral`&&@\expandafter\zeck@sep@with@sc
2181 \romannumeral`&&@\Zeck@FPair{#2+1}\xintiiadd
2182 }%
2183 \def\zeck@sep@with@sc #1#2{#1;#2;}%
2184 % \end{macrocode}
2185 % We will have |F_{A-1};F_{A};| as starting point. We want down to
2186 % |F_B|. If |B=A-1| there will be nothing more to do.
2187 % \begin{macrocode}
2188 \def\zeckthefseq_n -#1.#2.{%
2189 \expandafter\zeckthefseq_loop
2190 \the\numexpr #1-1\expandafter.%
2191 \romannumeral`&&@\expandafter\zeck@exch@with@sc
2192 \romannumeral`&&@\Zeck@FPair{#2}\xintiisub
2193 }%
2194 \def\zeck@exch@with@sc #1#2{#2;#1;}%
2195 % \end{macrocode}
2196 % Now leave in stream one (braced) number, and test if we have reached B and
2197 % until then apply standard Fibonacci recursion. This is all done using a
2198 % single looping macro, only termination branches to another one.
2199 %
2200 % We add a bit sub-optimality in having one single macro handling both
2201 % increasing and decreasing indices.
2202 % \begin{macrocode}
2203 \def\zeckthefseq_loop #1.#2;#3;#4{%
2204 {#3}\ifnum #1=\z@ \expandafter\zeckthefseq_end\fi
2205 \expandafter\zeckthefseq_loop\the\numexpr #1-1\expandafter.%
2206 \romannumeral0#4{#3}{#2};#2;#4%
2207 }%
2208 \def\zeckthefseq_end#1;#2;#3{{#2}\iffalse{\fi}}%
2209 % \end{macrocode}
2210 % \subsection{Zeckendorf representation}
2211 % \subsubsection{\csh{ZeckNearIndex}, \csh{ZeckMaxK}}
2212 %
2213 % If the ratio of logarithms $\log(\sqrt5 x)/\log\phi$ was the exact
2214 % mathematical value it would be certain (via rough estimates valid at least
2215 % for say $x\geq10$, and even smaller, but anyhow we can check manually it
2216 % does work) that its integer rounding gives an integer |K| such that either
2217 % |K| or |K-1| is the largest index |J| with $F_J\leq x$. But the computation
2218 % is done with only about 8 decimal digits of precision. So this assumption
2219 % fails certainly for $x$ having more than one hundred million decimal digits,
2220 % and quite probably with an input having ten million digits, as we do
2221 % not want to exceed $\phi^{10^7}/\sqrt5\approx\np{1.13e2089876}$. But with
2222 % one million decimal digits we are safe (see \autoref{sssec:maxe} for
2223 % related comments).
2224 %
2225 % As anyhow \xintname can handle multiplications only with operands of about
2226 % up to \dgts{13000} digits (with \TeX Live 2025 default memory settings), and
2227 % computation times limit reasonable inputs to less than \dgts{1000} digits,
2228 % there is no worry for us.
2229 %
2230 % \xintfracname's |\xintiRound{0}| is guaranteed to round correctly the input
2231 % it has been given. This input is some approximation to an exact theoretical
2232 % value involving ratio of logarithms (and square root of \dgts{5}). Prior to
2233 % rounding the computed numerical approximation, we are close to the exact
2234 % theoretical value, where ``close'' means we expect to have about \dgts{8}
2235 % leading digits in common (and we have already limited our scope so that we
2236 % are talking about a value quite less than \dgts{100000} at any rate). If
2237 % the computed rounding differs from the exact rounding of the exact value it
2238 % must be that argument $x$ is about mid-way (in log scale) between two
2239 % consecutive Fibonacci numbers. The conclusion is that the integer we obtain
2240 % after rounding can not be anything else than either |J| or |J+1|.
2241 %
2242 % The argument is more subtle than it looks. The conclusion is important to
2243 % us as it means we do not have to add extraneous checks involving computation
2244 % of one or more additional Fibonacci numbers.
2245 %
2246 % The formula with macros was obtained via an |\xintdeffloatfunc| and
2247 % |\xintverbosetrue| after having set |\xintDigits*| to \dgts{8}, and then we
2248 % optimized a bit manually. The advantage here is that we don't have to set
2249 % ourself |\xintDigits| and later restore it.
2250 %
2251 % We can not use (except if only caring about interactive sessions where we
2252 % control entirely the whole environment) |\XINTinFloatDiv| or
2253 % |\XINTinFloatMul| if we don't set |\xintDigits| (which is user customizable)
2254 % because they hardcode usage of |\XINTdigits|. This is e.g.\@ why we use
2255 % |\PoorManLogBaseTen_raw| and not |\PoorManLogBaseTen|.
2256 % \begin{macrocode}
2257 \def\ZeckNearIndex#1{\xintiRound{0}{%
2258 \xintFloatDiv[8]{\PoorManLogBaseTen_raw{\xintFloatMul[8]{2236068[-6]}{#1}}}%
2259 {20898764[-8]}%
2260 }%
2261 }%
2262 % \end{macrocode}
2263 % Now we compute the actual maximal index. This macro is now only for user
2264 % interface, as we dropped at |0.9c| adding a |maxk()| function to the
2265 % |\xinteval| interface.
2266 %
2267 % With |0.9d| replace negative input by its absolute value.
2268 % \begin{macrocode}
2269 \def\ZeckMaxK{\expanded\zeckmaxk}%
2270 \def\zeckmaxk#1{\expandafter\zeckmaxk_fork\romannumeral`&&@#1\xint:}%
2271 \def\zeckmaxk_fork#1{%
2272 \xint_UDzerominusfork
2273 #1-{\bgroup\zeck_abort}%
2274 0#1\zeckmaxk_a
2275 0-{\zeckmaxk_a#1}%
2276 \krof
2277 }%
2278 \def\zeckmaxk_a #1\xint:{%
2279 \expandafter\zeckmaxk_b
2280 \the\numexpr\ZeckNearIndex{#1}\xint:#1\xint:
2281 }%
2282 \def\zeckmaxk_b #1\xint:{%
2283 \expandafter\zeckmaxk_c
2284 \romannumeral`&&@\Zeck@@FPair{#1}#1\xint:
2285 }%
2286 \def\zeckmaxk_c #1#2#3\xint:#4\xint:{%
2287 \xintiiifGt{#1}{#4}%
2288 {{\expandafter}\the\numexpr#3-1\relax}%
2289 {{}#3}%
2290 }%
2291 % \end{macrocode}
2292 % \subsubsection{\csh{ZeckIndices}}
2293 % This starts by computing the maximal index. It then subtracts the Fibonacci
2294 % number from the input and loops.
2295 %
2296 % At |0.9d| let it (rather than returning empty output) accept a negative
2297 % argument (silently replaced by its absolute value).
2298 % \begin{macrocode}
2299 \def\ZeckIndices{\expanded\zeckindices}%
2300 \let\ZeckZeck\ZeckIndices
2301 \def\zeckindices#1{\bgroup\expandafter\zeckindices_fork\romannumeral`&&@#1\xint:}%
2302 \def\zeckindices_fork#1{%
2303 \xint_UDzerominusfork
2304 #1-\zeck_abort
2305 0#1\zeckindices_a
2306 0-{\zeckindices_a#1}%
2307 \krof
2308 }%
2309 \def\zeckindices_a #1\xint:{%
2310 \expandafter\zeckindices_b
2311 \the\numexpr\ZeckNearIndex{#1}\xint:#1\xint:
2312 }%
2313 \def\zeckindices_b #1\xint:{%
2314 \expandafter\zeckindices_c
2315 \romannumeral`&&@\Zeck@@FPair{#1}#1\xint:
2316 }%
2317 \def\zeckindices_c #1#2#3\xint:#4\xint:{%
2318 \xintiiifGt{#1}{#4}\zeckindices_A\zeckindices_B
2319 #1;#2;#3\xint:#4\xint:
2320 }%
2321 \def\zeckindices_A#1;#2;#3\xint:{%
2322 \the\numexpr#3-1\relax\zeckindices_loop{#2}%
2323 }%
2324 \def\zeckindices_B#1;#2;#3\xint:{%
2325 #3\zeckindices_loop{#1}%
2326 }%
2327 \def\zeckindices_loop #1#2\xint:{%
2328 \expandafter\zeckindices_loop_i
2329 \romannumeral0\xintiisub{#2}{#1}\xint:
2330 }%
2331 \def\zeckindices_loop_i#1{%
2332 \xint_UDzerofork#1\zeck_done 0{, \zeckindices_a#1}\krof
2333 }%
2334 % \end{macrocode}
2335 % \subsubsection{\csh{ZeckBList}}
2336 % This is the variant which produces the results as a sequence of braced
2337 % indices.
2338 %
2339 % Originally in \xintname, \xinttoolsname, the term ``list'' is used for
2340 % braced items. In the user manual of this package I have been using ``list''
2341 % more colloquially for comma separated values. Here I stick with \xintname
2342 % conventions but use |BList| (short for ``list of Braced items'') and not only
2343 % ``List'' in the name.
2344 %
2345 % At |0.9d| let it (rather than returning empty output) accept a negative
2346 % argument (silently replaced by its absolute value).
2347 % \begin{macrocode}
2348 \def\ZeckBList{\expanded\zeckblist}%
2349 \def\zeckblist#1{\bgroup\expandafter\zeckblist_fork\romannumeral`&&@#1\xint:}%
2350 \def\zeckblist_fork#1{%
2351 \xint_UDzerominusfork
2352 #1-\zeck_abort
2353 0#1\zeckblist_a
2354 0-{\zeckblist_a#1}%
2355 \krof
2356 }%
2357 \def\zeckblist_a #1\xint:{%
2358 \expandafter\zeckblist_b
2359 \the\numexpr\ZeckNearIndex{#1}\xint:#1\xint:
2360 }%
2361 \def\zeckblist_b #1\xint:{%
2362 \expandafter\zeckblist_c
2363 \romannumeral`&&@\Zeck@@FPair{#1}#1\xint:
2364 }%
2365 \def\zeckblist_c #1#2#3\xint:#4\xint:{%
2366 \xintiiifGt{#1}{#4}\zeckblist_A\zeckblist_B
2367 #1;#2;#3\xint:#4\xint:
2368 }%
2369 \def\zeckblist_A#1;#2;#3\xint:{%
2370 {\the\numexpr#3-1\relax}\zeckblist_loop{#2}%
2371 }%
2372 \def\zeckblist_B#1;#2;#3\xint:{%
2373 {#3}\zeckblist_loop{#1}%
2374 }%
2375 \def\zeckblist_loop#1#2\xint:{%
2376 \expandafter\zeckblist_loop_i
2377 \romannumeral0\xintiisub{#2}{#1}\xint:
2378 }%
2379 \def\zeckblist_loop_i#1{\xint_UDzerofork#1\zeck_done 0{\zeckblist_a#1}\krof}%
2380 % \end{macrocode}
2381 % \subsubsection{\csh{ZeckWord}}
2382 % This is slightly more complicated than \cs{ZeckIndices} and \cs{ZeckBList}
2383 % because we have to keep track of the previous index to know how many zeros
2384 % to inject.
2385 % \begin{macrocode}
2386 \def\ZeckWord{\expanded\zeckword}%
2387 \def\zeckword#1{\bgroup\expandafter\zeckword_fork\romannumeral`&&@#1\xint:}%
2388 \def\zeckword_fork#1{%
2389 \xint_UDzerominusfork
2390 #1-\zeck_abort
2391 0#1\zeckword_a
2392 0-{\zeckword_a#1}%
2393 \krof
2394 }%
2395 \def\zeckword_a #1\xint:{%
2396 \expandafter\zeckword_b\the\numexpr\ZeckNearIndex{#1}\xint:
2397 #1\xint:
2398 }%
2399 \def\zeckword_b #1\xint:{%
2400 \expandafter\zeckword_c\romannumeral`&&@\Zeck@@FPair{#1}#1\xint:
2401 }%
2402 \def\zeckword_c #1#2#3\xint:#4\xint:{%
2403 \xintiiifGt{#1}{#4}\zeckword_A\zeckword_B
2404 #1;#2;#3\xint:#4\xint:
2405 }%
2406 \def\zeckword_A#1;#2;#3\xint:#4\xint:{%
2407 \expandafter\zeckword_d
2408 \romannumeral0\xintiisub{#4}{#2}\xint:
2409 \the\numexpr#3-1.%
2410 }%
2411 \def\zeckword_B#1;#2;#3\xint:#4\xint:{%
2412 \expandafter\zeckword_d
2413 \romannumeral0\xintiisub{#4}{#1}\xint:
2414 #3.%
2415 }%
2416 \def\zeckword_d #1%
2417 {\xint_UDzerofork#1\zeckword_done0{1\zeckword_e}\krof #1}%
2418 \def\zeckword_done#1\xint:#2.{1\xintReplicate{#2-2}{0}\iffalse{\fi}}%
2419 \def\zeckword_e #1\xint:{%
2420 \expandafter\zeckword_f\the\numexpr\ZeckNearIndex{#1}\xint:
2421 #1\xint:
2422 }%
2423 \def\zeckword_f #1\xint:{%
2424 \expandafter\zeckword_g\romannumeral`&&@\Zeck@@FPair{#1}#1\xint:
2425 }%
2426 \def\zeckword_g #1#2#3\xint:#4\xint:{%
2427 \xintiiifGt{#1}{#4}\zeckword_gA\zeckword_gB
2428 #1;#2;#3\xint:#4\xint:
2429 }%
2430 \def\zeckword_gA#1;#2;#3\xint:#4\xint:{%
2431 \expandafter\zeckword_h
2432 \the\numexpr#3-1\expandafter.%
2433 \romannumeral0\xintiisub{#4}{#2}%
2434 \xint:
2435 }%
2436 \def\zeckword_gB#1;#2;#3\xint:#4\xint:{%
2437 \expandafter\zeckword_h
2438 \the\numexpr#3\expandafter.%
2439 \romannumeral0\xintiisub{#4}{#1}%
2440 \xint:
2441 }%
2442 \def\zeckword_h #1.#2\xint:#3.{%
2443 \xintReplicate{#3-#1-1}{0}%
2444 \zeckword_d #2\xint:#1.%
2445 }%
2446 % \end{macrocode}
2447 % \subsubsection{\csh{ZeckHexWord}}
2448 % \begin{macrocode}
2449 \def\ZeckHexWord{\romannumeral0\zeckhexword}%
2450 \def\zeckhexword#1{%
2451 \xintbintohex{%
2452 \expanded\bgroup\expandafter\zeckword_fork\romannumeral`&&@#1\xint:
2453 }%
2454 }%
2455 % \end{macrocode}
2456 % \subsubsection{\csh{ZeckNFromIndices}}
2457 % Spaces before commas are not a problem they disappear in |\numexpr|.
2458 %
2459 % Extraneous commas are skipped, in particular a final comma is allowed.
2460 %
2461 % Each item is \emph{f}-expanded to check not empty, but perhaps we could skip
2462 % expanding, as they end up in |\numexpr|. Advantage of expansion of each
2463 % item is that at any location is that can generate multiple indices from some
2464 % macro expansion inserting commas dynamically.
2465 % \begin{macrocode}
2466 \def\ZeckNFromIndices{\romannumeral0\zecknfromindices}%
2467 \def\zecknfromindices{\zeck@applyandiisum\Zeck@FN}%
2468 \def\zeck@applyandiisum {%
2469 \expandafter\xintiisum\expanded\zeck@applytocsv
2470 }%
2471 % \end{macrocode}
2472 % Macro |#1| is assumed to output something within braces.
2473 % \begin{macrocode}
2474 \def\zeck@applytocsv #1#2{%
2475 {{\expandafter\zeck@applytocsv_a\expandafter#1%
2476 \romannumeral`&&@#2,;}}%
2477 }%
2478 \def\zeck@applytocsv_a #1#2{%
2479 \if;#2\expandafter\zeck@applytocsv_done\fi
2480 \if,#2\expandafter\zeck@applytocsv_skip\fi
2481 \zeck@applytocsv_b #1#2%
2482 }%
2483 \def\zeck@applytocsv_b #1#2,{%
2484 #1{#2}%
2485 \expandafter\zeck@applytocsv_a\expandafter#1\romannumeral`&&@%
2486 }%
2487 \def\zeck@applytocsv_done#1\zeck@applytocsv_b#2;{}%
2488 \def\zeck@applytocsv_skip #1#2,{%
2489 \expandafter\zeck@applytocsv_a\expandafter#2\romannumeral`&&@%
2490 }%
2491 % \end{macrocode}
2492 % \subsubsection{\csh{ZeckNfromWord}}
2493 % The |\xintreversedigits| will \emph{f}-expand its argument.
2494 % \begin{macrocode}
2495 \def\ZeckNfromWord{\romannumeral0\zecknfromword}%
2496 \def\zecknfromword#1{%
2497 \expandafter\zecknfromword_a\romannumeral0\xintreversedigits{#1};%
2498 }%
2499 \def\zecknfromword_a{%
2500 \expandafter\xintiisum\expanded{{\iffalse}}\fi\zecknfromword_b 2.%
2501 }%
2502 \def\zecknfromword_b#1.#2{%
2503 \if;#2\expandafter\zecknfromword_done\fi
2504 \if#21\Zeck@@FN{#1}\fi
2505 \expandafter\zecknfromword_b\the\numexpr#1+1.%
2506 }%
2507 \def\zecknfromword_done#1.{\iffalse{{\fi}}}%
2508 % \end{macrocode}
2509 % \subsubsection{\csh{ZeckNfromHexWord}}
2510 % Added at |0.9d|.
2511 % \begin{macrocode}
2512 \def\ZeckNfromHexWord{\romannumeral0\zecknfromhexword}%
2513 \def\zecknfromhexword#1{%
2514 \expandafter\zecknfromword_a\romannumeral0\xintreversedigits{\xintHexToBin{#1}};%
2515 }%
2516 % \end{macrocode}
2517 % \subsection{Bergman representation}
2518 % \subsubsection{\csh{PhiIISign_ab}}
2519 % |\PhiIISign_ab| is for use with two already expanded arguments |{a}{b}|
2520 % and which are strict integers.
2521 %
2522 % The general macro which accepts both one (unbraced) integer or two (braced)
2523 % integers is defined later for support of the |phisign()| function.
2524 % \begin{macrocode}
2525 \def\PhiIISign_ab {\romannumeral0\phiiisign_ab}%
2526 \def\phiiisign_ab #1#2{%
2527 \xintiiifsgn{#1}%
2528 {% a < 0
2529 \xintiiifSgn{#2}%
2530 {-1}%
2531 {-1}%
2532 {% a < 0, b > 0, return 1 iff a^2+ab<b^2
2533 \xintiiifLt{\xintiiMul{#1}{\xintiiAdd{#1}{#2}}}{\xintiiSqr{#2}}%
2534 {1}%
2535 {-1}%
2536 }%
2537 }%
2538 {\xintiiSgn{#2}}%
2539 {% a > 0
2540 \xintiiifSgn{#2}%
2541 {% a > 0, b < 0, return 1 iff a^2+ab>b^2
2542 \xintiiifGt{\xintiiMul{#1}{\xintiiAdd{#1}{#2}}}{\xintiiSqr{#2}}%
2543 {1}%
2544 {-1}%
2545 }%
2546 {1}%
2547 {1}%
2548 }%
2549 }%
2550 % \end{macrocode}
2551 % \subsubsection{\cs{PhiMaxE}}
2552 % \label{sssec:maxe}
2553 % We want the greatest $k$ with $\phi^k \leq a + b \phi$, assuming that the
2554 % latter is strictly positive. We will do this using a careful computation
2555 % (i.e.\@ avoiding ``catastrophic cancellations'' if $a$ and $b$ are of
2556 % opposite signs) of
2557 % the base-ten logarithm of $a + b \phi$, with about 8 decimal digits of
2558 % precision. Rounding the quotient by $\log_{10}\phi$ to the
2559 % nearest integer we obtain a candidate |K|. We compute $\phi^K$ using
2560 % Fibonacci numbers and compare (using integer-only arithmetic). If this is
2561 % larger than $a + b \phi$ the seeked |k| is |K-1| else it is |K|. For why,
2562 % see the explanations relative to the computation of the Zeckendorf
2563 % representation and the next paragraph about theoretical limitation.
2564 %
2565 % The rounding to an integer of $\log(a+b\phi)/\log(\phi)$ obtained with 8
2566 % decimal digits of precision will not error by $2$ units or more if the input
2567 % was less than $\phi^{10^7}$, so for an $x$ which is an integer
2568 % having less than two million decimal digits, or say one million, this is
2569 % safe. And \xintname can only do computations with operands having less than
2570 % about |13000| digits (with \TeX Live 2025 default memory settings). If
2571 % using another programming context not having such limitations, and using
2572 % rather double precision floats, which gives slightly less than |16| decimal
2573 % digits of floating point precision, the upper bound would raise to about
2574 % $\phi^{10^{15}}$, and inputs with at most $10^{14}$ decimal digits are safe,
2575 % i.e.\@ all real life inputs are safe.
2576 %
2577 % Let us nevertheless explain how we could do without logarithms and without
2578 % any upper bound on size of the input. Let $x=a+b\phi$, assumed to be
2579 % positive. First, we test if $x<1$. We can do this using integers only. If
2580 % true, we multiply $x$ by $\phi$, $\phi^2$, $\phi^4$, using algebra in
2581 % $\ZZ[\phi] = \ZZ + \ZZ \phi$, until finding the smallest power of $2$ such
2582 % that $\phi^{2^n}x = x'\geq1$. The powers of $\phi$ are computed by repeated
2583 % squarings (and they can be pre-stored up to certain reasonable maximal $n$,
2584 % but we are discussing here a ``no prior bound'' situation). The searched-for
2585 % exponent $k(x)$ is $k(x')-2^n$. So we are reduced to the $x\geq1$ case. If
2586 % $x<\phi$, then $k(x)=0$. If $x\geq\phi=\phi^{2^0}$, repeated squaring of
2587 % $\phi$ and comparisons with $x$ using $u + v \phi$ representations will give
2588 % us the smallest $n\geq0$ with $\phi^{2^{n+1}}>x$. Then divide $x$ by
2589 % $\phi^{2^n}$ (or rather multiply with $\phi^{-2^n}$), again using
2590 % $\ZZ[\phi]$ algebra, obtaining some $x'$ with $1\leq x' < \phi^{2^n}$ and
2591 % $k(x)=k(x')+2^n$ with $0\leq k(x')<2^n$. After finitely many steps we will
2592 % have reduced to $[1,\phi)$ and the algorithm ends.
2593 %
2594 % The macro helpers handling the computation of the ratio of logarithms should
2595 % not make assumptions about |\xintDigits|. Here is the original source as it
2596 % was used to create the code (not any AI would be able to do that... but
2597 % \xintexprname succeeds!). In particular note the impressive nesting of
2598 % |\xintiiexpr| inside |\xintfloatexpr|. The log output (thanks to
2599 % |\xintverbosetrue|) was then edited by hand to not use macros using tacitly
2600 % |\XINTdigits|, and to reduce to 8 digits of precision as this is enough.
2601 %\begin{verbatim}
2602 % \xintverbosetrue
2603 % \xintdeffloatvar Phi := (1 + sqrt(5))/2;
2604 % \xintdeffloatvar Psi := (1 - sqrt(5))/2;
2605 % \xintdeffloatvar logPhi := log10(Phi);% would have been precomputed anyhow
2606 % \xintdefiifunc greedyA(a,b):= \xintfloatexpr
2607 % round(log10(a+b*Phi) / logPhi)\relax;
2608 % \xintdefiifunc greedyB(a,b):= \xintfloatexpr
2609 % round(log10(\xintiiexpr (a*(a+b) -sqr(b))\relax/(a+b*Psi))
2610 % / logPhi)\relax;
2611 %\end{verbatim}
2612 % \begin{macrocode}
2613 \def\bergman_nearindex_A#1;#2;{%
2614 \xintiRound {0}{%
2615 \xintFloatDiv[8]{%
2616 \PoorManLogBaseTen_raw
2617 {\xintFloatAdd[8]{#1}%
2618 {\xintFloatMul[8]{#2}{1618034[-6]}}}%
2619 }%
2620 {20898764[-8]}%
2621 }%
2622 }%
2623 \def\bergman_nearindex_B#1;#2;{%
2624 \xintiRound {0}{%
2625 \xintFloatDiv[8]{%
2626 \PoorManLogBaseTen_raw
2627 {\xintFloatDiv[8]%
2628 {\xintiiSub
2629 {\xintiiMul {#1}{\xintiiAdd{#1}{#2}}}%
2630 {\xintiiSqr {#2}}%
2631 }%
2632 {\xintFloatAdd[8]%
2633 {#1}{\xintFloatMul[8]{#2}{-618034[-6]}}%
2634 }%
2635 }%
2636 }%
2637 {20898764[-8]}%
2638 }%
2639 }%
2640 \def\PhiMaxE{\the\numexpr\phimaxe}%
2641 \def\phimaxe #1{%
2642 \expandafter\phimaxe_a\expanded{{#1}}%
2643 }%
2644 \def\phimaxe_a #1{\expandafter\phimaxe_b\string#1;}%
2645 \catcode`[=1 \catcode`]=2 \catcode`\{=12 % }
2646 \def\phimaxe_b #1[\if#1{\expandafter\phimaxe_X % }
2647 \else\expandafter\phimaxe_N\fi #1]%
2648 \catcode`[=12 \catcode`]=12 \catcode`\{=1 % }
2649 \def\phimaxe_N #1;{\phimaxe_ab {#1}{0};}%
2650 \def\phimaxe_X #1{\expandafter\phimaxe_ab\expandafter{\iffalse}\fi}%
2651 \def\PhiMaxE_ab {\romannumeral0\phimaxe_ab}%
2652 \def\phimaxe_ab #1#2;{%
2653 \expandafter\phimaxe_i\romannumeral`&&@%
2654 \ifnum\numexpr\xintiiSgn{#1}*\xintiiSgn{#2}\relax=-1
2655 \expandafter\bergman_nearindex_B
2656 \else \expandafter\bergman_nearindex_A
2657 \fi #1;#2;;#1;#2;%
2658 }%
2659 \def\phimaxe_i #1;{%
2660 \expandafter\phimaxe_j\romannumeral`&&@\zeck@fpair #1.#1;%
2661 }%
2662 \def\phimaxe_j #1#2#3;#4;#5;{%
2663 #3\ifnum
2664 \expandafter\PhiIISign_ab
2665 \expanded{{\xintiiSub{#2}{#4}}{\xintiiSub{#1}{#5}}}>\xint_c_
2666 -1\fi\relax
2667 }%
2668 % \end{macrocode}
2669 % \subsubsection{\csh{PhiBList}}
2670 %
2671 % Will serve (or rather a close derivative) as support for the
2672 % |phiexponents()| function in \cs{xinteval}, and is used both by
2673 % |\PhiExponents| and |\PhiBasePhi|.
2674 % \begin{macrocode}
2675 \def\PhiBList{\expanded\phiblist}%
2676 \def\phiblist #1{%
2677 \expandafter\phiblist_a\expanded{{#1}}%
2678 }%
2679 \def\phiblist_a #1{\expandafter\phiblist_b\string#1;}%
2680 \catcode`[=1 \catcode`]=2 \catcode`\{=12 % }
2681 \def\phiblist_b #1[\if#1{\expandafter\phiblist_X % }
2682 \else\expandafter\phiblist_N\fi #1]%
2683 \catcode`[=12 \catcode`]=12 \catcode`\{=1 % }
2684 \def\phiblist_N #1;{\phiblist_ab {#1}{0};}%
2685 \def\phiblist_X #1{\expandafter\phiblist_ab\expandafter{\iffalse}\fi}%
2686 \def\PhiBlist_ab {\expanded\phiblist_ab}%
2687 \def\phiblist_ab #1#2;{{%
2688 \ifcase\PhiIISign_ab{#1}{#2} %
2689 0\expandafter\phiblist_stop
2690 \or
2691 +\expandafter\phiblist_i
2692 \else
2693 -\expandafter\phiblist_neg
2694 \fi
2695 #1;#2;%
2696 }}%
2697 \def\phiblist_stop#1;#2;{}%
2698 % \end{macrocode}
2699 % Attention that adding minus signs here would fool |\xintiiSgn| which makes
2700 % no normalization and looks only at first token.
2701 %
2702 % TODO: check if it is more efficient to do |\expanded{\noexpand\foo...}|
2703 % rather than |\expandafter\foo\expanded{...}|.
2704 % \begin{macrocode}
2705 \def\phiblist_neg#1;#2;{%
2706 \expandafter\phiblist_i\expanded{\XINT_Opp#1;\XINT_Opp#2;}%
2707 }%
2708 \def\phiblist_i#1;#2;{%
2709 \expandafter\phiblist_j\romannumeral`&&@%
2710 \ifnum\numexpr\xintiiSgn{#1}*\xintiiSgn{#2}\relax=-1
2711 \expandafter\bergman_nearindex_B
2712 \else \expandafter\bergman_nearindex_A
2713 \fi #1;#2;;#1;#2;%
2714 }%
2715 \def\phiblist_j #1;{%
2716 \expandafter\phiblist_k\romannumeral`&&@\zeck@fpair #1.#1;%
2717 }%
2718 \def\phiblist_k #1#2#3;#4;#5;{%
2719 \if1\expandafter\PhiIISign_ab
2720 \expanded{{\xintiiSub{#2}{#4}}{\xintiiSub{#1}{#5}}}%
2721 \expandafter\phiblist_ci
2722 \else
2723 \expandafter\phiblist_cii
2724 \fi
2725 #1;#2;#3;#4;#5;%
2726 }%
2727 \def\phiblist_ci #1;#2;#3;#4;#5;{%
2728 {\the\numexpr#3-1\relax}%
2729 \expandafter\phiblist_again\expanded{%
2730 {\xintiiSub{\xintiiAdd{#2}{#4}}{#1}}%
2731 {\xintiiSub{#5}{#2}}%
2732 }%
2733 }%
2734 \def\phiblist_cii #1;#2;#3;#4;#5;{%
2735 {#3}%
2736 \expandafter\phiblist_again
2737 {\xintiiSub{#4}{#2}}%
2738 {\xintiiSub{#5}{#1}}%
2739 }%
2740 \def\phiblist_again #1#2{%
2741 \if0\PhiIISign_ab{#1}{#2}%
2742 \expandafter\phiblist_stop
2743 \else
2744 \expandafter\phiblist_i
2745 \fi
2746 #1;#2;%
2747 }%
2748 % \end{macrocode}
2749 % \subsubsection{\csh{PhiExponents}}
2750 % As this depends upon |\PhiBList| it will have to unbrace
2751 % at each step to check sign of the exponent.
2752 % \begin{macrocode}
2753 \def\PhiExponents{\expanded\phiexponents}%
2754 \def\phiexponents#1{{%
2755 \expandafter\phiexponents_a
2756 \expanded\expandafter\phiblist_a\expanded{{#1}}%
2757 ;%
2758 }}%
2759 \def\phiexponents_a #1{\if-#1.\fi\phiexponents_b}%
2760 \def\phiexponents_b #1{%
2761 \if;#1\expandafter\phiexponents_done\fi
2762 #1\phiexponents_c
2763 }%
2764 \def\phiexponents_c #1{%
2765 \if;#1\expandafter\phiexponents_done\fi
2766 \PhiExponentsSep#1\phiexponents_c
2767 }%
2768 \def\phiexponents_done#1\phiexponents_c{}%
2769 \def\PhiExponentsSep{, }%
2770 % \end{macrocode}
2771 % \subsubsection{\csh{PhiBasePhi}}
2772 % As this depends upon |\PhiBList| it will have to unbrace
2773 % at each step to check sign of the exponent.
2774 % \begin{macrocode}
2775 \def\PhiBasePhi{\expanded\phibasephi}%
2776 \def\phibasephi#1{{%
2777 \expandafter\phibasephi_a
2778 \expanded\expandafter\phiblist_a\expanded{{#1}}%
2779 ;%
2780 }}%
2781 \def\phibasephi_a #1{%
2782 \if-#1-\fi
2783 \if0#1\expandafter\xint_gob_til_sc\fi
2784 \phibasephi_b
2785 }%
2786 \def\phibasephi_b #1{\phibasephi_c #1.}%
2787 \def\phibasephi_c #1#2.{%
2788 \if-#1%
2789 0\PhiBasePhiSep\xintReplicate{#2-1}{0}%
2790 1\expandafter\phibasephi_n
2791 \else
2792 1\expandafter\phibasephi_p
2793 \fi
2794 #1#2.%
2795 }%
2796 \def\phibasephi_p #1.#2{\phibasephi_pa #1.#2\xint:}%
2797 \def\phibasephi_pa #1.#2{%
2798 \if;#2\xintReplicate{#1}{0}\expandafter\xint_gob_til_xint:\fi
2799 \phibasephi_pb #1.#2%
2800 }%
2801 \def\phibasephi_pb #1.#2#3\xint:{%
2802 \if-#2%
2803 \xintReplicate{#1}{0}\PhiBasePhiSep\xintReplicate{#3-1}{0}%
2804 1\expandafter\phibasephi_n
2805 \else
2806 \xintReplicate{#1-#2#3-1}{0}%
2807 1\expandafter\phibasephi_p
2808 \fi
2809 #2#3.%
2810 }%
2811 \def\phibasephi_n #1.#2{\phibasephi_na #1.#2\xint:}%
2812 \def\phibasephi_na #1.#2{%
2813 \if;#2\expandafter\xint_gob_til_xint:\fi
2814 \phibasephi_nb #1.#2%
2815 }%
2816 \def\phibasephi_nb #1.#2#3\xint:{%
2817 \xintReplicate{#1+#3-1}{0}%
2818 1\phibasephi_n #2#3.%
2819 }%
2820 \def\PhiBasePhiSep{.}%
2821 % \end{macrocode}
2822 % \subsubsection{\csh{PhiBaseHexPhi}}
2823 % Broken if |\PhiBasePhiSep| is not default.
2824 %
2825 % Attention for fractional part that we must first extend with trailing zeros
2826 % if needed to make it have 4N digits. (Expansion of integers will have an
2827 % even number of fractional digits, but of course this is not true of general
2828 % |a + b phi|.
2829 % \begin{macrocode}
2830 \def\PhiBaseHexPhi{\expanded\phibasehexphi}%
2831 \def\phibasehexphi#1{\expandafter\phibasehexphi_a\expanded{%
2832 \expandafter\phibasephi_a
2833 \expanded\expandafter\phiblist_a\expanded{{#1}}%
2834 ;}..,;%
2835 }%
2836 % \end{macrocode}
2837 % MEMO: fortunately the second |\xintBinToHex| will not trim leading zeros
2838 % which were originally zeros after the radix separator. But we also
2839 % must make sure to apply it to an input having a multiple of four number
2840 % of binary digits.
2841 % \begin{macrocode}
2842 \def\phibasehexphi_a #1.#2.#3#4;{{%
2843 \xintBinToHex{#1}%
2844 \if.#3.\xintBinToHex{\expanded{#2\expandafter\phibasehexphi_aux
2845 \romannumeral0\xintlength{#2}.}}\fi
2846 }}%
2847 % \end{macrocode}
2848 % Here |#1| is at least one.
2849 % \begin{macrocode}
2850 \def\phibasehexphi_aux #1.{\xintReplicate{4*((#1+1)/4) - #1}{0}}%
2851 % \end{macrocode}
2852 % \subsubsection{\csh{PhiXfromExponents}}
2853 % If the list starts with period, it means it represents
2854 % a negative number (or perhaps zero is there is nothing else).
2855 % \begin{macrocode}
2856 \def\PhiXfromExponents{\expanded\phixfromexponents}%
2857 \def\phixfromexponents#1{%
2858 \expandafter\phixfromexponents_a\romannumeral`&&@#1,;%
2859 }%
2860 \def\phixfromexponents_a #1{%
2861 \if.#1\expandafter\phixfromexponents_n
2862 \else\expandafter\phixfromexponents_p
2863 \fi #1%
2864 }%
2865 \def\phixfromexponents_p #1;{{%
2866 {\xintiiSum{\expanded{\zeck@applytocsv_a\Zeck@FNminusOne#1;}}}%
2867 {\xintiiSum{\expanded{\zeck@applytocsv_a\Zeck@FN#1;}}}%
2868 }}%
2869 \def\phixfromexponents_n .#1;{{%
2870 {\xintiiOpp{\xintiiSum{\expanded{\zeck@applytocsv_a\Zeck@FNminusOne#1;}}}}%
2871 {\xintiiOpp{\xintiiSum{\expanded{\zeck@applytocsv_a\Zeck@FN#1;}}}}%
2872 }}%
2873 % \end{macrocode}
2874 % \subsubsection{\csh{PhiXfromBasePhi}}
2875 % The radix separator must be an explicit period. There must be at least one digit
2876 % before the period, if the latter is there. Empty input is allowed. Input
2877 % must \emph{f}-expand completely thus input such as |\macroA.\macroB| is not
2878 % allowed.
2879 %
2880 % Coded the lazy way by first converting to comma separated list of exponents. The
2881 % |\phiexponentsfromrep|'s output has a final comma but this is ok.
2882 % \begin{macrocode}
2883 \def\PhiXfromBasePhi{\expanded\phixfrombasephi}%
2884 \def\phixfrombasephi{\expandafter\phixfromexponents\expanded\phiexponentsfromrep}%
2885 \def\phiexponentsfromrep#1{%
2886 {{\iffalse}\fi\expandafter\phiexponentsfromrep_a\romannumeral`&&@#1.;\xint:}%
2887 }%
2888 \def\phiexponentsfromrep_a #1{%
2889 \if-#1.\xint_dothis\phiexponentsfromrep_a\fi
2890 \if.#1\xint_dothis\zeck_done\fi
2891 \xint_orthat{\phiexponentsfromrep_b #1}%
2892 }%
2893 \def\phiexponentsfromrep_b #1.#2{%
2894 \expandafter\phiexponentsfromrep_c\romannumeral0\xintreversedigits{#1};%
2895 \if;#2\expandafter\zeck_done\else
2896 \expandafter\phiexponentsfromrep_i\fi #2%
2897 }%
2898 \def\phiexponentsfromrep_c{\phiexponentsfromrep_d 0.}%
2899 \def\phiexponentsfromrep_d#1.#2{%
2900 \if;#2\expandafter\xint_gob_til_dot\fi
2901 \if#21#1,\fi
2902 \expandafter\phiexponentsfromrep_d\the\numexpr#1+1.%
2903 }%
2904 \def\phiexponentsfromrep_i{\phiexponentsfromrep_j -1.}%
2905 \def\phiexponentsfromrep_j#1.#2{%
2906 \if;#2\expandafter\zeck_done\fi
2907 \if#21#1,\fi
2908 \expandafter\phiexponentsfromrep_j\the\numexpr#1-1.%
2909 }%
2910 % \end{macrocode}
2911 % \subsubsection{\csh{PhiXfromBaseHexPhi}}
2912 % Added at |0.9d|.
2913 % \begin{macrocode}
2914 \def\PhiXfromBaseHexPhi{\expanded\phixfrombasehexphi}%
2915 \def\phixfrombasehexphi{\expandafter\phixfromexponents
2916 \expanded\phiexponentsfromhexrep}%
2917 \def\phiexponentsfromhexrep#1{%
2918 {{\iffalse}\fi\expandafter\phiexponentsfromhexrep_a\romannumeral`&&@#1.;\xint:}%
2919 }%
2920 \def\phiexponentsfromhexrep_a #1{%
2921 \if-#1.\xint_dothis\phiexponentsfromhexrep_a\fi
2922 \if.#1\xint_dothis\zeck_done\fi
2923 \xint_orthat{\phiexponentsfromhexrep_b #1}%
2924 }%
2925 \def\phiexponentsfromhexrep_b #1.#2{%
2926 \expandafter\phiexponentsfromrep_c
2927 \romannumeral0\xintreversedigits{\xintHexToBin{#1}};%
2928 \if;#2\expandafter\zeck_done\else
2929 \expandafter\phiexponentsfromhexrep_i\fi #2%
2930 }%
2931 % \end{macrocode}
2932 % Attention that conversion from hexadecimal to binary must preserve leading
2933 % zeros!
2934 % \begin{macrocode}
2935 \def\phiexponentsfromhexrep_i#1;{%
2936 \expanded{\noexpand\phiexponentsfromrep_j -1.\xintCHexToBin{#1}};%
2937 }%
2938 % \end{macrocode}
2939 % \subsection{The Knuth Fibonacci multiplication}
2940 % \subsubsection{\csh{ZeckKMul}: Knuth definition}
2941 % Here a |\romannumeral0| trigger is used to match |\xintiisum|. Although
2942 % it induces defining one more macro we obide by the general coding style of
2943 % \xintname with a CamelCase then a lowercase macro, rather than having them
2944 % merged as only one.
2945 %
2946 % For the |\xinteval| we need a variant applying |\xintNum| to its arguments.
2947 % \begin{macrocode}
2948 \def\ZeckKMul{\romannumeral0\zeckkmul}%
2949 \def\zeckkmul#1#2{\expandafter\zeckkmul_a
2950 \expanded{\ZeckIndices{#1}%
2951 ,;%
2952 \ZeckIndices{#2}%
2953 ,,}%
2954 }%
2955 \def\ZeckKMulNum#1#2{\romannumeral0\zeckkmul{\xintNum{#1}}{\xintNum{#2}}}%
2956 % \end{macrocode}
2957 % The space token at start of |#2| after first one is not a problem
2958 % as it ends up in a |\numexpr| anyhow.
2959 % \begin{macrocode}
2960 \def\zeckkmul_a{\expandafter\xintiisum\expanded{{\iffalse}}\fi
2961 \zeckkmul_b}%
2962 \def\zeckkmul_b#1;#2,{%
2963 \if\relax#2\relax\expandafter\zeckkmul_done\fi
2964 \zeckkmul_c{#2}#1,\zeckkmul_b#1;%
2965 }%
2966 \def\zeckkmul_c#1#2,{%
2967 \if\relax#2\relax\expandafter\xint_gobble_iv\fi
2968 \Zeck@@FN{#1+#2}\zeckkmul_c{#1}%
2969 }%
2970 \def\zeckkmul_done#1;{\iffalse{{\fi}}}%
2971 % \end{macrocode}
2972 % \subsubsection{\csh{ZeckAMul}: Arnoux formula}
2973 % Here a |\romannumeral0| trigger is used to match |\xintiisum|.
2974 % \begin{macrocode}
2975 \def\ZeckAMul{\romannumeral0\zeckamul}%
2976 \def\ZeckAMulNum#1#2{\romannumeral0\zeckamul{\xintNum{#1}}{\xintNum{#2}}}%
2977 \def\zeckamul#1{\expandafter\zeckamul_in\romannumeral`&&@#1;}%
2978 \def\zeckamul_in#1;#2{\expandafter\zeckamul_a\romannumeral`&&@#2;#1;}%
2979 \def\zeckamul_a #1;#2;{\xintiiadd
2980 {\xintiiMul{#1}{#2}}
2981 {\xintiiAdd{\xintiiMul{#1}{\ZeckB{#2}}}{\xintiiMul{\ZeckB{#1}}{#2}}}%
2982 }%
2983 % \end{macrocode}
2984 % \subsubsection{\csh{ZeckB}: B operator}
2985 % Here a |\romannumeral0| trigger is used to match |\xintiisum|. It is a fact
2986 % of life that |\xintiiSum| needs to grab something at each item before
2987 % expanding it, rather than expanding prior to grabbing. So we use an
2988 % |\expanded| wrapper.
2989 % \begin{macrocode}
2990 \def\ZeckB{\romannumeral0\zeckb}%
2991 \def\zeckb#1{\xintiisum{\expanded{\iffalse}\fi
2992 \expandafter\zeckb_a\expanded\zeckindices{#1},,}}%
2993 % \end{macrocode}
2994 % |#1-1| is always positive.
2995 % \begin{macrocode}
2996 \def\zeckb_a#1,{%
2997 \if\relax#1\relax\expandafter\zeckb_done\fi
2998 \Zeck@@FN{#1-1}\zeckb_a
2999 }%
3000 \def\zeckb_done#1\zeckb_a{\iffalse{\fi}}%
3001 % \end{macrocode}
3002 % \subsection{Typesetting}
3003 % \subsubsection{\csh{ZeckPrintIndexedSum}}
3004 % Expandable, but needs \emph{x}-expansion. The default requires math mode,
3005 % at it uses |\sb|. We do not use |_| here due to its current catcode. It
3006 % only \emph{f}-expands its argument. No repeated or final comma is allowed.
3007 % \begin{macrocode}
3008 \def\ZeckPrintIndexedSumSep{+\allowbreak}%
3009 \def\ZeckPrintOne#1{F\sb{#1}}%
3010 \def\ZeckPrintIndexedSum#1{%
3011 \expandafter\zeckprintindexedsum\romannumeral`&&@#1,;%
3012 }%
3013 \def\zeckprintindexedsum#1{%
3014 \if,#1\expandafter\xint_gob_til_sc\fi \zeckprintindexedsum_a#1%
3015 }%
3016 \def\zeckprintindexedsum_a#1,{\ZeckPrintOne{#1}\zeckprintindexedsum_b}%
3017 \def\zeckprintindexedsum_b #1{%
3018 \if;#1\expandafter\xint_gob_til_sc\fi
3019 \ZeckPrintIndexedSumSep\zeckprintindexedsum_a#1%
3020 }%
3021 % \end{macrocode}
3022 % \subsubsection{\csh{PhiPrintIndexedSum}}
3023 % A clone of |\ZeckPrintIndexedSum| with its own namespace.
3024 % \begin{macrocode}
3025 \let\PhiPrintIndexedSumSep\ZeckPrintIndexedSumSep
3026 \def\PhiPrintOne#1{\phi\sp{#1}}%
3027 \def\PhiPrintIndexedSum#1{%
3028 \expandafter\phiprintindexedsum\romannumeral`&&@#1,;%
3029 }%
3030 \def\phiprintindexedsum#1{%
3031 \if,#1\expandafter\xint_gob_til_sc\fi \phiprintindexedsum_a#1%
3032 }%
3033 \def\phiprintindexedsum_a#1,{\PhiPrintOne{#1}\phiprintindexedsum_b}%
3034 \def\phiprintindexedsum_b #1{%
3035 \if;#1\expandafter\xint_gob_til_sc\fi
3036 \PhiPrintIndexedSumSep\phiprintindexedsum_a#1%
3037 }%
3038 % \end{macrocode}
3039 % \subsubsection{\csh{PhiTypesetX}}
3040 % \begin{macrocode}
3041 \def\PhiTypesetX #1{%
3042 \expandafter\PhiTypesetXPrint\expanded{#1}%
3043 }%
3044 \def\PhiTypesetXPrint #1#2{%
3045 \xintiiifSgn{#1}%
3046 {#1\xintiiifSgn{#2}{#2\phi}{}{+#2\phi}}%
3047 {\xintiiifSgn{#2}{#2\phi}{0}{#2\phi}}%
3048 {#1\xintiiifSgn{#2}{#2\phi}{}{+#2\phi}}%
3049 }%
3050 % \end{macrocode}
3051 % \subsection{Extensions of the \cs{xinteval} syntax}
3052 %
3053 % Initially functions and Knuth operator were added to |\xintiieval| only, but
3054 % when it was decided to overload the infix operators to handle inputs from
3055 % $\ZZ[\phi]$, it felt awkward not to include the division so finally we
3056 % support $\QQ(\phi)$ algebra, and for this had to switch to |\xinteval|.
3057 %
3058 % \subsubsection{Provisory ad hoc support for adding \xintexprname operators}
3059 % Unfortunately, contrarily to \bnumexprname, \xintexprname (at
3060 % |1.4o|) has no public interface to define an infix operator, and the macros
3061 % it defines to that end have acquired another meaning at end of loading
3062 % |xintexpr.sty|, so we have to copy quite a few lines of code. This is
3063 % provisory and will be removed when xintexpr.sty will have been udpated. We
3064 % also copy/adapt |\bnumdefinfix|.
3065 %
3066 % We test for existence of |\xintdefinfix| so as to be able to update upstream
3067 % and not have to sync it immediately. But perhaps upstream will choose some
3068 % other name than |\xintdefinfix|...
3069 % \begin{macrocode}
3070 \ifdefined\xintdefinfix
3071 \def\zeckdefinfix{\xintdefinfix {expr}}%
3072 \else
3073 \ifdefined\xint_noxpd\else\let\xint_noxpd\unexpanded\fi % support old xint
3074 \def\ZECK_defbin_c #1#2#3#4#5#6#7#8%
3075 {%
3076 \XINT_global\def #1##1% \XINT_#8_op_<op>
3077 {%
3078 \expanded{\xint_noxpd{#2{##1}}\expandafter}%
3079 \romannumeral`&&@\expandafter#3\romannumeral`&&@\XINT_expr_getnext
3080 }%
3081 \XINT_global\def #2##1##2##3##4% \XINT_#8_exec_<op>
3082 {%
3083 \expandafter##2\expandafter##3\expandafter
3084 {\romannumeral`&&@\XINT:NEhook:f:one:from:two{\romannumeral`&&@#7##1##4}}%
3085 }%
3086 \XINT_global\def #3##1% \XINT_#8_check-_<op>
3087 {%
3088 \xint_UDsignfork
3089 ##1{\expandafter#4\romannumeral`&&@#5}%
3090 -{#4##1}%
3091 \krof
3092 }%
3093 \XINT_global\def #4##1##2% \XINT_#8_checkp_<op>
3094 {%
3095 \ifnum ##1>#6%
3096 \expandafter#4%
3097 \romannumeral`&&@\csname XINT_#8_op_##2\expandafter\endcsname
3098 \else
3099 \expandafter ##1\expandafter ##2%
3100 \fi
3101 }%
3102 }%
3103 % \end{macrocode}
3104 % ATTENTION there is lacking at end here compared to the \bnumexprname version
3105 % an adjustment for updating minus operator,
3106 % if some other right precedence than 12, 14, 17 is used.
3107 % Doing this would requiring copying still more, so is not done.
3108 % \begin{macrocode}
3109 \def\ZECK_defbin_b #1#2#3#4#5%
3110 {%
3111 \expandafter\ZECK_defbin_c
3112 \csname XINT_#1_op_#2\expandafter\endcsname
3113 \csname XINT_#1_exec_#2\expandafter\endcsname
3114 \csname XINT_#1_check-_#2\expandafter\endcsname
3115 \csname XINT_#1_checkp_#2\expandafter\endcsname
3116 \csname XINT_#1_op_-\romannumeral\ifnum#4>12 #4\else12\fi\expandafter\endcsname
3117 \csname xint_c_\romannumeral#4\endcsname
3118 #5%
3119 {#1}% #8 for \ZECK_defbin_c
3120 \XINT_global
3121 \expandafter
3122 \let\csname XINT_expr_precedence_#2\expandafter\endcsname
3123 \csname xint_c_\romannumeral#3\endcsname
3124 }%
3125 % \end{macrocode}
3126 % These next two currently lifted from \bnumexprname with some adaptations,
3127 % see previous comment about precedences.
3128 %
3129 % We do not define the extra |\chardef|'s as does \bnumexprname to allow more
3130 % user choices of precedences, not only because nobody will ever use the
3131 % feature, but also because it needs extra configuration for minus unary
3132 % operator. (as mentioned above)
3133 % \begin{macrocode}
3134 \def\zeckdefinfix #1#2#3#4%
3135 {%
3136 \edef\ZECK_tmpa{#1}%
3137 \edef\ZECK_tmpa{\xint_zapspaces_o\ZECK_tmpa}%
3138 \edef\ZECK_tmpL{\the\numexpr#3\relax}%
3139 \edef\ZECK_tmpL
3140 {\ifnum\ZECK_tmpL<4 4\else\ifnum\ZECK_tmpL<23 \ZECK_tmpL\else 22\fi\fi}%
3141 \edef\ZECK_tmpR{\the\numexpr#4\relax}%
3142 \edef\ZECK_tmpR
3143 {\ifnum\ZECK_tmpR<4 4\else\ifnum\ZECK_tmpR<23 \ZECK_tmpR\else 22\fi\fi}%
3144 \ZECK_defbin_b {expr}\ZECK_tmpa\ZECK_tmpL\ZECK_tmpR #2%
3145 \expandafter\ZECK_dotheitselves\ZECK_tmpa\relax
3146 \unless\ifcsname
3147 XINT_expr_exec_-\romannumeral\ifnum\ZECK_tmpR>12 \ZECK_tmpR\else 12\fi
3148 \endcsname
3149 \xintMessage{zeckendorf}{Error}{Right precedence not among accepted values.}%
3150 \errhelp{Accepted values include 12, 14, and 17.}%
3151 \errmessage{Sorry, you can not use \ZECK_tmpR\space as right precedence.}%
3152 \fi
3153 \ifxintverbose
3154 \xintMessage{zeckendorf}{info}{infix operator \ZECK_tmpa\space
3155 \ifxintglobaldefs globally \fi
3156 does
3157 \xint_noxpd{#2}\MessageBreak with precedences \ZECK_tmpL, \ZECK_tmpR;}%
3158 \fi
3159 }%
3160 \def\ZECK_dotheitselves#1#2%
3161 {%
3162 \if#2\relax\expandafter\xint_gobble_ii
3163 \else
3164 \XINT_global
3165 \expandafter\edef\csname XINT_expr_itself_#1#2\endcsname{#1#2}%
3166 \unless\ifcsname XINT_expr_precedence_#1\endcsname
3167 \XINT_global
3168 \expandafter\edef\csname XINT_expr_precedence_#1\endcsname
3169 {\csname XINT_expr_precedence_\ZECK_tmpa\endcsname}%
3170 \XINT_global
3171 \expandafter\odef\csname XINT_expr_op_#1\endcsname
3172 {\csname XINT_expr_op_\ZECK_tmpa\endcsname}%
3173 \fi
3174 \fi
3175 \ZECK_dotheitselves{#1#2}%
3176 }%
3177 % \end{macrocode}
3178 % There is no ``undefine operator'' in \bnumexprname currently. Experimental,
3179 % I don't want to spend too much time. I sense there is a potential problem
3180 % with multi-character operators related to ``undoing the itselves'', because
3181 % of the mechanism which allows to use for example |;;| as short-cut for |;;;|
3182 % if |;;| was not pre-defined when |;;;| got defined. To undefine |;;|, I
3183 % would need to check if it really has been aliased to |;;;|, and I don't do
3184 % the effort here.
3185 % \begin{macrocode}
3186 \def\ZECK_undefbin_b #1#2%
3187 {%
3188 \XINT_global\expandafter\let
3189 \csname XINT_#1_op_#2\endcsname\xint_undefined
3190 \XINT_global\expandafter\let
3191 \csname XINT_#1_exec_#2\endcsname\xint_undefined
3192 \XINT_global\expandafter\let
3193 \csname XINT_#1_check-_#2\endcsname\xint_undefined
3194 \XINT_global\expandafter\let
3195 \csname XINT_#1_checkp_#2\endcsname\xint_undefined
3196 \XINT_global\expandafter\let
3197 \csname XINT_expr_precedence_#2\endcsname\xint_undefined
3198 \XINT_global\expandafter\let
3199 \csname XINT_expr_itself_#2\endcsname\xint_undefined
3200 }%
3201 \def\zeckundefinfix #1%
3202 {%
3203 \edef\ZECK_tmpa{#1}%
3204 \edef\ZECK_tmpa{\xint_zapspaces_o\ZECK_tmpa}%
3205 \ZECK_undefbin_b {expr}\ZECK_tmpa
3206 %% \ifxintverbose
3207 \xintMessage{zeckendorf}{Warning}{infix operator \ZECK_tmpa\space
3208 has been DELETED!}%
3209 %% \fi
3210 }%
3211 \fi
3212 \def\ZeckDeleteOperator#1{\zeckundefinfix{#1}}%
3213 % \end{macrocode}
3214 % Attention, this is like |\bnumdefinfix| and thus does not have same order
3215 % of arguments as the |\ZECK_defbin_b| above.
3216 % \begin{macrocode}
3217 \def\ZeckSetAsKnuthOp#1{\zeckdefinfix{#1}{\ZeckKMulNum}{14}{14}}%
3218 \def\ZeckSetAsArnouxOp#1{\zeckdefinfix{#1}{\ZeckAMulNum}{14}{14}}%
3219 % \end{macrocode}
3220 % \subsubsection{The \textdollar{} and \textdollar{}\textdollar{} as
3221 % infix operator for the Knuth multiplication}
3222 % \begin{macrocode}
3223 \ZeckSetAsArnouxOp{$}% $ (<-only to tame Emacs/AUCTeX highlighting)
3224 \ZeckSetAsKnuthOp{$$}% $$
3225 % \end{macrocode}
3226 %^^A pourquoi espace disparaît après le {**}? D'où le {}.
3227 %^^A mark-up copié collé depuis xintexpr
3228 % \subsubsection{Support macros for \texorpdfstring{$\QQ(\phi)$}{Q(phi)} algebra}
3229 % \paragraph{\csh{PhiSgn}}\mbox{}
3230 % \begin{macrocode}
3231 \def\PhiSign{\romannumeral0\phisign}%
3232 \def\phisign #1{%
3233 \expandafter\phisign_a\expanded{{#1}}%
3234 }%
3235 \def\phisign_a #1{\expandafter\phisign_b\string#1;}%
3236 \catcode`[=1 \catcode`]=2 \catcode`\{=12 % }
3237 \def\phisign_b #1[\if#1{\expandafter\phisign_X % }
3238 \else\expandafter\phisign_N\fi #1]%
3239 \catcode`[=12 \catcode`]=12 \catcode`\{=1 % }
3240 \def\phisign_N #1;{\XINT_sgn #1\xint:}%
3241 \def\phisign_X #1{\expandafter\phisign_ab\expandafter{\iffalse}\fi}%
3242 \def\PhiSign_ab {\romannumeral0\phisign_ab}%
3243 \def\phisign_ab #1#2{%
3244 \xintiiifsgn{#1}%
3245 {% a < 0
3246 \xintiiifSgn{#2}%
3247 {-1}%
3248 {-1}%
3249 {% a < 0, b > 0, return 1 iff a^2+ab<b^2
3250 \xintifLt{\xintMul{#1}{\xintAdd{#1}{#2}}}{\xintSqr{#2}}%
3251 {1}%
3252 {-1}%
3253 }%
3254 }%
3255 {\xintiiSgn{#2}}%
3256 {% a > 0
3257 \xintiiifSgn{#2}%
3258 {% a > 0, b < 0, return 1 iff a^2+ab>b^2
3259 \xintifGt{\xintMul{#1}{\xintAdd{#1}{#2}}}{\xintSqr{#2}}%
3260 {1}%
3261 {-1}%
3262 }%
3263 {1}%
3264 {1}%
3265 }%
3266 }%
3267 % \end{macrocode}
3268 % \paragraph{\csh{PhiAbs}}\mbox{}
3269 % \begin{macrocode}
3270 \def\PhiAbs{\romannumeral0\phiabs}%
3271 \def\phiabs #1{%
3272 \expandafter\phiabs_a\expanded{{#1}}%
3273 }%
3274 \def\phiabs_a #1{\expandafter\phiabs_b\string#1}%
3275 \catcode`[=1 \catcode`]=2 \catcode`\{=12 % }
3276 \def\phiabs_b #1[\if#1{\expandafter\phiabs_X % }
3277 \else\expandafter\phiabs_N\fi #1]%
3278 \catcode`[=12 \catcode`]=12 \catcode`\{=1 % }
3279 \let\phiabs_N \XINT_abs
3280 \def\phiabs_X #1{\expandafter\phiabs_x\expandafter{\iffalse}\fi}%
3281 \def\phiabs_x #1#2{\expanded{%
3282 \ifnum\PhiSign_ab {#1}{#2}<\xint_c_
3283 \expandafter\xint_firstoftwo\else\expandafter\xint_secondoftwo\fi
3284 {{\XINT_Opp#1}{\XINT_Opp#2}}{{#1}{#2}}%
3285 }%
3286 }%
3287 % \end{macrocode}
3288 % \paragraph{\csh{PhiNorm}}\mbox{}
3289 % \begin{macrocode}
3290 \def\PhiNorm{\romannumeral0\phinorm}%
3291 \def\phinorm #1{%
3292 \expandafter\phinorm_a\expanded{{#1}}%
3293 }%
3294 \def\phinorm_a #1{\expandafter\phinorm_b\detokenize{#1;}{#1}}%
3295 \catcode`[=1 \catcode`]=2 \catcode`\{=12 % }
3296 \def\phinorm_b #1#2;[\if#1{\expandafter\phinorm_X % }
3297 \else\expandafter\phinorm_N\fi]%
3298 \catcode`[=12 \catcode`]=12 \catcode`\{=1 % }
3299 \let\phinorm_N\xintsqr
3300 \def\phinorm_X #1{\phinorm_x #1}%
3301 \def\phinorm_x #1#2{\xintsub
3302 {\xintMul{#1}{\xintAdd{#1}{#2}}}%
3303 {\xintSqr{#2}}%
3304 }%
3305 % \end{macrocode}
3306 % \paragraph{\csh{PhiConj}}\mbox{}
3307 % \begin{macrocode}
3308 \def\PhiConj{\romannumeral0\phiconj}%
3309 \def\phiconj #1{%
3310 \expandafter\phiconj_a\expanded{{#1}}%
3311 }%
3312 \def\phiconj_a #1{\expandafter\phiconj_b\detokenize{#1;}#1}%
3313 \catcode`[=1 \catcode`]=2 \catcode`\{=12 % }
3314 \def\phiconj_b #1#2;[\if#1{\expandafter\phiconj_X % }
3315 \else\expandafter\phiconj_N\fi]%
3316 \catcode`[=12 \catcode`]=12 \catcode`\{=1 % }
3317 \let\phiconj_N\space
3318 \def\phiconj_X #1#2{\expanded{%
3319 {\xintAdd{#1}{#2}}{\XINT_Opp #2}%
3320 }}%
3321 % \end{macrocode}
3322 % \paragraph{\csh{PhiOpp}}\mbox{}
3323 % \begin{macrocode}
3324 \def\PhiOpp{\romannumeral0\phiopp}%
3325 \def\phiopp #1{%
3326 \expandafter\phiopp_a\expanded{{#1}}%
3327 }%
3328 \def\phiopp_a #1{\expandafter\phiopp_b\string#1}%
3329 \catcode`[=1 \catcode`]=2 \catcode`\{=12 % }
3330 \def\phiopp_b #1[\if#1{\expandafter\phiopp_X % }
3331 \else\expandafter\phiopp_N\fi #1]%
3332 \catcode`[=12 \catcode`]=12 \catcode`\{=1 % }
3333 \let\phiopp_N \XINT_opp
3334 \def\phiopp_X #1{\expandafter\phiopp_x\expandafter{\iffalse}\fi}%
3335 \def\phiopp_x #1#2{\expanded{%
3336 {\XINT_Opp #1}{\XINT_Opp #2}%
3337 }}%
3338 % \end{macrocode}
3339 % \paragraph{\csh{PhiAdd}}\mbox{}
3340 % \begin{macrocode}
3341 \def\PhiAdd{\romannumeral0\phiadd}%
3342 \def\phiadd #1#2{%
3343 \expandafter\phiadd_a\expanded{{#1}{#2}}%
3344 }%
3345 \def\phiadd_a #1{\expandafter\phiadd_b\string#1;}%
3346 \catcode`[=1 \catcode`]=2 \catcode`\{=12 % }
3347 \def\phiadd_b #1[\if#1{\expandafter\phiadd_X % }
3348 \else\expandafter\phiadd_N\fi #1]%
3349 \def\phiadd_N #1;#2[\expandafter\phiadd_n\string#2;[#1]]%
3350 \def\phiadd_n #1[\if#1{\expandafter\phiadd_nX % }
3351 \else\expandafter\phiadd_nn\fi #1]%
3352 \def\phiadd_nX #1[\expandafter\phiadd_nx\expandafter[\iffalse]\fi]%
3353 \def\phiadd_X #1[\expandafter\phiadd_x\expandafter[\iffalse]\fi]%
3354 % \end{macrocode}
3355 % |#1={a}{b}|.
3356 % \begin{macrocode}
3357 \def\phiadd_x #1;#2[\expandafter\phiadd_xa\string#2;#1]%
3358 \def\phiadd_xa#1[\if#1{\expandafter\phiadd_XX % }
3359 \else\expandafter\phiadd_xn\fi #1]%
3360 \def\phiadd_XX #1[\expandafter\phiadd_xx\expandafter[\iffalse]\fi]%
3361 \catcode`[=12 \catcode`]=12 \catcode`\{=1 % }
3362 \def\phiadd_nn #1;{\xintadd{#1}}%
3363 \def\phiadd_nx #1#2;#3{\expandafter
3364 {\romannumeral0\xintadd{#1}{#3}}{#2}%
3365 }%
3366 \def\phiadd_xn #1;#2{\expandafter
3367 {\romannumeral0\xintadd{#1}{#2}}%
3368 }%
3369 \def\phiadd_xx #1#2;#3#4{\expanded{%
3370 {\xintAdd{#1}{#3}}%
3371 {\xintAdd{#2}{#4}}%
3372 }}%
3373 % \end{macrocode}
3374 % \paragraph{\csh{PhiSub}}\mbox{}
3375 % \begin{macrocode}
3376 \def\PhiSub{\romannumeral0\phisub}%
3377 \def\phisub #1#2{%
3378 \expandafter\phisub_a\expanded{{#1}{#2}}%
3379 }%
3380 \def\phisub_a #1{\expandafter\phisub_b\string#1;}%
3381 \catcode`[=1 \catcode`]=2 \catcode`\{=12 % }
3382 \def\phisub_b #1[\if#1{\expandafter\phisub_X % }
3383 \else\expandafter\phisub_N\fi #1]%
3384 \def\phisub_N #1;#2[\expandafter\phisub_n\string#2;[#1]]%
3385 \def\phisub_n #1[\if#1{\expandafter\phisub_nX % }
3386 \else\expandafter\phisub_nn\fi #1]%
3387 \def\phisub_nX #1[\expandafter\phisub_nx\expandafter[\iffalse]\fi]%
3388 \def\phisub_X #1[\expandafter\phisub_x\expandafter[\iffalse]\fi]%
3389 % \end{macrocode}
3390 % |#1={a}{b}|.
3391 % \begin{macrocode}
3392 \def\phisub_x #1;#2[\expandafter\phisub_xa\string#2;#1]%
3393 \def\phisub_xa#1[\if#1{\expandafter\phisub_XX % }
3394 \else\expandafter\phisub_xn\fi #1]%
3395 \def\phisub_XX #1[\expandafter\phisub_xx\expandafter[\iffalse]\fi]%
3396 \catcode`[=12 \catcode`]=12 \catcode`\{=1 % }
3397 \def\phisub_nn #1;#2{\xintsub{#2}{#1}}%
3398 \def\phisub_nx #1#2;#3{\expanded{%
3399 {\xintSub{#3}{#1}}{\XINT_Opp#2}%
3400 }}%
3401 \def\phisub_xn #1;#2{\expandafter
3402 {\romannumeral0\xintsub{#2}{#1}}%
3403 }%
3404 \def\phisub_xx #1#2;#3#4{\expanded{%
3405 {\xintSub{#3}{#1}}%
3406 {\xintSub{#4}{#2}}%
3407 }}%
3408 % \end{macrocode}
3409 % \paragraph{\csh{PhiMul}}\mbox{}
3410 % \begin{macrocode}
3411 \def\PhiMul{\romannumeral0\phimul}%
3412 \def\phimul #1#2{%
3413 \expandafter\phimul_a\expanded{{#1}{#2}}%
3414 }%
3415 \def\phimul_a #1{\expandafter\phimul_b\string#1;}%
3416 \catcode`[=1 \catcode`]=2 \catcode`\{=12 % }
3417 \def\phimul_b #1[\if#1{\expandafter\phimul_X % }
3418 \else\expandafter\phimul_N\fi #1]%
3419 \def\phimul_N #1;#2[\expandafter\phimul_n\string#2;[#1]]%
3420 \def\phimul_n #1[\if#1{\expandafter\phimul_nX % }
3421 \else\expandafter\phimul_nn\fi #1]%
3422 \def\phimul_nX #1[\expandafter\phimul_nx\expandafter[\iffalse]\fi]%
3423 \def\phimul_X #1[\expandafter\phimul_x\expandafter[\iffalse]\fi]%
3424 % \end{macrocode}
3425 % |#1={a}{b}|.
3426 % \begin{macrocode}
3427 \def\phimul_x #1;#2[\expandafter\phimul_xa\string#2;#1]%
3428 \def\phimul_xa#1[\if#1{\expandafter\phimul_XX % }
3429 \else\expandafter\phimul_xn\fi #1]%
3430 \def\phimul_XX #1[\expandafter\phimul_xx\expandafter[\iffalse]\fi]%
3431 \catcode`[=12 \catcode`]=12 \catcode`\{=1 % }
3432 \def\phimul_nn#1;{\xintmul{#1}}%
3433 \def\phimul_nx#1#2;#3{\expanded{%
3434 {\xintMul{#1}{#3}}{\xintMul{#2}{#3}}%
3435 }}%
3436 \def\phimul_xn#1;#2#3{\expanded{%
3437 {\xintMul{#1}{#2}}{\xintMul{#1}{#3}}%
3438 }}%
3439 \def\phimul_xx #1#2;#3#4{%
3440 \expandafter\phimul_xx_a\expanded{%
3441 \xintMul{#1}{#3};% ca
3442 \xintMul{#2}{#4};% db
3443 \xintMul{#1}{#4};% da
3444 \xintMul{#2}{#3};% cb
3445 }%
3446 }%
3447 \def\phimul_xx_a #1;#2;#3;#4;{\expanded{%
3448 {\xintAdd{#1}{#2}}%
3449 {\xintAdd{#3}{\xintAdd{#4}{#2}}}%
3450 }}%
3451 % \end{macrocode}
3452 % \paragraph{\csh{PhiDiv}}\mbox{}
3453 % \begin{macrocode}
3454 \def\PhiDiv{\romannumeral0\phidiv}%
3455 \def\phidiv #1#2{%
3456 \expandafter\phidiv_a\expanded{{#1}{#2}}%
3457 }%
3458 \def\phidiv_a #1{\expandafter\phidiv_b\string#1;}%
3459 \catcode`[=1 \catcode`]=2 \catcode`\{=12 % }
3460 \def\phidiv_b #1[\if#1{\expandafter\phidiv_X % }
3461 \else\expandafter\phidiv_N\fi #1]%
3462 \def\phidiv_N #1;#2[\expandafter\phidiv_n\string#2;[#1]]%
3463 \def\phidiv_n #1[\if#1{\expandafter\phidiv_nX % }
3464 \else\expandafter\phidiv_nn\fi #1]%
3465 \def\phidiv_nX #1[\expandafter\phidiv_nx\expandafter[\iffalse]\fi]%
3466 \def\phidiv_X #1[\expandafter\phidiv_x\expandafter[\iffalse]\fi]%
3467 % \end{macrocode}
3468 % |#1={a}{b}|.
3469 % \begin{macrocode}
3470 \def\phidiv_x #1;#2[\expandafter\phidiv_xa\string#2;#1]%
3471 \def\phidiv_xa#1[\if#1{\expandafter\phidiv_XX % }
3472 \else\expandafter\phidiv_xn\fi #1]%
3473 \def\phidiv_XX #1[\expandafter\phidiv_xx\expandafter[\iffalse]\fi]%
3474 \catcode`[=12 \catcode`]=12 \catcode`\{=1 % }
3475 \def\phidiv_nn#1;#2{\xintdiv{#2}{#1}}%
3476 \def\phidiv_nx#1#2{%
3477 \expandafter\phidiv_nx_a\expanded{%
3478 {\xintSub{\xintMul{#1}{\xintAdd{#1}{#2}}}{\xintSqr{#2}}}%
3479 }{#1}{#2}%
3480 }%
3481 \def\phidiv_nx_a#1#2#3;#4{\expanded{%
3482 {\xintIrr{\xintDiv{\xintMul{#4}{\xintAdd{#2}{#3}}}{#1}}[0]}%
3483 {\xintIrr{\xintOpp{\xintDiv{\xintMul{#4}{#3}}{#1}}}[0]}%
3484 }}%
3485 \def\phidiv_xn #1;#2#3{\expanded{%
3486 {\xintIrr{\xintDiv{#2}{#1}}[0]}%
3487 {\xintIrr{\xintDiv{#3}{#1}}[0]}%
3488 }}%
3489 \def\phidiv_xx #1#2;#3#4{%
3490 \expandafter\phidiv_xx_a\expanded{%
3491 \expandafter\phimul_xx
3492 \expanded{{\xintAdd{#1}{#2}}{\XINT_Opp #2}};{#3}{#4}%
3493 {\xintSub{\xintMul{#1}{\xintAdd{#1}{#2}}}{\xintSqr{#2}}}%
3494 }%
3495 }%
3496 \def\phidiv_xx_a #1#2#3{%
3497 \expanded{%
3498 {\xintIrr{\xintDiv{#1}{#3}}[0]}%
3499 {\xintIrr{\xintDiv{#2}{#3}}[0]}%
3500 }%
3501 }%
3502 % \end{macrocode}
3503 % \paragraph{\csh{PhiPow}}\mbox{}
3504 % \begin{macrocode}
3505 \def\PhiPow{\romannumeral0\phipow}%
3506 \def\phipow #1#2{%
3507 \expandafter\phipow_a\expanded
3508 {{#1}{\xintNum{#2}}}%
3509 }%
3510 \def\phipow_a #1{\expandafter\phipow_b\string#1;}%
3511 \catcode`[=1 \catcode`]=2 \catcode`\{=12 % }
3512 \def\phipow_b #1[\if#1{\expandafter\phipow_X % }
3513 \else\expandafter\phipow_N\fi #1]%
3514 \catcode`[=12 \catcode`]=12 \catcode`\{=1 % }
3515 \def\phipow_N #1;{\xintpow{#1}}%
3516 \def\phipow_X #1{\expandafter\phipow_x\expandafter{\iffalse}\fi}%
3517 % \end{macrocode}
3518 % Let's handle negative exponents too, now that we use |\xinteval|.
3519 % \begin{macrocode}
3520 \def\phipow_x #1;#2{\phipow_fork #2;#1}%
3521 \def\phipow_fork #1{%
3522 \xint_UDzerominusfork
3523 0#1\phipow_neg
3524 #1-\phipow_zero
3525 0-\phipow_pos
3526 \krof #1%
3527 }%
3528 \def\phipow_zero 0;#1#2{{1}{0}}%
3529 \def\phipow_neg -{%
3530 \expandafter\phiinv_ab\romannumeral0\phipow_pos
3531 }%
3532 \def\phiinv_ab #1#2{%
3533 \expandafter\phiinv_c\expanded{%
3534 {\xintSub{\xintMul{#1}{\xintAdd{#1}{#2}}}{\xintSqr{#2}}}%
3535 }{#1}{#2}%
3536 }%
3537 \def\phiinv_c #1#2#3{\expanded{%
3538 {\xintIrr{\xintDiv{\xintAdd{#2}{#3}}{#1}}[0]}%
3539 {\xintIrr{\xintOpp{\xintDiv{#3}{#1}}}[0]}%
3540 }}%
3541 \def\phipow_pos #1;{%
3542 \expandafter\phipow_xa
3543 \expanded{10\xintDecToBin{#1}},;%
3544 }%
3545 \def\phipow_xa #1#2#3#4;{%
3546 \if#3,\expandafter\phipow_done\fi
3547 \if#31\expandafter\phipow_xo
3548 \else\expandafter\phipow_xe\fi
3549 {#1}{#2}#4;%
3550 }%
3551 \def\phipow_done \if#1\fi #2#3;#4#5{{#2}{#3}}%
3552 \def\phipow_xo #1#2{%
3553 \expandafter\phipow_xo_a\expanded{%
3554 \xintSqr{#1};\xintMul{#1}{#2};\xintSqr{#2};%
3555 }%
3556 }%
3557 \def\phipow_xo_a #1;#2;#3;{%
3558 \expandafter\phipow_xo_b\expanded{%
3559 \xintAdd{#1}{#3};\xintAdd{#2}{\xintAdd{#2}{#3}};%
3560 }%
3561 }%
3562 \def\phipow_xo_b#1;#2;#3;#4#5{%
3563 \expandafter\phipow_xa\romannumeral0%
3564 \phimul_xx {#1}{#2};{#4}{#5}#3;{#4}{#5}%
3565 }%
3566 \def\phipow_xe #1#2{%
3567 \expandafter\phipow_xe_a\expanded{%
3568 \xintSqr{#1};\xintMul{#1}{#2};\xintSqr{#2};%
3569 }%
3570 }%
3571 \def\phipow_xe_a #1;#2;#3;{%
3572 \expandafter\phipow_xa\expanded{%
3573 {\xintAdd{#1}{#3}}{\xintAdd{#2}{\xintAdd{#2}{#3}}}%
3574 }%
3575 }%
3576 % \end{macrocode}
3577 % \subsubsection{Overloading \texttt{+},
3578 % \texttt{\textendash}, \texorpdfstring{\texttt{\protect\lowast}}{*},
3579 % \texttt{/},
3580 % \texttt{\textasciicircum},
3581 % and \texorpdfstring{\texttt{\protect\lowast\protect\lowast}}{**}}
3582 %
3583 % The |**| is pre-aliased to |^| at \xintexprname level via
3584 % |\XINT_expr_itself_**|, so nothing to do here once |^| is handled.
3585 %
3586 % The unary |-| requires extra care.
3587 % \begin{macrocode}
3588 \zeckdefinfix{+}{\PhiAdd}{12}{12}%
3589 \zeckdefinfix{-}{\PhiSub}{12}{12}%
3590 \xintFor #1 in {xii,xiv,xvii}\do{%
3591 \expandafter\def\csname XINT_expr_exec_-#1\endcsname
3592 ##1##2##3%
3593 {%
3594 \expandafter ##1\expandafter ##2\expandafter
3595 {%
3596 \romannumeral`&&@\XINT:NEhook:f:one:from:one
3597 {\romannumeral`&&@\PhiOpp##3}%
3598 }%
3599 }%
3600 }%
3601 \zeckdefinfix{*}{\PhiMul}{14}{14}%
3602 \zeckdefinfix{/}{\PhiDiv}{14}{14}%
3603 \zeckdefinfix{^}{\PhiPow}{18}{17}%
3604 % \end{macrocode}
3605 % \subsubsection{Variables and functions for \cs{xinteval}}
3606 %
3607 % The macros computing Fibonacci numbers, Zeckendorf indices, and Bergman
3608 % exponents, were done originally assuming to be used with arguments in strict
3609 % integer format. But when operations are executed in |\xinteval| the
3610 % intermediate results will use the ``raw'' format described in the
3611 % \xintexprname manual, not the ``strict integer format''. We thus need
3612 % wrappers to apply |\xintNum| for normalization, even though this adds annoying
3613 % overhead. These wrappers can assume that the argument is already expanded.
3614 %
3615 % For macros handling input being either one unbraced integer or a pair of
3616 % braced integers this is more complicated. We separated |\PhiIISign_ab| from
3617 % |\PhiSign| to this aim. The former for optimized internal usage, only using
3618 % integer algebra. The latter uses the \xintfracname macros, so there is
3619 % no problem and we do not want to truncate arguments to integers. Similarly for
3620 % |\PhiAbs| no need to do something special.
3621 %
3622 % |\PhiMaxE| is integer-only, but in the end I decided to not provide an
3623 % |\xinteval| interface and to remove the one for |\ZeckMaxK|.
3624 %
3625 % For the support for |phiexponents()|, which is also integer only we have to
3626 % use |\xintNum|, the problem is that we can't do that prior to know if used
3627 % with an integer or a nutple. So |\Phi@BList| was done to handle that.
3628 % \begin{macrocode}
3629 \xintdefvar phi:=[0,1];%
3630 \xintdefvar psi:=[1,-1];%
3631 \def\XINT_expr_func_phinorm #1#2#3%
3632 {%
3633 \expandafter #1\expandafter #2\expandafter{%
3634 \romannumeral`&&@\XINT:NEhook:f:one:from:one
3635 {\romannumeral`&&@\PhiNorm#3}}%
3636 }%
3637 \def\XINT_expr_func_phiconj #1#2#3%
3638 {%
3639 \expandafter #1\expandafter #2\expandafter{%
3640 \romannumeral`&&@\XINT:NEhook:f:one:from:one
3641 {\romannumeral`&&@\PhiConj#3}}%
3642 }%
3643 \def\XINT_expr_func_phisign #1#2#3%
3644 {%
3645 \expandafter #1\expandafter #2\expandafter{%
3646 \romannumeral`&&@\XINT:NEhook:f:one:from:one
3647 {\romannumeral`&&@\PhiSign#3}}%
3648 }%
3649 \def\XINT_expr_func_phiabs #1#2#3%
3650 {%
3651 \expandafter #1\expandafter #2\expandafter{%
3652 \romannumeral`&&@\XINT:NEhook:f:one:from:one
3653 {\romannumeral`&&@\PhiAbs#3}}%
3654 }%
3655 \def\ZeckTheFNNum#1{\ZeckTheFN{\xintNum{#1}}}%
3656 \def\XINT_expr_func_fib #1#2#3%
3657 {%
3658 \expandafter #1\expandafter #2\expandafter{%
3659 \romannumeral`&&@\XINT:NEhook:f:one:from:one
3660 {\romannumeral`&&@\ZeckTheFNNum#3}}%
3661 }%
3662 \def\ZeckTheFSeqNum#1#2{\ZeckTheFSeq{\xintNum{#1}}{\xintNum{#2}}}%
3663 \def\XINT_expr_func_fibseq #1#2#3%
3664 {%
3665 \expandafter #1\expandafter #2\expandafter{%
3666 \romannumeral`&&@\XINT:NEhook:f:one:from:two
3667 {\romannumeral`&&@\ZeckTheFSeqNum#3}}%
3668 }%
3669 \def\ZeckBListNum #1{%
3670 \expanded\bgroup\expandafter\zeckblist_fork\romannumeral0\xintnum{#1}\xint:
3671 }%
3672 \def\XINT_expr_func_zeckindices #1#2#3%
3673 {%
3674 \expandafter #1\expandafter #2\expandafter{%
3675 \romannumeral`&&@\XINT:NEhook:f:one:from:one
3676 {\romannumeral`&&@\ZeckBListNum#3}}%
3677 }%
3678 % \end{macrocode}
3679 % TODO: I have forgotten now but I vaguely remember if compatibility
3680 % with usage of the defined function in |\xintdeffunc| is hoped for
3681 % that it should first expand its argument even though in our context
3682 % if purely numerical this is unneeded (and f-expansion will
3683 % end up hitting a brace if the input is a nutple). Adding anyhow.
3684 % I have other things in mind currently, to examine later, already
3685 % quite enough hours on this package.
3686 % \begin{macrocode}
3687 \def\Phi@BList#1{\expandafter\expandafter\expandafter
3688 \phi@blist_b\expandafter\string\romannumeral`&&@#1;}%
3689 \catcode`[=1 \catcode`]=2 \catcode`\{=12 % }
3690 \def\phi@blist_b #1[\if#1{\expandafter\phi@blist_X % }
3691 \else\expandafter\phi@blist_N\fi #1]%
3692 \catcode`[=12 \catcode`]=12 \catcode`\{=1 % }
3693 \def\phi@blist_N #1;{%
3694 \expandafter\xint_gobble_i\expanded
3695 \expandafter\phiblist_ab \expanded{{\xintNum{#1}}}{0};%
3696 }%
3697 \def\phi@blist_X #1{%
3698 \expandafter\phi@blist_x\expandafter{\iffalse}\fi
3699 }%
3700 \def\phi@blist_x #1#2;{%
3701 \expandafter\xint_gobble_i\expanded
3702 \expandafter\phiblist_ab \expanded{{\xintNum{#1}}{\xintNum{#2}}};%
3703 }%
3704 \def\XINT_expr_func_phiexponents #1#2#3%
3705 {%
3706 \expandafter #1\expandafter #2\expandafter{%
3707 \romannumeral`&&@\XINT:NEhook:f:one:from:one
3708 {\romannumeral`&&@\Phi@BList#3}}%
3709 }%
3710 % \end{macrocode}
3711 % ATTENTION! we leave the modified catcodes in place! (the question mark
3712 % has regained its catcode other though).
3713 %
3714 % \catcode`\<=0 \catcode`\>=11 \catcode`\*=11 \catcode`\/=11
3715 % \let</core>\relax
3716 % \def<*interactive>{\catcode`\<=12 \catcode`\>=12 \catcode`\*=12 \catcode`\/=12 }
3717 %</core>
3718 %<*interactive>
3719 % \makeatletter\global\c@CodelineNo\z@\makeatother
3720 % \section{Interactive code}
3721 % \label{sec:code:interactive}
3722 % Extracts to |zeckendorf.tex|.
3723 % \begin{macrocode}
3724 \input zeckendorfcore.tex
3725 \let\xintfirstoftwo\xint_firstoftwo
3726 \let\xintsecondoftwo\xint_secondoftw
3727 \let\zeckexprmapwithin\XINT:expr:mapwithin
3728 \def\zeckNumbraced#1{{\xintNum{#1}}}
3729 \xintexprSafeCatcodes
3730
3731 \let\ZeckShouldISayOrShouldIGo\iftrue
3732 \def\ZeckCmdQ{\let\ZeckShouldISayOrShouldIGo\iffalse}
3733 \let\ZeckCmdX\ZeckCmdQ
3734 \let\ZeckCmdx\ZeckCmdQ
3735 \let\ZeckCmdq\ZeckCmdQ
3736
3737 \newif\ifzeckphimode
3738 \newif\ifzeckindices
3739 \zeckindicestrue
3740 \newif\ifzeckfromN
3741 \zeckfromNtrue
3742 \newif\ifzeckmeasuretimes
3743 \newif\ifzeckevalonly
3744 \newif\ifzeckhex
3745
3746 \def\ZeckCmdP{%
3747 \zeckphimodetrue
3748 \ifzeckindices\ZeckCmdL\else\Zeck@CmdB\fi
3749 }
3750 \let\ZeckCmdp\ZeckCmdP
3751 \def\ZeckCmdZ{%
3752 \zeckphimodefalse
3753 \ifzeckindices\ZeckCmdL\else\Zeck@CmdB\fi
3754 }
3755 \let\ZeckCmdz\ZeckCmdZ
3756
3757 \def\PhiTypesetXPrint #1#2{a=#1, b=#2}
3758 \def\ZeckCmdL{%
3759 \zeckindicestrue
3760 \ifzeckphimode
3761 \def\ZeckFromN{\PhiExponents}%
3762 \def\ZeckToN##1{\PhiTypesetX{\PhiXfromExponents{##1}}}%
3763 \else
3764 \def\ZeckFromN{\ZeckIndices}%
3765 \def\ZeckToN{\ZeckNFromIndices}%
3766 \fi
3767 }
3768 \let\ZeckCmdl\ZeckCmdL
3769
3770 \def\ZeckCmdB{%
3771 \zeckindicesfalse
3772 \zeckhexfalse
3773 \Zeck@CmdB
3774 }
3775 \def\Zeck@CmdB{%
3776 \ifzeckphimode
3777 \ifzeckhex
3778 \def\ZeckFromN{\PhiBaseHexPhi}%
3779 \def\ZeckToN##1{\PhiTypesetX{\PhiXfromBaseHexPhi{##1}}}%
3780 \else
3781 \def\ZeckFromN{\PhiBasePhi}%
3782 \def\ZeckToN##1{\PhiTypesetX{\PhiXfromBasePhi{##1}}}%
3783 \fi
3784 \else
3785 \ifzeckhex
3786 \def\ZeckFromN{\ZeckHexWord}%
3787 \def\ZeckToN{\ZeckNfromHexWord}%
3788 \else
3789 \def\ZeckFromN{\ZeckWord}%
3790 \def\ZeckToN{\ZeckNfromWord}%
3791 \fi
3792 \fi
3793 }
3794 \let\ZeckCmdW\ZeckCmdB
3795 \let\ZeckCmdb\ZeckCmdB
3796 \let\ZeckCmdw\ZeckCmdB
3797
3798 \def\ZeckCmdC{%
3799 \zeckindicesfalse
3800 \zeckhextrue
3801 \Zeck@CmdB
3802 }
3803 \let\ZeckCmdc\ZeckCmdC
3804
3805 \def\ZeckConvert{%
3806 \csname Zeck\ifzeckfromN From\else To\fi N\endcsname
3807 }
3808 \def\ZeckCmdT{\ifzeckfromN\zeckfromNfalse\else\zeckfromNtrue\fi}
3809 \let\ZeckCmdt\ZeckCmdT
3810
3811 \expandafter\def\csname ZeckCmd@\endcsname{%
3812 \ifdefined\xinttheseconds
3813 \ifzeckmeasuretimes\zeckmeasuretimesfalse
3814 \else \zeckmeasuretimestrue
3815 \fi
3816 \else
3817 \immediate\write128{Sorry, this requires xintexpr 1.4n or later.}%
3818 \fi
3819 }
3820
3821 \def\ZeckCmdE{\ifzeckevalonly\zeckevalonlyfalse\else\zeckevalonlytrue\fi}
3822 \let\ZeckCmde\ZeckCmdE
3823
3824 \def\ZeckCmdH{\immediate\write128{\ZeckHelpPanel}}
3825 \let\ZeckCmdh\ZeckCmdH
3826
3827 \ZeckCmdL
3828
3829 \def\ZeckCommands{Enter input or command
3830 (q, z, p, l, w, b, c, t, e, @, or h for help).}
3831 \def\ZeckPrompt{%
3832 \ifzeckevalonly
3833 <<<Eval-only (e to quit)>>>^^J%
3834 [IN] expression =
3835 \else
3836 \ifzeckfromN
3837 \ifzeckphimode
3838 \ifzeckindices <convert to Bergman phi-exponents>^^J%
3839 \else
3840 \ifzeckhex
3841 <convert to Bergman hexphi-representation>^^J%
3842 \else
3843 <convert to Bergman phi-representation>^^J%
3844 \fi
3845 \fi
3846 \ZeckCommands^^J
3847 [IN] a + b phi =
3848 \else
3849 \ifzeckindices <convert to Zeckendorf indices>^^J%
3850 \else
3851 \ifzeckhex
3852 <convert to Zeckendorf hex-word>^^J%
3853 \else
3854 <convert to Zeckendorf word>^^J%
3855 \fi
3856 \fi
3857 \ZeckCommands^^J
3858 [IN] N =
3859 \fi
3860 \else
3861 \ifzeckphimode <convert to a + b phi>^^J
3862 \ZeckCommands^^J
3863 [IN]
3864 \ifzeckindices phi exponents =
3865 \else
3866 \ifzeckhex
3867 hexphi-representation =
3868 \else
3869 phi-representation =
3870 \fi
3871 \fi
3872 \else <convert to integer>^^J
3873 \ZeckCommands^^J
3874 [IN]
3875 \ifzeckindices indices =
3876 \else
3877 \ifzeckhex
3878 hex word =
3879 \else
3880 binary word =
3881 \fi
3882 \fi
3883 \fi
3884 \fi
3885 \fi
3886 }
3887 \newlinechar10
3888 \immediate\write128{}
3889 \immediate\write128{Welcome to Zeckendorf 0.9d (2025/11/16, JFB).}
3890
3891 \def\ZeckHelpPanel{Commands (lowercase also):^^J
3892 Q to quit. Also X.^^J
3893 H for this help.^^J
3894 Z to switch to Zeckendorf-mode (starting default).^^J
3895 P to switch to phi-mode.^^J
3896 L for indices or exponents.^^J
3897 W for binary words or reps. Also B.^^J
3898 C for hexadecimal words or reps.^^J
3899 T to toggle the direction of conversions.^^J
3900 E to toggle to and from \string\xinteval-only mode.^^J
3901 @ to toggle measurement of execution times.^^J
3902 ^^J
3903 - binary words, phi-representations, are parsed only by \string\edef.^^J
3904 - all other inputs are handled by \noexpand\xinteval so for example one^^J
3905 \space\space
3906 can use 2^100 or 100! or binomial(100,50). And a list of indices^^J
3907 \space\space
3908 or exponents can be for example seq(3*a+1, a=0..10).^^J
3909 ^^J
3910 \space\space The fib() function computes Fibonacci numbers.^^J
3911 \space\space The character $ serves as symbol for Knuth multiplication.^^J%
3912 **** empty input is not supported!
3913 no linebreaks in input! ****}
3914
3915 \immediate\write128{\ZeckHelpPanel}
3916
3917 \def\zeckpar{\par}
3918 \long\def\xintbye#1\xintbye{}
3919 \long\def\zeckgobbleii#1#2{}
3920 \long\def\zeckfirstoftwo#1#2{#1}
3921 \def\zeckonlyonehelper #1#2#3%
3922 \zeckonlyonehelper{\xintbye#2\zeckgobbleii\xintbye0}
3923
3924 \xintFor*#1 in {0123456789}\do{%
3925 \expandafter\def\csname ZeckCmd#1\endcsname{%
3926 \immediate\write128{%
3927 ** Due to under-funding, a lone #1 is not accepted. Inputs must have^^J%
3928 ** two characters at least. Think about a donation? Try 0#1.}}
3929 }%
3930 \xintloop
3931 \message{\ZeckPrompt}
3932 \read-1to\zeckbuf
3933 \ifx\zeckbuf\zeckpar
3934 \immediate\write128{**** empty input is not supported, please try again.}
3935 \else
3936 \edef\zeckbuf{\zeckbuf}
3937 % \end{macrocode}
3938 % Space token at end of |\zeckbuf| is annoying. We could have used
3939 % |\xintLength| which does not count space tokens.
3940 % \begin{macrocode}
3941 \if 1\expandafter\zeckonlyonehelper\zeckbuf\xintbye\zeckonlyonehelper1%
3942 \ifcsname ZeckCmd\expandafter\zeckfirstoftwo\zeckbuf\relax\endcsname
3943 \csname ZeckCmd\expandafter\zeckfirstoftwo\zeckbuf\relax\endcsname
3944 \else
3945 \immediate\write128{%
3946 **** Unrecognized command letter
3947 \expandafter\zeckfirstoftwo\zeckbuf\relax. Try again.^^J}
3948 \fi
3949 \else
3950 % \end{macrocode}
3951 % Using the conditional so that this can also be used by default
3952 % with older xint.
3953 %
3954 % With |0.9b| the time needed for parsing the input was not counted,
3955 % but this meant that measuring in the evaluation-only mode always
3956 % printed |0.0s|.
3957 %
3958 % |0.9c| has refactored here entirely.
3959 % \begin{macrocode}
3960 \ifzeckmeasuretimes\xintresettimer\fi
3961 \if1\ifzeckevalonly0\fi\ifzeckfromN0\fi\ifzeckindices0\fi1%
3962 \edef\ZeckIn{{\zeckbuf}}%
3963 \else
3964 \expandafter\def\expandafter\ZeckIn\expandafter{%
3965 \romannumeral0\xintbareeval\zeckbuf\relax}%
3966 % \end{macrocode}
3967 % |0.9c| uses |\xinteval|. It adds phi-mode to the interactive interface, but
3968 % as |1/phi| or anything doing an operation will inject ``raw xintfrac
3969 % format'', we have to be careful about that, because we use |\PhiExponents|
3970 % and |\PhiBasePhi| which are assuming being used with either an integer |a|
3971 % or a pair |{a}{b}|. Using here some core level auxiliary from \xintexprname
3972 % to avoid a dozen lines like what was done for |\Phi@BList|. For this to
3973 % work we need a variant of |\xintNum| which outputs with extra braces. This
3974 % was for the author a refreshing journey to revisit forgotten deep code
3975 % written years ago for \xintexprname. But it would be more efficient to do
3976 % something akin to the |\Phi@BList| business.
3977 %
3978 % By the way we have to do this not only for phi-mode, but also for
3979 % integer-mode, because some input such as |1e40| will be internally |1[40]|
3980 % which |\ZeckIndices| does not understand as it does not apply |\xintNum|. In
3981 % fact any input doing an operation such as an addition will be in ``raw
3982 % xintfrac format'' internally. So we have to do a normalization also for
3983 % lists of exponents or indices.
3984 % \begin{macrocode}
3985 \ifzeckevalonly\else
3986 \expandafter\def\expandafter\ZeckIn
3987 \expanded
3988 \expandafter\zeckexprmapwithin
3989 \expandafter\zeckNumbraced\ZeckIn
3990 % \end{macrocode}
3991 % For lists of exponents and indices the predefined macros expect
3992 % comma separated lists. We can either "print" using (full) |\xinteval|,
3993 % or use |\xintListwithSep|, or write a little helper requiring
3994 % only |\edef| expansion. We add one level of bracing removed later.
3995 % \begin{macrocode}
3996 \ifzeckfromN\else
3997 \expandafter\def\expandafter\ZeckIn\expandafter{%
3998 \expandafter{\romannumeral0\xintlistwithsep,\ZeckIn}%
3999 }%
4000 \fi
4001 \fi
4002 \fi
4003 \immediate\write128{%
4004 [OUT] \ifzeckevalonly
4005 \expanded\expandafter\XINTexprprint\expandafter.\ZeckIn
4006 \else
4007 \expandafter\ZeckConvert\ZeckIn
4008 \fi
4009 }%
4010 \ifzeckmeasuretimes
4011 \edef\tmp{\xinttheseconds}%
4012 \immediate\write128{%
4013 \ifzeckevalonly Evaluation \else Conversion \fi
4014 took \tmp s%
4015 }%
4016 \fi
4017 \fi
4018 \fi
4019 \ZeckShouldISayOrShouldIGo
4020 \repeat
4021
4022 \immediate\write128{Bye. Session was saved to log file (hard-wrapped too, alas).}
4023 \bye
4024 % \end{macrocode}
4025 % \catcode`\<=0 \catcode`\>=11 \catcode`\*=11 \catcode`\/=11
4026 % \let</interactive>\relax
4027 % \def<*sty>{\catcode`\<=12 \catcode`\>=12 \catcode`\*=12 \catcode`\/=12 }
4028 %</interactive>
4029 %<*sty>
4030 % \makeatletter\global\c@CodelineNo\z@\makeatother
4031 % \section{\LaTeX{} code}
4032 % Extracts to |zeckendorf.sty|.
4033 % \label{sec:code:latex}
4034 % \begin{macrocode}
4035 \NeedsTeXFormat{LaTeX2e}
4036 \ProvidesPackage{zeckendorf}
4037 [2025/11/16 v0.9d Zeckendorf and base-phi representations of big integers (JFB)]%
4038 \RequirePackage{xintexpr}
4039 \RequirePackage{xintbinhex}% superfluous if with xint 1.4n or later
4040 \input zeckendorfcore.tex
4041 \ZECKrestorecatcodesendinput%
4042 % \end{macrocode}
4043 % \catcode`\<=0 \catcode`\>=11 \catcode`\*=11 \catcode`\/=11
4044 % \let</sty>\relax
4045 % \def<*dtx>{\catcode`\<=12 \catcode`\>=12 \catcode`\*=12 \catcode`\/=12 }
4046 %</sty>
4047 %<*dtx>
4048 % \MakePercentComment
4049 \CheckSum {3064}%
4050 \makeatletter\check@checksum\makeatother%
4051 \Finale%
4052 %% End of file zeckendorf.dtx