5

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.

Werner
  • 603,163

2 Answers2

4

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

enter image description here

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}

Peter Grill
  • 223,288
  • Thanks Peter. Essentially I was looking for a good random number generator. –  Jul 08 '12 at 05:21
  • @Marvis: OK, If I get done with the flexible random banded matrix generator, I'll post it separately. – Peter Grill Jul 08 '12 at 05: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? –  Jul 08 '12 at 05: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 08 '12 at 05:48
  • Yes. Thanks. Every minute it gives a different number now. –  Jul 08 '12 at 06:02
3

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}
David Carlisle
  • 757,742