% \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