
PROCEDURE kendl1(data1,data2: glnarray; n: integer; VAR tau,z,prob: real);
(* Programs using routine KENDL1 must define type
TYPE
   glnarray = ARRAY [1..n] OF real;
in the calling routine *)
VAR
   n2,n1,k,j,is: integer;
   svar,aa,a2,a1: real;
BEGIN
   n1 := 0;
   n2 := 0;
   is := 0;
   FOR j := 1 to n-1 DO BEGIN
      FOR k := j+1 to n DO BEGIN
         a1 := data1[j]-data1[k];
         a2 := data2[j]-data2[k];
         aa := a1*a2;
         IF (aa <> 0.0) THEN BEGIN
            n1 := n1+1;
            n2 := n2+1;
            IF (aa > 0.0) THEN BEGIN
               is := is+1
            END ELSE BEGIN
               is := is-1
            END
         END ELSE BEGIN
            IF (a1 <> 0.0) THEN n1 := n1+1;
            IF (a2 <> 0.0) THEN n2 := n2+1
         END
      END
   END;
   tau := is/(sqrt(n1)*sqrt(n2));
   svar := (4.0*n+10.0)/(9.0*n*(n-1.0));
   z := tau/sqrt(svar);
   prob := erfcc(abs(z)/1.4142136)
END;
PROCEDURE kendl2(tab: gldarray; i,j,ip,jp: integer; VAR tau,z,prob: real);
(* Programs using routine KENDL2 must define type
TYPE
   gldarray = ARRAY [1..ip,1..jp] OF real;
in the calling program, where ip and jp are physical array dimensions
large enough to encompass the logical dimensions i and j.   *)
VAR
   nn,mm,m2,m1,lj,li,l,kj,ki,k: integer;
   svar,s,points,pairs,en2,en1: real;
BEGIN
   en1 := 0.0;
   en2 := 0.0;
   s := 0.0;
   nn := i*j;
   points := tab[i,j];
   FOR k := 0 to nn-2 DO BEGIN
      ki := k DIV j;
      kj := k-j*ki;
      points := points+tab[ki+1,kj+1];
      FOR l := k+1 to nn-1 DO BEGIN
         li := l DIV j;
         lj := l-j*li;
         m1 := li-ki;
         m2 := lj-kj;
         mm := m1*m2;
         pairs := tab[ki+1,kj+1]*tab[li+1,lj+1];
         IF (mm <> 0) THEN BEGIN
            en1 := en1+pairs;
            en2 := en2+pairs;
            IF (mm > 0) THEN BEGIN
               s := s+pairs
            END ELSE BEGIN
               s := s-pairs
            END
         END ELSE BEGIN
            IF (m1 <> 0) THEN en1 := en1+pairs;
            IF (m2 <> 0) THEN en2 := en2+pairs
         END
      END
   END;
   tau := s/sqrt(en1*en2);
   svar := (4.0*points+10.0)/(9.0*points*(points-1.0));
   z := tau/sqrt(svar);
   prob := erfcc(abs(z)/1.4142136)
END;
PROCEDURE ksone(VAR data: gldarray; n: integer; VAR d,prob: real);
(* Programs using routine KSONE must define the type
TYPE
   gldarray = ARRAY [1..n] OF real;
in the main routine. *)
VAR
   j: integer;
   fo,fn,ff,en,dt: real;
BEGIN
   sort(n,data);
   en := n;
   d := 0.0;
   fo := 0.0;
   FOR j := 1 to n DO BEGIN
      fn := j/en;
      ff := func(data[j]);
      IF (abs(fo-ff) > abs(fn-ff)) THEN BEGIN
         dt := abs(fo-ff)
      END ELSE BEGIN
         dt := abs(fn-ff)
      END;
      IF (dt > d) THEN d := dt;
      fo := fn
   END;
   prob := probks(sqrt(en)*d)
END;
PROCEDURE kstwo(VAR data1: glarray1; n1: integer;
      VAR data2: glarray2; n2: integer;
      VAR d,prob: real);
(* Programs using routine KSTWO must define the types
TYPE
   glarray1 = ARRAY [1..n1] OF real;
   glarray2 = ARRAY [1..n2] OF real;
in the main routine.   *)
VAR
   i,j2,j1: integer;
   dum,en1,en2,fo2,fo1,fn2,fn1,dt: real;
BEGIN
   IF (n2 > n1) THEN BEGIN
      writeln('pause in routine KSTWO');
      writeln('first input array must be the larger'); readln
   END;
   sort(n1,data1);
   FOR i := 1 to n2 DO BEGIN
      dum := data1[i];
      data1[i] := data2[i];
      data2[i] := dum
   END;
   sort(n2,data1);
   FOR i := 1 to n2 DO BEGIN
      dum := data1[i];
      data1[i] := data2[i];
      data2[i] := dum
   END;
   en1 := n1;
   en2 := n2;
   j1 := 1;
   j2 := 1;
   fo1 := 0.0;
   fo2 := 0.0;
   d := 0.0;
   WHILE ((j1 <= n1) AND (j2 <= n2)) DO BEGIN
      IF (data1[j1] < data2[j2]) THEN BEGIN
         fn1 := j1/en1;
         IF (abs(fn1-fo2) > abs(fo1-fo2)) THEN BEGIN
            dt := abs(fn1-fo2)
         END ELSE BEGIN
            dt := abs(fo1-fo2)
         END;
         IF (dt > d) THEN d := dt;
         fo1 := fn1;
         j1 := j1+1;
      END ELSE BEGIN
         fn2 := j2/en2;
         IF (abs(fn2-fo1) > abs(fo2-fo1)) THEN BEGIN
            dt := abs(fn2-fo1)
         END ELSE BEGIN
            dt := abs(fo2-fo1)
         END;
         IF (dt > d) THEN d := dt;
         fo2 := fn2;
         j2 := j2+1
      END
   END;
   prob := probks(sqrt(en1*en2/(en1+en2))*d)
END;
