
CLOUT("Determinant computations ....").

PROCEDURE delelem(V,i);
(*Delete element in list. V is a list. The i-th
element of V is deleted. 0 <= i <= length(V). *)
VAR   U, VP, v, j: LIST;
BEGIN
(*1*) IF i <= 0  THEN RETURN(V) END;
      IF V = NIL THEN RETURN(V) END;
(*2*) VP:=V; j:=0; U:=NIL;
      REPEAT j:=j+1;
             IF VP = NIL THEN RETURN(V) END;
             ADV(VP,v,VP); U:=COMP(v,U);
             UNTIL j = i;
(*3*) U:=RED(U); U:=INV(U); U:=CONC(U,VP);
      RETURN(U);
(*9*) END delelem.


PROCEDURE delcolumn(M,i);
(*Delete column in matrix. M is a matrix (i.e a list
of row lists). In each row the i-th element is deleted. *)
VAR   N, MP, V: LIST;
BEGIN
(*1*) IF i <= 0  THEN RETURN(M) END;
      IF M = NIL THEN RETURN(M) END;
(*2*) MP:=M; N:=NIL;
      WHILE MP # NIL DO ADV(MP,V,MP);
            V:=delelem(V,i); N:=COMP(V,N);
            END;
(*3*) N:=INV(N); RETURN(N);
(*9*) END delcolumn.


PROCEDURE det(M);
(*Determinant. M is a matrix (i.e a list
of row lists). The determinant of M is computed. *)
VAR   i, d, dp, s, N, MP, V, VP, v: LIST;
BEGIN
(*1*) d:=0;
      IF M = NIL THEN RETURN(d) END;
(*2*) ADV(M,V,MP);
      IF MP = NIL THEN RETURN(FIRST(V)) END;
(*3*) s:=1; i:=0;
      WHILE V # NIL DO ADV(V,v,V); i:=i+1;
            IF v # 0 THEN
               N:=delcolumn(MP,i); dp:=det(N);
               dp:=IPROD(v,dp);
               IF s < 0 THEN dp:=INEG(dp) END;
               d:=ISUM(d,dp);
               END;
            s:=-s;
            END;
(*4*) RETURN(d);
(*9*) END det.

PROCEDURE eh(i,j);
IF i = j THEN RETURN(1) ELSE RETURN(0) END eh.

PROCEDURE ut(i,j);
IF i <= j THEN RETURN(j) ELSE RETURN(0) END ut.

PROCEDURE lt(i,j);
IF i >= j THEN RETURN(j) ELSE RETURN(0) END lt.


PROCEDURE mat(f,k);
VAR   i, j, V, M: LIST;
BEGIN
(*1*) M:=NIL;
      IF k <= 0 THEN RETURN(M) END;
(*2*) i:=0;
      WHILE i < k DO i:=i+1; j:=0; V:=NIL;
            WHILE j < k DO j:=j+1;
                  V:=COMP(f(i,j),V) END;
            V:=INV(V); M:=COMP(V,M);
            END;
(*4*) M:=INV(M); RETURN(M);
(*9*) END mat.

A:=mat(ut,3).
d:=det(A).

A:=mat(lt,5).
d:=det(A).

CLOUT("... finished.").
