# Need Help Writing (a Pascal) Matrix in Mathematica

I want to write a function $f[n]$ in Mathematica which gives me an $n\times n$ lower triangular Pascal matrix with a row of zeros in between each nonzero row. That is, I want the matrices \begin{pmatrix} 1&0&0\\ 0&0&0\\ 1&1&0\\ \end{pmatrix} \begin{pmatrix} 1&0&0&0&0\\ 0&0&0&0&0\\ 1&1&0&0&0\\ 0&0&0&0&0\\ 1&2&1&0&0\\ \end{pmatrix}

\begin{pmatrix} 1&0&0&0&0&0&0\\ 0&0&0&0&0&0&0\\ 1&1&0&0&0&0&0\\ 0&0&0&0&0&0&0\\ 1&2&1&0&0&0&0\\ 0&0&0&0&0&0&0\\ 1&3&3&1&0&0&0\\ \end{pmatrix}

Thanks for your help.

-

## migrated from math.stackexchange.comDec 9 '12 at 13:24

This question came from our site for people studying math at any level and professionals in related fields.

luis, I updated my answer. Hopefully it is now easier to understand and more practical. – Mr.Wizard Dec 9 '12 at 20:28
Here is a related thread. – J. M. Feb 17 at 17:13

Simplistically you could use Binomial and Array, and then interleave zero rows. (See the edit history of this post for an example.) A better method is to craft a function using Binomial that returns the right value directly, which would be useful if you don't want to generate the entire array at once. The function below indexes from zero, which is unusual in Mathematica but aligns with Binomial.

f1 = If[OddQ @ #, 0, Binomial[#/2, #2]] &;


A 7 x 7 matrix:

Array[f1, {7, 7}, 0] // MatrixForm


For large matrices you will find that other methods to generate the underlying triangle are faster:

f2[n_] :=
With[{pt = NestList[{0, ##} + {##, 0} & @@ # &, {1}, n - 1]},
ArrayFlatten @
Riffle[{{PadRight[pt, {n, 2 n - 1}]}} ~Transpose~ {3, 2, 1}, {{0}}]
]


This function builds the full array to a specified degree:

f2[4] // MatrixForm


It is significantly faster than the first function:

Array[f1, {1500, 1500}, 0]; // Timing // First

f2[1500]; // Timing // First


2.668

0.687

There are going to be faster methods using the symmetry of the triangle and compilation but I will not elaborate unless asked, as the question does not mention performance.

-

One way :

f[n_?IntegerQ] := Riffle[PadRight[#, 2 n + 1] & /@
Outer[Binomial[#1, #2] &, Range[0, n], Range[0, n]],
{ConstantArray[0, 2 n + 1]}]

f[2] // MatrixForm


-

Something like this?

makeArray[nu_] :=
Riffle[
PadRight[#, 2*nu - 1] & /@ Table[
Binomial[n, k],
{n, 0, nu - 1},
{k, 0, n}
],
ConstantArray[0, {1, 2*nu - 1}]
]

makeArray[4] // MatrixForm


-

Of course, you can use SparseArray[] to generate your padded Pascal matrix:

With[{n = 7},
SparseArray[{j_, k_} /; OddQ[j] && j >= k :>
Binomial[Quotient[j, 2], k - 1], {n, n}] // Normal]
{{1, 0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0, 0}, {1, 1, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0}, {1, 2, 1, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0, 0},
{1, 3, 3, 1, 0, 0, 0}}

-

You could use LinearAlgebraPascalMatrix` function provided by Mathematica.

-
Can you expand on this answer, maybe with code to demonstrate your claim? – J. M. Feb 17 at 17:11