# Writing a recursive matrix pattern

Consider the following matrix

\begin{equation}
\begin{pmatrix}
B_{0,0} & B_{0,1} \\
B_{1,0} & B_{1,1}
\end{pmatrix}
\end{equation}


The next matrix in the iteration is going to replace B_{i,j} with the following matrix

\begin{equation}
\begin{pmatrix}
B_{i0,j0} & B_{i0,j1} \\
B_{i1,j0} & B_{i1,j1}
\end{pmatrix}
\end{equation}


for i and j = 0 , 1, to get a 4 x 4 matrix. Is there an easy way (with for loops) to write this down? We can continue with this construction (see my comments below) to get a 8 x 8 matrix and so on.

• what should the result should look like ? – percusse Apr 27 '16 at 14:44
• – Steven B. Segletes Apr 27 '16 at 14:45
• @StevenB.Segletes, yes, it is related, but I am hoping for something simpler since there is no image involved. Somehow this should be more basic since it only involves latex symbols. – passerby51 Apr 27 '16 at 14:49
• @percusse, it should be a 16x16 matrix with entries that look like B_{ir,js} as i,j,r,s range over 0 and 1. – passerby51 Apr 27 '16 at 14:49
• Where is the recursion then? – percusse Apr 27 '16 at 15:05

Not really recursive. But, hey, it works!

\documentclass{article}
\usepackage{amsmath}
\usepackage{xparse}

\ExplSyntaxOn

\NewDocumentCommand{\recursematrix}{O{B}m}
{% #1 is the (optional) symbol for the coefficient, #2 is the step
\begin{pmatrix}
\passerby_recursematrix:nn { #1 } { #2 }
\end{pmatrix}
}

\tl_new:N \l_passerby_recursebody_tl

\cs_new_protected:Nn \passerby_recursematrix:nn
{
\int_step_inline:nnnn { 0 } { 1 } { \fp_to_int:n { 2**#2 } - 1}
{
\int_step_inline:nnnn { 0 } { 1 } { \fp_to_int:n { 2**#2 } -1 }
{
\tl_put_right:Nx \l_passerby_recursebody_tl
{
\exp_not:n { #1 }
\sb
{
\passerby_padded_binary:nn { #2 } { ##1 },
\passerby_padded_binary:nn { #2 } { ####1 }
}
&
}
}
\tl_put_right:Nn \l_passerby_recursebody_tl { \hspace{-2\arraycolsep} \\ }
}
\tl_use:N \l_passerby_recursebody_tl
}

{
\int_compare:nTF { #2 = 0 }
{
\prg_replicate:nn { #1 } { 0 }
}
{
\prg_replicate:nn { #1 - 1 - \fp_to_int:n { floor( ln(#2)/ln(2) ) } } { 0 }
\int_to_bin:n { #2 }
}
}

\ExplSyntaxOff

\begin{document}

$\recursematrix{1}$
$\recursematrix{2}$
$\recursematrix{3}$

\end{document} This is the output of \recursematrix[X]{4} to illustrate the optional argument. It requires also

\setcounter{MaxMatrixCols}{17} I think the first two matrices are what the OP asked for.

I believe I have got the 3rd level, as well. The key is in realizing that the indices are basically in binary (base 2) notation.

EDIT, To keep up with egreg, I also did 4th level.

\documentclass[landscape]{article}
\usepackage[margin=1cm]{geometry}
\usepackage{amsmath}
\newcommand\Amatrix{%
\begin{matrix}
B_{{#1}0,{#2}0} & B_{{#1}0,{#2}1} \\ B_{{#1}1,{#2}0} & B_{{#1}1,{#2}1}
\end{matrix}
}
\makeatletter
\newcommand\makeAnmatrix{%
\expandafter\newcommand\csname #1matrix\endcsname{%
\begin{matrix}
\csname\@gobble#1matrix\endcsname{{##1}0}{{##2}0} &
\csname\@gobble#1matrix\endcsname{{##1}0}{{##2}1}\\
\csname\@gobble#1matrix\endcsname{{##1}1}{{##2}0} &
\csname\@gobble#1matrix\endcsname{{##1}1}{{##2}1}
\end{matrix}
}%
}
\makeatother
\makeAnmatrix{AA} % ALLOWS TO DEPTH 2
\makeAnmatrix{AAA} % ALLOWS TO DEPTH 3
\makeAnmatrix{AAAA} % ALLOWS TO DEPTH 4
\begin{document}
\begin{equation*}\begin{pmatrix}\Amatrix{}{}\end{pmatrix}\end{equation*}
\begin{equation*}\begin{pmatrix}\AAmatrix{}{}\end{pmatrix}\end{equation*}
\begin{equation*}\begin{pmatrix}\AAAmatrix{}{}\end{pmatrix}\end{equation*}
\tiny
\begin{equation*}\begin{pmatrix}\AAAAmatrix{}{}\end{pmatrix}\end{equation*}
\end{document}  