Banded matrix with random entries

What is the optimal way to get banded matrices with random entries in LaTeX? By random, I mean the following: It should give me different entries after every compilation.

-

2 Answers

You could use any random number generator function. Here is an example of the one using the one built in pgf:

Code:

\documentclass{article}
\usepackage{amsmath}
\usepackage{pgf}

\newcommand{\Rand}{\pgfmathparse{random(100)}\pgfmathresult}%

\newcommand{\NewMatrix}{%
\begin{bmatrix}
\Rand & 0     & 0       \\
0     & \Rand &  0      \\
0     & 0     & \Rand 0 \\
\end{bmatrix}
}%

\begin{document}
$\NewMatrix$, $\NewMatrix$, $\NewMatrix$
\end{document}

-
Thanks Peter. Essentially I was looking for a good random number generator. – user1876 Jul 8 '12 at 5:21
@Marvis: OK, If I get done with the flexible random banded matrix generator, I'll post it separately. – Peter Grill Jul 8 '12 at 5:24
I managed to write a banded matrix generator using your code. The only thing is every time I compile, I get the same set of matrices. Is it possible to get a different one each time? – user1876 Jul 8 '12 at 5:34
@Marvis: I think you just need to set the seed to be the current second -- you should get different results each minute (I think) by default. – Peter Grill Jul 8 '12 at 5:48
Yes. Thanks. Every minute it gives a different number now. – user1876 Jul 8 '12 at 6:02

Taking the cue from Peter, here is the code I wrote to generate a random matrix and a random banded matrix.

$$\NewMatrix{m}{n}$$ produces a matrix of size m by n.

$$\BandMatrix{m}{n}{b}$$produces a matrix of size m by n with bandwidth b.

Code:

\documentclass{article}
\usepackage{amsmath}
\usepackage{forloop}
\usepackage{pgf}
\usepackage{ifthen}

\newcommand{\Rand}{\pgfmathparse{random(10)}\pgfmathresult}%

\newcounter{row_number}
\newcounter{col_number}
\newcounter{band}

\newcommand{\NewMatrix}[2]{%
\begin{bmatrix}
\forloop{row_number}{1}{\value{row_number} < #1}{%%
\forloop{col_number}{1}{\value{col_number} < #2}{%%%
\Rand &
}%%%
\Rand
\\
}%%
\forloop{col_number}{1}{\value{col_number} < #2}{%%%%
\Rand &
}%%%%
\Rand
\end{bmatrix}
}%

\newcommand{\BandMatrix}[3]{%
\begin{bmatrix}
\forloop{row_number}{1}{\value{row_number} < #1}{%%
\forloop{col_number}{1}{\value{col_number} < #2}{%%%
\ifthenelse{\value{col_number} < \numexpr\value{row_number} + #3 + 1 \and \value{col_number} > \numexpr\value{row_number} - #3 - 1}{\Rand &}{0 &}
}%%%
\ifthenelse{\value{col_number} < \numexpr\value{row_number} + #3 + 1 \and \value{col_number} > \numexpr\value{row_number} - #3 - 1}{\Rand \\}{0 \\}
}%%
\forloop{col_number}{1}{\value{col_number} < #2}{%%%%
\ifthenelse{\value{col_number} < \numexpr\value{row_number} + #3 + 1 \and \value{col_number} > \numexpr\value{row_number} - #3 - 1}{\Rand &}{0 &}
}%%%%
\ifthenelse{\value{col_number} < \numexpr\value{row_number} + #3 + 1 \and \value{col_number} > \numexpr\value{row_number} - #3 - 1}{\Rand}{0}
\end{bmatrix}
}%

\begin{document}
$$\NewMatrix{11}{4}$$
$$\BandMatrix{10}{8}{3}$$
\end{document}

-
Use $...$ for display math instead of $$. See: Why is $…$ preferable to$$?. – Peter Grill Jul 8 '12 at 6:48
Also, wonder if your condition of where the band is is off by 1. You specify 8 and 3 in \BandMatrix, but the first 4 rows are non-zero, and the first 6 columns are zero. I haven't dealt with banded-matrices before so perhaps I am misunderstood the specification. – Peter Grill Jul 8 '12 at 6:53