Pascal matrix generation: Difference between revisions

Content added Content deleted
mNo edit summary
(Add MATLAB implementation)
Line 3,506: Line 3,506:
But since the builtin function MatrixExp works by first computing eigenvalues this is
But since the builtin function MatrixExp works by first computing eigenvalues this is
likely to be slower for large Pascal matrices
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}}==
=={{header|Maxima}}==