4
$\begingroup$

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.

$\endgroup$
2
  • $\begingroup$ luis, I updated my answer. Hopefully it is now easier to understand and more practical. $\endgroup$
    – Mr.Wizard
    Dec 9, 2012 at 20:28
  • $\begingroup$ Here is a related thread. $\endgroup$ Feb 17, 2016 at 17:13

5 Answers 5

5
$\begingroup$

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.

$\endgroup$
2
$\begingroup$

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}}
$\endgroup$
1
$\begingroup$

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

$\endgroup$
1
$\begingroup$

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

$\endgroup$
0
$\begingroup$

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) $$

$\endgroup$
1
  • 3
    $\begingroup$ Can you expand on this answer, maybe with code to demonstrate your claim? $\endgroup$ Feb 17, 2016 at 17:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.