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.com Dec 9 '12 at 13:24

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

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

Mathematica graphics

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

Mathematica graphics

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.

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}}

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

mat2

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

Mathematica graphics

You could use LinearAlgebra`PascalMatrix function provided by Mathematica.

For example:

LinearAlgebra`PascalMatrix[6]//CholeskyDecomposition//Transpose//MatrixForm

gives

$$ \left( \begin{array}{cccccc} 1 & 0 & 0 & 0 & 0 & 0 \\ 1 & 1 & 0 & 0 & 0 & 0 \\ 1 & 2 & 1 & 0 & 0 & 0 \\ 1 & 3 & 3 & 1 & 0 & 0 \\ 1 & 4 & 6 & 4 & 1 & 0 \\ 1 & 5 & 10 & 10 & 5 & 1 \\ \end{array} \right) $$

To obtain the answer to your question:

MatrixForm[Most[Transpose[CholeskyDecomposition[LinearAlgebra`PascalMatrix[6]]]/.a:{_Integer..}:>Sequence[Join[a,ConstantArray[0,Length[a]-1]],ConstantArray[0,2Length[a]-1]]]]

yields

$$ \left( \begin{array}{ccccccccccc} 1 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 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 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 \\ 1 & 2 & 1 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 \\ 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 \\ 1 & 3 & 3 & 1 & 0 & 0 & 0 & 0 & 0 & 0 & 0 \\ 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 \\ 1 & 4 & 6 & 4 & 1 & 0 & 0 & 0 & 0 & 0 & 0 \\ 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 \\ 1 & 5 & 10 & 10 & 5 & 1 & 0 & 0 & 0 & 0 & 0 \\ \end{array} \right) $$

  • 3
    Can you expand on this answer, maybe with code to demonstrate your claim? – J. M. is computer-less Feb 17 '16 at 17:11

Your Answer

 
discard

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.