RosettaCodeData/Task/Determinant-and-permanent/XPL0/determinant-and-permanent.xpl0
2025-02-27 18:35:13 -05:00

48 lines
1,010 B
Text

func DetPerm(Det, A, N); \Return value of determinant or permanent of A, order N
int Det, A, N;
int B Sum, Term;
int I, K, L;
[if N = 1 then return A(0, 0);
B:= Reserve((N-1)*4);
Sum:= 0;
for I:= 0 to N-1 do
[L:= 0;
for K:= 0 to N-1 do
if K # I then
[B(L):= @A(K, 1); L:= L+1];
Term:= A(I, 0) * DetPerm(Det, B, N-1);
if Det & I&1 then Term:= -Term;
Sum:= Sum + Term;
];
return Sum;
];
int Arrays, I;
[Arrays:= [
[ [1, 2],
[3, 4] ],
[ [-2, 2, -3],
[-1, 1, 3],
[ 2, 0, -1] ],
[ [ 1, 2, 3, 4],
[ 4, 5, 6, 7],
[ 7, 8, 9, 10],
[10, 11, 12, 13] ],
[ [ 0, 1, 2, 3, 4],
[ 5, 6, 7, 8, 9],
[10, 11, 12, 13, 14],
[15, 16, 17, 18, 19],
[20, 21, 22, 23, 24] ]
];
for I:= 0 to 3 do
[Text(0, "Determinant: ");
IntOut(0, DetPerm(true, Arrays(I), I+2));
CrLf(0);
Text(0, "Permanent : ");
IntOut(0, DetPerm(false, Arrays(I), I+2));
CrLf(0); CrLf(0);
];
]