Debian Codegraph @

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

raw
1 % \iffalse meta-comment
2 %
3 % Copyright (C) 2007 by David Roderick % -----------------------------------
4 %
5 % This file may be distributed and/or modified under the
6 % conditions of the LaTeX Project Public License, either version 1.2
7 % of this license or (at your option) any later version.
8 % The latest version of this license is in:
9 %
10 % http://www.latex-project.org/lppl.txt
11 %
12 % and version 1.2 or later is part of all distributions of LaTeX
13 % version 1999/12/01 or later.
14 %
15 % \fi
16 %
17 % \iffalse
18 %<package>\NeedsTeXFormat{LaTeX2e}[2005/12/01]
19 %<package>\ProvidesPackage{binomexp}
20 %<package> [2007/01/07 v1.0 My first attempt]
21 %
22 %<*driver>
23 \documentclass{ltxdoc}
24 \usepackage{binomexp}
25 \EnableCrossrefs
26 \CodelineIndex
27 \RecordChanges
28 \begin{document}
29 \DocInput{binomexp.dtx}
30 \end{document}
31 %</driver>
32 % \fi
33 %
34 % \CheckSum{310}
35 %% \CharacterTable
36 %% {Upper-case \A\B\C\D\E\F\G\H\I\J\K\L\M\N\O\P\Q\R\S\T\U\V\W\X\Y\Z
37 %% Lower-case \a\b\c\d\e\f\g\h\i\j\k\l\m\n\o\p\q\r\s\t\u\v\w\x\y\z
38 %% Digits \0\1\2\3\4\5\6\7\8\9
39 %% Exclamation \! Double quote \" Hash (number) \#
40 %% Dollar \$ Percent \% Ampersand \&
41 %% Acute accent \' Left paren \( Right paren \)
42 %% Asterisk \* Plus \+ Comma \,
43 %% Minus \- Point \. Solidus \/
44 %% Colon \: Semicolon \; Less than \<
45 %% Equals \= Greater than \> Question mark \?
46 %% Commercial at \@ Left bracket \[ Backslash \\
47 %% Right bracket \] Circumflex \^ Underscore \_
48 %% Grave accent \` Left brace \{ Vertical bar \|
49 %% Right brace \} Tilde \~}
50 %%
51 % \changes{v1.0}{2007/01/07}{Initial version}
52 %
53 % \GetFileInfo{binomexp.sty}
54 %
55 % \DoNotIndex{\the,\#}
56 %
57 % \title{The \textsf{binomexp} package\thanks{This document
58 % corresponds to \textsf{binomexp}~\fileversion, dated \filedate.}}
59 % \author{David Roderick \\ \texttt{angel\_ov\_north at tiscali dot co dot uk}}
60 %
61 % \maketitle
62 %
63 % \begin{abstract}
64 % Calculates and prints successive lines of Pascal's triangle..
65 % \makeatletter
66 % \binomexp@putpascal{4}{5}{f}{s}{f}{s} \par
67 % and also will typset the following proof
68 % \binomexp@proof{n}{r}
69 % \end{abstract}
70 %
71 % \section{Introduction}
72 %
73 % A very simple package with simple usage. Putting `binomexp' (which
74 % is also typed exactly the same way than \marg{binomexp} inside
75 % of the argumentative input of the the |\usepackage| commands
76 % enables the user to do two extra things.
77 % \begin{itemize} \item
78 % print any successive rows of Pascal's triangle which will fit on
79 % the page up until the power as 31, at which point \LaTeX{} runs
80 % out of brain power. \\
81 % \item Use a piece of code which Morten H\o gholm wrote which allows
82 % the cells inside of an array or a tabular to be repeated in a
83 % similar way than those may be repeated inside of the initial
84 % description of said array or tabular.
85 % \end{itemize}
86 %
87 % \section{Usage}
88 % Binomexp ought to load ifthen and calc by itself. If you have already loaded
89 % these packages using |\usepackage{calc,ifthen}| unload these therefore.
90 % You must then use the command as |\makeatletter| so to get the command names with the symbol as @ inside of those to function.
91 %
92 % \DescribeMacro{\binomexp@putpascal}
93 % |\binomexp@putpascal| \marg{number as lower power}
94 % \marg{number as higher power} \marg{symbol as first variable}
95 % \marg{symbol as second variable} \marg{symbol again as first variable}
96 % \marg{symbol again as second variable}
97 % \par|\binomexp@putpascal{7}{9}{f}{x}{f}{x}| will typset the rows as 7, 8, and 9 of
98 % Pascal's triangle. The first column will have $(f+x)^{power}$.
99 % The reason why you have to input the symbol again is because the
100 % user might like to use a |\cdot| or whatever in the other columns
101 % except the first column.
102 % And that's it really.
103 % \DescribeMacro{\binomexp@proof}
104 % |\binomexp@proof| \marg{number as row variable}
105 % \marg{number as column variable}
106 % will typeset the mathematical proof of Pascal's triangle, which is based upon % the observation that the co-efficient is equal with the number of possible
107 % combinations of the column variable out of the row variable.
108 % \StopEventually{\PrintIndex}
109 %
110 % \section{How I wrote it.}
111 %
112 % \begin{macrocode}
113 \RequirePackage{calc,ifthen}
114 % \end{macrocode}
115 % Morten H\o gholm wrote the following code.
116 % \begin{macrocode}
117 \newcommand\binomexp@replicate[2]{%
118 \ifnum#1>\z@ \expandafter\@firstofone
119 \else
120 \expandafter\@gobble
121 \fi
122 {#2\expandafter\binomexp@replicate\expandafter{\number\numexpr#1-1\relax}{#2}}%
123 }
124 % \end{macrocode}
125 % Morten's code allows the following.
126 % \begin{verbatim}
127 % \begin{document}
128 % \makeatletter
129 % \begin{tabular}{|*{6}{|c|}|}
130 % something1 \binomexp@replicate{4}{& something2}Blah&stuff\\
131 % something1 \binomexp@replicate{4}{& something2}Blah&stuff\\
132 % Third row with line atop from second to fifth column:
133 % \cline{2-5}something1 \binomexp@replicate{4}{& something2}Blah&stuff\\
134 % \end{tabular}
135 % \end{document}
136 % \end{verbatim}
137 % You can invoke Morten's code either by loading the |\usepackage{binomexp}|
138 % within the preamble, and then by putting |\makeatletter|, or by including
139 % the following code somewhere (perhaps a preamble).
140 % \begin{verbatim}
141 % \makeatletter
142 % \newcommand\binomexp@replicate[2]{%
143 % \ifnum#1>\z@ \expandafter\@firstofone
144 % \else
145 % \expandafter\@gobble
146 % \fi
147 % {#2\expandafter\binomexp@replicate\expandafter{\number\numexpr#1-1\relax}{#2}}%
148 % }
149 % \makeatother
150 % \end{verbatim}
151 % \begin{macro}{\binomexp@call}
152 % the |\newcommand| as |\binomexp@call| makes things nice and pretty within a cell
153 % \begin{macrocode}
154 \newcommand{\binomexp@call}[1]{\rule[-0.125cm]{0mm}{0.5cm}\mbox{$#1$}}
155 % \end{macrocode}
156 % \end{macro}
157 %
158 % \begin{macro}{\binomexp@up}
159 % the |\newcommand| as |\binomexp@up| is by the power of the series which ascends
160 % \begin{macrocode}
161 \newcounter{binomexp@up}
162 \newcommand{\binomexp@up}{\number\value{binomexp@up}
163 \addtocounter{binomexp@up}{1}}
164 % \end{macrocode}
165 % \end{macro}
166 %
167 % \begin{macro}{\binomexp@down}
168 % the |\newcommand| as |\binomexp@down| is by the power of the series which descends
169 % \begin{macrocode}
170 \newcounter{binomexp@down}
171 \newcommand{\binomexp@down}{\number\value{binomexp@down}
172 \addtocounter{binomexp@down}{-1}}
173 % \end{macrocode}
174 % \end{macro}
175 %
176 % \begin{macro}{\binomexp@columns}
177 % an array of so many columns
178 % \begin{macrocode}
179 \newcounter{binomexp@columns}
180 % \end{macrocode}
181 % \end{macro}
182 %
183 %
184 % \begin{macro}{\binomexp@power}
185 % $(f+s)^{power}$
186 % \begin{macrocode}
187 \newcounter{binomexp@power}
188 % \end{macrocode}
189 % \end{macro}
190 %
191 % \begin{macro}{\binomexp@pascalstart}
192 % \begin{macro}{\binomexp@pascalstop}
193 % \begin{macro}{\binomexp@emptytimes}
194 % the next 3 counters are used within the |\binomexp@putpascal| command
195 % \begin{macrocode}
196 \newcounter{binomexp@pascalstart}
197 \newcounter{binomexp@pascalstop}
198 \newcounter{binomexp@emptytimes}
199 % \end{macrocode}
200 % \end{macro}
201 % \end{macro}
202 % \end{macro}
203 %
204 % \begin{macro}{\binomexp@variable1}
205 % \begin{macro}{\binomexp@variable2}
206 % \begin{macro}{\binomexp@answervar}
207 % the following 3 counters are used within the process of calculation as
208 % |\binomexp@printpascal|
209 % \begin{macrocode}
210 \newcounter{binomexp@variable1}
211 \newcounter{binomexp@variable2}
212 \newcounter{binomexp@answervar}
213 % \end{macrocode}
214 % \end{macro}
215 % \end{macro}
216 % \end{macro}
217 %
218 % \begin{macro}{\binomexp@sub}
219 % \begin{macrocode}
220 \newcounter{binomexp@sub}
221 % \end{macrocode}
222 % \end{macro}
223 %
224 % \begin{macro}{\binomexp@printpascal}
225 % to calculate the coefficients of the Pascal's triangle
226 % \begin{macrocode}
227 \protect\newcommand*{\binomexp@printpascal}{
228 \addtocounter{binomexp@power}{1}
229 \expandafter\edef\csname
230 binomexp@morten\roman{binomexp@power}exporti\endcsname{1}
231 \setcounter{binomexp@sub}{2}
232 \setcounter{binomexp@variable1}{\numexpr\number\value{binomexp@power}+1\relax}
233 \whiledo{\number\numexpr\number\value{binomexp@power}+1\relax>
234 \value{binomexp@sub}}{
235 \setcounter{binomexp@variable1}{\numexpr\number\value{binomexp@sub}-1\relax}
236 \setcounter{binomexp@variable2}{\value{binomexp@sub}}
237 \setcounter{binomexp@answervar}{\number\numexpr\csname
238 binomexp@x\roman{binomexp@variable1}\endcsname\relax+\number\numexpr\csname
239 binomexp@x\roman{binomexp@variable2}\endcsname\relax}
240 \expandafter\edef\csname binomexp@y\roman{binomexp@sub}\endcsname
241 {\number\value{binomexp@answervar}}\relax
242 \addtocounter{binomexp@sub}{1}
243 }
244 % \end{macrocode}
245 % TRANSFER PART
246 % set counter as binomexp@sub to 1
247 % \begin{macrocode}
248 \setcounter{binomexp@sub}{2}
249 % \end{macrocode}
250 % create a loop which shall get the binomexp@y values and put those into the
251 % appropriate binomexp@x values.
252 % Also export the y values by this same corresponding power into a length called
253 % binomexp@morten|\roman{power}|export|\roman{binomexp@sub}|
254 % \begin{macrocode}
255 \whiledo{\numexpr\number\value{binomexp@power}+1\relax>\value{binomexp@sub}}{
256 \setcounter{binomexp@answervar}{\number\numexpr\csname
257 binomexp@y\roman{binomexp@sub}\endcsname\relax}
258 \expandafter\edef\csname binomexp@x\roman{binomexp@sub}\endcsname
259 {\number\value{binomexp@answervar}}
260 % \end{macrocode}
261 % Here is how I exported the values to the table.
262 % \begin{macrocode}
263 \expandafter\edef\csname
264 binomexp@morten\roman{binomexp@power}export\roman{binomexp@sub}\endcsname
265 {\number\value{binomexp@answervar}}
266 % \end{macrocode}
267 % \begin{macrocode}
268 \addtocounter{binomexp@sub}{1}
269 }
270 \setcounter{binomexp@variable1}
271 {\numexpr\number\value{binomexp@power}+1\relax}
272 \expandafter\edef\csname
273 binomexp@x\roman{binomexp@variable1}\endcsname{1}
274 \expandafter\edef\csname
275 binomexp@morten\roman{binomexp@power}export\roman{binomexp@variable1}\endcsname{1}
276 % \end{macrocode}
277 % To see what is happening add the following lines at this place.
278 % \begin{verbatim}
279 % power is \number\value{binomexp@power}\par
280 % \setcounter{binomexp@variable2}{1}
281 % \whiledo{\value{binomexp@variable2}<
282 % \numexpr\number\value{binomexp@power}+2\relax}{
283 % binomexp@morten\roman{binomexp@power}export\roman{binomexp@variable2} is
284 % \csname binomexp@morten\roman{binomexp@power}export\roman{binomexp@%
285 % variable2}\endcsname\relax\par\addtocounter{binomexp@variable2}{1}}
286 % \end{verbatim}
287 % \begin{macrocode}
288 }
289 % \end{macrocode}
290 % \end{macro}
291 %
292 % \begin{macro}{\binomexp@putpascal}
293 % set binomexp@xi as 1 \par
294 % binomexp@xi never alters
295 % \begin{macrocode}
296 \newcommand*\binomexp@putpascal[6]{\par
297 \expandafter\edef\csname binomexp@xi\endcsname{1}
298 % \end{macrocode}
299 % set an eventuality for binomexp@xi by the power as zero
300 % \begin{macrocode}
301 \expandafter\edef\csname binomexp@mortenexporti\endcsname{1}
302 % \end{macrocode}
303 % we'll need to start power as zero by the way |\binomexp@printpascal|
304 % is transfigured.
305 % \begin{macrocode}
306 \setcounter{binomexp@power}{0}
307 % \end{macrocode}
308 % wrap the chipolatas in stringy bacon.
309 % \begin{macrocode}
310 \setcounter{binomexp@pascalstart}{#1}
311 \setcounter{binomexp@pascalstop}{#2+1}
312 % \end{macrocode}
313 % now calculate all the co-efficients.
314 % \begin{macrocode}
315 \setcounter{binomexp@emptytimes}{\value{binomexp@pascalstop}}
316 \whiledo{\value{binomexp@emptytimes}>1}{
317 \binomexp@printpascal \addtocounter{binomexp@emptytimes}{-1}
318 }
319 % \end{macrocode}
320 % work out the number of columns
321 % \begin{macrocode}
322 \setcounter{binomexp@columns}
323 {\numexpr\number\value{binomexp@pascalstop}+2\relax}
324 % \end{macrocode}
325 % now the table
326 % \begin{macrocode}
327 \begin{math} \begin{array}{@{}|c|*{\value{binomexp@columns}}{|c}|@{}}
328 % \end{macrocode}
329 % repeat the number of rows so many times
330 % \begin{macrocode}
331 \whiledo{\value{binomexp@pascalstart}<
332 \numexpr\number\value{binomexp@pascalstop}-1\relax}{
333 % \end{macrocode}
334 % prime the binomexp@up gun and cock.
335 % \begin{macrocode}
336 \setcounter{binomexp@up}{0}
337 % \end{macrocode}
338 % prime the binomexp@down gun and cock.
339 % \begin{macrocode}
340 \setcounter{binomexp@down}{\value{binomexp@pascalstart}}
341 \binomexp@call{(#3+#4)^{\number\numexpr\number\value{binomexp@pascalstart}\relax}}
342 \binomexp@replicate{\numexpr\number\value{binomexp@pascalstart}+1\relax}
343 {&\binomexp@call{\csname
344 binomexp@morten\romannumeral\numexpr\value{binomexp@pascalstart}\relax
345 export\romannumeral\numexpr\value{binomexp@up}+1\relax\endcsname
346 #5^{\binomexp@down} #6^{\binomexp@up}}}\\
347 \addtocounter{binomexp@pascalstart}{1}
348 }
349 % \end{macrocode}
350 % add one more row for luck
351 % \begin{macrocode}
352 \setcounter{binomexp@up}{0}
353 \setcounter{binomexp@down}{\value{binomexp@pascalstart}}
354 \binomexp@call{(#3+#4)^{\number\numexpr\number\value{binomexp@pascalstart}\relax}}
355 \binomexp@replicate{\numexpr\number\value{binomexp@pascalstart}+1\relax}
356 {&\binomexp@call{\csname
357 binomexp@morten\romannumeral\numexpr\value{binomexp@pascalstart}\relax
358 export\romannumeral\numexpr\value{binomexp@up}+1\relax\endcsname
359 #5^{\binomexp@down} #6^{\binomexp@up}}}
360 \end{array} \end{math}
361 }
362 % \end{macrocode}
363 % \end{macro}
364 %
365 %
366 % \begin{macro}{\binomexp@proof}
367 % This command prints a mathematical proof of the
368 % Pascals's triangle based upon obervation.
369 % \begin{macrocode}
370 \newcommand{\binomexp@proof}[2]{
371 \[ 7!=7\cdot6\cdot5\cdot4\cdot3\cdot2\cdot1 \hspace*{5em}
372 {#1 \choose #2} = \frac{#1!}{(#1-#2)!\cdot #2!}=
373 \frac{#1!}{(#1-#2)!\cdot(#1-(#1-#2))!}={#1 \choose #1-#2}
374 \]
375 \begin{eqnarray*} {#1 - 1 \choose #2 - 1} + {#1 - 1 \choose #2}
376 &=& \frac{(#1 - 1)!}{(#2 - 1)!\cdot[(#1 - 1) - (#2 - 1)]!} +
377 \frac{(#1 - 1)!}{#2!\cdot[(#1 - 1) - #2)]!}\\
378 &=& (#1 - 1)!\cdot\left(\frac{1}{(#2 - 1)!\cdot(#1 - #2)!} +
379 \frac{1}{#2!\cdot[(#1 - #2) - 1)]!}\right) \\
380 &=& (#1 - 1)! \cdot\frac{#2 + (#1 - #2)}{#2! (#1 - #2)!} \\
381 &=& \frac{#1!}{#2!(#1 - #2)!} = {#1 \choose #2}
382 \end{eqnarray*}
383 \[ \frac{#2}{#2!\cdot(#1-#2)!} = \frac{1}{(#2-1)!\cdot(#1-#2)!}
384 \hspace*{5em} \mbox{because} \hspace*{5em} \\
385 \frac{6}{6!\cdot(#1-#2)!} = \frac{1}{5!\cdot(#1-#2)!} \]
386 \begin{eqnarray*}
387 (#2 + 1)\cdot {#1 + 1 \choose #2 + 1} &=& (#2 + 1)\cdot
388 \frac{(#1 + 1)!}{((#2 + 1)!\cdot ((#1 + 1) - (#2 + 1))!}\\
389 &=& (#2 + 1)\cdot \frac{(#1 + 1)!}{(#2 + 1)!\cdot (#1 - #2)!}\\
390 &=& (#1 + 1)\cdot \frac{#1!}{#2!\cdot (#1 - #2)!} = (#1 + 1)\cdot
391 {#1 \choose #2}\\
392 \end{eqnarray*}
393 }
394 % \end{macrocode}
395 % \end{macro}
396 %
397 % \Finale
398 % \PrintIndex
399 \endinput