4
$\begingroup$

I am trying to write a function that can create a Pascal matrix. So far I have

P[m_, n_] := Table[(#1!/(#2! (#1 - #2)!)) & /@ Thread[i + j - 1, i - 1], {i, 1, m}, {j, 1, n}] // MatrixForm 

but this doesn't give me the right matrix and I am not sure what is wrong.

Can someone tell me where the error is? Thanks.

$\endgroup$

5 Answers 5

5
$\begingroup$

A much simpler solution:

P[m_, n_] := MatrixForm@Table[Binomial[i + j-2, i-1], {i, 1, m}, {j, 1, n}]; 

or something closer to your attempt:

P[m_, n_] := Table[Apply[(#1!/(#2! (#1 - #2)!)) &, {i + j - 2, i - 1}], {i, 1, m}, {j, 1, n}] // MatrixForm 
$\endgroup$
2
  • $\begingroup$ Can you tell what is wrong with my code? $\endgroup$ Commented Nov 24, 2015 at 3:59
  • $\begingroup$ @Simple You used Thread wrong. You might have confused Threadwith Apply? You also need to use i+j-2. I'll add a solution that's closer to your style. $\endgroup$ Commented Nov 24, 2015 at 4:05
12
$\begingroup$

Oh poor Stephen Wolfram.. no one honors his life's work by giving an answer that uses a cellular automaton. Let me fix this. Here a version for square matrices

P[n_] := #.Transpose[#] &@ CellularAutomaton[{{i_, j_} :> i + j}, {{1}, 0}, n - 1] 

Mathematica graphics

And for rectangular arrays, just truncate the above solution

P[n_, m_] := P[Max[n, m]][[;; n, ;; m]] 
$\endgroup$
1
  • 8
    $\begingroup$ I have a solution using Feynman diagrams too, but seems off topic :) $\endgroup$ Commented Nov 24, 2015 at 5:16
6
$\begingroup$

From here

l[n_] := SparseArray[{i_, j_} -> Binomial[i - 1, j - 1], n] u[n_] := SparseArray[{i_, j_} -> Binomial[j - 1, i - 1], n] s[n_] := l[n].u[n] l[7] // MatrixForm u[7] // MatrixForm s[7] // MatrixForm 

Mathematica graphics

Or equivalently:

l[n_] := MatrixExp@SparseArray[Band[{2, 1}] -> Range[n - 1], n] u[n_] := MatrixExp@SparseArray[Band[{1, -n + 1}] -> Range[n - 1], n] s[n_] := l[n].u[n] l[7] // MatrixForm u[7] // MatrixForm s[7] // MatrixForm 
$\endgroup$
1
  • 1
    $\begingroup$ Alternatively: Array[1/((#1 + #2 - 1) Beta[#1, #2]) &, {7, 7}]. Then the triangles can be extracted via CholeskyDecomposition[]. $\endgroup$ Commented Nov 24, 2015 at 4:22
6
$\begingroup$

Here is a nicely compact solution, whose proof is left to the interested reader:

pascal[n_Integer?Positive] := NestList[Accumulate, ConstantArray[1, n], n - 1] 

It's surprisingly quick:

With[{n = 50}, AbsoluteTiming[Array[Binomial[#1 + #2, #1] &, {n, n}, {0, 0}];]] {0.054873, Null} AbsoluteTiming[pascal[50];] {0.001829, Null} 
$\endgroup$
6
$\begingroup$

You can use LinearAlgebra`PascalMatrix.

Example:

In[57]:= LinearAlgebra`PascalMatrix[3] Out[57]= {{1, 1, 1}, {1, 2, 3}, {1, 3, 6}} 

It is very quick too:

In[65]:= AbsoluteTiming[LinearAlgebra`PascalMatrix[50];] Out[65]= {0.000190, Null} 

It has one option:

In[86]:= Options[LinearAlgebra`PascalMatrix] Out[86]= {WorkingPrecision -> ∞} 
$\endgroup$
0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.