Pascal matrix generation: Difference between revisions
m
→{{header|Wren}}: Minor tidy
m (→{{header|Wren}}: Minor tidy) |
|||
(32 intermediate revisions by 23 users not shown) | |||
Line 1:
{{task|Matrices}}
A pascal matrix is a two-dimensional square matrix holding numbers from [[Pascal's triangle]], also known as [[Evaluate binomial coefficients|binomial coefficients]] and which can be shown as <big><sup>n</sup>C<sub>r</sub>.</big>
Line 44:
The [[Cholesky decomposition]] 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
s[0] = [1] * n
L(i) 1 .< n
L(j) i .< n
s[i][j] = s[i - 1][j - 1] + s[i][j - 1]
R s
F pascal_low(n)
V upp = pascal_upp(n)
V s = [[0] * n] * n
L(x) 0 .< n
L(y) 0 .< n
s[y][x] = upp[x][y]
R s
F pascal_sym(n)
V s = [[1] * n] * n
L(i) 1 .< n
L(j) 1 .< n
s[i][j] = s[i - 1][j] + s[i][j - 1]
R s
F pp(mat)
print(‘[’mat.map(String).join(",\n ")‘]’)
-V n = 5
print(‘Upper:’)
pp(pascal_upp(n))
print("\nLower:")
pp(pascal_low(n))
print("\nSymmetric:")
pp(pascal_sym(n))</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>
=={{header|360 Assembly}}==
<
PASCMATR CSECT
USING PASCMATR,R13 base register
Line 179 ⟶ 240:
XDEC DS CL12 temp
YREGS
END PASCMATR</
{{out}}
<pre>
Line 200 ⟶ 261:
1 4 10 20 35
1 5 15 35 70
</pre>
=={{header|Action!}}==
<syntaxhighlight lang="action!">BYTE FUNC Index(BYTE i,j,dim)
RETURN (i*dim+j)
PROC PascalUpper(BYTE ARRAY mat BYTE dim)
BYTE i,j
FOR i=0 TO dim-1
DO
FOR j=0 TO dim-1
DO
IF i>j THEN
mat(Index(i,j,dim))=0
ELSEIF i=j OR i=0 THEN
mat(Index(i,j,dim))=1
ELSE
mat(Index(i,j,dim))=mat(Index(i-1,j-1,dim))+mat(Index(i,j-1,dim))
FI
OD
OD
RETURN
PROC PascalLower(BYTE ARRAY mat BYTE dim)
BYTE i,j
FOR i=0 TO dim-1
DO
FOR j=0 TO dim-1
DO
IF i<j THEN
mat(Index(i,j,dim))=0
ELSEIF i=j OR j=0 THEN
mat(Index(i,j,dim))=1
ELSE
mat(Index(i,j,dim))=mat(Index(i-1,j-1,dim))+mat(Index(i-1,j,dim))
FI
OD
OD
RETURN
PROC PascalSymmetric(BYTE ARRAY mat BYTE dim)
BYTE i,j
FOR i=0 TO dim-1
DO
FOR j=0 TO dim-1
DO
IF i=0 OR j=0 THEN
mat(Index(i,j,dim))=1
ELSE
mat(Index(i,j,dim))=mat(Index(i-1,j,dim))+mat(Index(i,j-1,dim))
FI
OD
OD
RETURN
PROC PrintMatrix(BYTE ARRAY mat BYTE dim)
BYTE i,j,v
FOR i=0 TO dim-1
DO
FOR j=0 TO dim-1
DO
v=mat(Index(i,j,dim))
IF v<10 THEN
Print(" ")
ELSEIF v<100 THEN
Print(" ")
FI
PrintB(v)
OD
PutE()
OD
RETURN
PROC Main()
BYTE ARRAY mat(25)
BYTE dim=[5]
PrintE("Pascal upper matrix:")
PascalUpper(mat,dim)
PrintMatrix(mat,dim)
PutE()
PrintE("Pascal lower matrix:")
PascalLower(mat,dim)
PrintMatrix(mat,dim)
PutE()
PrintE("Pascal symmetric matrix:")
PascalSymmetric(mat,dim)
PrintMatrix(mat,dim)
RETURN</syntaxhighlight>
{{out}}
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Pascal_matrix_generation.png Screenshot from Atari 8-bit computer]
<pre>
Pascal upper 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
Pascal lower 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
Pascal symmetric 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|Ada}}==
<
with Ada.Text_IO; use Ada.Text_IO;
with Ada.Integer_Text_IO; use Ada.Integer_Text_IO;
Line 287 ⟶ 466:
Put("Symmetric:"); New_Line(1);
Print(Symmetric(n));
end PascalMatrix;</
{{out}}
<pre>What dimension Pascal matrix would you like? 5
Line 310 ⟶ 489:
=={{header|ALGOL 68}}==
<
# returns an upper Pascal matrix of size n #
PROC upper pascal matrix = ( INT n )[,]INT:
Line 370 ⟶ 549:
print( ( "symmetric:", newline ) ); print matrix( symmetric pascal matrix( 5 ), 2 )
END</
{{out}}
<pre>
Line 395 ⟶ 574:
=={{header|ALGOL W}}==
{{Trans|ALGOL_68}}
<
% initialises m to an upper Pascal matrix of size n %
% the bounds of m must be at least 1 :: n, 1 :: n %
Line 460 ⟶ 639:
end
end.</
{{out}}
<pre>
Line 482 ⟶ 661:
1 5 15 35 70
</pre>
=={{header|APL}}==
{{works with|Dyalog APL}}
<syntaxhighlight lang="apl">upper ← ∘.!⍨¯1+⍳
lower ← ⍉(∘.!⍨¯1+⍳)
symmetric ← (⊢![1]∘.+⍨)¯1+⍳</syntaxhighlight>
{{out}}
<pre style='line-height:normal'>
((⊂upper),(⊂lower),(⊂symmetric)) 5
┌─────────┬─────────┬────────────┐
│1 1 1 1 1│1 0 0 0 0│1 1 1 1 1│
│0 1 2 3 4│1 1 0 0 0│1 2 3 4 5│
│0 0 1 3 6│1 2 1 0 0│1 3 6 10 15│
│0 0 0 1 4│1 3 3 1 0│1 4 10 20 35│
│0 0 0 0 1│1 4 6 4 1│1 5 15 35 70│
└─────────┴─────────┴────────────┘</pre>
=={{header|AppleScript}}==
By composition of generic functions:
<
-- pascalMatrix :: ((Int, Int) -> (Int, Int)) -> Int -> [[Int]]
Line 820 ⟶ 1,015:
return lst
end tell
end zipWith</
{{Out}}
<pre>Lower
Line 842 ⟶ 1,037:
[1, 4, 10, 20, 35]
[1, 5, 15, 35, 70]</pre>
=={{header|AutoHotkey}}==
<syntaxhighlight lang="autohotkey">n := 5
MsgBox, 262144, ,% ""
. "Pascal upper-triangular :`n" show(Pascal_Upper(n))
. "`n`nPascal lower-triangular :`n" show(Pascal_Lower(n))
. "`n`nPascal symmetric:`n" show(Pascal_Symm(n))
return
show(obj){
for i, o in obj{
line := ""
for j, v in o
line .= v ", "
res .= "[" Trim(line, ", ") "]`n,"
}
return "[" Trim(res, "`n,") "]"
}
Pascal_Upper(n){
obj := fillObj(n)
loop % n
obj[1, A_Index] := 1
loop % n-1
obj[A_Index+1, 1] := 0
for i, o in obj
for j, v in o
if !(i = 1 or j = 1)
obj[i, j] := obj[i, j-1] + obj[i-1, j-1]
return obj
}
Pascal_Lower(n){
obj := fillObj(n)
loop % n
obj[A_Index, 1] := 1
loop % n-1
obj[1, A_Index+1] := 0
for i, o in obj
for j, v in o
if !(i = 1 or j = 1)
obj[i, j] := obj[i-1, j] + obj[i-1, j-1]
return obj
}
Pascal_Symm(n){
obj := fillObj(n)
loop % n
obj[A_Index, 1] := 1
loop % n-1
obj[1, A_Index+1] := 1
for i, o in obj
for j, v in o
if !(i = 1 or j = 1)
obj[i, j] := obj[i-1, j] + obj[i, j-1]
return obj
}
fillObj(n){
obj := []
loop % n{
i := A_Index
loop % n
obj[i, A_Index] := 0
}
return obj
}</syntaxhighlight>
{{out}}
<pre>Pascal upper-triangular :
[[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]]
Pascal lower-triangular :
[[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]]
Pascal 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>
=={{header|BASIC}}==
<syntaxhighlight lang="basic">10 DEFINT A-Z: S=5: DIM M(S,S)
20 PRINT "Lower-triangular matrix:": GOSUB 200: GOSUB 100
30 PRINT "Upper-triangular matrix:": GOSUB 300: GOSUB 100
40 PRINT "Symmetric matrix:": GOSUB 400: GOSUB 100
50 END
100 REM *** Print the matrix M ***
110 FOR Y=1 TO S
120 FOR X=1 TO S
130 PRINT USING " ##";M(X,Y);
140 NEXT X
150 PRINT
160 NEXT Y
170 PRINT
180 RETURN
200 REM *** Generate the lower-triangular matrix ***
210 FOR X=1 TO S: FOR Y=1 TO S
220 ON -(X>Y)-2*(X=Y OR X=1) GOTO 240,250
230 M(X,Y)=M(X-1,Y-1)+M(X,Y-1): GOTO 260
240 M(X,Y)=0: GOTO 260
250 M(X,Y)=1: GOTO 260
260 NEXT Y,X
270 RETURN
300 REM *** Generate the upper-triangular matrix ***
310 FOR X=1 TO S: FOR Y=1 TO S
320 ON -(X<Y)-2*(X=Y OR Y=1) GOTO 340,350
330 M(X,Y)=M(X-1,Y-1)+M(X-1,Y): GOTO 360
340 M(X,Y)=0: GOTO 360
350 M(X,Y)=1: GOTO 360
360 NEXT Y,X
370 RETURN
400 REM *** Generate the symmetric matrix ***
410 FOR X=1 TO S: FOR Y=1 TO S
420 IF X=1 OR Y=1 THEN M(X,Y)=1 ELSE M(X,Y)=M(X-1,Y)+M(X,Y-1)
430 NEXT Y,X
440 RETURN</syntaxhighlight>
{{out}}
<pre>Lower-triangular 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
Upper-triangular 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
Symmetric 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|BCPL}}==
<syntaxhighlight lang="bcpl">get "libhdr"
manifest $( size = 5 $)
// Matrix index
let ix(mat, n, x, y) = mat+y*n+x
let lower(m, n) be
for y=0 to n-1
for x=0 to n-1 do
!ix(m,n,x,y) :=
x>y -> 0,
x=y | x=0 -> 1,
!ix(m,n,x-1,y-1) + !ix(m,n,x,y-1)
let upper(m, n) be
for y=0 to n-1
for x=0 to n-1 do
!ix(m,n,x,y) :=
x<y -> 0,
x=y | y=0 -> 1,
!ix(m,n,x-1,y-1) + !ix(m,n,x-1,y)
let symmetric(m, n) be
for y=0 to n-1
for x=0 to n-1 do
!ix(m,n,x,y) :=
x=0 | y=0 -> 1,
!ix(m,n,x-1,y) + !ix(m,n,x,y-1)
// Print matrix
let writemat(m, n, d) be
for y=0 to n-1
$( for x=0 to n-1
$( writed(!ix(m,n,x,y), d)
wrch(' ')
$)
wrch('*N')
$)
// Generate and print 5-by-5 matrices
let start() be
$( let mat = vec size * size
writes("Upper-triangular matrix:*N")
upper(mat, size) ; writemat(mat, size, 2)
writes("*NLower-triangular matrix:*N")
lower(mat, size) ; writemat(mat, size, 2)
writes("*NSymmetric matrix:*N")
symmetric(mat, size) ; writemat(mat, size, 2)
$)</syntaxhighlight>
{{out}}
<pre>Upper-triangular 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-triangular 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 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|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}}==
<syntaxhighlight lang="c">
#include <stdio.h>
#include <stdlib.h>
Line 918 ⟶ 1,368:
return 0;
}
</syntaxhighlight>
{{out}}
<pre>
Line 942 ⟶ 1,392:
=={{header|C sharp}}==
<
public static class PascalMatrixGeneration
Line 1,007 ⟶ 1,457:
}
}</
{{out}}
<pre>
Line 1,030 ⟶ 1,480:
=={{header|C++}}==
{{works with|GCC|version 7.2.0 (Ubuntu 7.2.0-8ubuntu3.2) }}
<
#include <vector>
Line 1,087 ⟶ 1,537:
std::cout << "PASCAL SYMMETRIC MATRIX" << std::endl;
print_matrix(pascal_symmetric(5));
}</
{{out}}
<pre>
Line 1,109 ⟶ 1,559:
1 5 15 35 70
</pre>
=={{header|Clojure}}==
<syntaxhighlight lang="clojure">(defn binomial-coeff [n k]
(reduce #(quot (* %1 (inc (- n %2))) %2)
1
(range 1 (inc k))))
(defn pascal-upper [n]
(map
(fn [i]
(map (fn [j]
(binomial-coeff j i))
(range n)))
(range n)))
(defn pascal-lower [n]
(map
(fn [i]
(map (fn [j]
(binomial-coeff i j))
(range n)))
(range n)))
(defn pascal-symmetric [n]
(map
(fn [i]
(map (fn [j]
(binomial-coeff (+ i j) i))
(range n)))
(range n)))
(defn pascal-matrix [n]
(println "Upper:")
(run! println (pascal-upper n))
(println)
(println "Lower:")
(run! println (pascal-lower n))
(println)
(println "Symmetric:")
(run! println (pascal-symmetric n)))</syntaxhighlight>
{{out}}
<pre>=> (pascal-matrix 5)
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>
=={{header|CLU}}==
<syntaxhighlight lang="clu">matrix = array[array[int]]
make_matrix = proc (gen: proctype (int,int,matrix) returns (int),
size: int) returns (matrix)
m: matrix := matrix$fill_copy(0, size, array[int]$fill(0, size, 0))
for y: int in int$from_to(0, size-1) do
for x: int in int$from_to(0, size-1) do
m[y][x] := gen(x,y,m)
end
end
return(m)
end make_matrix
lower = proc (x,y: int, m: matrix) returns (int)
if x>y then return(0)
elseif x=y | x=0 then return(1)
else return( m[y-1][x-1] + m[y-1][x] )
end
end lower
upper = proc (x,y: int, m: matrix) returns (int)
if x<y then return(0)
elseif x=y | y=0 then return(1)
else return( m[y-1][x-1] + m[y][x-1] )
end
end upper
symmetric = proc (x,y: int, m: matrix) returns (int)
if x=0 | y=0 then return(1)
else return(m[y][x-1] + m[y-1][x])
end
end symmetric
print_matrix = proc (s: stream, m: matrix, w: int)
for line: array[int] in matrix$elements(m) do
for item: int in array[int]$elements(line) do
stream$putright(s, int$unparse(item), w)
stream$putc(s, ' ')
end
stream$putl(s, "")
end
end print_matrix
start_up = proc ()
po: stream := stream$primary_output()
stream$putl(po, "Upper-triangular matrix:")
print_matrix(po, make_matrix(upper,5), 1)
stream$putl(po, "\nLower-triangular matrix:")
print_matrix(po, make_matrix(lower,5), 1)
stream$putl(po, "\nSymmetric matrix:")
print_matrix(po, make_matrix(symmetric,5), 2)
end start_up</syntaxhighlight>
{{out}}
<pre>Upper-triangular 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-triangular 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 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|Common Lisp}}==
<
(dotimes (i n)
(setf (aref a i 0) 1))
Line 1,175 ⟶ 1,765:
1 3 6 10 15
1 4 10 20 35
1 5 15 35 70</
=={{header|D}}==
{{trans|Python}}
<
auto binomialCoeff(in uint n, in uint k) pure nothrow {
Line 1,205 ⟶ 1,795:
writefln("\nLower:\n%(%(%2d %)\n%)", pascalLow(n));
writefln("\nSymmetric:\n%(%(%2d %)\n%)", pascalSym(n));
}</
{{out}}
<pre>Upper:
Line 1,227 ⟶ 1,817:
1 4 10 20 35
1 5 15 35 70</pre>
=={{header|Delphi}}==
See [https://rosettacode.org/wiki/Pascal_matrix_generation Pascal].
=={{header|Elixir}}==
<
defp ij(n), do: for i <- 1..n, j <- 1..n, do: {i,j}
Line 1,274 ⟶ 1,867:
Pascal.lower_triangle(5)
IO.puts "Pascal symmetric matrix:"
Pascal.symmetic_triangle(5)</
{{out}}
Line 1,297 ⟶ 1,890:
[1, 5, 15, 35, 70]
</pre>
=={{header|Excel}}==
===LAMBDA===
Binding the names PASCALMATRIX, BINCOEFF and SYMMETRIC to the following lambda expressions in the Name Manager of the Excel WorkBook:
(See [https://www.microsoft.com/en-us/research/blog/lambda-the-ultimatae-excel-worksheet-function/ LAMBDA: The ultimate Excel worksheet function])
{{Works with|Office 365 betas 2021}}
<syntaxhighlight lang="lisp">PASCALMATRIX
=LAMBDA(n,
LAMBDA(f,
LET(
ixs, SEQUENCE(n, n, 0, 1),
f(BINCOEFF)(
QUOTIENT(ixs, n)
)(
MOD(ixs, n)
)
)
)
)
BINCOEFF
=LAMBDA(n,
LAMBDA(k,
IF(n < k,
0,
QUOTIENT(FACT(n), FACT(k) * FACT(n - k))
)
)
)
SYMMETRIC
=LAMBDA(f,
LAMBDA(a,
LAMBDA(b,
f(a + b)(b)
)
)
)</syntaxhighlight>
and also assuming the following generic bindings in the Name Manager for the WorkBook:
<syntaxhighlight lang="lisp">FLIP
=LAMBDA(f,
LAMBDA(a,
LAMBDA(b,
f(b)(a)
)
)
)
ID
=LAMBDA(x, x)</syntaxhighlight>
{{Out}}
{| class="wikitable"
|-
|||style="text-align:right; font-family:serif; font-style:italic; font-size:120%;"|fx
! colspan="8" style="text-align:left; vertical-align: bottom; font-family:Arial, Helvetica, sans-serif !important;"|=PASCALMATRIX(5)( ID )
|- style="text-align:center; font-family:Arial, Helvetica, sans-serif !important; background-color:#000000; color:#ffffff;"
|
| A
| B
| C
| D
| E
| F
| G
| H
|-
| style="text-align:center; font-family:Arial, Helvetica, sans-serif !important; background-color:#000000; color:#ffffff" | 1
| style="font-style:italic" |
| colspan="7" style="font-weight:bold" | Lower
|-
| style="text-align:center; font-family:Arial, Helvetica, sans-serif !important; background-color:#000000; color:#ffffff" | 2
| style="font-style:italic" |
| style="text-align:center; background-color:#cbcefb" | 1
| style="text-align:center" | 0
| style="text-align:center" | 0
| style="text-align:center" | 0
| style="text-align:center" | 0
|
|
|-
| style="text-align:center; font-family:Arial, Helvetica, sans-serif !important; background-color:#000000; color:#ffffff" | 3
| style="font-style:italic" |
| style="text-align:center" | 1
| style="text-align:center" | 1
| style="text-align:center" | 0
| style="text-align:center" | 0
| style="text-align:center" | 0
|
|
|-
| style="text-align:center; font-family:Arial, Helvetica, sans-serif !important; background-color:#000000; color:#ffffff" | 4
| style="font-style:italic" |
| style="text-align:center" | 1
| style="text-align:center" | 2
| style="text-align:center" | 1
| style="text-align:center" | 0
| style="text-align:center" | 0
|
|
|-
| style="text-align:center; font-family:Arial, Helvetica, sans-serif !important; background-color:#000000; color:#ffffff" | 5
| style="font-style:italic" |
| style="text-align:center" | 1
| style="text-align:center" | 3
| style="text-align:center" | 3
| style="text-align:center" | 1
| style="text-align:center" | 0
|
|
|-
| style="text-align:center; font-family:Arial, Helvetica, sans-serif !important; background-color:#000000; color:#ffffff" | 6
| style="font-style:italic" |
| style="text-align:center" | 1
| style="text-align:center" | 4
| style="text-align:center" | 6
| style="text-align:center" | 4
| style="text-align:center" | 1
|
|
|}
{| class="wikitable"
|-
|||style="text-align:right; font-family:serif; font-style:italic; font-size:120%;"|fx
! colspan="8" style="text-align:left; vertical-align: bottom; font-family:Arial, Helvetica, sans-serif !important;"|=PASCALMATRIX(5)( FLIP )
|- style="text-align:center; font-family:Arial, Helvetica, sans-serif !important; background-color:#000000; color:#ffffff;"
|
| A
| B
| C
| D
| E
| F
| G
| H
|-
| style="text-align:center; font-family:Arial, Helvetica, sans-serif !important; background-color:#000000; color:#ffffff" | 1
|
| colspan="7" style="font-weight:bold" | Upper
|-
| style="text-align:center; font-family:Arial, Helvetica, sans-serif !important; background-color:#000000; color:#ffffff" | 2
|
| style="text-align:center; background-color:#cbcefb" | 1
| style="text-align:center" | 1
| style="text-align:center" | 1
| style="text-align:center" | 1
| style="text-align:center" | 1
|
|
|-
| style="text-align:center; font-family:Arial, Helvetica, sans-serif !important; background-color:#000000; color:#ffffff" | 3
|
| style="text-align:center" | 0
| style="text-align:center" | 1
| style="text-align:center" | 2
| style="text-align:center" | 3
| style="text-align:center" | 4
|
|
|-
| style="text-align:center; font-family:Arial, Helvetica, sans-serif !important; background-color:#000000; color:#ffffff" | 4
|
| style="text-align:center" | 0
| style="text-align:center" | 0
| style="text-align:center" | 1
| style="text-align:center" | 3
| style="text-align:center" | 6
|
|
|-
| style="text-align:center; font-family:Arial, Helvetica, sans-serif !important; background-color:#000000; color:#ffffff" | 5
|
| style="text-align:center" | 0
| style="text-align:center" | 0
| style="text-align:center" | 0
| style="text-align:center" | 1
| style="text-align:center" | 4
|
|
|-
| style="text-align:center; font-family:Arial, Helvetica, sans-serif !important; background-color:#000000; color:#ffffff" | 6
|
| style="text-align:center" | 0
| style="text-align:center" | 0
| style="text-align:center" | 0
| style="text-align:center" | 0
| style="text-align:center" | 1
|
|
|}
{| class="wikitable"
|-
|||style="text-align:right; font-family:serif; font-style:italic; font-size:120%;"|fx
! colspan="8" style="text-align:left; vertical-align: bottom; font-family:Arial, Helvetica, sans-serif !important;"|=PASCALMATRIX(5)( SYMMETRIC )
|- style="text-align:center; font-family:Arial, Helvetica, sans-serif !important; background-color:#000000; color:#ffffff;"
|
| A
| B
| C
| D
| E
| F
| G
| H
|-
| style="text-align:center; font-family:Arial, Helvetica, sans-serif !important; background-color:#000000; color:#ffffff" | 1
|
| colspan="7" style="font-weight:bold" | Symmetric
|-
| style="text-align:center; font-family:Arial, Helvetica, sans-serif !important; background-color:#000000; color:#ffffff" | 2
|
| style="text-align:center; background-color:#cbcefb" | 1
| style="text-align:center" | 1
| style="text-align:center" | 1
| style="text-align:center" | 1
| style="text-align:center" | 1
|
|
|-
| style="text-align:center; font-family:Arial, Helvetica, sans-serif !important; background-color:#000000; color:#ffffff" | 3
|
| style="text-align:center" | 1
| style="text-align:center" | 2
| style="text-align:center" | 3
| style="text-align:center" | 4
| style="text-align:center" | 5
|
|
|-
| style="text-align:center; font-family:Arial, Helvetica, sans-serif !important; background-color:#000000; color:#ffffff" | 4
|
| style="text-align:center" | 1
| style="text-align:center" | 3
| style="text-align:center" | 6
| style="text-align:center" | 10
| style="text-align:center" | 15
|
|
|-
| style="text-align:center; font-family:Arial, Helvetica, sans-serif !important; background-color:#000000; color:#ffffff" | 5
|
| style="text-align:center" | 1
| style="text-align:center" | 4
| style="text-align:center" | 10
| style="text-align:center" | 20
| style="text-align:center" | 35
|
|
|-
| style="text-align:center; font-family:Arial, Helvetica, sans-serif !important; background-color:#000000; color:#ffffff" | 6
|
| style="text-align:center" | 1
| style="text-align:center" | 5
| style="text-align:center" | 15
| style="text-align:center" | 35
| style="text-align:center" | 70
|
|
|}
=={{header|Factor}}==
{{works with|Factor|0.99 2020-01-23}}
<
math.matrices prettyprint sequences ;
Line 1,315 ⟶ 2,178:
[ upper "Upper:" ]
[ symmetric "Symmetric:" ] tri
[ print simple-table. nl ] 2tri@</
{{out}}
<pre>
Line 1,338 ⟶ 2,201:
1 4 10 20 35
1 5 15 35 70
</pre>
=={{header|Fermat}}==
<syntaxhighlight lang="fermat">&a; {set mode to 0-indexed matrices}
Func Pasmat( n, t ) =
;{create a Pascal matrix of size n by n}
;{t=0 -> upper triangular, 1 -> lower triangular,2->symmetric}
Array m[n, n]; {result is stored in array m}
if t = 0 then
[m]:=[<i=0,n-1><j=0,n-1> Bin(j,i) ];
fi;
if t = 1 then
[m]:=[<i=0,n-1><j=0,n-1> Bin(i,j) ];
fi;
if t = 2 then
[m]:=[<i=0,n-1><j=0,n-1> Bin(i+j,i) ];
fi;
.;
Pasmat(5, 0);
!!([m);
!;
Pasmat(5, 1);
!!([m);
!;
Pasmat(5, 2);
!!([m);</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 1,343 ⟶ 2,251:
The following program uses features of Fortran 2003.
<
implicit none
Line 1,420 ⟶ 2,328:
a = pascal_symmetric(n)
call print_matrix(a)
end program</
<syntaxhighlight lang="text"> Size?
5
Lower Pascal Matrix
Line 1,441 ⟶ 2,349:
1 3 6 10 15
1 4 10 20 35
1 5 15 35 70</
=={{header|FreeBASIC}}==
<syntaxhighlight lang="freebasic">
sub print_matrix( M() as integer )
'displays a matrix
for row as integer = 0 to ubound(M, 1)
for col as integer = 0 to ubound(M, 2)
print using "#### ";M(row, col);
next col
print
next row
return
end sub
function fact( n as uinteger ) as uinteger
'quick and dirty factorial
if n<2 then return 1 else return n*fact(n-1)
end function
function nCp( n as uinteger, p as uinteger ) as uinteger
'quick and dirty binomial
if p>n then return 0 else return fact(n)/(fact(p)*fact(n-p))
end function
sub make_pascal( M() as integer, typ as const ubyte )
'allocate the matrix first
'typ 0 = jCi, 1=iCj, 2=(j+i)Ci
for i as uinteger = 0 to ubound(M,1)
for j as uinteger = 0 to ubound(M,2)
select case typ
case 0
M(i,j) = nCp(j, i)
case 1
M(i,j) = nCp(i, j)
case 2
M(i,j) = nCp(i + j, j)
case else
M(i, j) = 0
end select
next j
next i
return
end sub
dim as integer M(0 to 4, 0 to 4)
print "Upper triangular"
make_pascal( M(), 0 )
print_matrix( M() )
print "Lower triangular"
make_pascal( M(), 1 )
print_matrix( M() )
print "Symmetric"
make_pascal( M(), 2 )
print_matrix( M() )
print "Technically the matrix needn't be square :)"
dim as integer Q(0 to 4, 0 to 9)
make_pascal( Q(), 2 )
print_matrix( Q() )</syntaxhighlight>
{{out}}<pre>
Upper triangular
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 triangular
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
Technically the matrix needn't be square :)
1 1 1 1 1 1 1 1 1 1
1 2 3 4 5 6 7 8 9 10
1 3 6 10 15 21 28 36 45 55
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>
=={{header|Go}}==
{{trans|Kotlin}}
<
import (
Line 1,525 ⟶ 2,559:
printMatrix("Pascal lower-triangular matrix", pascalLowerTriangular(5))
printMatrix("Pascal symmetric matrix", pascalSymmetric(5))
}</
{{out}}
Line 1,552 ⟶ 2,586:
=={{header|Haskell}}==
<
import System.Environment (getArgs)
import Text.Printf (printf)
Line 1,586 ⟶ 2,620:
printMat "Upper triangular" $ pascUp n
printMat "Symmetric" $ pascSym n
_ -> error "Usage: pascmat <number>"</
{{out}}
Line 1,620 ⟶ 2,654:
Or, in terms of binomial coefficients and coordinate transformations:
<
import Data.
import Data.Ix (range)
import Data.List.Split (chunksOf)
import Data.Tuple (swap)
---------------------- PASCAL MATRIX ---------------------
pascalMatrix :: ((Int, Int) -> (Int, Int)) -> Int -> [Int]
pascalMatrix f n =
bc . f
<$> range
((0, 0), join bimap pred (n, n))
-- Binomial coefficient
bc :: (Int, Int) -> Int
bc (n, k) =
foldr
(\x a -> quot (a * succ (n - x)) x)
1
[k, pred k .. 1]
--------------------------- TEST -------------------------
matrixSize = 5 :: Int
Line 1,639 ⟶ 2,684:
mapM_
putStrLn
( unlines
(
swap, -- Upper
\(a, b) -> (a + b, b) -- Symmetric
]
<*> [matrixSize]
)
)</syntaxhighlight>
{{Out}}
<pre>Lower
Line 1,671 ⟶ 2,722:
=={{header|J}}==
<syntaxhighlight lang="j"> !/~ i. 5
1 1 1 1 1
0 1 2 3 4
Line 1,689 ⟶ 2,739:
1 3 6 10 15
1 4 10 20 35
1 5 15 35 70</
Explanation:
Line 1,699 ⟶ 2,749:
For the final example we use an unadorned <code>!</code> but prepare tables for its x and y values. Its right argument is a sum table, and its left argument is a left identity table. They look like this:
<
0 1 2 3 4
1 2 3 4 5
Line 1,710 ⟶ 2,760:
2 2 2 2 2
3 3 3 3 3
4 4 4 4 4</
The parenthesis in these last two examples are redundant - they could have been omitted without changing the result, but were left in place for emphasis.
Line 1,717 ⟶ 2,767:
Translation of [[Pascal_matrix_generation#Python|Python]] via [[Pascal_matrix_generation#D|D]]
{{works with|Java|8}}
<
import java.util.List;
import java.util.function.Function;
Line 1,761 ⟶ 2,811:
print("Symmetric:", pascalSym(5));
}
}</
<pre>Upper:
Line 1,788 ⟶ 2,838:
{{Trans|Haskell}}
===ES6===
<
'use strict';
Line 2,077 ⟶ 3,127:
// MAIN ---
return main();
})();</
{{Out}}
<pre>Lower:
Line 2,102 ⟶ 3,152:
=={{header|jq}}==
{{works with|jq|1.4}}
<
# Note: 'transpose' is defined in recent versions of jq
Line 2,124 ⟶ 3,174:
def pad(n): tostring | (n - length) * " " + .;
def row: reduce .[] as $x (""; . + ($x|pad(4)));
reduce .[] as $row (""; . + "\n\($row|row)");</
<
def pascal_upper:
. as $n
Line 2,143 ⟶ 3,193:
| reduce range(1; $n) as $i
(.; reduce range(1; $n) as $j
(.; .[$i][$j] = .[$i-1][$j] + .[$i][$j-1]) ) ;</
'''Example''':
<
| ("\nUpper:", (pascal_upper | pp),
"\nLower:", (pascal_lower | pp),
"\nSymmetric:", (pascal_symmetric | pp)
)</
{{out}}
<
Upper:
Line 2,175 ⟶ 3,225:
1 3 6 10 15
1 4 10 20 35
1 5 15 35 70</
=={{header|Julia}}==
Julia has a built-in <code>binomial</code> function to compute the binomial coefficients, and we can construct the Pascal matrices with this function using list comprehensions:
<
5×5 Array{Int64,2}:
1 1 1 1 1
Line 2,202 ⟶ 3,252:
1 4 10 20 35
1 5 15 35 70
</syntaxhighlight>
=={{header|Kotlin}}==
<
fun binomial(n: Int, k: Int): Int {
Line 2,236 ⟶ 3,286:
printMatrix("Pascal lower-triangular matrix", pascalLowerTriangular(5))
printMatrix("Pascal symmetric matrix", pascalSymmetric(5))
}</
{{out}}
Line 2,263 ⟶ 3,313:
=={{header|Lua}}==
<
local f = 1
for i = 2, n do
Line 2,309 ⟶ 3,359:
for _, form in pairs({"upper", "lower", "symmetric"}) do
show(pascalMatrix(form, 5))
end</
{{out}}
<pre>Upper:
Line 2,333 ⟶ 3,383:
=={{header|Maple}}==
<syntaxhighlight lang="maple">
PascalUT := proc(n::integer)
local M := Matrix(n,n):
Line 2,380 ⟶ 3,430:
Pascal(5);
</syntaxhighlight>
{{out}}<pre>
[1 1 1 1 1]
Line 2,414 ⟶ 3,464:
</pre>
=={{header|Mathematica}}/{{header|Wolfram Language}}==
One solution is to generate a symmetric Pascal matrix then use the built in method to
compute the upper Pascal matrix. This would be done as follows:
<
upperPascal[size_] := CholeskyDecomposition[symPascal@size]
Line 2,425 ⟶ 3,475:
Column[MapThread[
Labeled[Grid[#1@5], #2, Top] &, {{upperPascal, lowerPascal,
symPascal}, {"Upper", "Lower", "Symmetric"}}]]</
{{out}}
<pre>
Line 2,451 ⟶ 3,501:
It is also possible to directly compute a lower Pascal matrix as follows:
<
MatrixExp[
SparseArray[{Band[{2, 1}] -> Range[size - 1]}, {size, size}]]]</
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}}==
Using the function “binom” from module “math” of standard library.
<syntaxhighlight lang="nim">import math, sequtils, strutils
type SquareMatrix = seq[seq[Natural]]
func newSquareMatrix(n: Positive): SquareMatrix =
## Create a square matrix.
newSeqWith(n, newSeq[Natural](n))
func pascalUpperTriangular(n: Positive): SquareMatrix =
## Create an upper Pascal matrix.
result = newSquareMatrix(n)
for i in 0..<n:
for j in i..<n:
result[i][j] = binom(j, i)
func pascalLowerTriangular(n: Positive): SquareMatrix =
## Create a lower Pascal matrix.
result = newSquareMatrix(n)
for i in 0..<n:
for j in i..<n:
result[j][i] = binom(j, i)
func pascalSymmetric(n: Positive): SquareMatrix =
## Create a symmetric Pascal matrix.
result = newSquareMatrix(n)
for i in 0..<n:
for j in 0..<n:
result[i][j] = binom(i + j, i)
proc print(m: SquareMatrix) =
## Print a square matrix.
let matMax = max(m.mapIt(max(it)))
let length = ($matMax).len
for i in 0..m.high:
echo "| ", m[i].mapIt(($it).align(length)).join(" "), " |"
echo "Upper:"
print pascalUpperTriangular(5)
echo "\nLower:"
print pascalLowerTriangular(5)
echo "\nSymmetric:"
print pascalSymmetric(5)</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>
=={{header|PARI/GP}}==
<
Pl(n)={matpascal(n-1)}
printf("%d",Pl(5))
</syntaxhighlight>
{{out}}
<pre>
Line 2,474 ⟶ 3,707:
[1 4 6 4 1]
</pre>
<
Pu(n)={Pl(n)~}
printf("%d",Pu(5))
</syntaxhighlight>
{{out}}
<pre>
Line 2,490 ⟶ 3,723:
[0 0 0 0 1]
</pre>
<
Ps(n)={matrix(n,n,n,g,binomial(n+g-2,n-1))}
printf("%d",Ps(5))
</syntaxhighlight>
{{out}}
<pre>
Line 2,508 ⟶ 3,741:
=={{header|Pascal}}==
<
const N = 5;
Line 2,577 ⟶ 3,810:
writeln('');
readln;
end.</
{{out}}
<pre>Upper:
Line 2,601 ⟶ 3,834:
=={{header|Perl}}==
<
use warnings;
use strict;
Line 2,660 ⟶ 3,893:
pretty(lower(5, 5));
say '-' x 14;
pretty(symmetric(5, 5));</
{{out}}
<pre>1, 1, 1, 1, 1
Line 2,682 ⟶ 3,915:
=={{header|Phix}}==
{{trans|Fortran}}
<!--<syntaxhighlight lang="phix">(phixonline)-->
<span style="color: #008080;">function</span> <span style="color: #000000;">pascal_upper</span><span style="color: #0000FF;">(</span><span style="color: #004080;">integer</span> <span style="color: #000000;">n</span><span style="color: #0000FF;">)</span>
<span style="color: #004080;">sequence</span> <span style="color: #000000;">res</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">repeat</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">repeat</span><span style="color: #0000FF;">(</span><span style="color: #000000;">0</span><span style="color: #0000FF;">,</span><span style="color: #000000;">n</span><span style="color: #0000FF;">),</span><span style="color: #000000;">n</span><span style="color: #0000FF;">)</span>
<span style="color: #000000;">res</span><span style="color: #0000FF;">[</span><span style="color: #000000;">1</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">repeat</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">n</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">2</span> <span style="color: #008080;">to</span> <span style="color: #000000;">n</span> <span style="color: #008080;">do</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">j</span><span style="color: #0000FF;">=</span><span style="color: #000000;">2</span> <span style="color: #008080;">to</span> <span style="color: #000000;">i</span> <span style="color: #008080;">do</span>
<span style="color: #000000;">res</span><span style="color: #0000FF;">[</span><span style="color: #000000;">j</span><span style="color: #0000FF;">,</span><span style="color: #000000;">i</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">res</span><span style="color: #0000FF;">[</span><span style="color: #000000;">j</span><span style="color: #0000FF;">,</span><span style="color: #000000;">i</span><span style="color: #0000FF;">-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">]+</span><span style="color: #000000;">res</span><span style="color: #0000FF;">[</span><span style="color: #000000;">j</span><span style="color: #0000FF;">-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">i</span><span style="color: #0000FF;">-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">]</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #008080;">return</span> <span style="color: #000000;">res</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
<span style="color: #008080;">function</span> <span style="color: #000000;">pascal_lower</span><span style="color: #0000FF;">(</span><span style="color: #004080;">integer</span> <span style="color: #000000;">n</span><span style="color: #0000FF;">)</span>
<span style="color: #004080;">sequence</span> <span style="color: #000000;">res</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">repeat</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">repeat</span><span style="color: #0000FF;">(</span><span style="color: #000000;">0</span><span style="color: #0000FF;">,</span><span style="color: #000000;">n</span><span style="color: #0000FF;">),</span><span style="color: #000000;">n</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #000000;">n</span> <span style="color: #008080;">do</span>
<span style="color: #000000;">res</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">,</span><span style="color: #000000;">1</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">1</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">2</span> <span style="color: #008080;">to</span> <span style="color: #000000;">n</span> <span style="color: #008080;">do</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">j</span><span style="color: #0000FF;">=</span><span style="color: #000000;">2</span> <span style="color: #008080;">to</span> <span style="color: #000000;">i</span> <span style="color: #008080;">do</span>
<span style="color: #000000;">res</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">,</span><span style="color: #000000;">j</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">res</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">j</span><span style="color: #0000FF;">]+</span><span style="color: #000000;">res</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">j</span><span style="color: #0000FF;">-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">]</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #008080;">return</span> <span style="color: #000000;">res</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
<span style="color: #008080;">function</span> <span style="color: #000000;">pascal_symmetric</span><span style="color: #0000FF;">(</span><span style="color: #004080;">integer</span> <span style="color: #000000;">n</span><span style="color: #0000FF;">)</span>
<span style="color: #004080;">sequence</span> <span style="color: #000000;">res</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">repeat</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">repeat</span><span style="color: #0000FF;">(</span><span style="color: #000000;">0</span><span style="color: #0000FF;">,</span><span style="color: #000000;">n</span><span style="color: #0000FF;">),</span><span style="color: #000000;">n</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #000000;">n</span> <span style="color: #008080;">do</span>
<span style="color: #000000;">res</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">,</span><span style="color: #000000;">1</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">1</span>
<span style="color: #000000;">res</span><span style="color: #0000FF;">[</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">i</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">1</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">2</span> <span style="color: #008080;">to</span> <span style="color: #000000;">n</span> <span style="color: #008080;">do</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">j</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">2</span> <span style="color: #008080;">to</span> <span style="color: #000000;">n</span> <span style="color: #008080;">do</span>
<span style="color: #000000;">res</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">,</span><span style="color: #000000;">j</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">res</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">j</span><span style="color: #0000FF;">]+</span><span style="color: #000000;">res</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">,</span><span style="color: #000000;">j</span><span style="color: #0000FF;">-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">]</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #008080;">return</span> <span style="color: #000000;">res</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
<span style="color: #7060A8;">ppOpt</span><span style="color: #0000FF;">({</span><span style="color: #004600;">pp_Nest</span><span style="color: #0000FF;">,</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #004600;">pp_IntCh</span><span style="color: #0000FF;">,</span><span style="color: #004600;">false</span><span style="color: #0000FF;">,</span><span style="color: #004600;">pp_IntFmt</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"%2d"</span><span style="color: #0000FF;">})</span>
<span style="color: #7060A8;">puts</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"=== Pascal upper matrix ===\n"</span><span style="color: #0000FF;">)</span>
<span style="color: #7060A8;">pp</span><span style="color: #0000FF;">(</span><span style="color: #000000;">pascal_upper</span><span style="color: #0000FF;">(</span><span style="color: #000000;">5</span><span style="color: #0000FF;">))</span>
<span style="color: #7060A8;">puts</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"=== Pascal lower matrix ===\n"</span><span style="color: #0000FF;">)</span>
<span style="color: #7060A8;">pp</span><span style="color: #0000FF;">(</span><span style="color: #000000;">pascal_lower</span><span style="color: #0000FF;">(</span><span style="color: #000000;">5</span><span style="color: #0000FF;">))</span>
<span style="color: #7060A8;">puts</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"=== Pascal symmetrical matrix ===\n"</span><span style="color: #0000FF;">)</span>
<span style="color: #7060A8;">pp</span><span style="color: #0000FF;">(</span><span style="color: #000000;">pascal_symmetric</span><span style="color: #0000FF;">(</span><span style="color: #000000;">5</span><span style="color: #0000FF;">))</span>
<!--</syntaxhighlight>-->
{{out}}
<pre>
Line 2,750 ⟶ 3,985:
=={{header|PicoLisp}}==
<
Low '(A B)
Upp '(B A)
Line 2,779 ⟶ 4,014:
(pascal 4 Low)
(pascal 4 Upp)
(pascal 4 Sym)</
{{out}}
<pre> 1 0 0 0 0
Line 2,800 ⟶ 4,035:
=={{header|PL/I}}==
Version I
<syntaxhighlight lang="pli">PASCAL_MATRIX: PROCEDURE OPTIONS (MAIN); /* derived from Fortran version 18 Decenber 2021 */
pascal_lower: procedure(a);
declare a(*,*) fixed binary;
declare (n, i, j) fixed binary;
n = hbound(a,1);
a = 0;
a(*, 1) = 1;
do i = 2 to n;
do j = 2 to i;
a(i, j) = a(i - 1, j) + a(i - 1, j - 1);
end;
end;
end pascal_lower;
pascal_upper: procedure(a);
declare a(*,*) fixed binary;
declare (n, i, j) fixed binary;
n = hbound(a,1);
a = 0;
a(1, *) = 1;
do i = 2 to n;
do j = 2 to i;
a(j, i) = a(j, i - 1) + a(j - 1, i - 1);
end;
end;
end pascal_upper;
pascal_symmetric: procedure(a);
declare a(*,*) fixed binary;
declare (n, i, j) fixed binary;
n = hbound(a,1);
a = 0;
a(*, 1) = 1;
a(1, *) = 1;
do i = 2 to n;
do j = 2 to n;
a(i, j) = a(i - 1, j) + a(i, j - 1);
end;
end;
end pascal_symmetric;
declare n fixed binary;
put ('Size of matrix?');
get (n);
begin;
declare a(n, n) fixed binary;
put skip list ('Lower Pascal Matrix');
call pascal_lower(a);
put edit (a) (skip, (n) f(3) );
put skip list ('Upper Pascal Matrix');
call pascal_upper(a);
put edit (a) (skip, (n) f(3) );
put skip list ('Symmetric Pascal Matrix');
call pascal_symmetric(a);
put edit (a) (skip, (n) f(3) );
end;
end PASCAL_MATRIX;
</syntaxhighlight>
{{out}}
<pre>
Size of matrix?
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
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
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>
Version II
<syntaxhighlight lang="pli">
PASCAL_MATRIX: PROCEDURE OPTIONS (MAIN); /* derived from Fortran version 18 Decenber 2021 */
define structure 1 array, 2 b(5,5) fixed binary;
declare A type (array);
pascal_lower: procedure() returns (type(array));
declare A type (array);
declare (n, i, j) fixed binary;
n = hbound(A.b,1);
A.b = 0;
A.b(*, 1) = 1;
do i = 2 to n;
do j = 2 to i;
A.b(i, j) = A.b(i - 1, j) + A.b(i - 1, j - 1);
end;
end;
return (A);
end pascal_lower;
pascal_upper: procedure() returns (type(array));
declare A type (array);
declare (n, i, j) fixed binary;
n = hbound(A.b,1);
A.b = 0;
A.b(1, *) = 1;
do i = 2 to n;
do j = 2 to i;
A.b(j, i) = A.b(j, i - 1) + A.b(j - 1, i - 1);
end;
end;
return (A);
end pascal_upper;
pascal_symmetric: procedure() returns (type(array));
declare A type (array);
declare (n, i, j) fixed binary;
n = hbound(A.b,1);
A.b = 0;
A.b(*, 1) = 1;
A.b(1, *) = 1;
do i = 2 to n;
do j = 2 to n;
A.b(i, j) = A.b(i - 1, j) + A.b(i, j - 1);
end;
end;
return (A);
end pascal_symmetric;
declare C type (array);
declare n fixed binary initial ((hbound(C.b,1)));
put skip list ('Lower Pascal Matrix');
C = pascal_lower();
put edit (C.b) (skip, (n) f(3) );
put skip list ('Upper Pascal Matrix');
C = pascal_upper();
put edit (C.b) (skip, (n) f(3) );
put skip list ('Symmetric Pascal Matrix');
C = pascal_symmetric();
put edit (C.b) (skip, (n) f(3) );
end PASCAL_MATRIX;
</syntaxhighlight>
{{trans|Rexx}}
<
pat: Proc Options(main);
Dcl (HBOUND,MAX,RIGHT) Builtin;
Line 2,887 ⟶ 4,277:
End;
End;</
{{out}}
<pre>Pascal upper triangular matrix
Line 2,911 ⟶ 4,301:
=={{header|PureBasic}}==
<
Define.i x=5, I, J
Line 2,964 ⟶ 4,354:
Input()
End</
{{out}}
<pre>Upper:
Line 2,992 ⟶ 4,382:
===Python: Procedural===
'''Summing adjacent values''':
<
def pascal_upp(n):
Line 3,021 ⟶ 4,411:
pp(pascal_low(n))
print("\nSymmetric:")
pp(pascal_sym(n))</
{{out}}
Line 3,048 ⟶ 4,438:
'''Using a binomial coefficient generator function''':
<
result = 1
for i in range(1, k+1):
Line 3,061 ⟶ 4,451:
def pascal_sym(n):
return [[binomialCoeff(i+j, i) for j in range(n)] for i in range(n)]</
{{out}}
Line 3,068 ⟶ 4,458:
===Python: Functional===
Defining binomial coefficients in terms of reduce:
<
from functools import reduce
Line 3,203 ⟶ 4,593:
# MAIN ---
if __name__ == '__main__':
main()</
{{Out}}
<pre>Symmetric:
Line 3,227 ⟶ 4,617:
=={{header|R}}==
<
a <- matrix(0, n, n)
a[, 1] <- 1
Line 3,263 ⟶ 4,653:
}
a
}</
The results follow
<
[,1] [,2] [,3] [,4] [,5]
[1,] 1 0 0 0 0
Line 3,294 ⟶ 4,684:
[3,] 1 3 6 10 15
[4,] 1 4 10 20 35
[5,] 1 5 15 35 70</
=={{header|Racket}}==
<syntaxhighlight lang="racket">#lang racket
(require math/number-theory)
Line 3,322 ⟶ 4,711:
(printf "Upper:~%~a~%" (matrix->string (pascal-upper-matrix 5)))
(printf "Lower:~%~a~%" (matrix->string (pascal-lower-matrix 5)))
(printf "Symmetric:~%~a~%" (matrix->string (pascal-symmetric-matrix 5)))</
{{out}}
<pre>Upper:
Line 3,351 ⟶ 4,740:
{{Works with|rakudo|2016-12}}
Here is a rather more general solution than required. The <tt>grow-matrix</tt> function will grow any N by N matrix into an N+1 x N+1 matrix, using any function of the three leftward/upward neighbors, here labelled "West", "North", and "Northwest". We then define three iterator functions that can grow Pascal matrices, and use those iterators to define three constants, each of which is an infinite sequence of ever-larger Pascal matrices. Normal subscripting then pulls out the ones of the specified size.
<syntaxhighlight lang="raku"
sub grow-matrix(@matrix, &func) {
my $n = @matrix.shape eq '*' ?? 1 !! @matrix.shape[0];
Line 3,387 ⟶ 4,776:
}
say '';
}</
{{out}}
<pre> 1 1 1 1 1
Line 3,410 ⟶ 4,799:
===separate generation===
Commentary: <sup>1</sup>/<sub>3</sub> of the REXX program deals with the displaying of the matrix.
<
numeric digits 50 /*be able to calculate huge factorials.*/
parse arg N . /*obtain the optional matrix size (N).*/
Line 3,444 ⟶ 4,833:
say $ || left(',', r\==s)left("]", r==s) /*show row. */
end /*r*/
return</
{{out|output|text= when using the default input:}}
<pre>
Line 3,471 ⟶ 4,860:
===consolidated generation===
This REXX version uses a consolidated generation subroutine, even though this Rosetta Code implies to use '''functions''' (instead of a single function).
<
numeric digits 50 /*be able to calculate huge factorials.*/
parse arg N . /*obtain the optional matrix size (N).*/
Line 3,506 ⟶ 4,895:
say $ || left(',', r\==s)left(']', r==s) /*show row. */
end /*r*/
return</
{{out|output|text= is identical to the 1<sup>st</sup> REXX version.}} <br><br>
=={{header|Ring}}==
<
# Project : Pascal matrix generation
Line 3,584 ⟶ 4,973:
see nl
next
</syntaxhighlight>
Output:
<pre>
Line 3,607 ⟶ 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>
=={{header|Ruby}}==
'''Summing adjacent values:'''
<
require 'pp'
Line 3,620 ⟶ 5,046:
pp ng; puts
g.each{|i| g.each{|j| ng[i][j] = (i==0 or j==0) ? 1 : ng[i-1][j ]+ng[i][j-1]}}
pp ng</
{{out}}
<pre>
Line 3,643 ⟶ 5,069:
===Binomial coefficient:===
<
def binomial_coeff(n,k) (1..k).inject(1){|res,i| res * (n-i+1) / i} end
Line 3,658 ⟶ 5,084:
puts "\nPascal symmetric matrix:"
pp pascal_symmetric(5)</
{{out}}
Line 3,685 ⟶ 5,111:
=={{header|Scala}}==
<
object pascal{
Line 3,749 ⟶ 5,175:
}
}
</syntaxhighlight>
=={{header|Scheme}}==
Using SRFI-25:
<syntaxhighlight lang="scheme">(import (srfi 25))
(define-syntax dotimes
(syntax-rules ()
((_ (i n) body ...)
(do ((i 0 (+ i 1)))
((>= i n))
body ...))))
(define (pascal-upper n)
(let ((p (make-array (shape 0 n 0 n) 0)))
(dotimes (i n)
(array-set! p 0 i 1))
(dotimes (i (- n 1))
(dotimes (j (- n 1))
(array-set! p (+ 1 i) (+ 1 j)
(+ (array-ref p i j)
(array-ref p (+ 1 i) j)))))
p))
(define (pascal-lower n)
(let ((p (make-array (shape 0 n 0 n) 0)))
(dotimes (i n)
(array-set! p i 0 1))
(dotimes (i (- n 1))
(dotimes (j (- n 1))
(array-set! p (+ 1 i) (+ 1 j)
(+ (array-ref p i j)
(array-ref p i (+ 1 j))))))
p))
(define (pascal-symmetric n)
(let ((p (make-array (shape 0 n 0 n) 0)))
(dotimes (i n)
(array-set! p i 0 1)
(array-set! p 0 i 1))
(dotimes (i (- n 1))
(dotimes (j (- n 1))
(array-set! p (+ 1 i) (+ 1 j)
(+ (array-ref p (+ 1 i) j)
(array-ref p i (+ 1 j))))))
p))
(define (print-array a)
(let ((r (array-end a 0))
(c (array-end a 1)))
(dotimes (row (- r 1))
(dotimes (col (- c 1))
(display (array-ref a row col))
(display #\space))
(newline))))</syntaxhighlight>
<syntaxhighlight lang="scheme">(print-array (pascal-upper 6))</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
</pre>
<syntaxhighlight lang="scheme">(print-array (pascal-lower 6))</syntaxhighlight>
{{out}}
<pre>
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
</pre>
<syntaxhighlight lang="scheme">(print-array (pascal-symmetric 6))</syntaxhighlight>
{{out}}
<pre>
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|Sidef}}==
{{trans|
<
var m = matrix
var s = m.len
Line 3,781 ⟶ 5,291:
functions.map { |f|
f(4).map { .map{ '%2s' % _ }.join(' ') }.join("\n")
}.join("\n\n").say</
{{out}}
<pre>
Line 3,804 ⟶ 5,314:
=={{header|Stata}}==
Here are variants for the lower matrix.
<
function pascal1(n) {
return(comb(J(1,n,0::n-1),J(n,1,0..n-1)))
Line 3,834 ⟶ 5,343:
return(s)
}
end</
One could trivially write functions for the upper matrix (same operations with transposed matrices).
Line 3,840 ⟶ 5,349:
immediately deduced:
<
: a
1 2 3 4 5
Line 3,870 ⟶ 5,379:
4 | 1 4 10 20 |
5 | 1 5 15 35 70 |
+--------------------------+</
The last is a symmetric matrix, but Stata only shows the lower triangular part of symmetric matrices.
=={{header|Tcl}}==
<syntaxhighlight lang="tcl">
package require math
Line 3,909 ⟶ 5,418:
pascal::$type 5
}
</syntaxhighlight>
{{out}}
<pre>
Line 3,936 ⟶ 5,445:
=={{header|VBA}}==
{{trans|Phix}}
<
Private Function pascal_upper(n As Integer)
Dim res As Variant: ReDim res(n, n)
Line 3,984 ⟶ 5,493:
Debug.Print "=== Pascal symmetrical matrix ==="
pp pascal_symmetric(5)
End Sub</
<pre>=== Pascal upper matrix ===
1 1 1 1 1
Line 4,005 ⟶ 5,514:
=={{header|VBScript}}==
<syntaxhighlight lang="vb">
Function pascal_upper(i,j)
WScript.StdOut.Write "Pascal Upper"
Line 4,071 ⟶ 5,580:
Call pascal_lower(0,4)
Call pascal_symmetric(0,4)
</syntaxhighlight>
{{Out}}
Line 4,095 ⟶ 5,604:
1 4 10 20 35
1 5 15 35 70
</pre>
=={{header|Wren}}==
{{libheader|Wren-fmt}}
{{libheader|Wren-math}}
{{libheader|Wren-matrix}}
<syntaxhighlight lang="wren">import "./fmt" for Fmt
import "./math" for Int
import "./matrix" for Matrix
var binomial = Fn.new { |n, k|
if (n == k) return 1
var prod = 1
var i = n - k + 1
while (i <= n) {
prod = prod * i
i = i + 1
}
return prod / Int.factorial(k)
}
var pascalUpperTriangular = Fn.new { |n|
var m = List.filled(n, null)
for (i in 0...n) {
m[i] = List.filled(n, 0)
for (j in 0...n) m[i][j] = binomial.call(j, i)
}
return Matrix.new(m)
}
var pascalSymmetric = Fn.new { |n|
var m = List.filled(n, null)
for (i in 0...n) {
m[i] = List.filled(n, 0)
for (j in 0...n) m[i][j] = binomial.call(i+j, i)
}
return Matrix.new(m)
}
var pascalLowerTriangular = Fn.new { |n| pascalSymmetric.call(n).cholesky() }
var n = 5
System.print("Pascal upper-triangular matrix:")
Fmt.mprint(pascalUpperTriangular.call(n), 2, 0)
System.print("\nPascal lower-triangular matrix:")
Fmt.mprint(pascalLowerTriangular.call(n), 2, 0)
System.print("\nPascal symmetric matrix:")
Fmt.mprint(pascalSymmetric.call(n), 2, 0)</syntaxhighlight>
{{out}}
<pre>
Pascal upper-triangular 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|
Pascal lower-triangular 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|
Pascal symmetric 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|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>
=={{header|zkl}}==
{{trans|Python}}
<
fcn pascal_upp(n){ [[(i,j); n; n; '{ binomial(j,i) }]]:toMatrix(_) } // [[..]] is list comprehension
fcn pascal_low(n){ [[(i,j); n; n; binomial]]:toMatrix(_) }
Line 4,106 ⟶ 5,770:
cols:=ns.len().toFloat().sqrt().toInt();
ns.pump(List,T(Void.Read,cols-1),List.create)
}</
<
fmt:=("%3d "*m.len() + "\n").fmt;
m.pump(String,'wrap(col){ fmt(col.xplode()) });
Line 4,114 ⟶ 5,778:
println("Upper:\n", pascal_upp(N):prettyPrint(_));
println("Lower:\n", pascal_low(N):prettyPrint(_));
println("Symmetric:\n",pascal_sym(N):prettyPrint(_));</
{{out}}
<pre>
|