Pascal matrix generation: Difference between revisions

m
imported>Maxima enthusiast
No edit summary
m (→‎{{header|Wren}}: Minor tidy)
(2 intermediate revisions by 2 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,261:
 
=={{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⊸+)⟜↕
Line 1,821 ⟶ 1,820:
=={{header|Delphi}}==
See [https://rosettacode.org/wiki/Pascal_matrix_generation Pascal].
 
=={{header|Elixir}}==
<syntaxhighlight lang="elixir">defmodule Pascal do
Line 2,722:
 
=={{header|J}}==
 
<syntaxhighlight lang="j"> !/~ i. 5
1 1 1 1 1
Line 3,507 ⟶ 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}}==
Line 4,616 ⟶ 4,687:
 
=={{header|Racket}}==
 
<syntaxhighlight lang="racket">#lang racket
(require math/number-theory)
Line 5,244 ⟶ 5,314:
 
=={{header|Stata}}==
 
Here are variants for the lower matrix.
 
Line 5,541 ⟶ 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|
9,482

edits