next up previous
Next: libsimsort Up: libsim Previous: libsimswedish


	  libsimmath - additonal mathematical utilities

	  The  following  operations  are  offering   facilities   for
	  mathematical	realted	 problems,  some often used functions,
	  conveninet summation of arrays, and some support for	random
	  number generation.

	     external real procedure Sigma2;
	     long real array A(1:1000);
	     long real Variance,Mean,Sum;
	     integer N,ix;
	       N:=init;	N:=min(N,1000);
	     for ix:=1 step 1 until N do
	     Outtext( Variance,Mean,Sum,N"); outimage;"
	     OutReal(Sum,5,10);OutReal(N,5,10);	outimage;

	  Various people at QZ and FOA,	Stockholm.

	     integer procedure ILog(X);
	     long real X;
	  Useful   when	  editing   numbers   into   texts.    Returns
	  Log10(Abs(X))+1, i.e.	 integer digits	in X (excluding	sign),
	  if Abs(X)>=1,	0 if X=0 and number of leading zero  decimals,
	  if Abs(X)<1

	     integer procedure ILog2i (x);
	     long real x;
	  Returns ln(x)/ln(2), power of	2 for x, rounded to integer.

	     integer procedure ISum(IA,N);
	     integer array IA;
	     integer N;	! Size of array	IA (1:N) ;
	  Return Sum of	integer	array IA (1:N).

	     long real procedure RSum(RA,N);
	     real array	RA;
	     integer N;	! Size of array	RA (1:N) ;
	  Return Sum of	real array RA (1:N).

	     long real procedure LSum(LA,N);
	     long real array LA;
	     integer N;	! Size of array	LA (1:N) ;
	  Return Sum of	long real array	LA (1:N).

	     real procedure Sigma2(Mean,Sum,RA,N);
	     name Mean,Sum; ! -	output parameters;
	     real Mean;	! - mean value (Sigma2 returns Variance);
	     real Sum;	! - sum	of elements RA(1:N) ;
	     real array	RA; integer N; ! input paramters, RA(1:N) ;
	  SIGMA2 calculates the	variance, mean value and sum of	a real
	  array	RA, from RA(1) to RA(N).

	     procedure SigMean(Sigma2,Mean,K,XK1);
	     name Sigma2,Mean,K; ! In/Out parameters ;
	     long real Sigma2; ! In: previous variance,	Out: updated
	     taking Xk1	into account;
	     long real Mean;   ! In: previous mean,	Out: updated
	     taking Xk1	into account;
	     integer K;	       ! In: Number of observations, Out:
	     real XK1;	       ! Input:	The new	observation of X,
	     observation K+1;
	  Calculates  Mean-value  and  Variance	 for   a   statistical
	  material  in	an  iterative fashion. Given mean and variance
	  for k	operations and	the  new  observation  k+1,  mean  and
	  variance is updated.	Example	of use:	    begin
		external procedure SigMean;
		long real SigmaAccum,MeanAccum;integer Obs; !  Updated
	  by SigMean;
		real X;	! the just found observation. ;
		long real StandDev; ! Calculated when all observations
		Obs:= 0; mean:=	0; sigma2:= 0; ! Initialization	;
		inimage; ! One value per input line;
		while enfile do
		   X:=inreal; !	The new	observation, X(Obs+1) ;
		      !	Updates	SigmaAccum,MeanAccum and Obs;
		end while;
		StandDev:= Sqrt(SigmaAccum*Obs/(Obs-1));
		Outtext(  Mean,	 Variance,  Observations:");
		Outreal(MeanAccum,5,20); Outreal(SigmaAccum,5,20);
		Outreal(StandDev,5,20);	Outint(Obs,10);	Outimage;
	  Note:	compared to Sigma2, SigMean may	lose  some  precision.
	  The  advantage of using SigMean over Sigma2 is that only one
	  observation is need at the time rather than all observations
	  (possibly many) needed by Sigma2.

	     procedure Scramble(IntArray,Bottom,Top,U);
	     name U; ! Integer variable	to be updated;
	     integer array IntArray; integer
	     Bottom,Top;!IntArray(Bottom:Top) shuffled;
	     integer U;	! Random seed, as for other random drawing
	     procedures. Init to odd no.;
	  Scramble performs a Random permutation of  elements  in  the
	  integer array	IntArray in the	interval (Bottom:Top).

     class PerGen
	     class PerGen(A,N);
	     integer array A; integer N; ! Array to permute A(1:N);
	  Class	PerGen generates all the possible permutations of  the
	  contents  in	integer	 array	a(1:N),	one at a time.	PerGen
	  objects works	as iterators, 'Call' the  object  to  generate
	  next	permutation  in	a.  The	permutations will be generated
	  in such manner that the last elements	of A will change  most
	  slowly.  Example:  begin
	     external class PerGen;
	     integer array a(1:3);
	     ref (PerGen) pg;
	     a(1):=1; a(2):=2; a(3):=3;
	     pg:- new PerGen(a,n);
	     while pg.Cycles < 2 do ! consider first cycle only	;
		  outint(a(1),3);   outint(a(2),3);    outint(a(3),3);
		Call(pg);  ! generate new permutation;
	  end -	Result (one cycle of 6 permutations):
	  1  2	3 /  2	1  3 /	3  1  2	/  1  3	 2 /  2	 3  1 /	 3   2

	  Note	that permutations  are	generated  systematically  and
	  that	the  order  will  be  REVERSED	when  one  N! cycle is
	  completed.  Thus for N = 2 then result will be  (if  A(1)=1,
	  A(2)=2  initially):  (1 2) (2	1) / (2	1) (1 2) / (1 2) (2 1)
	  etc. (2 permutations per cycle).

	     integer procedure Cycles;
	  Return the current permutation cycle	number,	 1,2,...   The
	  number   is	increased  after  N!  permutations  have  been
	  generated (including the inital one).	Cycle is  returning  2
	  when	A  contains  the first permutation of the second cycle
	  (which is equal to the last permutation of the first	cycle,
	  since	 the  order  of	 permutations  are  reversed with each