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