Pascal matrix generation: Difference between revisions

m
m (syntax highlighting fixup automation)
m (→‎{{header|Wren}}: Minor tidy)
(8 intermediate revisions by 7 users not shown)
Line 1:
{{task|Matrices}}
 
A pascal matrix is a two-dimensional square matrix holding numbers from &nbsp; [[Pascal's triangle]], &nbsp; also known as &nbsp; [[Evaluate binomial coefficients|binomial coefficients]] &nbsp; and which can be shown as &nbsp; <big><sup>n</sup>C<sub>r</sub>.</big>
Line 44:
The &nbsp; [[Cholesky decomposition]] &nbsp; of a Pascal symmetric matrix is the Pascal lower-triangle matrix of the same size.
<br><br>
 
 
=={{header|11l}}==
{{trans|Python}}
 
<syntaxhighlight lang="11l">F pascal_upp(n)
V s = [[0] * n] * n
Line 1,259:
1 4 10 20 35
1 5 15 35 70</pre>
 
=={{header|BQN}}==
<code>C</code> is the combinations function, taken from BQNcrate. The rest of the problem is simple with table (<code>⌜</code>).
<syntaxhighlight lang="bqn">C←(-÷○(×´)1⊸+)⟜↕
Upr←C˜⌜˜↕
Lwr←C⌜˜↕
Sym←(+C⊣)⌜˜↕</syntaxhighlight>
 
<syntaxhighlight lang="bqn"> Upr 5
┌─
╵ 1 1 1 1 1
0 1 2 3 4
0 0 1 3 6
0 0 0 1 4
0 0 0 0 1
Lwr 5
┌─
╵ 1 0 0 0 0
1 1 0 0 0
1 2 1 0 0
1 3 3 1 0
1 4 6 4 1
Sym 5
┌─
╵ 1 1 1 1 1
1 2 3 4 5
1 3 6 10 15
1 4 10 20 35
1 5 15 35 70
┘</syntaxhighlight>
 
=={{header|C}}==
Line 1,787 ⟶ 1,820:
=={{header|Delphi}}==
See [https://rosettacode.org/wiki/Pascal_matrix_generation Pascal].
 
=={{header|Elixir}}==
<syntaxhighlight lang="elixir">defmodule Pascal do
Line 2,399 ⟶ 2,433:
1 4 10 20 35 56 84 120 165 220
1 5 15 35 70 126 210 330 495 715
</pre>
 
=={{header|Frink}}==
Frink has built-in functions for efficiently calculating arbitrary-sized binomial coefficients, initializing matrices based on a function, and formatting matrices using Unicode characters.
<syntaxhighlight lang="frink">println[formatMatrix[new array[[5,5], {|r,c| binomial[c,r]}]]]
println[formatMatrix[new array[[5,5], {|r,c| binomial[r,c]}]]]
println[formatMatrix[new array[[5,5], {|r,c| binomial[r+c, c]}]]]</syntaxhighlight>
{{out}}
<pre>
┌ ┐
│1 1 1 1 1│
│ │
│0 1 2 3 4│
│ │
│0 0 1 3 6│
│ │
│0 0 0 1 4│
│ │
│0 0 0 0 1│
└ ┘
┌ ┐
│1 0 0 0 0│
│ │
│1 1 0 0 0│
│ │
│1 2 1 0 0│
│ │
│1 3 3 1 0│
│ │
│1 4 6 4 1│
└ ┘
┌ ┐
│1 1 1 1 1│
│ │
│1 2 3 4 5│
│ │
│1 3 6 10 15│
│ │
│1 4 10 20 35│
│ │
│1 5 15 35 70│
└ ┘
</pre>
 
Line 2,646 ⟶ 2,722:
 
=={{header|J}}==
 
<syntaxhighlight lang="j"> !/~ i. 5
1 1 1 1 1
Line 3,431 ⟶ 3,506:
But since the builtin function MatrixExp works by first computing eigenvalues this is
likely to be slower for large Pascal matrices
 
=={{header|MATLAB}}==
{{trans|Mathematica/Wolfram_Language}}
<syntaxhighlight lang="MATLAB">
clear all;close all;clc;
 
size = 5; % size of Pascal matrix
 
% Generate the symmetric Pascal matrix
symPascalMatrix = symPascal(size);
 
% Generate the upper triangular Pascal matrix
upperPascalMatrix = upperPascal(size);
 
% Generate the lower triangular Pascal matrix
lowerPascalMatrix = lowerPascal(size);
 
% Display the matrices
disp('Upper Pascal Matrix:');
disp(upperPascalMatrix);
 
disp('Lower Pascal Matrix:');
disp(lowerPascalMatrix);
 
disp('Symmetric Pascal Matrix:');
disp(symPascalMatrix);
 
 
function symPascal = symPascal(size)
% Generates a symmetric Pascal matrix of given size
row = ones(1, size);
symPascal = row;
for k = 2:size
row = cumsum(row);
symPascal = [symPascal; row];
end
end
 
function upperPascal = upperPascal(size)
% Generates an upper triangular Pascal matrix using Cholesky decomposition
upperPascal = chol(symPascal(size));
end
 
function lowerPascal = lowerPascal(size)
% Generates a lower triangular Pascal matrix using Cholesky decomposition
lowerPascal = chol(symPascal(size))';
end
</syntaxhighlight>
{{out}}
<pre>
Upper Pascal Matrix:
1 1 1 1 1
0 1 2 3 4
0 0 1 3 6
0 0 0 1 4
0 0 0 0 1
 
Lower Pascal Matrix:
1 0 0 0 0
1 1 0 0 0
1 2 1 0 0
1 3 3 1 0
1 4 6 4 1
 
Symmetric Pascal Matrix:
1 1 1 1 1
1 2 3 4 5
1 3 6 10 15
1 4 10 20 35
1 5 15 35 70
</pre>
 
 
=={{header|Maxima}}==
Using built-in functions genmatrix and binomial
<syntaxhighlight lang="maxima">
/* Function that returns a lower Pascal matrix */
lower_pascal(n):=genmatrix(lambda([i,j],binomial(i-1,j-1)),n,n)$
 
/* Function that returns an upper Pascal matrix */
upper_pascal(n):=genmatrix(lambda([i,j],binomial(j-1,i-1)),n,n)$
 
/* Function that returns a symmetric Pascal matrix (the matricial multiplication of a lower and an upper of the same size) */
symmetric_pascal(n):=lower_pascal(n).upper_pascal(n)$
 
/* Test cases */
lower_pascal(5);
upper_pascal(5);
symmetric_pascal(5);
</syntaxhighlight>
{{out}}
<pre>
matrix(
[1, 0, 0, 0, 0],
[1, 1, 0, 0, 0],
[1, 2, 1, 0, 0],
[1, 3, 3, 1, 0],
[1, 4, 6, 4, 1]
)
 
matrix(
[1, 1, 1, 1, 1],
[0, 1, 2, 3, 4],
[0, 0, 1, 3, 6],
[0, 0, 0, 1, 4],
[0, 0, 0, 0, 1]
)
 
matrix(
[1, 1, 1, 1, 1],
[1, 2, 3, 4, 5],
[1, 3, 6, 10, 15],
[1, 4, 10, 20, 35],
[1, 5, 15, 35, 70]
)
</pre>
 
=={{header|Nim}}==
Line 4,496 ⟶ 4,687:
 
=={{header|Racket}}==
 
<syntaxhighlight lang="racket">#lang racket
(require math/number-theory)
Line 4,806 ⟶ 4,996:
1 4 10 20 35
1 5 15 35 70
</pre>
 
=={{header|RPL}}==
≪ → n
≪ { } n + n + 0 CON
1 n '''FOR''' h 1 n '''FOR''' j
'''IF''' h j ≥ '''THEN''' { } j + h + h 1 - j 1 - COMB PUT '''END'''
'''NEXT NEXT'''
≫ ≫ ‘'''PASUT'''’ STO
≪ '''PASUT''' TRN ≫ ‘'''PASLT'''’ STO
≪ → n
≪ { } n + n + 0 CON
1 n '''FOR''' h 1 n '''FOR''' j
{ } j + h + h j + 2 - j 1 - COMB PUT
'''NEXT NEXT'''
≫ ≫ ‘'''PASYM'''’ STO
 
5 '''PASUT''' 5 '''PASLT''' 5 '''PASYM'''
{{out}}
<pre>
3: [[ 1 1 1 1 1 ]
[ 0 1 2 3 4 ]
[ 0 0 1 3 6 ]
[ 0 0 0 1 4 ]
[ 0 0 0 0 1 ]]
2: [[ 1 0 0 0 0 ]
[ 1 1 0 0 0 ]
[ 1 2 1 0 0 ]
[ 1 3 3 1 0 ]
[ 1 4 6 4 1 ]]
1: [[ 1 1 1 1 1 ]
[ 1 2 3 4 5 ]
[ 1 3 6 10 15 ]
[ 1 4 10 20 35 ]
[ 1 5 15 35 70 ]]
</pre>
 
Line 5,087 ⟶ 5,314:
 
=={{header|Stata}}==
 
Here are variants for the lower matrix.
 
Line 5,384 ⟶ 5,610:
{{libheader|Wren-math}}
{{libheader|Wren-matrix}}
<syntaxhighlight lang="ecmascriptwren">import "./fmt" for Fmt
import "./math" for Int
import "./matrix" for Matrix
 
var binomial = Fn.new { |n, k|
Line 5,449 ⟶ 5,675:
| 1 4 10 20 35|
| 1 5 15 35 70|
</pre>
 
=={{header|XPL0}}==
{{trans|ALGOL W}}
<syntaxhighlight lang "XPL0"> \Initialises M to an upper Pascal matrix of size N
\The bounds of M must be at least 1 :: N, 1 :: N
procedure UpperPascalMatrix ( M, N );
integer M, N, J, I;
begin
for J := 1 to N do M( 1, J ) := 1;
for I := 2 to N do begin
M( I, 1 ) := 0;
for J := 2 to N do M( I, J ) := M( I - 1, J - 1 ) + M( I, J - 1 )
end \for_I
end; \UpperPascalMatrix
 
\Initialises M to a lower Pascal matrix of size N
\The bounds of M must be at least 1 :: N, 1 :: N
procedure LowerPascalMatrix ( M, N );
integer M, N, I, J;
begin
for I := 1 to N do M( I, 1 ) := 1;
for J := 2 to N do begin
M( 1, J ) := 0;
for I := 2 to N do M( I, J ) := M( I - 1, J - 1 ) + M( I - 1, J )
end \for_J
end; \LowerPascalMatrix
 
\Initialises M to a symmetric Pascal matrix of size N
\The bounds of M must be at least 1 :: N, 1 :: N
procedure SymmetricPascalMatrix ( M, N );
integer M, N, I, J;
begin
for I := 1 to N do begin
M( I, 1 ) := 1;
M( 1, I ) := 1
end; \for_I
for J := 2 to N do for I := 2 to N do M( I, J ) := M( I, J - 1 ) + M( I - 1, J )
end; \SymmetricPascalMatrix
 
\Test the Pascal matrix procedures
\Print the matrix M with the specified field width
\The bounds of M must be at least 1 :: N, 1 :: N
procedure PrintMatrix ( M, N, FieldWidth );
integer M, N, I, J;
begin
Format(3, 0);
for I := 1 to N do begin
for J := 1 to N do RlOut(0, float( M( I, J ) ) );
CrLf(0)
end; \for_I
end; \PrintMatrix
 
integer M( 1+10, 1+10 );
integer N, W;
begin
N := 5; W := 2;
UpperPascalMatrix( M, N );
Text(0, "upper:^m^j" ); PrintMatrix( M, N, W );
LowerPascalMatrix( M, N );
Text(0, "lower:^m^j" ); PrintMatrix( M, N, W );
SymmetricPascalMatrix( M, N );
Text(0, "symmetric:^m^j" ); PrintMatrix( M, N, W )
end</syntaxhighlight>
{{out}}
<pre>
upper:
1 1 1 1 1
0 1 2 3 4
0 0 1 3 6
0 0 0 1 4
0 0 0 0 1
lower:
1 0 0 0 0
1 1 0 0 0
1 2 1 0 0
1 3 3 1 0
1 4 6 4 1
symmetric:
1 1 1 1 1
1 2 3 4 5
1 3 6 10 15
1 4 10 20 35
1 5 15 35 70
</pre>
 
9,482

edits