DERF[function]

PURPOSE
The DERF function evaluates the integral (SQRT(4/)exp(-x^2)dx) from zero to some X.


TYPE REQUIREMENTS

TYPE
FLOAT = EXTENDED OR DOUBLE OR REAL;




It is highly recomended that the extended precision variables be used for this function.  Great pains were taken to enable this function to be accurate.  The accuracy can only be realized if the extended precision numbers are used.  The function uses references taken from National Bureau of Standards accurate to 15 digits.  These references dictated the accuracy of the function. 


CALLING PROCEDURE

VAR
YOUT:FLOAT;{Output of the function}
XIN:FLOAT;{Function input}

YOUT:=DERF(XIN);


EXAMPLE

The DERF function is related to the area under the Standard Normal Curve.  The example shows this relationship and prints the area under the standard normal curve for - to x where x = 0 to 4.

REFERENCES

Tables of the Error Function and It's Derivative, U. S. Department of Commerce, National Bureau of Standards, Washington, 1954.

James, M. L., Smith, G. M., Wolford, J. C.:  Applied Numerical Methods for Digital Computation With Fortran and CSMP, Harper and Row, New York, 1977. 

PROGRAM TEST_DERF;
TYPE
FLOAT = REAL;
FIXXED = INTEGER;
VAR
AREA, XIN : FLOAT;
IX : FIXXED;
PROCEDURE see;
VAR
R : Rect;
BEGIN
HideAll;
SetRect(R, 0, 35, 550, 330);
SettextRect(R);
Showtext;
END;
FUNCTION DERF (XIN : FLOAT) : FLOAT;
VAR
H, SUM, X, XSTART, XEND, TEMP : FLOAT;
F : ARRAY[0..6] OF FLOAT;
V : ARRAY[0..13] OF FLOAT;
I, J, K, MULT, N : FIXXED;
BEGIN
V[0] := 0.0;
V[1] := 0.520499877813047;
V[2] := 0.842700792949715;
V[3] := 0.966105146475311;
V[4] := 0.995322265018953;
V[5] := 0.999593047982555;
V[6] := 0.999977909503001;
V[7] := 0.999999256901628;
V[8] := 0.999999984582742;
V[9] := 1.0 - 1.966160e-10;
V[10] := 1.0 - 1.5374598e-12;
V[11] := 1.0 - 7.3578479e-15;
V[12] := 1.0 - 2.1519737e-17;
V[13] := 1.0 - 3.8421483e-20;
IF (XIN < 0.0) THEN
BEGIN
MULT := -1;
XIN := MULT * XIN;
END
ELSE
MULT := 1;
J := ROUND(2.0 * XIN);
TEMP := 1.0;
IF (J < 13) THEN
BEGIN
N := 3;
XSTART := J * 0.5;
XEND := XIN;
H := (XEND - XSTART) / (N * 6.0);
SUM := 0.0;
X := XSTART;
FOR I := 1 TO N DO
BEGIN
FOR K := 0 TO 5 DO
BEGIN
F[K] := EXP(-X * X);
X := X + H;
END;
F[6] := EXP(-X * X);
SUM := SUM + (41 * F[0] + 216 * F[1] + 27 * F[2]);
SUM := SUM + (272 * F[3] + 27 * F[4] + 216 * F[5] + 41 * F[6]);
END;
TEMP := (SUM * H / 140.0) * SQRT(1.0 / ARCTAN(1.0)) + V[J];
END;
IF (TEMP > 1.0) THEN
TEMP := 1.0;
DERF := TEMP * MULT;
END;
BEGIN
see;
WRITELN('        X              NORMAL DISTRIBUTION');
FOR IX := 0 TO 20 DO
BEGIN
XIN := IX / 10;
AREA := 0.5 * DERF(XIN / sqrt(2.0)) + 0.5;
WRITELN(XIN : 10 : 5, AREA : 30 : 15);
END;
END.



MULTI_DIFEQ  [procedure]



PURPOSE
 
MULTI_DIFEQ solves a system of ordinary differential equations using the 
fourth order Runge-Kutta Method.  This system can be composed of several 
first or higher order equations.  It is necessary to define a procedure 
VALUE which describes the system of equations. When given the value of 
the independent variable and the values for all of the dependent variables 
except one, VALUE must determine the value of the remaining dependent 
variable.  Value must perform this function for all of the dependent 
variables.  The procedure VALUE must accept the dependent variable, an 
array of the values of the independent variables and their derivatives 
along with a delta value for the independent variable.  MULTI_DIFEQ 
requires the passage of the dependent variable, a delta value for that 
variable, the array of values for dependent variables and their derivatives, the number of equations,  and the number of iterations to be performed before returning to the calling procedure.  On completion, 
MULTI_DIFEQ passes back the updated dependent and independent variables along with the derivatives for the dependent variables. The dependent variable will equal the input dependent variable plus the delta dependent variable times the number of iterations. Second or higher order equations can be solved by breaking the equations into a set of first order equations.


TYPE REQUIREMENTS

Type
FLOAT = REAL or DOUBLE or EXTENDED;
DIM= ARRAY [1..NUM_OF_EQUATIONS] of FLOAT;
FIXXED = INTEGER or LONGINT;


CALLING PROCEDURE

var
TIME:FLOAT;{Independent variable}
DT:FLOAT;{Delta TIME};
CURRENT:DIM;{Array of dependent variables}
DIDT:DIM;{Array of derivatives of dependent variables}
NUMEQUA:FIXXED;{Total number of equations}
ITERATE:FIXXED;{Number of times MULTI_DIFEQ will iterate before returning}

Define the procedure VALUE.  The calling sequence to value is 

VALUE(TIME,CURRENT,DIDT,DT);.

Define NUMEQUA, DT, and ITERATE.  Define the initial values for TIME, 
CURRENT and DIDT.  Then call the procedure.

MULTI_DIFEQ(TIME,DT,CURRENT,DIDT,NUMEQUA,ITERATE);

When the procerdure finishes TIME, CURRENT, and DIDT will have been 
updated.


EXAMPLE

The example solves the system of equations

                              dX/dt + Y = 3e2t
                              dY/dt + X = 0
                              X(0) = 2, Y(0) =0

from t= 0.0 to t=0.05.  The analytical solution to this set of equations is

                       X = -exp(t/2) + exp(-t/2) + 2exp(2t)
                       Y = exp(t/2) + exp(-t/2) -exp(2t).

This problem and it's solution were taken from page 408 and 426 of the 
second reference.


REFERENCES
James, M. L., Smith, G. M., Wolford, J. C.:  Applied Numerical Methods for 
Digital Computation With Fortran and CSMP, Harper and Row, New York, 
1977.

Ross, S. L.: Introduction to Ordinary Differential Equations 2nd edition, 
Xerox College Publishing, Lexington 1974.

McCormic, J. M., Salvadori M. G.: Numerical Methods in Fortran, Prentice-
Hall, Englewood Cliffs, 1965.


FIND_DYDX[function]


PURPOSE 

FIND_DYDX will calculate the derivative of a function defined by the user supplied function F.  FIND_DYDX can use forward, backward, or central difference formulas with errors on the order of dx4.  The derivatives are calculated using five values of the function and the Newton-Gregory interpolating formulas.  The five independent variables used in the calculation vary from the point of interest by integer multiples of dx.

TYPE REQUIREMENTS

TYPE
FLOAT = REAL or DOUBLE or EXTENDED;
FIXXED = INTEGER or LONGINT;

CALLING PROCEDURE

var
X:FLOAT;{Point for which the derivative is desired} 
DX:FLOAT;{Delta x used in calculating the  derivative}
DIR:FIXXED;{Integer value which determines what type of difference formula to use in the calculation.  1- forward, 0- central, -1 - backward}

Define the function F of type FLOAT with input XIN also of type FLOAT.
Define XIN, DX and DIR.  Call FIND_DYDX.

YPRIME:=FIND_DYDX(XIN,DX,DIR);

EXAMPLE

The example reads in a value for X and DIR, calculates and prints d(sin(x))/dx and cos(x).  This will continue until a value of x = 0 is read.




REFERENCES

James, M. L., Smith, G. M., Wolford, J. C.:  Applied Numerical Methods for Digital Computation With Fortran and CSMP, Harper and Row, New York, 1977. 

PROGRAM TEST_FIND_DYDX;
TYPE
FLOAT = REAL;
FIXXED = INTEGER;
VAR
X, Y, DX : FLOAT;
DIR : FIXXED;
PROCEDURE see;
VAR
R : Rect;
BEGIN
HideAll;
SetRect(R, 0, 35, 550, 330);
SettextRect(R);
Showtext;
END;
FUNCTION F (x : FLOAT) : FLOAT;
BEGIN
F := SIN(X);
END;
FUNCTION FIND_DYDX (X, H : FLOAT;
I : FIXXED) : FLOAT;
VAR
DYDX : FLOAT;
BEGIN
IF (I >= 1) THEN
BEGIN
DYDX := -3.0 * F(X + 4 * H) + 16.0 * F(X + 3 * H);
DYDX := DYDX + (-36.0 * F(X + 2 * H) + 48.0 * F(X + H) - 25.0 * F(X));
FIND_DYDX := DYDX / (12.0 * H);
END
ELSE IF (I <= 1) THEN
BEGIN
DYDX := +3.0 * F(X - 4 * H) - 16.0 * F(X - 3 * H);
DYDX := DYDX + (+36.0 * F(X - 2 * H) - 48.0 * F(X - H) + 25.0 * F(X));
FIND_DYDX := DYDX / (12.0 * H);
END
ELSE
BEGIN
DYDX := (-F(X + 2 * H) + 8.0 * F(X + H) - 8.0 * F(X - H) + F(X - 2 * H));
FIND_DYDX := DYDX / (12.0 * H);
END;
END;
BEGIN
see;
DX := 0.01;
DIR := 1;
REPEAT
WRITELN(' FINDING THE DERIVATIVE OF SIN(X)');
WRITELN(' ENTER X AND DIRECTION (-1,0,1)');
readln(x, DIR);
Y := FIND_DYDX(X, DX, DIR);
WRITELN(X : 10 : 5, '    CALCULATED=  ', Y : 13 : 10, '       ACTUAL=  ', COS(X) : 13 : 10);
UNTIL (X = 0.0);
END.



As a registered owned of MACMATHPASCAL you will receive a printed and bound copy of the documentation for all of the routines.  In addition you will receive $5 for every copy of MACMATHPASCAL which you give to your friends and they become  registered owners.  You will not receive the $5 unless you are registered.  The registration fee is $25.



GAUSS_SEIDEL[procedure]



PURPOSE 

GAUSS_SEIDEL is designed to solve a large system of linear equations using the Gauss-Seidel iteration method.  The Gauss-Seidel method assumes values for  all of the variables.  It then solves the first of the linear equations for the first variable, x1, in terms of the other variables.  In this manner a new value for x1 is determined. Using this new x1 the second equation is used to determine the new value for the second variable.  This is repeated for all of the equations.  The whole process is repeated for  a predetermined number of iterations or until the  change in the variables is smaller than some predetermined value.

The Gauss-Seidel method will not work for all systems of equations.  In general, the method works best when the diagonal elements of the matrix are much larger than the other elements of the matrix.  To be specific, when the absolute value of the diagonal element is larger than the sum of the absolute values of all of the other elements convergence is assured.  Such a matrix is known as diagonal.  A diagonal system is sufficient to ensure convergence, but is not necessary.

For a large diagonal matrix the Gauss-Seidel method is perferred over matrix inversion methods.  Assuming a good first guess for the values of X, the method can be faster.  The accuracy of the solution increases with the
number of iterations.  Round-off errors do not play an important role in the solution.  In fact, the Gauss-Seidel method is often used to increase the accuracy of solutions determined by matrix inversion methods.  The second reference listed below offers an excellent discussion of Gauss-Seidel accuracy and speed.
 

TYPE REQUIREMENTS

TYPE
 FLOAT = EXTENDED or DOUBLE or REAL;
 DATA = ARRAY[1..MAXSIZE] OF FLOAT;
 SQUARE = ARRAY[1..MAXSIZE OF ARRAY,1..MAXSIZE OF ARRAY] OF FLOAT;
 FIXXED = INTEGER or LONGINT;


CALLING PROCEDURE

VAR
X:DATA;{The initial guess of the  solution of the system}
Y:DATA;{The right side of the system}
COEF:SQUARE;{The left side of the system}
N:FIXXED{Number of equations in the  system}
ITMAX:FIXXED;{Maximum number of iterations}
ERRMAX:FLOAT;{If the square root of the sum of the squares of the change in the x values is less than ERRMAX  the routine terminates}

On return, ITMAX is the number of iterations completed and ERRMAX is the square root of the sum of the squares of the last change in the x values.

Define X, Y, COEF, N, ITMAX and ERRMAX.  Call GAUSS_SIEDEL

GAUSS_SIEDEL(COEF,X,Y,N,ITMAX,ERRMAX);


EXAMPLE

The example program solves the system of twelve equations defined by
i:=0;
for j:=1 to 12
  begin
    Y[j]:=j*j;
    for k:=1 to 12
      begin
       i:=i+1;
       coef[j,k]:=i
       if(k=j) then
         coef[j,k]:=coef[j,k]*5000
      end
end;.

The starting X is one, ITMAX = 30, and ERRMAX = 0.0001.


REFERENCES
James, M. L., Smith, G. M., Wolford, J. C.:  Applied Numerical Methods for Digital Computation With Fortran and CSMP, Harper and Row, New York, 1977.

Isaacson, E., Keller, H. B.:  Analysis of Numerical Methods, John Wiley & Sons, New York, 1966. 

PROGRAM TEST_GAUSS_SEIDEL;
CONST
MAXSIZE = 12;
TYPE
FLOAT = REAL;
DATA = ARRAY[1..MAXSIZE] OF FLOAT;
SQUARE = ARRAY[1..MAXSIZE, 1..MAXSIZE] OF FLOAT;
FIXXED = INTEGER;
VAR
COEF : SQUARE;
X, Y : DATA;
N, ITMAX, K, J, I : FIXXED;
ERRMAX : FLOAT;
PROCEDURE see;
VAR
R : Rect;
BEGIN
HideAll;
SetRect(R, 0, 35, 550, 330);
SettextRect(R);
Showtext;
END;
PROCEDURE GAUSS_SEIDEL (COEF : SQUARE;
VAR X : DATA;
Y : DATA;
N : FIXXED;
VAR ITMAX : FIXXED;
VAR ERRMAX : FLOAT);
VAR
I, L, J, K : FIXXED;
ERROR, XOLD, SUM : FLOAT;
BEGIN
I := 1;
REPEAT
{WRITELN(I);}
L := I;
ERROR := 0.0;
FOR J := 1 TO N DO
BEGIN
XOLD := X[J];
SUM := 0.0;
FOR K := 1 TO N DO
IF (K <> J) THEN
SUM := SUM + X[K] * COEF[J, K];
X[J] := (Y[J] - SUM) / COEF[J, J];
ERROR := ERROR + SQR(XOLD - X[J]);
END;
{WRITELN(I : 5, SQRT(ERROR) : 20 : 10);}
I := I + 1;
UNTIL ((SQRT(ERROR) <= ERRMAX) OR (I > ITMAX));
ITMAX := L;
ERRMAX := SQRT(ERROR);
END;
BEGIN
see;
I := 0;
N := MAXSIZE;
WRITELN(' SETTING UP A SYSTEM OF ', N : 3, ' LINEAR EQUATIONS');
ITMAX := 30;
ERRMAX := 0.0001;
FOR J := 1 TO N DO
BEGIN
X[J] := 1.0;
Y[J] := J * J;
FOR K := 1 TO N DO
BEGIN
I := I + 1;
COEF[J, K] := I;
IF (K = J) THEN
COEF[J, K] := COEF[J, K] * 5000;
END;
END;
WRITELN(' SOLVING THESE EQUATIONS');
GAUSS_SEIDEL(COEF, X, Y, N, ITMAX, ERRMAX);
WRITELN(' THE SOLUTION IS-');
FOR K := 1 TO N DO
WRITELN(X[K] : 20 : 10);
WRITELN;
WRITELN(' MAXIMUM ERROR ', ERRMAX : 20 : 10, '  WITH ', ITMAX : 3, ' ITERATIONS');
END.


INVERT[procedure]

PURPOSE 

INVERT caclulates the inverse of a matrix in place using Gauss-Jordan reduction with partial pivoting.  INVERT assumes the inverse of the matrix does exist.  


TYPE REQUIREMENTS
 const
  MAXSIZE = MAXIMUM SIZE OF THE ARRAY;

TYPE
 FLOAT = EXTENDED or DOUBLE or REAL;
 DATA = ARRAY[1..MAXSIZE OF ARRAY] OF FLOAT;
 SQUARE = ARRAY[1..MAXSIZE OF ARRAY,1..MAXSIZE OF ARRAY] OF FLOAT;
 FIXXED = INTEGER or LONGINT;


CALLING PROCEDURE

VAR
MATRIX:SQUARE;{Matrix to be inverted}
SIZE:FIXXED;{Number of elements in a 
 row or column}
Define MATRIX and SIZE and call INVERT

INVERT(MATRIX,SIZE);

On return the inverted matrix is stored in MATRIX.


EXAMPLE

The example program reads in a three by three matrix from the keyboard, inverts it, and then prints it to the screen.


REFERENCES

James, M. L., Smith, G. M., Wolford, J. C.:  Applied Numerical Methods for Digital Computation With Fortran and CSMP, Harper and Row, New York, 1977.


PROGRAM TEST_INVERT;
CONST
MAXSIZE = 10;
TYPE
FLOAT = REAL;
FIXXED = INTEGER;
SQUARE = ARRAY[1..MAXSIZE, 1..MAXSIZE] OF FLOAT;
VAR
MATRIX : SQUARE;
SIZE, KIN : FIXXED;
PROCEDURE see;
VAR
R : Rect;
BEGIN
HideAll;
SetRect(R, 0, 35, 550, 330);
SettextRect(R);
Showtext;
END;
PROCEDURE INVERT (VAR MATRIX : SQUARE;
SIZE : FIXXED);
VAR
SWITCH : ARRAY[1..MAXSIZE, 1..2] OF FIXXED;
K, JJ, KP1, I, J, L, KROW, IROW : FIXXED;
PIVOT, TEMP : FLOAT;
BEGIN
FOR K := 1 TO SIZE DO
BEGIN
JJ := K;
IF (K <> SIZE) THEN
BEGIN
KP1 := K + 1;
PIVOT := ABS(MATRIX[K, K]);
FOR I := KP1 TO SIZE DO
BEGIN
TEMP := ABS(MATRIX[I, K]);
IF (PIVOT < TEMP) THEN
BEGIN
PIVOT := TEMP;
JJ := I;
END;
END;
END;
SWITCH[K, 1] := K;
SWITCH[K, 2] := JJ;
IF (JJ <> K) THEN
FOR J := 1 TO SIZE DO
BEGIN
TEMP := MATRIX[JJ, J];
MATRIX[JJ, J] := MATRIX[K, J];
MATRIX[K, J] := TEMP;
END;
FOR J := 1 TO SIZE DO
IF (J <> K) THEN
MATRIX[K, J] := MATRIX[K, J] / MATRIX[K, K];
MATRIX[K, K] := 1.0 / MATRIX[K, K];
FOR I := 1 TO SIZE DO
IF (I <> K) THEN
FOR J := 1 TO SIZE DO
IF (J <> K) THEN
MATRIX[I, J] := MATRIX[I, J] - MATRIX[K, J] * MATRIX[I, K];
FOR I := 1 TO SIZE DO
IF (I <> K) THEN
MATRIX[I, K] := -MATRIX[I, K] * MATRIX[K, K];
END;
FOR L := 1 TO SIZE DO
BEGIN
K := SIZE - L + 1;
KROW := SWITCH[K, 1];
IROW := SWITCH[K, 2];
IF (KROW <> IROW) THEN
FOR I := 1 TO SIZE DO
BEGIN
TEMP := MATRIX[I, KROW];
MATRIX[I, KROW] := MATRIX[I, IROW];
MATRIX[I, IROW] := TEMP;
END;
END;
END;
BEGIN
see;
SIZE := 3;
FOR KIN := 1 TO 3 DO
BEGIN
WRITELN(' INPUT ROW # ', KIN : 2, ' OF A 3 BY 3 MATRIX');
READLN(MATRIX[KIN, 1], MATRIX[KIN, 2], MATRIX[KIN, 3]);
END;
INVERT(MATRIX, SIZE);
WRITELN(' INVERTED MATRIX');
FOR KIN := 1 TO 3 DO
WRITELN(MATRIX[KIN, 1] : 20 : 10, MATRIX[KIN, 2] : 20 : 10, MATRIX[KIN, 3] : 20 : 10);
END.

REFERENCES
James, M. L., Smith, G. M., Wolford, J. C.:  Applied Numerical Methods for Digital Computation With Fortran and CSMP, Harper and Row, New York, 1977. 


LEAST_SQUARE[procedure]


PURPOSE 

LEAST_SQUARE calculates the least square fit of data to the curve of the form y = ax^n for n = 0 to NMAX. NMAX is less than the number of data  points.


TYPE REQUIREMENTS
const
MAXPOWER = the maximum power for the polynomial
MAXSIZE = A MAXIMUM SIZE FOR DATA ARRAY MUST BE GREATER THEN MAXPOWER

TYPE
FLOAT = REAL or DOUBLE or EXTENDED;
DATA = ARRAY[1..MAX NUMBER OF POINTS] of Float;
RAY = ARRAY [1..NMAX+1] OF FLOAT;
FIXXED = INTEGER of LONGINT;


CALLING PROCEDURE

var

X:DATA;{Array containing the independent variable}
Y:DATA;{Array containing the dependent variable}
NDP:FIXXED;{The number of data points}
COEF:RAY;{The output least square parameter of the above equation} 
POWER:FLOAT;{NMAX in the above equation}


Define X, Y, NDP, POWER.  Then call

LEAST_SQUARE(COEF,X,Y,NDP,POWER);.

On return COEF contains the coefients of the polynomial least square fit.




EXAMPLE

The example program reads in NDP, POWER, X, and Y from the keyboard and prints COEF.


REFERENCES

James, M. L., Smith, G. M., Wolford, J. C.:  Applied Numerical Methods for Digital Computation With Fortran and CSMP, Harper and Row, New York, 1977.
  LEAST_SQUARE.PAS  LEAST_SQUARE.PAS  18  PROGRAM TEST_LEAST_SQUARE (INPUT, OUTPUT);
CONST
MAXSIZE = 10;
maxpower = 9;
TYPE
FLOAT = extended;
FIXXED = INTEGER;
SQUARE = ARRAY[1..MAXSIZE, 1..MAXSIZE] OF FLOAT;
RAY = ARRAY[1..20] OF FLOAT;
DATA = ARRAY[1..100] OF FLOAT;
VAR
X, Y : DATA;
COEF : RAY;
INPUTS, POWER, NDP : FIXXED;
PROCEDURE see;
VAR
R : Rect;
BEGIN
HideAll;
SetRect(R, 0, 35, 550, 330);
SettextRect(R);
Showtext;
END;
PROCEDURE LEAST_SQUARE (VAR COEF : RAY;
X, Y : DATA;
NDP, POWER : FIXXED);
VAR
I, J, K, NCOEF : FIXXED;
A : SQUARE;
LEFT : RAY;
f : ARRAY[1..maxsize, 0..maxpower] OF float;
PROCEDURE INVERT (VAR MATRIX : SQUARE;
SIZE : FIXXED);
VAR
SWITCH : ARRAY[1..maxsize, 1..2] OF FIXXED;
K, JJ, KP1, I, J, L, KROW, IROW : FIXXED;
PIVOT, TEMP : FLOAT;
BEGIN
FOR K := 1 TO SIZE DO
BEGIN
JJ := K;
IF (K <> SIZE) THEN
BEGIN
KP1 := K + 1;
PIVOT := ABS(MATRIX[K, K]);
FOR I := KP1 TO SIZE DO
BEGIN
TEMP := ABS(MATRIX[I, K]);
IF (PIVOT < TEMP) THEN
BEGIN
PIVOT := TEMP;
JJ := I;
END;
END;
END;
SWITCH[K, 1] := K;
SWITCH[K, 2] := JJ;
IF (JJ <> K) THEN
FOR J := 1 TO SIZE DO
BEGIN
TEMP := MATRIX[JJ, J];
MATRIX[JJ, J] := MATRIX[K, J];
MATRIX[K, J] := TEMP;
END;
FOR J := 1 TO SIZE DO
IF (J <> K) THEN
MATRIX[K, J] := MATRIX[K, J] / MATRIX[K, K];
MATRIX[K, K] := 1.0 / MATRIX[K, K];
FOR I := 1 TO SIZE DO
IF (I <> K) THEN
FOR J := 1 TO SIZE DO
IF (J <> K) THEN
MATRIX[I, J] := MATRIX[I, J] - MATRIX[K, J] * MATRIX[I, K];
FOR I := 1 TO SIZE DO
IF (I <> K) THEN
MATRIX[I, K] := -MATRIX[I, K] * MATRIX[K, K];
END;
FOR L := 1 TO SIZE DO
BEGIN
K := SIZE - L + 1;
KROW := SWITCH[K, 1];
IROW := SWITCH[K, 2];
IF (KROW <> IROW) THEN
FOR I := 1 TO SIZE DO
BEGIN
TEMP := MATRIX[I, KROW];
MATRIX[I, KROW] := MATRIX[I, IROW];
MATRIX[I, IROW] := TEMP;
END;
END;
END;
BEGIN
{define f}
FOR k := 1 TO ndp DO
BEGIN
f[k, 0] := 1.0;
IF (x[k] <> 0.0) THEN
BEGIN
FOR i := 1 TO power DO
f[k, i] := f[k, i - 1] * x[k];
END
ELSE
BEGIN
FOR i := 1 TO power DO
f[k, i] := 0.0;
END;
END;
NCOEF := POWER + 1;
FOR I := 1 TO NCOEF DO
FOR J := 1 TO NCOEF DO
BEGIN
A[I, J] := 0.0;
FOR K := 1 TO NDP DO
A[I, J] := A[I, J] + F[K, I - 1] * F[K, J - 1];
END;
INVERT(A, NCOEF);
FOR I := 1 TO NCOEF DO
BEGIN
LEFT[I] := 0.0;
FOR K := 1 TO NDP DO
LEFT[I] := LEFT[I] + F[K, I - 1] * Y[K];
END;
FOR I := 1 TO NCOEF DO
BEGIN
COEF[I] := 0.0;
FOR K := 1 TO NCOEF DO
COEF[I] := COEF[I] + A[I, K] * LEFT[K];
END;
END;
BEGIN
see;
WRITELN(' HOW MANY DATA POINTS ?');
READLN(NDP);
WRITELN(' WHAT ORDER OF FIT ? ( MUST BE LESS THEN ', NDP, ')');
READLN(POWER);
FOR INPUTS := 1 TO NDP DO
BEGIN
WRITELN(' ENTER X AND Y');
READLN(X[INPUTS], Y[INPUTS]);
END;
LEAST_SQUARE(COEF, X, Y, NDP, POWER);
WRITELN('THE LEAST SQUARE FIT IS - Y =');
FOR INPUTS := 1 TO (POWER + 1) DO
IF (INPUTS = 1) THEN
WRITELN(' ', COEF[INPUTS] : 20 : 10)
ELSE
WRITELN('+', COEF[INPUTS] : 20 : 10, ' * X  TO THE ', (INPUTS - 1) : 3);
END. LEAST_SQUARE.PAS     DRE (INPUT  FREE    Free Object        DEL (COEF : SQUARE;
         var X : DATA;
         Y : DATA;
         N : FIXXED;
         var ITMAX : FIXXED;
         var ERRMAX : FLOAT);
  var
   I, L, J, K : FIXXED;
   ERROR, XOLD, SUM : FLOAT;
 begin
  I := 1;
  repeat
   WRITELN(I);
   L := I;
   ERROR := 0.0;
   for J := 1 to N do
    begin
     XOLD := X[J];
     SUM := 0.0;
     for K := 1 to N do
      if (K <> J) then
       SUM := SUM + X[K] * COEF[J, K];
     X[J] := (Y[J] - SUM) / COEF[J, J];
     ERROR := ERROR + SQR(XOLD - X[J]);
    end;
   WRITELN(I : 5, SQRT(ERROR) : 20 : 10);
   I := I + 1;
  until ((SQRT(ERROR) <= ERRMAX) or (I > ITMAX));
  ITMAX := L;
  ERRMAX := SQRT(ERROR);
 end;
begin
 I := 0;
 N := MAXSIZE;
 ITMAX := 30;
 ERRMAX := 0.0001;
 for J := 1 to N do
  begin
   X[J] := 1.0;
   Y[J] := J * J;
   for K := 1 to N do
    begin
     I := I + 1;
     COEF[J, K] := I;
     if (K = J) then
      COEF[J, K] := COEF[J, K] * 5000;
    end;
  end;
 GAUSS_SEIDEL(COEF, X, Y, N, ITMAX, ERRMAX);
 for K := 1 to N do
  WRITELN(X[K] : 20 : 10);
 WRITELN;
 WRITELN(ERRMAX : 20 : 10, ITMAX : 10);
end.



THIS ROUTINE CALCULATES A STANDARD LOAN AMORTIZATION.  THIS PROGRAM WAS INCLUDED SO YOU COULD FIGURE HOW TO PAY FOR YOUR MAC II.  TO QUIT THE PROGRAM ENTER A PRINCIPAL OF 0.0 WHEN ASKED. 

NEW_mac (INPUT, OUTPUT);
TYPE
FLOAT = EXTENDED;
VAR
P, N, R, TEMP, I, Y, YEARS, PR, LOST : FLOAT;
YEARINT : ARRAY[0..40] OF FLOAT;
J, K, MONTH, NYEAR : INTEGER;
TABLE : BOOLEAN;
TF : CHAR;
MAC : TEXT;
DATA : STRING;
PROCEDURE see;
VAR
R : Rect;
BEGIN
HideAll;
SetRect(R, 0, 35, 550, 330);
SettextRect(R);
Showtext;
END;
PROCEDURE EXAMP;
VAR
DUM : CHAR;
BEGIN
TEXTSIZE(9);
WRITELN('THIS EXAMPLE PROGRAM DEMONSTRATES SOME OF CAPABILITIES');
WRITELN('THE MACMATHPASCAL PACKAGE FROM COHERET COGNITION');
WRITELN('THE MACMATHPASCAL PACKAGE IS A COLLECTION OF SCIENTIFIC AND');
WRITELN('ENGINEERING SUBROUTINES TO BE INCORPORATED INTO YOUR PASCAL PROGRAMS');
WRITELN;
WRITELN('INCLUDED IN THE PACKAGE IS A ROUTINE WHICH CALCULATES A STANDARD');
WRITELN('LOAN AMORIZATION.  THIS PROGRAM WAS INCLUDED SO YOU COULD FIGURE');
WRITELN('HOW TO PAY FOR YOUR MAC II.');
WRITELN('TO QUIT ENTER PRINCIPAL=0.0  APPLE HAS STARTED TO GIVE THEM AWAY?');
WRITELN;
WRITELN('RETURN TO CONTIUE');
READLN;
TEXTSIZE(18);
WRITELN;
WRITELN;
WRITELN('    MACMATHPASCAL');
WRITELN('    COHERENT COGNITION');
WRITELN('    BOX 24114');
WRITELN('    SAN DIEGO, CA  92142');
WRITELN('    619-278-4141');
TEXTSIZE(9);
WRITELN;
WRITELN;
WRITELN('RETURN TO CONTIUE');
READLN;
END;
FUNCTION x_to_Y (X, Y : FLOAT) : FLOAT;
VAR
XTEMP : FLOAT;
BEGIN
XTEMP := LN(X);
XTEMP := XTEMP * Y;
X_TO_Y := EXP(XTEMP);
END;
BEGIN
see;
EXAMP;
WRITELN(' INPUT PRINCIPAL');
READLN(P);
WHILE (P > 0.0) DO
BEGIN
WRITELN(' ANNUAL INTEREST');
READLN(I);
{N IS THE NUMBER OF PAYMENTS IN A YEAR}
N := 12.0;
WRITELN('YEARS');
READLN(Y);
Y := Y;
YEARS := Y;
TEMP := (I / 100.0) / N + 1.0;
TEMP := X_TO_Y(TEMP, (N * Y));
R := ((I / 100.0) * P / N) / (1.0 - 1.0 / TEMP);
WRITELN('THE PAYMENT ON A LOAN OF $', P : 10 : 2);
WRITELN('AT AN INTEREST RATE OF ', I : 4 : 2, '% FOR ', Y : 5 : 2, ' YEARS');
WRITELN('IS $', R : 7 : 2);
K := ROUND(12 * YEARS) - 1;
WRITELN('PAYMENT      REMAINING PRINCIPAL      TOTAL INTEREST');
FOR J := 1 TO K DO
BEGIN
Y := YEARS - J * 1.0 / 12.0;
TEMP := (I / 100.0) / N + 1.0;
TEMP := X_TO_Y(TEMP, (N * Y));
TEMP := ((I / 100.0) * 1.0 / N) / (1.0 - 1.0 / TEMP);
PR := R / TEMP;
LOST := (R * J) - (P - PR);
WRITELN(J : 5, PR : 20 : 2, LOST : 20 : 2);
IF (((J DIV 12) * 12) = J) THEN
BEGIN
WRITELN('RETURN TO CONTINUE');
READLN;
END;
END;
LOST := LOST + (R - PR);
PR := 0.0;
J := K + 1;
WRITELN(J : 5, PR : 20 : 2, LOST : 20 : 2);
WRITELN(' INPUT PRINCIPAL');
READLN(P);
END;
END.


ALAG3D, PLOT3[function]

PURPOSE 

ALAG3D performs an interpolation of the function U=U(x,y) defined on a 4 x 4  or larger grid by performing a weighted averaging of eight Lagrangian interpolations in the area of the point of interest. The X values and the Y values need not be  uniformly spaced and X need not equal Y.   ALAG3D  requires the passage of an X and Y array, the  X and Y array size, a 2d Z array and the points XIN, YIN for which the interpolation is to be done.


TYPE REQUIREMENTS

CONST
NUMX = maximum number of X data points-1;
{may be larger than XSIZE}
NUMY = maximum number of Y data points-1;
{may be larger than YSIZE}

Type
FLOAT = REAL or DOUBLE or EXTENDED;
DATAX = ARRAY [1..NUMX] of FLOAT;
DATAY = ARRAY [1..NUMY] of FLOAT; 
RECTANG = ARRAY [0..NUMX,0..NUMY] of FLOAT;
FIXXED = INTEGER or LONGINT;


CALLING PROCEDURE
var
X:DATAX;{Known X values}
Y:DATAY;{Known Y values}
Z:RECTANG;{Known Z values}
XSIZE,YSIZE FIXXED;{Size of X AND Y array}
X0,Y0,Z0:FLOAT;{Input points and output point  for the interpolation}

DEFINE X,Y and Z arrays.
Pick the points for interpolation X0, Y0.
Z0:=INTERP2D(X0,Y0,X,Y,Z,NUMX,MUNY);




EXAMPLE

The example routine defines Z as Z=sin(x)*cos(y) on a 10 X 10 grid.  X and Y range from 0 to (9/5).  The output of ALAG3D is then used to generate a
three dimensional plot of the function from X = 0 to (28/15), Y = 0 to (19/20).  The function is plotted using the procedure PLOT3.  PLOT3 will scale and plot to a point or scale and move to a point from the current pen position given the point (x,y,z)  and three Euler angles (alpha,beta,gamma). It assumes the screen limits have been defined as XMIN,XMAX,YMIN,YMAX.  To get PLOT3 to move to a point without drawing set color equal to 0.  If color is 1 then the line will be drawn.  The calling procedure for PLOT3 is 
PLOT3(x,y,z,alpha,beta,gama,color);

x, y, z, alpha, beta, and gamma are of type float. Color is of type integer.



REFERENCES

James, M. L., Smith, G. M., Wolford, J. C.:  Applied Numerical Methods for Digital Computation With Fortran and CSMP, Harper and Row, New York, 1977.

Spiegel, M. R..: Mathematical Handbook of Formulas and Tables,  McGraw-Hill, New York, 1968.


PROGRAM TEST_ALAG3D;
TYPE
FLOAT = REAL;
DATA = ARRAY[1..20] OF FLOAT;
RECTANG = ARRAY[1..20, 1..20] OF FLOAT;
FIXXED = INTEGER;
VAR
X0, Y0, Z0, XMAX, XMIN, YMAX, YMIN, A, B, C : FLOAT;
X, Y : DATA;
Z : RECTANG;
NX, NY, K, K2 : FIXXED;
PROCEDURE see;
VAR
R : Rect;
BEGIN
HideAll;
SetRect(R, 0, 35, 550, 400);
SetDRAWINGRect(R);
ShowDRAWING;
END;
FUNCTION alag3d (X0, Y0 : FLOAT;
X, Y : DATA;
Z : RECTANG;
NX, NY : FIXXED) : FLOAT;
VAR
I, J, K, L, M : FIXXED;
U1, U2, U3, U4, V1, V2, V3, V4 : FLOAT;
LA1, LA2, LA3, LA4, LA5, LA6, LA7, LA8 : FLOAT;
QUIT : BOOLEAN;
BEGIN
L := 2;
M := NX - 1;
Quit := FALSE;
REPEAT
REPEAT
I := (L + M) DIV 2;
UNTIL ((L - I) <= 0);
IF (((L - I) = 0) OR ((X[I] - X0) = 0.0)) THEN
Quit := True;
IF (NOT quit) THEN
IF ((X[I] - X0) > 0) THEN
M := I
ELSE
BEGIN
IF (X[I + 1] - X0 < 0) THEN
L := I
ELSE
Quit := True
END;
UNTIL (Quit);
Quit := FALSE;
L := 2;
M := NY - 1;
REPEAT
REPEAT
J := (L + M) DIV 2
UNTIL ((L - J) <= 0);
IF (((L - J) = 0) OR ((Y[J] - Y0) = 0.0)) THEN
Quit := True;
IF (NOT quit) THEN
IF ((Y[J] - Y0) > 0) THEN
M := J
ELSE
BEGIN
IF (Y[J + 1] - Y0 < 0) THEN
L := J
ELSE
Quit := True;
END;
UNTIL (Quit);
U1 := ((X[I - 1] - X[I]) * (X[I - 1] - X[I + 1]) * (X[I - 1] - X[I + 2]));
U1 := (X0 - X[I]) * (X0 - X[I + 1]) * (X0 - X[I + 2]) / U1;
U2 := ((X[I] - X[I - 1]) * (X[I] - X[I + 1]) * (X[I] - X[I + 2]));
U2 := (X0 - X[I - 1]) * (X0 - X[I + 1]) * (X0 - X[I + 2]) / U2;
U3 := ((X[I + 1] - X[I - 1]) * (X[I + 1] - X[I]) * (X[I + 1] - X[I + 2]));
U3 := (X0 - X[I - 1]) * (X0 - X[I]) * (X0 - X[I + 2]) / U3;
U4 := ((X[I + 2] - X[I - 1]) * (X[I + 2] - X[I]) * (X[I + 2] - X[I + 1]));
U4 := (X0 - X[I - 1]) * (X0 - X[I]) * (X0 - X[I + 1]) / U4;
V1 := ((Y[J - 1] - Y[J]) * (Y[J - 1] - Y[J + 1]) * (Y[J - 1] - Y[J + 2]));
V1 := (Y0 - Y[J]) * (Y0 - Y[J + 1]) * (Y0 - Y[J + 2]) / V1;
V2 := ((Y[J] - Y[J - 1]) * (Y[J] - Y[J + 1]) * (Y[J] - Y[J + 2]));
V2 := (Y0 - Y[J - 1]) * (Y0 - Y[J + 1]) * (Y0 - Y[J + 2]) / V2;
V3 := ((Y[J + 1] - Y[J - 1]) * (Y[J + 1] - Y[J]) * (Y[J + 1] - Y[J + 2]));
V3 := (Y0 - Y[J - 1]) * (Y0 - Y[J]) * (Y0 - Y[J + 2]) / V3;
V4 := ((Y[J + 2] - Y[J - 1]) * (Y[J + 2] - Y[J]) * (Y[J + 2] - Y[J + 1]));
V4 := (Y0 - Y[J - 1]) * (Y0 - Y[J]) * (Y0 - Y[J + 1]) / V4;
LA1 := (Z[I - 1, J - 1] * U1 + Z[I, J - 1] * U2 + Z[I + 1, J - 1] * U3 + Z[I + 2, J - 1] * U4) * V1;
LA2 := (Z[I - 1, J] * U1 + Z[I, J] * U2 + Z[I + 1, J] * U3 + Z[I + 2, J] * U4) * V2;
LA3 := (Z[I - 1, J + 1] * U1 + Z[I, J + 1] * U2 + Z[I + 1, J + 1] * U3 + Z[I + 2, J + 1] * U4) * V3;
LA4 := (Z[I - 1, J + 2] * U1 + Z[I, J + 2] * U2 + Z[I + 1, J + 2] * U3 + Z[I + 2, J + 2] * U4) * V4;
LA5 := (Z[I - 1, J - 1] * V1 + Z[I - 1, J] * V2 + Z[I - 1, J + 1] * V3 + Z[I - 1, J + 2] * V4) * U1;
LA6 := (Z[I, J - 1] * V1 + Z[I, J] * V2 + Z[I, J + 1] * V3 + Z[I, J + 2] * V4) * U2;
LA7 := (Z[I + 1, J - 1] * V1 + Z[I + 1, J] * V2 + Z[I + 1, J + 1] * V3 + Z[I + 1, J + 2] * V4) * U3;
LA8 := (Z[I + 2, J - 1] * V1 + Z[I + 2, J] * V2 + Z[I + 2, J + 1] * V3 + Z[I + 2, J + 2] * V4) * U4;
alag3d := 0.5 * (LA1 + LA2 + LA3 + LA4 + LA5 + LA6 + LA7 + LA8);
END;
PROCEDURE plot3 (x, y, z, alpha, beta, gamma : FLOAT;
color : FIXXED);
VAR
x1, x2, y1, y2, x3, y3 : FLOAT;
xint, yint : integer;
BEGIN
x1 := x * cos(gamma) - y * sin(gamma);
y1 := x * sin(gamma) + y * cos(gamma);
y2 := y1 * cos(beta) - z * sin(beta);
x3 := x1 * cos(alpha) - y2 * sin(alpha);
y3 := x1 * sin(alpha) + y2 * cos(alpha);
yint := 200 - round((y3 - ymin) * 200 / (ymax - ymin));
xint := round((x3 - xmin) * 495 / (xmax - xmin));
IF (color = 1) THEN
lineto(xint, yint)
ELSE
moveto(xint, yint);
END;
BEGIN
see;
XMAX := 6.0;
XMIN := -6.0;
YMAX := 7.0;
YMIN := -7.0;
FOR K := 1 TO 10 DO
BEGIN
X[K] := 3.1415926 * (K - 1.0) / 5.0;
FOR K2 := 1 TO 10 DO
BEGIN
Y[K2] := 3.1415926 * (K2 - 1.0) / 5.0;
Z[K, K2] := SIN(X[K]) * COS(Y[K2]);
END;
END;
A := 3.14;
B := 0.707;
C := 0.707;
FOR K := 1 TO 15 DO
BEGIN
X0 := 3.1415926 * (K - 1.0) / 7.5;
FOR K2 := 1 TO 20 DO
BEGIN
Y0 := 3.1415926 * (K2 - 1.0) / 10;
Z0 := ALAG3D(X0, Y0, X, Y, Z, 10, 10);
IF (K2 = 1) THEN
PLOT3(X0, Y0, Z0, A, B, C, 0)
ELSE
PLOT3(X0, Y0, Z0, A, B, C, 1);
END;
END;
FOR K := 1 TO 3 DO
BEGIN
Y0 := 3.1415926 * (K - 1.0) * 0.95;
FOR K2 := 1 TO 40 DO
BEGIN
X0 := 3.1415926 * (K2 - 1.0) * (28.0 / 15.0) / 39;
Z0 := ALAG3D(X0, Y0, X, Y, Z, 10, 10);
IF (K2 = 1) THEN
PLOT3(X0, Y0, Z0, A, B, C, 0)
ELSE
PLOT3(X0, Y0, Z0, A, B, C, 1);
END;
END;
END. 

REFERENCES

James, M. L., Smith, G. M., Wolford, J. C.:  Applied Numerical Methods for Digital Computation With Fortran and CSMP, Harper and Row, New York, 1977.

Spiegel, M. R..: Mathematical Handbook of Formulas and Tables,  McGraw-Hill, New York, 1968.



INTERP3D[function]



PURPOSE 

INTERP3D performs an interpolation of the function U=U(x,y,z) defined on a 5 x 5  x 5 or larger grid using the first and second derivative terms of the three dimensional Taylor series.  Derivatives are calculated using various finite-difference approximations with errors on the order of x3 andx4.  The x values, y values, and the z values must be uniformly spaced, but x need not equal y or z.   INTERP3D requires the passage of an X, Y, and Z array, the  X,Y and Z array size a 3d U array and the points XIN,YIN,ZIN for which the interpolation is to be done.


TYPE REQUIREMENTS

CONST
NUMX = maximum number of x data points-1;
{may be larger than XSIZE}
NUMY = maximum number of y data points-1;
{may be larger than YSIZE}
NUMZ = maximum number of z data points-1;
{may be larger than ZSIZE}
Type
FLOAT = REAL or DOUBLE or EXTENDED;
DATAX = ARRAY [0..NUMX] of FLOAT;
DATAY = ARRAY [0..NUMY] of FLOAT; 
DATAZ = ARRAY [0..NUMZ] of FLOAT;
CUBE = ARRAY [0..NUMX,0..NUMY,0..NUMZ] of FLOAT;
FIXXED = INTEGER or LONGINT;


CALLING PROCEDURE

var
X:DATAX;{Known X values}
Y:DATAY;{Known Y values}
Z:DATAZ;{Known Z values}



U:CUBE;{Known U=U(X,Y,Z)}
XSIZE,YSIZE,ZSIZE:FIXXED;{Size of X, Y, and Z array}
XIN,YIN,ZIN,UOUT:FLOAT;{Input points and output point  for the interpolation}

DEFINE X,Y,Z and U arrays.
Pick the points for interpolation XIN, YIN, and ZIN.
UOUT:=INTERP2D(XIN,YIN,ZIN,X,Y,Z,U,XSIZE,YSIZE,ZSIZE);


EXAMPLE

The example routine defines U as U=sin(x)*cos(y)*tan(z) on a 10 X 10  X 10 grid.  X,Y, and Z range from 0 to 1.  Then 200 XIN,YIN and ZIN variables are picked.  XIN=YIN=ZIN.  The input ranges from zero to 1. As the calculation progresses, the maximum error of the interpolation which has occured is printed to the screen along with the point at which it occurs.  At the end, the maximum error is reprinted.  


REFERENCES

James, M. L., Smith, G. M., Wolford, J. C.:  Applied Numerical Methods for Digital Computation With Fortran and CSMP, Harper and Row, New York, 1977.

Spiegel, M. R.: Mathematical Handbook of Formulas and Tables,  McGraw-Hill, New York, 1968.


PROGRAM TEST_3DINTERP;
TYPE
FLOAT = REAL;
FIXXED = INTEGER;
VAR
I, J, K : FIXXED;
XINP, YINP, ZINP, UOUT, ERR, XSAVE, MISTAKE : FLOAT;
X : ARRAY[0..10] OF FLOAT;
Y : ARRAY[0..10] OF FLOAT;
Z : ARRAY[0..10] OF FLOAT;
U : ARRAY[0..10, 0..10, 0..10] OF FLOAT;
PROCEDURE see;
VAR
R : Rect;
BEGIN
HideAll;
SetRect(R, 0, 35, 550, 330);
SettextRect(R);
Showtext;
END;
FUNCTION INTERP3D (XIN, YIN, ZIN : FLOAT;
XSIZE, YSIZE, ZSIZE : FIXXED) : FLOAT;
VAR
I, J, L, K, M : FIXXED;
DFDX, DFDY, DFDZ, DX, DY, DZ, D2FDXDX, D2FDYDY, D2FDZDZ : FLOAT;
D2UDXDY, D2UDXDZ, D2UDYDZ, FX, FXX, FY, FYY, FZ, FZZ : FLOAT;
INTER, DELX, DELY, DELZ : FLOAT;
INTERP2D : FLOAT;
DUDX, DUDY : ARRAY[1..5] OF FLOAT;
PROCEDURE F_DUDX (I, J, K : FIXXED);
BEGIN
IF (I <= 1) THEN
BEGIN
DFDX := (-3.0 * U[(I + 4), J, K] + 16.0 * U[(I + 3), J, K]);
DFDX := DFDX + (-36.0 * U[(I + 2), J, K] + 48.0 * U[(I + 1), J, K] - 25.0 * U[I, J, K]);
DFDX := DFDX / (12.0 * DX)
END
ELSE IF (I >= (XSIZE - 1)) THEN
BEGIN
DFDX := (3.0 * U[(I - 4), J, K] - 16.0 * U[(I - 3), J, K]);
DFDX := DFDX + (36.0 * U[(I - 2), J, K] - 48.0 * U[(I - 1), J, K] + 25.0 * U[I, J, K]);
DFDX := DFDX / (12.0 * DX)
END
ELSE
BEGIN
DFDX := -U[(I + 2), J, K] + 8.0 * U[(I + 1), J, K] - 8.0 * U[(I - 1), J, K] + U[(I - 2), J, K];
DFDX := DFDX / (12.0 * DX)
END
END;
PROCEDURE F_DUDY (I, J, K : FIXXED);
BEGIN
IF (J <= 1) THEN
BEGIN
DFDY := (-3.0 * U[I, (J + 4), K] + 16.0 * U[I, (J + 3), K] - 36.0 * U[I, (J + 2), K]);
DFDY := DFDY + (48.0 * U[I, (J + 1), K] - 25.0 * U[I, J, K]);
DFDY := DFDY / (12.0 * DY)
END
ELSE IF (J >= (YSIZE - 1)) THEN
BEGIN
DFDY := (3.0 * U[I, (J - 4), K] - 16.0 * U[I, (J - 3), K]);
DFDY := DFDY + (36.0 * U[I, (J - 2), K] - 48.0 * U[I, (J - 1), K] + 25.0 * U[I, J, K]);
DFDY := DFDY / (12.0 * DY)
END
ELSE
BEGIN
DFDY := (-U[I, (J + 2), K] + 8.0 * U[I, (J + 1), K] - 8.0 * U[I, (J - 1), K]);
DFDY := DFDY + U[I, (J - 2), K];
DFDY := DFDY / (12.0 * DY)
END;
END;
PROCEDURE F_DUDZ (I, J, K : FIXXED);
BEGIN
IF (K <= 1) THEN
BEGIN
DFDZ := (-3.0 * U[I, J, K + 4] + 16.0 * U[I, J, K + 3] - 36.0 * U[I, J, K + 2]);
DFDZ := (DFDZ + (48.0 * U[I, J, K + 1] - 25.0 * U[I, J, K])) / (12.0 * DY)
END
ELSE IF (K >= (ZSIZE - 1)) THEN
BEGIN
DFDZ := (3.0 * U[I, J, K - 4] - 16.0 * U[I, J, K - 3]);
DFDZ := DFDZ + (36.0 * U[I, J, K - 2] - 48.0 * U[I, J, K - 1]);
DFDZ := (DFDZ + (25.0 * U[I, J, K])) / (12.0 * DY)
END
ELSE
BEGIN
DFDZ := -U[I, J, K + 2] + 8.0 * U[I, J, K + 1] - 8.0 * U[I, J, K - 1];
DFDZ := DFDZ + U[I, J, K - 2];
DFDZ := DFDZ / (12.0 * DZ)
END
END;
PROCEDURE F_D2UDXDX (I, J, K : FIXXED);
BEGIN
IF (I <= 1) THEN
BEGIN
D2FDXDX := (70.0 * U[I, J, K] - 208.0 * U[(I + 1), J, K]);
D2FDXDX := D2FDXDX + (228.0 * U[(I + 2), J, K] - 112.0 * U[(I + 3), J, K]);
D2FDXDX := (D2FDXDX + 22.0 * U[(I + 4), J, K]) / (24.0 * DX * DX)
END
ELSE IF (I >= (XSIZE - 1)) THEN
BEGIN
D2FDXDX := -(-70.0 * U[I, J, K] + 208.0 * U[(I - 1), J, K]);
D2FDXDX := D2FDXDX - (-228.0 * U[(I - 2), J, K]);
D2FDXDX := D2FDXDX - (112.0 * U[(I - 3), J, K] - 22.0 * U[I - 4, J, K]);
D2FDXDX := D2FDXDX / (24.0 * DX * DX)
END
ELSE
BEGIN
D2FDXDX := -U[(I + 2), J, K] + 16.0 * U[(I + 1), J, K] - 30.0 * U[I, J, K];
D2FDXDX := D2FDXDX + 16.0 * U[(I - 1), J, K] - U[(I - 2), J, K];
D2FDXDX := D2FDXDX / (12.0 * SQR(DX))
END
END;
PROCEDURE F_D2UDYDY (I, J, K : FIXXED);
BEGIN
IF (J <= 1) THEN
BEGIN
D2FDYDY := (70.0 * U[I, J, K] - 208.0 * U[I, (J + 1), K]);
D2FDYDY := D2FDYDY + (228.0 * U[I, (J + 2), K] - 112.0 * U[I, (J + 3), K]);
D2FDYDY := D2FDYDY + (22.0 * U[I, (J + 4), K]);
D2FDYDY := D2FDYDY / (24.0 * DY * DY)
END
ELSE IF (J >= (YSIZE - 1)) THEN
BEGIN
D2FDYDY := -(-70.0 * U[I, J, K] + 208.0 * U[I, (J - 1), K]);
D2FDYDY := D2FDYDY - (-228.0 * U[I, (J - 2), K] + 112.0 * U[I, (J - 3), K]);
D2FDYDY := D2FDYDY - (-22.0 * U[I, (J - 4), K]);
D2FDYDY := D2FDYDY / (24.0 * DY * DY)
END
ELSE
BEGIN
D2FDYDY := -U[I, (J + 2), K] + 16.0 * U[I, (J + 1), K] - 30.0 * U[I, J, K];
D2FDYDY := D2FDYDY + 16.0 * U[I, (J - 1), K] - U[I, (J - 2), K];
D2FDYDY := D2FDYDY / (12.0 * SQR(DY))
END
END;
PROCEDURE F_D2UDZDZ (I, J, K : FIXXED);
BEGIN
IF (K <= 1) THEN
BEGIN
D2FDZDZ := (70.0 * U[I, J, K] - 208.0 * U[I, J, K + 1]);
D2FDZDZ := D2FDZDZ + (228.0 * U[I, J, K + 2] - 112.0 * U[I, J, K + 3]);
D2FDZDZ := (D2FDZDZ + (22.0 * U[I, J, K + 4])) / (24.0 * DX * DX)
END
ELSE IF (K >= (ZSIZE - 1)) THEN
BEGIN
D2FDZDZ := -(-70.0 * U[I, J, K] + 208.0 * U[I, J, K - 1]);
D2FDZDZ := D2FDZDZ - (-228.0 * U[I, J, K - 2] + 112.0 * U[I, J, K - 3]);
D2FDZDZ := (D2FDZDZ - (-22.0 * U[I, J, K - 4])) / (24.0 * DX * DX)
END
ELSE
BEGIN
D2FDZDZ := -U[I, J, K + 2] + 16.0 * U[I, J, K + 1] - 30.0 * U[I, J, K];
D2FDZDZ := D2FDZDZ + 16.0 * U[I, J, K - 1] - U[I, J, K - 2];
D2FDZDZ := D2FDZDZ / (12.0 * SQR(DZ))
END
END;
BEGIN
IF ((XIN <= X[XSIZE]) AND (XIN >= X[0])) THEN
IF ((YIN <= Y[YSIZE]) AND (YIN >= Y[0])) THEN
IF ((ZIN <= Z[ZSIZE]) AND (ZIN >= Z[0])) THEN
BEGIN
I := XSIZE;
WHILE ((XIN <= X[I]) AND (I > 0)) DO
I := I - 1;
IF (ABS(XIN - X[I + 1]) < ABS(XIN - X[I])) THEN
I := I + 1;
J := YSIZE;
WHILE ((YIN <= Y[J]) AND (J > 0)) DO
J := J - 1;
IF (ABS(YIN - Y[J + 1]) < ABS(YIN - Y[J])) THEN
J := J + 1;
K := ZSIZE;
WHILE ((ZIN <= Z[K]) AND (K > 0)) DO
K := K - 1;
IF (ABS(ZIN - Z[K + 1]) < ABS(ZIN - Z[K])) THEN
K := K + 1;
DX := X[1] - X[0];
DY := Y[1] - Y[0];
DZ := Z[1] - Z[0];
INTERP2D := 0.0;
F_DUDX(I, J, K);
F_DUDY(I, J, K);
F_DUDZ(I, J, K);
F_D2UDXDX(I, J, K);
F_D2UDYDY(I, J, K);
F_D2UDZDZ(I, J, K);
FX := DFDX;
FY := DFDY;
FZ := DFDZ;
FXX := D2FDXDX;
FYY := D2FDYDY;
FZZ := D2FDZDZ;
IF (J >= (YSIZE - 1)) THEN
BEGIN
L := 0;
FOR M := J DOWNTO J - 4 DO
BEGIN
L := L + 1;
F_DUDX(I, M, K);
DUDX[L] := DFDX;
END;
D2UDXDY := (3.0 * DUDX[5] - 16.0 * DUDX[4]);
D2UDXDY := D2UDXDY + (36.0 * DUDX[3] - 48.0 * DUDX[2] + 25.0 * DUDX[1]);
D2UDXDY := D2UDXDY / (12.0 * DY)
END
ELSE IF (J <= 1) THEN
BEGIN
L := 0;
FOR M := J TO J + 4 DO
BEGIN
L := L + 1;
F_DUDX(I, M, K);
DUDX[L] := DFDX;
END;
D2UDXDY := (-3.0 * DUDX[5] + 16.0 * DUDX[4] - 36.0 * DUDX[3]);
D2UDXDY := D2UDXDY + (48.0 * DUDX[2] - 25.0 * DUDX[1]);
D2UDXDY := D2UDXDY / (12.0 * DY)
END
ELSE
BEGIN
L := 0;
FOR M := J - 2 TO J + 2 DO
BEGIN
L := L + 1;
F_DUDX(I, M, K);
DUDX[L] := DFDX;
END;
D2UDXDY := -DUDX[5] + 8.0 * DUDX[4] - 8.0 * DUDX[2] + DUDX[1];
D2UDXDY := D2UDXDY / (12.0 * DY)
END
END;
IF (K >= (ZSIZE - 1)) THEN
BEGIN
L := 0;
FOR M := K DOWNTO K - 4 DO
BEGIN
L := L + 1;
F_DUDX(I, J, K);
DUDX[L] := DFDX;
END;
D2UDXDZ := (3.0 * DUDX[5] - 16.0 * DUDX[4]);
D2UDXDZ := D2UDXDZ + (36.0 * DUDX[3] - 48.0 * DUDX[2] + 25.0 * DUDX[1]);
D2UDXDZ := D2UDXDZ / (12.0 * DZ)
END
ELSE IF (K <= 1) THEN
BEGIN
L := 0;
FOR M := K TO K + 4 DO
BEGIN
L := L + 1;
F_DUDX(I, J, M);
DUDX[L] := DFDX
END;
D2UDXDZ := (-3.0 * DUDX[5] + 16.0 * DUDX[4] - 36.0 * DUDX[3]);
D2UDXDZ := D2UDXDZ + (48.0 * DUDX[2] - 25.0 * DUDX[1]);
D2UDXDZ := D2UDXDZ / (12.0 * DZ)
END
ELSE
BEGIN
L := 0;
FOR M := K - 2 TO K + 2 DO
BEGIN
L := L + 1;
F_DUDX(I, J, M);
DUDX[L] := DFDX;
END;
D2UDXDZ := -DUDX[5] + 8.0 * DUDX[4] - 8.0 * DUDX[2] + DUDX[1];
D2UDXDZ := D2UDXDZ / (12.0 * DZ)
END;
IF (K >= (ZSIZE - 1)) THEN
BEGIN
L := 0;
FOR M := K DOWNTO K - 4 DO
BEGIN
L := L + 1;
F_DUDY(I, J, K);
DUDY[L] := DFDY;
END;
D2UDYDZ := (3.0 * DUDY[5] - 16.0 * DUDY[4]);
D2UDYDZ := D2UDYDZ + (36.0 * DUDY[3] - 48.0 * DUDY[2] + 25.0 * DUDY[1]);
D2UDYDZ := D2UDYDZ / (12.0 * DZ)
END
ELSE IF (K <= 1) THEN
BEGIN
L := 0;
FOR M := K TO K + 4 DO
BEGIN
L := L + 1;
F_DUDY(I, J, M);
DUDY[L] := DFDY;
END;
D2UDYDZ := (-3.0 * DUDY[5] + 16.0 * DUDY[4] - 36.0 * DUDY[3]);
D2UDYDZ := D2UDYDZ + (48.0 * DUDY[2] - 25.0 * DUDY[1]);
D2UDYDZ := D2UDYDZ / (12.0 * DZ)
END
ELSE
BEGIN
L := 0;
FOR M := K - 2 TO K + 2 DO
BEGIN
L := L + 1;
F_DUDY(I, J, M);
DUDY[L] := DFDY;
END;
D2UDYDZ := -DUDY[5] + 8.0 * DUDY[4] - 8.0 * DUDY[2] + DUDY[1];
D2UDYDZ := D2UDYDZ / (12.0 * DZ);
END;
DX := X[0] - X[1];
DY := Y[0] - Y[1];
DZ := Z[0] - Z[1];
DELX := XIN - X[I];
DELY := YIN - Y[J];
DELZ := ZIN - Z[K];
INTER := U[I, J, K] + DELX * FX + DELY * FY + DELX * DELX * FXX / 2.0;
INTER := INTER + DELY * DELY * FYY / 2.0;
INTERP2D := INTER + DELX * DELY * D2UDXDY;
INTERP2D := INTERP2D + DELZ * FZ + DELZ * DELZ * FZZ / 2.0;
INTERP3D := INTERP2D + DELZ * DELX * D2UDXDZ + DELZ * DELY * D2UDYDZ;
END;
BEGIN
see;
WRITELN('GENERATING A THREE DIMENSIONAL SPACE IN WHICH TO TEST THE ');
WRITELN('INTERPOLATION ROUTINE.  THIS WILL TAKE SOME TIME');
WRITELN;
WRITELN(' THE FUNCTION BEING USED IS -');
WRITELN('F=SIN(X) * COS(Y) * SIN(Z) / COS(Z)');
FOR I := 0 TO 10 DO
BEGIN
X[I] := I;
Y[I] := I;
Z[I] := I;
FOR K := 0 TO 10 DO
FOR J := 0 TO 10 DO
U[I, J, K] := SIN(I / 10.0) * COS(J / 10.0) * SIN(K / 10.0) / COS(K / 10.0);
END;
ERR := 0.0;
FOR I := 0 TO 250 DO
BEGIN
YINP := I;
XINP := YINP / 25.0;
YINP := XINP;
ZINP := YINP;
UOUT := INTERP3D(XINP, YINP, ZINP, 10, 10, 10);
MISTAKE := ABS(SIN(XINP / 10.0) * COS(YINP / 10.0) * SIN(ZINP / 10.0) / COS(ZINP / 10.0) - UOUT);
IF (MISTAKE > ERR) THEN
BEGIN
WRITELN('X=', XINP : 10 : 5, '  Y=', YINP : 10 : 5, '  Z=', ZINP : 20 : 10);
WRITELN(' INTERPOLATION =', UOUT : 20 : 10, '  ERROR=', MISTAKE : 20 : 10);
ERR := MISTAKE;
XSAVE := YINP;
END;
END;
WRITELN('MAXIMUM ERR WAS ', ERR : 20 : 10, ' AT ', XSAVE : 20 : 10);
MISTAKE := SIN(XSAVE / 10.0) * COS(XSAVE / 10.0) * SIN(XSAVE / 10.0) / COS(XSAVE / 10.0);
WRITELN(' ANSWER SHOULD BE ', MISTAKE : 20 : 10);
END. 


AREA[function]


PURPOSE 

AREA will calculate the integral of a function defined by the user supplied function F.  AREA uses various Newton-Cotes formulas to calculate the integral by quadrature.  The particular formula used is determined by the number of integration points.  If the number of points minus one is divisible by six a formula will be used with an error on the order of dx9.  If this does not hold, then the integral will be divided into two sections. One section will contain 6N+1 points and the other will contain the rest of the function.  If one point remains the trapezoid rule will be used.  If two points remain Simpson's one third rule is applied.  Simpson's three-eighths rule is next.   It is desirable for speed and accuracy to use a number of points such that the auxiliary functions need not be used.  If possible, use (6N +1) points.


TYPE REQUIREMENTS

TYPE
FLOAT = REAL or DOUBLE or EXTENDED;
FIXXED = INTEGER or LONGINT;


CALLING PROCEDURE

var
XSTART:FLOAT;{Starting point for the integration}
XFINISH:FLOAT;{Ending point for the integration}
POINTS:FIXXED;{Number of integration points}

Define the function F of type FLOAT with input XIN also of type FLOAT.
Define XSTART, XFINISH, and POINTS.  Then call

INTEGRAL_OF_FUNCTION:=AREA(XSTART,XFINISH,POINTS);





EXAMPLE
The example calculates sin(x)dx for 0 to /2 using 3 to 100 points.  This value along with the number of integration points is printed.


REFERENCES

James, M. L., Smith, G. M., Wolford, J. C.:  Applied Numerical Methods for Digital Computation With Fortran and CSMP, Harper and Row, New York, 1977.

  15  PROGRAM TEST_AREA;
TYPE
FLOAT = REAL;
FIXXED = INTEGER;
VAR
XMIN, XMAX, YOUT : FLOAT;
N : FIXXED;
PROCEDURE see;
VAR
R : Rect;
BEGIN
HideAll;
SetRect(R, 0, 35, 550, 330);
SettextRect(R);
Showtext;
END;

FUNCTION F (X : FLOAT) : FLOAT;
BEGIN
F := SIN(X);
END;
FUNCTION AREA (XSTART, XFINISH : FLOAT;
POINTS : FIXXED) : FLOAT;
VAR
I, N, REMAIN : FIXXED;
SUM, LEFT, X, H : FLOAT;
BEGIN
N := (POINTS - 1) DIV 6;
REMAIN := POINTS - 1 - N * 6;
SUM := 0.0;
LEFT := 0.0;
H := (XFINISH - XSTART) / (POINTS - 1);
{WRITELN(H : 10 : 5);}
IF (N > 0) THEN
FOR I := 1 TO N DO
BEGIN
X := XSTART + (I - 1) * H * 6;
SUM := SUM + (41 * F(X) + 216 * F(X + H) + 27 * F(X + 2 * H));
SUM := SUM + (272 * F(X + 3 * H) + 27 * F(X + 4 * H));
SUM := SUM + (216 * F(X + 5 * H) + 41 * F(X + 6 * H));
END;
SUM := (SUM * H / 140.0);
IF (REMAIN > 0) THEN
BEGIN
X := XSTART + N * H * 6;
IF (REMAIN = 1) THEN
LEFT := (F(X) + F(XFINISH)) / 2.0;
IF (REMAIN = 2) THEN
LEFT := (F(X) + 4.0 * F(X + H) + F(XFINISH)) / 3.0;
IF (REMAIN = 3) THEN
LEFT := 3.0 * (F(X) + 3.0 * F(X + H) + 3.0 * F(X + 2 * H) + F(XFINISH)) / 8.0;
IF (REMAIN = 4) THEN
BEGIN
LEFT := (7 * F(X) + 32 * F(X + H));
LEFT := (12 * F(X + 2 * H) + 32 * F(X + 3 * H) + 7 * F(XFINISH)) + LEFT;
LEFT := LEFT * (2.0 / 45.0);
END;
IF (REMAIN = 5) THEN
BEGIN
LEFT := (19 * F(X) + 75 * F(X + H));
LEFT := (50 * F(X + 2 * H) + 50 * F(X + 3 * H)) + LEFT;
LEFT := (75 * F(X + 4 * H) + 19 * F(XFINISH)) + LEFT;
LEFT := LEFT * (5.0 / 288.0);
END;
LEFT := LEFT * H;
END;
AREA := SUM + LEFT;
END;
BEGIN
see;
XMIN := 0.0;
XMAX := ARCTAN(1.0) * 2.0;
WRITELN('  INTEGRATION OF SIN(X) FOR X = 0 TO PI');
FOR N := 3 TO 100 DO
BEGIN
YOUT := AREA(XMIN, XMAX, N);
WRITELN(' NUMBER OF POINTS ', N : 5, ' VALUE OF INTEGRATION ', YOUT : 20 : 10);
END;
END. Area.Pas         F FLOAT = REAL;
  RFREE    Free Object        AREA(XMIN, XMAX, N);
   WRITELN(N : 5, YOUT : 20 : 10);
  end;
end.  15 Area.Pas         F  LFREE    Free Object        MAXSIZE] of FLOAT;
  INTDATA = array[1..3] of FIXXED;
 var
  REAL_PART, COMPLEX_PART : FLOAT;
  D : DATA;
  NDIM, ISIGN, IFORM, IOUT, COUNT : FIXXED;
  NN : INTDATA;
 procedure FFT512 (var D : DATA;
         ISIGN, IFORM, NDIM : FIXXED;
         NN : INTDATA);
{*                         COPYWRITE COHERENT COGNITION 1985                        *}
  var
   I, I1, I1MAX, I1RNG, I2, I2MAX, I3, ICASE : FIXXED;
   IDIM, IDIV, IF2, IFMIN : FIXXED;
   IFP1, IFP2, IMAX, IMIN, INON2, IPAR, IQUOT, IREM : FIXXED;
   J, J1, J2, J2MAX, J3, J3MAX, JMAX, JMIN : FIXXED;
   K1, K2, K3, K4, KDIF, KMIN, KSTEP, L, LMAX : FIXXED;
   M, MMAX, N, NHALF, NON2P, NP0 : FIXXED;
   NP1, NP1TW, NP2, NP2HF, NPREV : FIXXED;
   NTOT, NTWO, NWORK : FIXXED;
   DIFI, DIFR : FLOAT;
   OLDSI, OLDSR, RTHLF, SI, SR, STMPI, STMPR, SUMI, SUMR : FLOAT;
   T2I, T2R, T3I, T3R, T4I, T4R, TI, TR, THETA, THETM : FLOAT;
   TWOPI, TWOWR, U1I, U1R, U2I, U2R, U3I, U3R, U4I, U4R : FLOAT;
   W2I, W2R, W3I, W3R, WI, WMINI, WMINR, WR : FLOAT;
   WSTPI, WSTPR, WT : FLOAT;
   W : DATA;
   IFACT : array[1..16] of FIXXED;
  procedure zeroit;
   var
    izero : fixxed;
  begin
   for izero := 1 to 16 do
    IFACT[izero] := 0;
   I := 0;
   I1 := 0;
   I1MAX := 0;
   I1RNG := 0;
   I2 := 0;
   I2MAX := 0;
   I3 := 0;
   ICASE := 0;
   IDIM := 0;
   IDIV := 0;
   IF2 := 0;
   IFMIN := 0;
   IFP1 := 0;
   IFP2 := 0;
   IMAX := 0;
   IMIN := 0;
   INON2 := 0;
   IPAR := 0;
   IQUOT := 0;
   IREM := 0;
   J := 0;
   J1 := 0;
   J2 := 0;
   J2MAX := 0;
   J3 := 0;
   J3MAX := 0;
   JMAX := 0;
   JMIN := 0;
   K1 := 0;
   K2 := 0;
   K3 := 0;
   K4 := 0;
   KDIF := 0;
   KMIN := 0;
   KSTEP := 0;
   L := 0;
   LMAX := 0;
   M := 0;
   MMAX := 0;
   N := 0;
   NHALF := 0;
   NON2P := 0;
   NP0 := 0;
   NP1 := 0;
   NP1TW := 0;
   NP2 := 0;
   NP2HF := 0;
   NPREV := 0;
   NTOT := 0;
   NTWO := 0;
   NWORK := 0;
  end;
  procedure Z_299;
   var
    J, IOUT : INTEGER;
    FLAG1 : BOOLEAN;
  begin
  {THE NEXT LINE WAS ADDED}
   NON2P := 0;
   N := NN[IDIM];
   NP2 := NP1 * N;
   M := N;
   NTWO := NP1;
   IF2 := 1;
   IDIV := 2;
   IQUOT := M div IDIV;
   IREM := M - IDIV * IQUOT;
   while ((IQUOT - IDIV) >= 0) and (IREM = 0) do
    begin
     NTWO := NTWO + NTWO;
     IFACT[IF2] := IDIV;
     IF2 := IF2 + 1;
     M := IQUOT;
     IQUOT := M div IDIV;
     IREM := M - IDIV * IQUOT;
    end;
   if ((IQUOT - IDIV) < 0) then
    begin
     INON2 := IF2;
     if IREM = 0 then
      NTWO := NTWO + NTWO
     else
      IFACT[IF2] := M
    end
   else
    begin
     IDIV := 3;
     INON2 := IF2;
     IQUOT := M div IDIV;
     IREM := M - IDIV * IQUOT;
     while (IQUOT - IDIV) >= 0 do
      begin
       if IREM = 0 then
        begin
         IFACT[IF2] := IDIV;
         IF2 := IF2 + 1;
         M := IQUOT;
         IQUOT := M div IDIV;
         IREM := M - IDIV * IQUOT;
        end
       else
        begin
         IDIV := IDIV + 2;
         IQUOT := M div IDIV;
         IREM := M - IDIV * IQUOT;
        end
      end;
     IFACT[IF2] := M;
     NON2P := NP2 div NTWO;
    end;
   ICASE := 1;
   IFMIN := 1;
   I1RNG := NP1;
   if (((IDIM - 4) < 0) and (IFORM <= 0)) then
    begin
     ICASE := 2;
     I1RNG := NP0 * (1 + NPREV div 2);
     if ((IDIM - 1) <= 0) then
      begin
       ICASE := 3;
       I1RNG := NP1;
       if ((NTWO - NP1) > 0) then
        begin
         ICASE := 4;
         IFMIN := 2;
         NTWO := NTWO div 2;
         N := N div 2;
         NP2 := NP2 div 2;
         NTOT := NTOT div 2;
         I := 1;
         for J := 1 to NTOT do
          begin
           D[J] := D[I];
           I := I + 2;
          end;
        end;
      end;
    end;
   if ((NON2P - 1) <= 0) then
    begin
     NP2HF := NP2 div 2;
     J := 1;
     I2 := 1;
     repeat
      if ((J - I2) < 0) then
       begin
        I1MAX := I2 + NP1 - 2;
        I1 := I2;
        repeat
         I3 := I1;
         repeat
          J3 := J + I3 - I2;
          TR := D[I3];
          TI := D[I3 + 1];
          D[I3] := D[J3];
          D[I3 + 1] := D[J3 + 1];
          D[J3] := TR;
          D[J3 + 1] := TI;
          I3 := I3 + NP2;
         until (I3 > NTOT);
         I1 := I1 + 2;
        until (I1 > I1MAX);
       end;
      M := NP2HF;
      if ((J - M) > 0) then
       begin
        repeat
         J := J - M;
         M := M div 2;
        until (((M - NP1) < 0) or ((J - M) <= 0));
       end;
      J := J + M;
      I2 := I2 + NP1;
     until (I2 > NP2);
    end;
   if ((NON2P - 1) > 0) then
    begin
     NWORK := 2 * N;
     I1 := 1;
     repeat
      I3 := I1;
      repeat
       J := I3;
       I := 1;
       repeat
        if (ICASE <> 3) then
         begin
          W[I] := D[J];
          W[I + 1] := D[J + 1];
         end
        else
         begin
          W[I] := D[J];
          W[I + 1] := 0.0;
         end;
        IFP2 := NP2;
        IF2 := IFMIN;
        IFP1 := IFP2 div IFACT[IF2];
        J := J + IFP1;
        FLAG1 := FALSE;
        repeat
         FLAG1 := FALSE;
         if ((J - I3 - IFP2) >= 0) then
          begin
           FLAG1 := TRUE;
           J := J - IFP2;
           IFP2 := IFP1;
           IF2 := IF2 + 1;
           if ((IFP2 - NP1) <= 0) then
            FLAG1 := FALSE;
          end;
         if FLAG1 then
          begin
           IFP1 := IFP2 div IFACT[IF2];
           J := J + IFP1;
          end;
        until (not (FLAG1));
        I := I + 2;
       until (I > NWORK);
       I2MAX := I3 + NP2 - NP1;
       I := 1;
       I2 := I3;
       repeat
        D[I2] := W[I];
        D[I2 + 1] := W[I + 1];
        I := I + 2;
        I2 := I2 + NP1;
       until (I2 > I2MAX);
       I3 := I3 + NP2;
      until (I3 > NTOT);
      I1 := I1 + 2;
     until (I1 > NP1);
    end;
  end;
  function MAX0 (J, K : LONGINT) : LONGINT;
   var
    T : LONGINT;
  begin
   T := J;
   if (K > J) then
    T := K;
   MAX0 := T;
  end;
  procedure THREE_599;
   var
    FLAG2 : BOOLEAN;
    IOUT : LONGINT;
  begin
   NP1TW := NP1 + NP1;
   IPAR := NTWO div NP1;
   while ((IPAR - 2) > 0) do
    IPAR := IPAR div 4;
   if ((IPAR - 2) = 0) then
    begin
     I1 := 1;
     repeat
      K1 := I1;
      repeat
       K2 := K1 + NP1;
       TR := D[K2];
       TI := D[K2 + 1];
       D[K2] := D[K1] - TR;
       D[K2 + 1] := D[K1 + 1] - TI;
       D[K1] := D[K1] + TR;
       D[K1 + 1] := D[K1 + 1] + TI;
       K1 := K1 + NP1TW;
      until (K1 > NTOT);
      I1 := I1 + 2;
     until (I1 > I1RNG);
    end;
   MMAX := NP1;
   while ((MMAX - NTWO div 2) < 0) do
    begin
     LMAX := MAX0(NP1TW, MMAX div 2);
     L := NP1;
     repeat
      M := L;
      FLAG2 := FALSE;
      if ((MMAX - NP1) > 0) then
       begin
        FLAG2 := TRUE;
        THETA := (-TWOPI * L) / (4.0 * MMAX);
        if (ISIGN >= 0) then
         THETA := -THETA;
        WR := COS(THETA);
        WI := SIN(THETA);
       end;
      repeat
       if (FLAG2) then
        begin
         W2R := WR * WR - WI * WI;
         W2I := 2.0 * WR * WI;
         W3R := W2R * WR - W2I * WI;
         W3I := W2R * WI + W2I * WR;
        end;
       FLAG2 := TRUE;
       I1 := 1;
       repeat

        KMIN := I1 + IPAR * M;
        if ((MMAX - NP1) <= 0) then
         KMIN := I1;
        KDIF := IPAR * MMAX;
        KSTEP := 4 * KDIF;
        while ((KSTEP - NTWO) <= 0) do
         begin
          begin
           K1 := KMIN;
           repeat
            K2 := K1 + KDIF;
            K3 := K2 + KDIF;
            K4 := K3 + KDIF;
            if ((MMAX - NP1) <= 0) then
             begin
              U1R := D[K1] + D[K2];
              U1I := D[K1 + 1] + D[K2 + 1];
              U2R := D[K3] + D[K4];
              U2I := D[K3 + 1] + D[K4 + 1];
              U3R := D[K1] - D[K2];
              U3I := D[K1 + 1] - D[K2 + 1];
              if (ISIGN < 0) then
               begin
                U4R := D[K3 + 1] - D[K4 + 1];
                U4I := D[K4] - D[K3];
               end
              else
               begin
                U4R := D[K4 + 1] - D[K3 + 1];
                U4I := D[K3] - D[K4];
               end;
             end
            else
             begin
              T2R := W2R * D[K2] - W2I * D[K2 + 1];
              T2I := W2R * D[K2 + 1] + W2I * D[K2];
              T3R := WR * D[K3] - WI * D[K3 + 1];
              T3I := WR * D[K3 + 1] + WI * D[K3];
              T4R := W3R * D[K4] - W3I * D[K4 + 1];
              T4I := W3R * D[K4 + 1] + W3I * D[K4];
              U3R := D[K1] - T2R;
              U1I := D[K1 + 1] + T2I;
              U2R := T3R + T4R;
              U3I := D[K1 + 1] - T2I;
              U1R := D[K1] + T2R;
              U2I := T3I + T4I;
              if (ISIGN < 0) then
               begin
                U4R := T3I - T4I;
                U4I := T4R - T3R;
               end;
              if (ISIGN > 0) then
               begin
                U4R := T4I - T3I;
                U4I := T3R - T4R;
               end;
             end;
            D[K1] := U1R + U2R;
            D[K1 + 1] := U1I + U2I;
            D[K2] := U3R + U4R;
            D[K2 + 1] := U3I + U4I;
            D[K3] := U1R - U2R;
            D[K3 + 1] := U1I - U2I;
            D[K4] := U3R - U4R;
            D[K4 + 1] := U3I - U4I;
            K1 := K1 + KSTEP;
           until (K1 > NTOT);
          end;
          KDIF := KSTEP;
          KMIN := 4 * (KMIN - I1) + I1;
          KSTEP := 4 * KDIF;
         end;
        I1 := I1 + 2
       until (I1 > I1RNG);
       M := M + LMAX;
       if ((M - MMAX) <= 0) then
        begin
         if (ISIGN < 0) then
          begin
           TR := WR;
           WR := (WR + WI) * RTHLF;
           WI := (WI - TR) * RTHLF
          end
         else
          begin
           TR := WR;
           WR := (WR - WI) * RTHLF;
           WI := (TR + WI) * RTHLF;
          end;
        end;
      until ((M - MMAX) > 0);
      L := L + NP1TW;
     until (L > LMAX);
     IPAR := 3 - IPAR;
     MMAX := MMAX + MMAX;
    end;
  end;
  procedure SIX_699;
   var
    TEST : BOOLEAN;
    IOUT : LONGINT;
  begin
   IFP1 := NTWO;
   IF2 := INON2;
   repeat
    IFP2 := IFACT[IF2] * IFP1;
    THETA := -TWOPI / IFACT[IF2];
    if (ISIGN > 0) then
     THETA := -THETA;
    WSTPR := COS(THETA);
    WSTPI := SIN(THETA);
    J1 := 1;
    repeat
     THETM := (J1 - 1) * (-TWOPI) / IFP2;
     if (ISIGN >= 0) then
      THETM := -THETM;
     WMINR := COS(THETM);
     WMINI := SIN(THETM);
     I1MAX := J1 + I1RNG - 2;
     I1 := J1;
     repeat
      I3 := I1;
      repeat
       I := 1;
       WR := WMINR;
       WI := WMINI;
       J2MAX := I3 + IFP2 - IFP1;
       J2 := I3;
       repeat
        TWOWR := WR + WR;
        J3MAX := J2 + NP2 - IFP2;
        J3 := J2;
        repeat
         JMIN := J3 - J2 + I3;
         J := JMIN + IFP2 - IFP1;
         SR := D[J];
         SI := D[J + 1];
         OLDSR := 0.0;
         OLDSI := 0.0;
         J := J - IFP1;
         repeat
          STMPR := SR;
          STMPI := SI;
          SR := TWOWR * SR - OLDSR + D[J];
          SI := TWOWR * SI - OLDSI + D[J + 1];
          OLDSR := STMPR;
          OLDSI := STMPI;
          J := J - IFP1;
         until ((J - JMIN) <= 0);
         W[I] := WR * SR - WI * SI - OLDSR + D[J];
         W[I + 1] := WI * SR + WR * SI - OLDSI + D[J + 1];
         I := I + 2;
         J3 := J3 + IFP2;
        until (J3 > J3MAX);
        WT := WR * WSTPI;
        WR := WR * WSTPR - WI * WSTPI;
        WI := WI * WSTPR + WT;
        J2 := J2 + IFP1;
       until (J2 > J2MAX);
       I := 1;
       J2 := I3;
       repeat
        J3MAX := J2 + NP2 - IFP2;
        J3 := J2;
        repeat
         D[J3] := W[I];
         D[J3 + 1] := W[I + 1];
         I := I + 2;
         J3 := J3 + IFP2;
        until (J3 > J3MAX);
        J2 := J2 + IFP1;
       until (J2 > J2MAX);
       I3 := I3 + NP2;
      until (I3 > NTOT);
      I1 := I1 + 2;
     until (I1 > I1MAX);
     J1 := J1 + NP1;
    until (J1 > IFP1);
    IF2 := IF2 + 1;
    IFP1 := IFP2;
   until ((IFP1 - NP2) >= 0);
  end;
  procedure SEVEN_799;
   var
    FLAG3 : BOOLEAN;
  begin
   NHALF := N;
   N := N + N;
   THETA := -TWOPI / N;
   if (ISIGN >= 0) then
    THETA := -THETA;
   WSTPR := COS(THETA);
   WSTPI := SIN(THETA);
   WR := WSTPR;
   WI := WSTPI;
   IMIN := 3;
   JMIN := 2 * NHALF - 1;
   if ((IMIN - JMIN) < 0) then
    repeat
     J := JMIN;
     I := IMIN;
     repeat
      SUMR := (D[I] + D[J]) / 2.0;
      SUMI := (D[I + 1] + D[J + 1]) / 2.0;
      DIFR := (D[I] - D[J]) / 2.0;
      DIFI := (D[I + 1] - D[J + 1]) / 2.0;
      TR := WR * SUMI + WI * DIFR;
      TI := WI * SUMI - WR * DIFR;
      D[I] := SUMR + TR;
      D[I + 1] := DIFI + TI;
      D[J] := SUMR - TR;
      D[J + 1] := -DIFI + TI;
      J := J + NP2;
      I := I + NP2;
     until (I > NTOT);
     IMIN := IMIN + 2;
     JMIN := JMIN - 2;
     WT := WR * WSTPI;
     WR := WR * WSTPR - WI * WSTPI;
     WI := WI * WSTPR + WT;
    until ((IMIN - JMIN) >= 0);
   if (((IMIN - JMIN) = 0) and (ISIGN < 0)) then
    begin
     I := IMIN;
     repeat
      D[I + 1] := (-D[I + 1]);
      I := I + NP2;
     until (I > NTOT);
    end;
   NP2 := NP2 + NP2;
   NTOT := NTOT + NTOT;
   J := NTOT + 1;
   IMAX := NTOT div 2 + 1;
   repeat
    IMIN := IMAX - 2 * NHALF;
    I := IMIN;
    I := I + 2;
    J := J - 2;
    while ((I - IMAX) < 0) do
     begin
      D[J] := D[I];
      D[J + 1] := -D[I + 1];
      I := I + 2;
      J := J - 2;
     end;
    D[J] := D[IMIN] - D[IMIN + 1];
    D[J + 1] := 0.0;
    FLAG3 := FALSE;
    if ((I - J) < 0) then
     begin
      FLAG3 := TRUE;
      I := I - 2;
      J := J - 2;
      while ((I - IMIN) > 0) do
       begin
        D[J] := D[I];
        D[J + 1] := D[I + 1];
        I := I - 2;
        J := J - 2;
       end;
     end;
   until not FLAG3;
   D[1] := D[1] + D[2];
   D[2] := 0.0;
  end;
  procedure EIGHT_899;
  begin
   I3 := 1;
   repeat
    I2MAX := I3 + NP2 - NP1;
    I2 := I3;
    repeat
     IMIN := I2 + I1RNG;
     IMAX := I2 + NP1 - 2;
     JMAX := 2 * I3 + NP1 - IMIN;
     if ((I2 - I3) > 0) then
      JMAX := JMAX + NP2;
     if ((IDIM - 2) > 0) then
      begin
       J := JMAX + NP0;
       I := IMIN;
       repeat
        D[I] := D[J];
        D[I + 1] := -D[J + 1];
        J := J - 2;
        I := I + 2;
       until (I > IMAX);
      end;
     J := JMAX;
     I := IMIN;
     repeat
      D[I] := D[J];
      D[I + 1] := -D[J + 1];
      J := J - NP0;
      I := I + NP0;
     until (I > IMAX);
     I2 := I2 + NP1;
    until (I2 > I2MAX);
    I3 := I3 + NP2;
   until (I3 > NTOT);
  end;
 begin
  TWOPI := 8.0 * ARCTAN(1.0);
  RTHLF := 1.0 / SQRT(2.0);
  NTOT := 2;
  for IDIM := 1 to NDIM do
   NTOT := NTOT * NN[IDIM];
  NP1 := 2;
  for IDIM := 1 to NDIM do
   begin
    Z_299;
    if ((NTWO - NP1) > 0) then
     begin
      THREE_599;
      if ((NON2P - 1) > 0) then
       SIX_699;
     end
    else if ((NON2P - 1) > 0) then
     SIX_699;
    if (ICASE = 4) then
     SEVEN_799;
    if (ICASE = 2) then
     EIGHT_899;
    NP0 := NP1;
    NP1 := NP2;
    NPREV := N;
   end;
 end;
begin
 IFORM := 1;
 ISIGN := -1;
 NDIM := 1;
 WRITELN('HOW MANY DATA POINTS');
 READLN(NN[1]);
 NN[2] := 0;
 NN[3] := 0;
 for IOUT := 1 to NN[1] do
  begin
   WRITELN('ENTER REAL AND COMPLEX POINT');
   READLN(REAL_PART, COMPLEX_PART);
   D[((IOUT - 1) * 2 + 1)] := REAL_PART;
   D[(IOUT * 2)] := COMPLEX_PART;
  end;
 FFT512(D, ISIGN, IFORM, NDIM, NN);
 writeln('AND THE ANSWER IS');
 writeln('***********************************************');
 for IOUT := 1 to NN[1] do
  writeln(D[(IOUT - 1) * 2 + 1] : 20 : 10, D[IOUT * 2] : 20 : 10);
 writeln('***********************************************');
end.  FFT512.PAS  FFT512.PAS  FFT512 , FFT128[procedure]



FFT512 and FFT128 perform a fast Fourier transform or inverse transform on an array of data.  This data can represent a single, double, or triple dimensional system.  


TYPE REQUIREMENTS

TYPE
FLOAT =  EXTENDED OR DOUBLE OR REAL;
FIXXED = INTEGER OR LONGINT;
DATA = ARRAY[1..NDP*2] OF FLOAT;


CALLING PROCEDURE

VAR
D:DATA;
NN:ARRAY[1..3] OF FIXXED;
NDIM,ISIGN,IFORM:FIXXED;

D, of type DATA, is the data to be transformed.   The first element of the array is the real part of the data to be transformed. The second element is a complex value. This alternation continues until the array is filled.  For a two dimensional, (a[i,j]), system the array may be filled as follows.

for j:=1 to number_of_columns   do
 for i:=1 to number_of_rows       do
  begin
    readln(realpart,imagpart)
    D[(i+(j-1)*number_of_rows)*2-1]=realpart
    D[(i+(j-1)*number_of_rows)*2]=imagpart
  end;

For a three dimensional system assume the system is defined as
a[1..kk,1..jj,1..ii].  The data would be read into d as follows.





for i:=1 to ii do
 for j:=1 to jj do
  for k:=1 to kk do
   begin
    readln(realpart,imagpart);
    d[(k+(j-1)*kk+(i-1)*kk*jj)*2-1]:=realpart;
    d[(k+(j-1)*kk+(i-1)*kk*jj)*2]:=imagpart;
   end;

On return from the routine, D contains the transformed data. One further note on D.  It should be dimensioned a size two times the number of points for which the transform is being taken to account for both the real and complex portion of the data.

NN of type Array[1..3] of FIXXED, contains the number of points in each dimension.  In the above example, NN[1]=number_of_rows, NN[2]=number_of_colomns. NN[3]=0.

NDIM of type FIXXED, the number of dimensions 1,2, or 3.

ISIGN of type FIXXED,  implies the direction of the transform. -1 implies forward, +1 implies the inverse transform.  

IFORM of type FIXXED,  if the complex portion of the data is zero, set IFORM:=0.  If the complex portion of the data is not zero, set IFORM:=1.  If IFORM is zero, the transform is much faster.  This applies only for FFT512.  For FFT128 IFORM is a dummy argument.

The calling procedure is simple
Define the variables as mentioned above and then

FFT512(D,ISIGN,IFORM,NDIM,NN);

When the routine finishes, D will contain the transformed data.

It is recomended when using this routine with Apple Pascal, that only Longint integers should be used.  This will prevent an Integer overflow when large arrays are being transformed.  There is also an array in FFT512 and FFT128 called WORK.  It should be dimensioned twice as large as the the largest dimension of the data which is not a power of two.  If all dimensions are powers of two, then set the dimension of work to [1..2].  





EXAMPLE

The example program for  FFT512 reads in from the keyboard the number of real and complex data points for a single dimensional array.  It then reads in type data in pairs.  The data is transformed and printed back to the screen.

The example program for FFT128 transforms a four by four function in two dimensions after reading the data from the keyboard.  The transformed data is then printed to the screen.


A NOTE ON SPEED.  

As mentioned in the introductory remarks, Apple's Pascal is faster if extended precision numbers are used.  They can also use up space quickly.  In general, the more factorable the number of data values in each dimension is, the faster the routine will run.  For example a 3600 point transfer will run slightly faster than a 3500 point transform.  However, a 4098  (212) point transform is much quicker.  Also set IFORM equal to zero whenever possible.


REFERENCES

BRIGHAM, E. O.:  The Fast Fourier Transform, Prentice-Hall, Englewood Cliffs, 1970.

Baase, S.:  Computer Algorithms:  Introduction to Design and Analysis, Addison-Wesley, Menlo Park 1983.

Pavlidis,T.: Algorithms for Graphics and Image Processing, Computer Science Press, Rockville, 1982. 


NEWTON_ZERO[procedure]


PURPOSE 

NEWTON_ZERO solves a user supplied transcendental equation F(x), by search and the Newton_Raphson method.  This procedure also requires a function DFUNC(x) which is dF(x)/dx.  If the function is dificult to differentiate, the function FIND_DYDY can be used to find dF(x)/dx.  This practice is discouraged because it can lead to instabilities.


TYPE REQUIREMENTS

TYPE
FLOAT = REAL or DOUBLE or EXTENDED;


CALLING PROCEDURE

var

X:FLOAT;{Starting value for the search for the solution}
DX:FLOAT;{Initial delta x}





MIN_FUNC:FLOAT;{If the absolute value of F   MIN_FUNC then it is assumed  the solution is found}
MIN_DX:FLOAT;{If succesive iterations do not  change x by more than MIN_DX  it is assumed the solution is   found}

Define the function F of type FLOAT with input XIN also of type FLOAT.
Define the function DFUNC of type FLOAT with input XIN also of type FLOAT.
Define X, DX, MIN_FUNC,  and MIN_DX.  Then

NEWTON_ZERO(X,DX,MIN_FUNC,MIN_DX);

On return, X is the zero of the function F.


EXAMPLE

The example calculates and prints the zero of the function F(X)=SIN(X)-COS(X).


REFERENCES

James, M. L., Smith, G. M., Wolford, J. C.:  Applied Numerical Methods for Digital Computation With Fortran and CSMP, Harper and Row, New York, 1977.



MULTI_DIFEQ  [procedure]



PURPOSE
 
MULTI_DIFEQ solves a system of ordinary differential equations using the 
fourth order Runge-Kutta Method.  This system can be composed of several 
first or higher order equations.  It is necessary to define a procedure 
VALUE which describes the system of equations. When given the value of 
the independent variable and the values for all of the dependent variables 
except one, VALUE must determine the value of the remaining dependent 
variable.  Value must perform this function for all of the dependent 
variables.  The procedure VALUE must accept the dependent variable, an 
array of the values of the independent variables and their derivatives 
along with a delta value for the independent variable.  MULTI_DIFEQ 
requires the passage of the dependent variable, a delta value for that 
variable, the array of values for dependent variables and their derivatives, the number of equations,  and the number of iterations to be performed before returning to the calling procedure.  On completion, 
MULTI_DIFEQ passes back the updated dependent and independent variables along with the derivatives for the dependent variables. The dependent variable will equal the input dependent variable plus the delta dependent variable times the number of iterations. Second or higher order equations can be solved by breaking the equations into a set of first order equations.


TYPE REQUIREMENTS

Type
FLOAT = REAL or DOUBLE or EXTENDED;
DIM= ARRAY [1..NUM_OF_EQUATIONS] of FLOAT;
FIXXED = INTEGER or LONGINT;


CALLING PROCEDURE

var
TIME:FLOAT;{Independent variable}
DT:FLOAT;{Delta TIME};
CURRENT:DIM;{Array of dependent variables}
DIDT:DIM;{Array of derivatives of dependent variables}
NUMEQUA:FIXXED;{Total number of equations}
ITERATE:FIXXED;{Number of times MULTI_DIFEQ will iterate before returning}

Define the procedure VALUE.  The calling sequence to value is 

VALUE(TIME,CURRENT,DIDT,DT);.

Define NUMEQUA, DT, and ITERATE.  Define the initial values for TIME, 
CURRENT and DIDT.  Then call the procedure.

MULTI_DIFEQ(TIME,DT,CURRENT,DIDT,NUMEQUA,ITERATE);

When the procerdure finishes TIME, CURRENT, and DIDT will have been 
updated.


EXAMPLE

The example solves the system of equations

                              dX/dt + Y = 3e2t
                              dY/dt + X = 0
                              X(0) = 2, Y(0) =0

from t= 0.0 to t=0.05.  The analytical solution to this set of equations is

                       X = -exp(t/2) + exp(-t/2) + 2exp(2t)
                       Y = exp(t/2) + exp(-t/2) -exp(2t).

This problem and it's solution were taken from page 408 and 426 of the 
second reference.


REFERENCES
James, M. L., Smith, G. M., Wolford, J. C.:  Applied Numerical Methods for 
Digital Computation With Fortran and CSMP, Harper and Row, New York, 
1977.

Ross, S. L.: Introduction to Ordinary Differential Equations 2nd edition, 
Xerox College Publishing, Lexington 1974.

McCormic, J. M., Salvadori M. G.: Numerical Methods in Fortran, Prentice-
Hall, Englewood Cliffs, 1965.



PROGRAM TEST_MULTI_DIFEQ (INPUT, OUTPUT);
TYPE
FLOAT = EXTENDED;
FIXXED = INTEGER;
DIM = ARRAY[1..2] OF FLOAT;
VAR
CURRENT, DIDT : DIM;
TIME, DT, XAN, YAN, YAN1, YAN2, YAN3 : FLOAT;
I, J, IT : FIXXED;
lpt1 : text;
PROCEDURE see;
VAR
R : Rect;
BEGIN
HideAll;
SetRect(R, 0, 35, 550, 330);
SettextRect(R);
Showtext;
END;
PROCEDURE VALUE (VAR A2 : FLOAT;
VAR B2, C2 : DIM;
H : FLOAT);
VAR
X, Y, DYDT, DXDT, T, dum : FLOAT;
BEGIN
DXDT := C2[1];
DYDT := C2[2];
X := B2[1];
Y := B2[2];
T := A2;
DXDT := 3.0 * EXP(2.0 * T) - Y;
DYDT := -X;
C2[1] := DXDT;
C2[2] := DYDT;
END;
PROCEDURE MULTI_DIFEQ (VAR X, H : FLOAT;
VAR Y, YP : DIM;
NUMEQUA, ITERATE : FIXXED);
VAR
TEMP2, TEMP3, TEMP4, YEND, YTEMP : DIM;
XEND, H2, dum, tmltemp : FLOAT;
I, K, NUMEQU : FIXXED;
BEGIN
NUMEQU := NUMEQUA;
XEND := X;
H2 := H / 2.0;
FOR I := 1 TO NUMEQU DO
YEND[I] := Y[I];
FOR I := 1 TO ITERATE DO
BEGIN
VALUE(XEND, YEND, YP, H);
XEND := XEND + H2;
FOR K := 1 TO NUMEQU DO
YTEMP[K] := YEND[K] + YP[K] * H2;
VALUE(XEND, YTEMP, TEMP2, H);
FOR K := 1 TO NUMEQU DO
YTEMP[K] := YEND[K] + TEMP2[K] * H2;
VALUE(XEND, YTEMP, TEMP3, H);
XEND := XEND + H2;
FOR K := 1 TO NUMEQU DO
YTEMP[K] := YEND[K] + TEMP3[K] * H;
VALUE(XEND, YTEMP, TEMP4, H);
FOR K := 1 TO NUMEQU DO
BEGIN
{YEND[K]:=(H / 6.0)*(YP[K]+TEMP4[K]+2.0*(TEMP2[K]+TEMP3[K]))+YEND[K];}
tmltemp := (YP[K] + TEMP4[K] + 2.0 * (TEMP2[K] + TEMP3[K]));
YEND[K] := (H / 6.0) * tmltemp + YEND[K];
END;
END;
VALUE(XEND, YEND, YP, H);
FOR K := 1 TO NUMEQU DO
BEGIN
Y[K] := YEND[K];
END;
X := XEND;
END;
BEGIN
see;
CURRENT[1] := 2.0;
CURRENT[2] := 0.0;
DIDT[1] := 0.0;
DIDT[2] := 0.0;
DT := 0.001;
TIME := 0.0;
I := 2;
IT := 2;
WRITELN('    T             X (CALCULATED)      Y (CALCULATED)');
WRITELN('                  X ANALYTICAL        Y ANALYTICAL');
FOR J := 1 TO 25 DO
BEGIN
MULTI_DIFEQ(TIME, DT, CURRENT, DIDT, I, IT);
XAN := -EXP(TIME) / 2.0 + EXP(-TIME) / 2.0 + 2.0 * EXP(2.0 * TIME);
  {YAN := +EXP(TIME) / 2.0 + EXP(-TIME) / 2.0 - 1.0 * EXP(2.0 * TIME);}
YAN := EXP(TIME) / 2.0 + EXP(-TIME) / 2.0 - 1.0 * EXP(2.0 * TIME);
WRITELN(TIME : 10 : 4, CURRENT[1] : 20 : 14, CURRENT[2] : 20 : 14);
WRITELN(XAN : 30 : 14, YAN : 20 : 14);
END;
READLN;
END.



LAGRANGE[function]



PURPOSE
 
LAGRANGE performs the Lagrangian interpolation for a single point and a set of data.  The data points need not be evenly spaced.  The interpolation uses a polynomial of order equal to the number of data points.   The polynomial is of the form

Pn(x) =f(xj)((X-Xk)/(Xj-Xk)).

The sum and product are taken from zero to the number of data points.  The product is not performed for j=k.

TYPE REQUIREMENTS

TYPE
 FLOAT = EXTENDED or DOUBLE or REAL;
 FIXXED = INTEGER OF LONGINT;
 DATA = ARRAY[1..NUMBER OF DATA POINTS] OF FLOAT;





CALLING PROCEDURE

VAR
X:DATA;{INDEPENDENT VARIABLE  ARRAY}
F:DATA;{DEPENDENT VARIABLE ARRAY}
P:DATA;{OUTPUT LAGRANGE  COEFICIENTS}
XIN:FLOAT;{INTERPOLATION POINT}
NDP:FIXXED;{NUMBER OF DATA POINTS}
YOUT:FLOAT;{OUTPUT OF INTERPOLATION}

DEFINE X,F,NDP, AND XIN.  THEN CALL

YOUT:=LAGRANGE(X,F,P,NDP,XIN);


EXAMPLE

The example program reads in four sets of X, F pairs.  It will then read in an XIN point for which to perform the interpolation.  The interpolation is performed and the value is printed.  It will then read in one more XIN and print the interpolation.


REFERENCES

Wendroff,B.: Theroretical Numerical Anaylsis, Academic Press, New York, 1966.


PROGRAM TEST_LAGRANGE;
TYPE
FLOAT = REAL;
FIXXED = INTEGER;
DATA = ARRAY[1..20] OF FLOAT;
VAR
X, F, P : DATA;
XIN, YOUT : FLOAT;
POINTS, INPUT : FIXXED;
PROCEDURE see;
VAR
R : Rect;
BEGIN
HideAll;
SetRect(R, 0, 35, 550, 330);
SettextRect(R);
Showtext;
END;
FUNCTION LAGRANGE (VAR X, Y, P : DATA;
NDP : FIXXED;
XIN : FLOAT) : FLOAT;
VAR
I, J : FIXXED;
TOTAL : FLOAT;
BEGIN
FOR J := 1 TO NDP DO
BEGIN
P[J] := 1.0;
FOR I := 1 TO NDP DO
IF (I <> J) THEN
P[J] := P[J] * (XIN - X[I]) / (X[J] - X[I]);
END;
TOTAL := 0.0;
FOR I := 1 TO NDP DO
TOTAL := TOTAL + P[I] * Y[I];
LAGRANGE := TOTAL;
END;
BEGIN
see;
POINTS := 4;
WRITELN(' ENTER FOUR X,Y PAIRS');
FOR INPUT := 1 TO 4 DO
READLN(X[INPUT], F[INPUT]);
WRITELN(' ENTER A X VALUE');
READLN(XIN);
YOUT := LAGRANGE(X, F, P, POINTS, XIN);
WRITELN(' Y= ', YOUT : 20 : 10);
WRITELN(' ENTER A X VALUE');
READLN(XIN);
YOUT := LAGRANGE(X, F, P, POINTS, XIN);
WRITELN(' Y= ', YOUT : 20 : 10);
END.



SPLINE(function)



PURPOSE 

The SPLINE routine smoothly interpolates a function, Y=Y(x).  The function is defined by a set of known values for Y at three or more values for the independent varable X. The  X values need not be uniformly spaced.  This function defines the natural cubic interpolatory spline.  In deriving the cubic interpolatory it is assumed Y is cubic in the interval between two points (Y'' is a constant).  It is further assumed that Y'' is zero at the end points of the function.  SPLINE works well for any class of functions for which the conditions are approximately true.  SPLINE uses the X, Y, and y'' values to perform the interpolation.  The y'' values can be found using the SPL procedure which can be found in the same file with SPLINE


TYPE REQUIREMENTS

TYPE
 FLOAT = EXTENDED or DOUBLE or REAL;
 DATA = ARRAY[1..50] OF FLOAT;
 FIXXED = INTEGER or LONGINT;

In addition, the procedure SPL is required.


CALLING PROCEDURE

VAR
X,F,S:DATA;
Z:FLOAT;
NDP:FIXXED;
{X - INDEPENDENT VARIABLE ARRAY}
{F - DEPENDENT VARIABLE ARRAY}
{S - F'' ARRAY}
{Z - INTERPOLATION POINT}
{NDP - NUMBER OF DATA POINTS}






SPL(X,F,S,NDP);{CALL SPL TO DETERMINE S}
READLN(Z);{READ IN THE INTERPOLATION  POINT}
Y:=SPLINE(Z,X,F,S,NDP);{FIND VALUE OF Y AT Z}
WRITELN(Y);{PRINT OUT VALUE OF Y AT Z}


EXAMPLE

The example program reads the number of data points (NDP).  It then reads in the data for two functions (X[1-NDP]), F1[1-NDP], and F2[1-NDP] and calls SPL for F1 and F2.  The program will then read in z values for which to find the interpolated F1 and F2 values and print the values until z=X[1].


REFERENCES

Wendroff,B.: Theroretical Numerical Anaylsis, Academic Press, New York, 1966.


PROGRAM TEST_SPLINE;
TYPE
FLOAT = REAL;
FIXXED = INTEGER;
DATA = ARRAY[1..50] OF FLOAT;
VAR
I, IIN, NDP : FIXXED;
ZIN : FLOAT;
K : FLOAT;
X2, F2, S2 : DATA;
X : DATA;
F : DATA;
S : DATA;
PROCEDURE see;
VAR
R : Rect;
BEGIN
HideAll;
SetRect(R, 0, 35, 550, 330);
SettextRect(R);
Showtext;
END;
PROCEDURE SPL (VAR X, F, S : DATA;
NDP : FIXXED);
TYPE
TEMP_STORE = ARRAY[1..65] OF FLOAT;
VAR
D : TEMP_STORE;
E : TEMP_STORE;
NM1, NM2, IP1, I, J, NSUB_INT : FIXXED;
DOLD, DNEW : FLOAT;
BEGIN
NSUB_INT := NDP - 1;
NM1 := NSUB_INT - 1;
D[1] := X[2] - X[1];
DOLD := (F[2] - F[1]) / D[1];
FOR I := 1 TO NM1 DO
BEGIN
IP1 := I + 1;
D[IP1] := X[I + 2] - X[IP1];
E[I] := 2.0 * (D[IP1] + D[I]);
DNEW := (F[I + 2] - F[IP1]) / D[IP1];
S[IP1] := 6.0 * (DNEW - DOLD);
DOLD := DNEW;
END;
S[2] := S[2] / E[1];
NM2 := NM1 - 1;
FOR I := 1 TO NM2 DO
BEGIN
IP1 := I + 1;
D[IP1] := D[IP1] / E[I];
E[IP1] := E[IP1] - SQR(D[IP1]) * E[I];
S[I + 2] := (S[I + 2] - D[IP1] * E[I] * S[IP1]) / E[IP1];
END;
FOR I := 1 TO NM2 DO
BEGIN
J := NSUB_INT - I;
S[J] := S[J] - D[J] * S[J + 1];
END;
S[1] := 0.0;
S[NSUB_INT + 1] := 0.0;
END;
FUNCTION SPLINE (Z : FLOAT;
X, F, S : DATA;
NDP : FIXXED) : FLOAT;
VAR
ALPHA, BETA, HI, H1, S1 : FLOAT;
I : FIXXED;
FUNCTION QUBE (XIN : FLOAT) : FLOAT;
BEGIN
QUBE := XIN * XIN * XIN;
END;
BEGIN
IF (X[1] < Z) AND (Z < X[NDP]) THEN
BEGIN
I := 1;
WHILE Z > X[I] DO
BEGIN
I := I + 1;
END;
I := I - 1;
HI := X[I + 1] - X[I];
ALPHA := F[I + 1] / HI - S[I + 1] * HI / 6.0;
BETA := F[I] / HI - S[I] * HI / 6.0;
S1 := (S[I] * QUBE(X[I + 1] - Z) + S[I + 1] * QUBE(Z - X[I])) / (6.0 * HI);
SPLINE := S1 + ALPHA * (Z - X[I]) + BETA * (X[I + 1] - Z);
END;
IF Z <= X[1] THEN
BEGIN
H1 := X[2] - X[1];
S1 := (F[2] - F[1]) / H1 - H1 * S[2] / 6.0;
SPLINE := F[1] + S1 * (Z - X[1]);
END;
IF Z >= X[NDP] THEN
BEGIN
H1 := X[NDP] - X[NDP - 1];
S1 := (F[NDP] - F[NDP - 1]) / H1 - H1 * S[NDP - 1] / 6.0;
SPLINE := F[NDP] + S1 * (Z - X[NDP]);
END;
END;
BEGIN
see;
WRITELN(' HOW MANY DATA POINTS?');
READLN(NDP);
FOR IIN := 1 TO NDP DO
BEGIN
WRITELN(' ENTER X,F1,F2');
READLN(X[IIN], F[IIN], F2[IIN]);
END;
SPL(X, F, S, NDP);
SPL(X, F2, S2, NDP);
ZIN := X[2];
REPEAT
WRITELN(' ENTER X');
READLN(ZIN);
K := SPLINE(ZIN, X, F, S, NDP);
WRITELN('XIN =', ZIN : 15 : 7, ' Y =  ', K : 15 : 7);
K := SPLINE(ZIN, X, F2, S2, NDP);
WRITELN('XIN =', ZIN : 15 : 7, ' Y2 =', K : 15 : 7);
UNTIL (ZIN = X[1]);
END. SPLINE.PAS  GRANGE.P  A     @FREE    Free Object        t
  WRITELN(' ENTER X');
  READLN(ZIN);
  K := SPLINE(ZIN, X, F, S, NDP);
  WRITELN('XIN =', ZIN : 15 : 7, ' Y =  ', K : 15 : 7);
  K := SPLINE(ZIN, X, F2, S2, NDP);
  WRITELN('XIN =', ZIN : 15 : 7, ' Y2 =', K : 15 : 7);
 until (ZIN = X[1]);
end.



FIND_DYDX[function]


PURPOSE 

FIND_DYDX will calculate the derivative of a function defined by the user supplied function F.  FIND_DYDX can use forward, backward, or central difference formulas with errors on the order of dx4.  The derivatives are calculated using five values of the function and the Newton-Gregory interpolating formulas.  The five independent variables used in the calculation vary from the point of interest by integer multiples of dx.

TYPE REQUIREMENTS

TYPE
FLOAT = REAL or DOUBLE or EXTENDED;
FIXXED = INTEGER or LONGINT;

CALLING PROCEDURE

var
X:FLOAT;{Point for which the derivative is desired} 
DX:FLOAT;{Delta x used in calculating the  derivative}
DIR:FIXXED;{Integer value which determines what type of difference formula to use in the calculation.  1- forward, 0- central, -1 - backward}

Define the function F of type FLOAT with input XIN also of type FLOAT.
Define XIN, DX and DIR.  Call FIND_DYDX.

YPRIME:=FIND_DYDX(XIN,DX,DIR);

EXAMPLE

The example reads in a value for X and DIR, calculates and prints d(sin(x))/dx and cos(x).  This will continue until a value of x = 0 is read.




REFERENCES

James, M. L., Smith, G. M., Wolford, J. C.:  Applied Numerical Methods for Digital Computation With Fortran and CSMP, Harper and Row, New York, 1977. 



LEAST_LOG[procedure]


PURPOSE 

LEAST_LOG calculates the least square fit of data to the curve of the form y=AeBx.


TYPE REQUIREMENTS

TYPE
FLOAT = REAL or DOUBLE or EXTENDED;
DATA = ARRAY[1..MAX NUMBER OF POINTS] of Float;
FIXXED = INTEGER of LONGINT;









CALLING PROCEDURE

var

X:DATA;{Array containing the independent variable}
Y:DATA;{Array containing the dependent variable}
I:FIXXED;{The number of data points}
A:FLOAT;{The output least square parameter of the above equation} 
B:FLOAT;{The output least square parameter of the above equation}

Define X, Y and I.  Then call

LOG_LEAST(X,Y,I,A,B);.

On return A and B are the least square curve fit to the form y=AeBx.


EXAMPLE

The example used data from the reference listed below, page 298, to calculate and print A and B.


REFERENCES

James, M. L., Smith, G. M., Wolford, J. C.:  Applied Numerical Methods for Digital Computation With Fortran and CSMP, Harper and Row, New York, 1977.


PROGRAM TEST_LEAST_LOG;
TYPE
FLOAT = REAL;
FIXXED = INTEGER;
DATA = ARRAY[1..10] OF FLOAT;
VAR
I : FIXXED;
A, B : FLOAT;
XIN, YIN : DATA;
PROCEDURE see;
VAR
R : Rect;
BEGIN
HideAll;
SetRect(R, 0, 35, 550, 330);
SettextRect(R);
Showtext;
END;
PROCEDURE LEAST_LOG (XIN, YIN : DATA;
NDP : FIXXED;
VAR A, B : FLOAT);
VAR
RPOINT, SX, SLNY, SX2, SXLNY, C1, C2 : FLOAT;
J : FIXXED;
BEGIN
RPOINT := NDP;
SX := 0.0;
SLNY := 0.0;
SX2 := 0.0;
SXLNY := 0.0;
FOR J := 1 TO NDP DO
BEGIN
SX := SX + XIN[J];
SLNY := SLNY + LN(YIN[J]);
SX2 := SX2 + XIN[J] * XIN[J];
SXLNY := SXLNY + XIN[J] * LN(YIN[J]);
END;
C2 := ((SLNY / RPOINT) - (SXLNY / SX)) / ((SX / RPOINT) - (SX2 / SX));
C1 := (SLNY - SX * C2) / RPOINT;
A := EXP(C1);
B := C2;
END;
BEGIN
see;
XIN[1] := 0.0;
XIN[2] := 16.0;
XIN[3] := 32;
YIN[1] := 17;
YIN[2] := 9;
YIN[3] := 5;
WRITELN(' INPUT DATA -');
FOR I := 1 TO 3 DO
WRITELN('  (X,Y)    ', XIN[I] : 10 : 5, '     ', YIN[I] : 10 : 5);
I := 3;
LEAST_LOG(XIN, YIN, I, A, B);
WRITELN;
WRITELN(' LEAST SQUARE FIT');
WRITELN('    Y=', A : 20 : 10, '* EXP( ', B : 20 : 10, ' *X)');
END.



INTERP3D[function]



PURPOSE 

INTERP3D performs an interpolation of the function U=U(x,y,z) defined on a 5 x 5  x 5 or larger grid using the first and second derivative terms of the three dimensional Taylor series.  Derivatives are calculated using various finite-difference approximations with errors on the order of x3 andx4.  The x values, y values, and the z values must be uniformly spaced, but x need not equal y or z.   INTERP3D requires the passage of an X, Y, and Z array, the  X,Y and Z array size a 3d U array and the points XIN,YIN,ZIN for which the interpolation is to be done.


TYPE REQUIREMENTS

CONST
NUMX = maximum number of x data points-1;
{may be larger than XSIZE}
NUMY = maximum number of y data points-1;
{may be larger than YSIZE}
NUMZ = maximum number of z data points-1;
{may be larger than ZSIZE}
Type
FLOAT = REAL or DOUBLE or EXTENDED;
DATAX = ARRAY [0..NUMX] of FLOAT;
DATAY = ARRAY [0..NUMY] of FLOAT; 
DATAZ = ARRAY [0..NUMZ] of FLOAT;
CUBE = ARRAY [0..NUMX,0..NUMY,0..NUMZ] of FLOAT;
FIXXED = INTEGER or LONGINT;


CALLING PROCEDURE

var
X:DATAX;{Known X values}
Y:DATAY;{Known Y values}
Z:DATAZ;{Known Z values}





U:CUBE;{Known U=U(X,Y,Z)}
XSIZE,YSIZE,ZSIZE:FIXXED;{Size of X, Y, and Z array}
XIN,YIN,ZIN,UOUT:FLOAT;{Input points and output point  for the interpolation}

DEFINE X,Y,Z and U arrays.
Pick the points for interpolation XIN, YIN, and ZIN.
UOUT:=INTERP2D(XIN,YIN,ZIN,X,Y,Z,U,XSIZE,YSIZE,ZSIZE);


EXAMPLE

The example routine defines U as U=sin(x)*cos(y)*tan(z) on a 10 X 10  X 10 grid.  X,Y, and Z range from 0 to 1.  Then 200 XIN,YIN and ZIN variables are picked.  XIN=YIN=ZIN.  The input ranges from zero to 1. As the calculation progresses, the maximum error of the interpolation which has occured is printed to the screen along with the point at which it occurs.  At the end, the maximum error is reprinted.  


REFERENCES

James, M. L., Smith, G. M., Wolford, J. C.:  Applied Numerical Methods for Digital Computation With Fortran and CSMP, Harper and Row, New York, 1977.

Spiegel, M. R.: Mathematical Handbook of Formulas and Tables,  McGraw-Hill, New York, 1968.

program TEST_3DINTERP;
 type
  FLOAT = REAL;
  FIXXED = INTEGER;
 var
  I, J, K : FIXXED;
  XINP, YINP, ZINP, UOUT, ERR, XSAVE, MISTAKE : FLOAT;
  X : array[0..10] of FLOAT;
  Y : array[0..10] of FLOAT;
  Z : array[0..10] of FLOAT;
  U : array[0..10, 0..10, 0..10] of FLOAT;
 function INTERP3D (XIN, YIN, ZIN : FLOAT;
         XSIZE, YSIZE, ZSIZE : FIXXED) : FLOAT;
  var
   I, J, L, K, M : FIXXED;
   DFDX, DFDY, DFDZ, DX, DY, DZ, D2FDXDX, D2FDYDY, D2FDZDZ : FLOAT;
   D2UDXDY, D2UDXDZ, D2UDYDZ, FX, FXX, FY, FYY, FZ, FZZ : FLOAT;
   INTER, DELX, DELY, DELZ : FLOAT;
   INTERP2D : FLOAT;
   DUDX, DUDY : array[1..5] of FLOAT;
  procedure F_DUDX (I, J, K : FIXXED);
  begin
   if (I <= 1) then
    begin
     DFDX := (-3.0 * U[(I + 4), J, K] + 16.0 * U[(I + 3), J, K]);
     DFDX := DFDX + (-36.0 * U[(I + 2), J, K] + 48.0 * U[(I + 1), J, K] - 25.0 * U[I, J, K]);
     DFDX := DFDX / (12.0 * DX)
    end
   else if (I >= (XSIZE - 1)) then
    begin
     DFDX := (3.0 * U[(I - 4), J, K] - 16.0 * U[(I - 3), J, K]);
     DFDX := DFDX + (36.0 * U[(I - 2), J, K] - 48.0 * U[(I - 1), J, K] + 25.0 * U[I, J, K]);
     DFDX := DFDX / (12.0 * DX)
    end
   else
    begin
     DFDX := -U[(I + 2), J, K] + 8.0 * U[(I + 1), J, K] - 8.0 * U[(I - 1), J, K] + U[(I - 2), J, K];
     DFDX := DFDX / (12.0 * DX)
    end
  end;
  procedure F_DUDY (I, J, K : FIXXED);
  begin
   if (J <= 1) then
    begin
     DFDY := (-3.0 * U[I, (J + 4), K] + 16.0 * U[I, (J + 3), K] - 36.0 * U[I, (J + 2), K]);
     DFDY := DFDY + (48.0 * U[I, (J + 1), K] - 25.0 * U[I, J, K]);
     DFDY := DFDY / (12.0 * DY)
    end
   else if (J >= (YSIZE - 1)) then
    begin
     DFDY := (3.0 * U[I, (J - 4), K] - 16.0 * U[I, (J - 3), K]);
     DFDY := DFDY + (36.0 * U[I, (J - 2), K] - 48.0 * U[I, (J - 1), K] + 25.0 * U[I, J, K]);
     DFDY := DFDY / (12.0 * DY)
    end
   else
    begin
     DFDY := (-U[I, (J + 2), K] + 8.0 * U[I, (J + 1), K] - 8.0 * U[I, (J - 1), K]);
     DFDY := DFDY + U[I, (J - 2), K];
     DFDY := DFDY / (12.0 * DY)
    end;
  end;
  procedure F_DUDZ (I, J, K : FIXXED);
  begin
   if (K <= 1) then
    begin
     DFDZ := (-3.0 * U[I, J, K + 4] + 16.0 * U[I, J, K + 3] - 36.0 * U[I, J, K + 2]);
     DFDZ := (DFDZ + (48.0 * U[I, J, K + 1] - 25.0 * U[I, J, K])) / (12.0 * DY)
    end
   else if (K >= (ZSIZE - 1)) then
    begin
     DFDZ := (3.0 * U[I, J, K - 4] - 16.0 * U[I, J, K - 3]);
     DFDZ := DFDZ + (36.0 * U[I, J, K - 2] - 48.0 * U[I, J, K - 1]);
     DFDZ := (DFDZ + (25.0 * U[I, J, K])) / (12.0 * DY)
    end
   else
    begin
     DFDZ := -U[I, J, K + 2] + 8.0 * U[I, J, K + 1] - 8.0 * U[I, J, K - 1];
     DFDZ := DFDZ + U[I, J, K - 2];
     DFDZ := DFDZ / (12.0 * DZ)
    end
  end;
  procedure F_D2UDXDX (I, J, K : FIXXED);
  begin
   if (I <= 1) then
    begin
     D2FDXDX := (70.0 * U[I, J, K] - 208.0 * U[(I + 1), J, K]);
     D2FDXDX := D2FDXDX + (228.0 * U[(I + 2), J, K] - 112.0 * U[(I + 3), J, K]);
     D2FDXDX := (D2FDXDX + 22.0 * U[(I + 4), J, K]) / (24.0 * DX * DX)
    end
   else if (I >= (XSIZE - 1)) then
    begin
     D2FDXDX := -(-70.0 * U[I, J, K] + 208.0 * U[(I - 1), J, K]);
     D2FDXDX := D2FDXDX - (-228.0 * U[(I - 2), J, K]);
     D2FDXDX := D2FDXDX - (112.0 * U[(I - 3), J, K] - 22.0 * U[I - 4, J, K]);
     D2FDXDX := D2FDXDX / (24.0 * DX * DX)
    end
   else
    begin
     D2FDXDX := -U[(I + 2), J, K] + 16.0 * U[(I + 1), J, K] - 30.0 * U[I, J, K];
     D2FDXDX := D2FDXDX + 16.0 * U[(I - 1), J, K] - U[(I - 2), J, K];
     D2FDXDX := D2FDXDX / (12.0 * SQR(DX))
    end
  end;
  procedure F_D2UDYDY (I, J, K : FIXXED);
  begin
   if (J <= 1) then
    begin
     D2FDYDY := (70.0 * U[I, J, K] - 208.0 * U[I, (J + 1), K]);
     D2FDYDY := D2FDYDY + (228.0 * U[I, (J + 2), K] - 112.0 * U[I, (J + 3), K]);
     D2FDYDY := D2FDYDY + (22.0 * U[I, (J + 4), K]);
     D2FDYDY := D2FDYDY / (24.0 * DY * DY)
    end
   else if (J >= (YSIZE - 1)) then
    begin
     D2FDYDY := -(-70.0 * U[I, J, K] + 208.0 * U[I, (J - 1), K]);
     D2FDYDY := D2FDYDY - (-228.0 * U[I, (J - 2), K] + 112.0 * U[I, (J - 3), K]);
     D2FDYDY := D2FDYDY - (-22.0 * U[I, (J - 4), K]);
     D2FDYDY := D2FDYDY / (24.0 * DY * DY)
    end
   else
    begin
     D2FDYDY := -U[I, (J + 2), K] + 16.0 * U[I, (J + 1), K] - 30.0 * U[I, J, K];
     D2FDYDY := D2FDYDY + 16.0 * U[I, (J - 1), K] - U[I, (J - 2), K];
     D2FDYDY := D2FDYDY / (12.0 * SQR(DY))
    end
  end;
  procedure F_D2UDZDZ (I, J, K : FIXXED);
  begin
   if (K <= 1) then
    begin
     D2FDZDZ := (70.0 * U[I, J, K] - 208.0 * U[I, J, K + 1]);
     D2FDZDZ := D2FDZDZ + (228.0 * U[I, J, K + 2] - 112.0 * U[I, J, K + 3]);
     D2FDZDZ := (D2FDZDZ + (22.0 * U[I, J, K + 4])) / (24.0 * DX * DX)
    end
   else if (K >= (ZSIZE - 1)) then
    begin
     D2FDZDZ := -(-70.0 * U[I, J, K] + 208.0 * U[I, J, K - 1]);
     D2FDZDZ := D2FDZDZ - (-228.0 * U[I, J, K - 2] + 112.0 * U[I, J, K - 3]);
     D2FDZDZ := (D2FDZDZ - (-22.0 * U[I, J, K - 4])) / (24.0 * DX * DX)
    end
   else
    begin
     D2FDZDZ := -U[I, J, K + 2] + 16.0 * U[I, J, K + 1] - 30.0 * U[I, J, K];
     D2FDZDZ := D2FDZDZ + 16.0 * U[I, J, K - 1] - U[I, J, K - 2];
     D2FDZDZ := D2FDZDZ / (12.0 * SQR(DZ))
    end
  end;
 begin
  if ((XIN <= X[XSIZE]) and (XIN >= X[0])) then
   if ((YIN <= Y[YSIZE]) and (YIN >= Y[0])) then
    if ((ZIN <= Z[ZSIZE]) and (ZIN >= Z[0])) then
     begin
      I := XSIZE;
      while ((XIN <= X[I]) and (I > 0)) do
       I := I - 1;
      if (ABS(XIN - X[I + 1]) < ABS(XIN - X[I])) then
       I := I + 1;
      J := YSIZE;
      while ((YIN <= Y[J]) and (J > 0)) do
       J := J - 1;
      if (ABS(YIN - Y[J + 1]) < ABS(YIN - Y[J])) then
       J := J + 1;
      K := ZSIZE;
      while ((ZIN <= Z[K]) and (K > 0)) do
       K := K - 1;
      if (ABS(ZIN - Z[K + 1]) < ABS(ZIN - Z[K])) then
       K := K + 1;
      DX := X[1] - X[0];
      DY := Y[1] - Y[0];
      DZ := Z[1] - Z[0];
      INTERP2D := 0.0;
      F_DUDX(I, J, K);
      F_DUDY(I, J, K);
      F_DUDZ(I, J, K);
      F_D2UDXDX(I, J, K);
      F_D2UDYDY(I, J, K);
      F_D2UDZDZ(I, J, K);
      FX := DFDX;
      FY := DFDY;
      FZ := DFDZ;
      FXX := D2FDXDX;
      FYY := D2FDYDY;
      FZZ := D2FDZDZ;
      if (J >= (YSIZE - 1)) then
       begin
        L := 0;
        for M := J downto J - 4 do
         begin
          L := L + 1;
          F_DUDX(I, M, K);
          DUDX[L] := DFDX;
         end;
        D2UDXDY := (3.0 * DUDX[5] - 16.0 * DUDX[4]);
        D2UDXDY := D2UDXDY + (36.0 * DUDX[3] - 48.0 * DUDX[2] + 25.0 * DUDX[1]);
        D2UDXDY := D2UDXDY / (12.0 * DY)
       end
      else if (J <= 1) then
       begin
        L := 0;
        for M := J to J + 4 do
         begin
          L := L + 1;
          F_DUDX(I, M, K);
          DUDX[L] := DFDX;
         end;
        D2UDXDY := (-3.0 * DUDX[5] + 16.0 * DUDX[4] - 36.0 * DUDX[3]);
        D2UDXDY := D2UDXDY + (48.0 * DUDX[2] - 25.0 * DUDX[1]);
        D2UDXDY := D2UDXDY / (12.0 * DY)
       end
      else
       begin
        L := 0;
        for M := J - 2 to J + 2 do
         begin
          L := L + 1;
          F_DUDX(I, M, K);
          DUDX[L] := DFDX;
         end;
        D2UDXDY := -DUDX[5] + 8.0 * DUDX[4] - 8.0 * DUDX[2] + DUDX[1];
        D2UDXDY := D2UDXDY / (12.0 * DY)
       end
     end;
  if (K >= (ZSIZE - 1)) then
   begin
    L := 0;
    for M := K downto K - 4 do
     begin
      L := L + 1;
      F_DUDX(I, J, K);
      DUDX[L] := DFDX;
     end;
    D2UDXDZ := (3.0 * DUDX[5] - 16.0 * DUDX[4]);
    D2UDXDZ := D2UDXDZ + (36.0 * DUDX[3] - 48.0 * DUDX[2] + 25.0 * DUDX[1]);
    D2UDXDZ := D2UDXDZ / (12.0 * DZ)
   end
  else if (K <= 1) then
   begin
    L := 0;
    for M := K to K + 4 do
     begin
      L := L + 1;
      F_DUDX(I, J, M);
      DUDX[L] := DFDX
     end;
    D2UDXDZ := (-3.0 * DUDX[5] + 16.0 * DUDX[4] - 36.0 * DUDX[3]);
    D2UDXDZ := D2UDXDZ + (48.0 * DUDX[2] - 25.0 * DUDX[1]);
    D2UDXDZ := D2UDXDZ / (12.0 * DZ)
   end
  else
   begin
    L := 0;
    for M := K - 2 to K + 2 do
     begin
      L := L + 1;
      F_DUDX(I, J, M);
      DUDX[L] := DFDX;
     end;
    D2UDXDZ := -DUDX[5] + 8.0 * DUDX[4] - 8.0 * DUDX[2] + DUDX[1];
    D2UDXDZ := D2UDXDZ / (12.0 * DZ)
   end;
  if (K >= (ZSIZE - 1)) then
   begin
    L := 0;
    for M := K downto K - 4 do
     begin
      L := L + 1;
      F_DUDY(I, J, K);
      DUDY[L] := DFDY;
     end;
    D2UDYDZ := (3.0 * DUDY[5] - 16.0 * DUDY[4]);
    D2UDYDZ := D2UDYDZ + (36.0 * DUDY[3] - 48.0 * DUDY[2] + 25.0 * DUDY[1]);
    D2UDYDZ := D2UDYDZ / (12.0 * DZ)
   end
  else if (K <= 1) then
   begin
    L := 0;
    for M := K to K + 4 do
     begin
      L := L + 1;
      F_DUDY(I, J, M);
      DUDY[L] := DFDY;
     end;
    D2UDYDZ := (-3.0 * DUDY[5] + 16.0 * DUDY[4] - 36.0 * DUDY[3]);
    D2UDYDZ := D2UDYDZ + (48.0 * DUDY[2] - 25.0 * DUDY[1]);
    D2UDYDZ := D2UDYDZ / (12.0 * DZ)
   end
  else
   begin
    L := 0;
    for M := K - 2 to K + 2 do
     begin
      L := L + 1;
      F_DUDY(I, J, M);
      DUDY[L] := DFDY;
     end;
    D2UDYDZ := -DUDY[5] + 8.0 * DUDY[4] - 8.0 * DUDY[2] + DUDY[1];
    D2UDYDZ := D2UDYDZ / (12.0 * DZ);
   end;
  DX := X[0] - X[1];
  DY := Y[0] - Y[1];
  DZ := Z[0] - Z[1];
  DELX := XIN - X[I];
  DELY := YIN - Y[J];
  DELZ := ZIN - Z[K];
  INTER := U[I, J, K] + DELX * FX + DELY * FY + DELX * DELX * FXX / 2.0;
  INTER := INTER + DELY * DELY * FYY / 2.0;
  INTERP2D := INTER + DELX * DELY * D2UDXDY;
  INTERP2D := INTERP2D + DELZ * FZ + DELZ * DELZ * FZZ / 2.0;
  INTERP3D := INTERP2D + DELZ * DELX * D2UDXDZ + DELZ * DELY * D2UDYDZ;
 end;
begin
 for I := 0 to 10 do
  begin
   X[I] := I;
   Y[I] := I;
   Z[I] := I;
   for K := 0 to 10 do
    for J := 0 to 10 do
     U[I, J, K] := SIN(I / 10.0) * COS(J / 10.0) * SIN(K / 10.0) / COS(K / 10.0);
  end;
 ERR := 0.0;
 for I := 0 to 250 do
  begin
   YINP := I;
   XINP := YINP / 25.0;
   YINP := XINP;
   ZINP := YINP;
   UOUT := INTERP3D(XINP, YINP, ZINP, 10, 10, 10);
   MISTAKE := ABS(SIN(XINP / 10.0) * COS(YINP / 10.0) * SIN(ZINP / 10.0) / COS(ZINP / 10.0) - UOUT);
   if (MISTAKE > ERR) then
    begin
     WRITELN(XINP : 10 : 5, YINP : 10 : 5, ZINP : 20 : 10);
     WRITELN(UOUT : 20 : 10, MISTAKE : 20 : 10);
     ERR := MISTAKE;
     XSAVE := YINP;
    end;
  end;
 WRITELN('MAXIMUM ERR WAS ', ERR : 20 : 10, ' AT ', XSAVE : 20 : 10);
 MISTAKE := SIN(XSAVE / 10.0) * COS(XSAVE / 10.0) * SIN(XSAVE / 10.0) / COS(XSAVE / 10.0);
 WRITELN(' ANSWER SHOULD BE ', MISTAKE : 20 : 10);
end. 

GRAF2[procedure]
WRITE_TITLE[procedure]
LAB_XY_AXIS[procedure]
GRID[procedure]
NUMXY[procedure]

PURPOSE

This set of procedures are designed to plot a user defined function y=y(x) or set of data  on grid, label the axies and give the plot a title.

TYPE REQUIREMENTS

TYPE
FLOAT = REAL OR DOUBLE OR EXTENDED;
FIXXED = INTEGER;
LETTERS = ARRAY[1..50] OF CHAR;
DATA = ARRAY [1..300] OF FLOAT;

CALLING PROCEDURE

var
XLAB,YLAB,TITLE  : LETTERS;
XPLOT, YPLOT  :  DATA;
XMAX,XMIN,YMAX,YMIN : FLOAT;
NX,NY,NT,NDP,NGRIDX,NGRIDY : FIXXED;

For this program to work with Macadvantage Pascal the comment markings in the first few lines must be removed. The five references to the procedure WRITEDRAW must be changed to WRITE.  The references to HIDEALL, SETRECT, SETDRAWINGRECT, and SHOWDRAWING in the procedure GRID must be removed.

The procedure PLOT2 is required as a subroutine for all of the procedures.  GRID must be the first procedure called.  It will define the graphic grid on which the plotting will occur.  In addition it will draw the axis and any grid markings.  The number of grid lines for both axes is passed to the procedure (NGRIDX,NGRIDY).  These values must be defined.   Before the procedure is called the global variables XMAX, YMAX, XMIN, YMIN must be defined as the minimum and maximum extents for the plot.  

GRID(NGRIDX,NGIRDY);

After GRID is called all of the other procedures may be called in any order.

FOR WRITE_TITLE define the number of characters in the title NT and an array TITLE of type letter which contains NT characters then

WRITE_TITLE(TITLE,NT);

will draw and center the title over the plot.

FOR LAB_XY_AXIS define the number of characters in the x axis label and the y axis label NX, NY and  array XLAB and YLAB of type letter which contains NX and NY characters then

LAB_XY_AXIS(XLAB,YLAB,NX,NY);

will draw and center axis labels on the plot.

NUMXY will write numeric labels on both axes starting with the minimum and continuing until the maxiximum values are reached.  This procedure has two input parameters NX, NY. These parameters determine the number of intermediate numeric values which will be plotted.  Define NX and NY and call

NUMXY(NX,NY);

GRAF2 will perform the ploting given of an array of data containing the independt variable (x) and a corresponding array of independent data (Y) and the number of points to plot.  Define NDP (number of independent or dependent data points), XPLOT, and YPLOT the data arrays and call

GRAF2(XPLOT,YPLOT,NDP);

to plot the data.  


EXAMPLE

The example will plot a sixth order ploynomial which represents the log of atmospheric density from 100 to 400 km.  The program will ask for the title, and the axes labels to be typed.  



PROGRAM TEST_DRAW_GRAPH;
{    uses {$UR:Mac Interface}
{    MacCore, QDTypes, TBTypes, }
{    QuickDraw(Moveto, Lineto, MOVE, LINE, TEXTSIZE, SETRECT);}
TYPE
LETTERS = ARRAY[1..50] OF CHAR;
FLOAT = REAL;
FIXXED = INTEGER;
DATA = ARRAY[1..300] OF FLOAT;
VAR
XLAB, YLAB, TITLE : LETTERS;
XPLOT, YPLOT : DATA;
X, Y, XMAX, XMIN, YMAX, YMIN : FLOAT;
NX, NY, NT, K, K2, NDP : FIXXED;
PROCEDURE see;
VAR
R : Rect;
BEGIN
HideAll;
SetRect(R, 0, 35, 550, 400);
SettextRect(R);
Showtext;
END;
PROCEDURE plot2 (x, y : FLOAT;
color : FIXXED);
{if color =0 move from present point to x,y }
{if color = 1 draw from present point to x,y}
VAR
xint, yint : integer;
BEGIN
yint := 270 - round((y - ymin) * 200 / (ymax - ymin));
xint := 80 + round((x - xmin) * 360 / (xmax - xmin));
IF (color = 1) THEN
lineto(xint, yint)
ELSE
moveto(xint, yint);
END;
PROCEDURE GRAF2 (XDAT, YDAT : DATA;
NDP : FIXXED);
VAR
J : FIXXED;
BEGIN
PLOT2(XDAT[1], YDAT[1], 0);
FOR J := 1 TO NDP DO
PLOT2(XDAT[J], YDAT[J], 1);
END;
PROCEDURE WRITE_TITLE (TITLE : LETTERS;
NT : FIXXED);
VAR
I, I2 : FIXXED;
XSTART : FLOAT;
BEGIN
I := NT DIV 2;
XSTART := (XMAX + XMIN) / 2.0;
FOR I2 := 1 TO NT DO
BEGIN
PLOT2(XSTART, YMAX, 0);
MOVE(15 * (I2 - I), -20);
WRITEDRAW(TITLE[I2]);
END;
END;
PROCEDURE LAB_XY_AXIS (xlab, ylab : letters;
nx, ny : fixxed);
VAR
I, J, I2, J2 : fixxed;
XSTART, YSTART : FLOAT;
BEGIN
I := NX DIV 2;
XSTART := (XMAX + XMIN) / 2.0;
FOR I2 := 1 TO NX DO
BEGIN
PLOT2(XSTART, YMIN, 0);
MOVE(10 * (I2 - I), 20);
WRITEDRAW(XLAB[I2]);
END;
J := NY DIV 2;
YSTART := (YMAX + YMIN) / 2.0;
FOR J2 := 1 TO NY DO
BEGIN
PLOT2(XMIN, YSTART, 0);
MOVE(-70, -10 * (J - J2));
WRITEDRAW(YLAB[J2]);
END;
END;
PROCEDURE grid (i, j : fixxed);
VAR
i2, j2 : fixxed;
X, Y, DX, DY : FLOAT;
R : Rect;
BEGIN
{THE NEXT FOUR LINES MUST BE REMOVED IF USING MACADVANTAGE PASCAL}
HideAll;
SetRect(R, 0, 35, 550, 400);
SetDrawingRect(R);
ShowDrawing;
IF (i > 0) THEN
BEGIN
dx := (xmax - xmin) / i;
FOR I2 := 0 TO I DO
BEGIN
X := XMIN + DX * I2;
PLOT2(X, YMIN, 0);
PLOT2(X, YMAX, 1);
END;
END;
IF (J > 0) THEN
BEGIN
dY := (Ymax - Ymin) / J;
FOR J2 := 0 TO J DO
BEGIN
Y := YMIN + DY * J2;
PLOT2(XMIN, Y, 0);
PLOT2(XMAX, Y, 1);
END;
END;
END;
PROCEDURE numxy (i, j : fixxed);
VAR
dx, dy, xwrite, ywrite : float;
ix, iy, i2, j2 : fixxed;
BEGIN
textsize(9);
dx := (xmax - xmin) / i;
FOR i2 := 0 TO i DO
BEGIN
xwrite := xmin + i2 * dx;
plot2(xwrite, ymin, 0);
line(0, 3);
line(0, -3);
move(-10, 10);
writedraw(xwrite : 8);
END;
dy := (ymax - ymin) / j;
FOR j2 := 0 TO j DO
BEGIN
ywrite := ymin + j2 * dy;
plot2(xmin, ywrite, 0);
line(-3, 0);
line(3, 0);
move(-50, 0);
writedraw(ywrite : 8);
END;
END;
BEGIN
see;
{define the bounds for the plot}
XMAX := 400.0;
XMIN := 120.0;
YMAX := 20.0;
YMIN := 12.0;
{define the axis labels and title}
writeln('how many letters in x axis label');
readln(nx);
WRITELN('TYPE X AXIS LABEL');
WRITELN;
FOR ndp := 1 TO nx DO
read(xlab[ndp]);
WRITELN;
writeln('how many letters in y axis label');
WRITELN;
readln(ny);
WRITELN('TYPE Y AXIS LABEL');
WRITELN;
FOR ndp := 1 TO ny DO
read(ylab[ndp]);
WRITELN;
writeln('how many letters in the title');
readln(nt);
WRITELN('TYPE TITLE LABEL');
WRITELN;
FOR ndp := 1 TO nt DO
read(TITLE[ndp]);
{number of data points}
NDP := 140;
{generate the data to plot}
{the equation below repersents the log of atmoshpereic density}
{from 120 to 400 km (1972 standard) the maximum error is about 1.7%}
FOR k := 1 TO NDP DO
BEGIN
XPLOT[K] := 2.0 * (K + 60);
x := xplot[k];
y := 52.0984865319 - x * 7.941841234081721268e-1;
y := y + 7.543724423635822518e-3 * x * x;
y := y - 3.857218155663838521e-5 * x * x * x;
y := y + 1.096796103181713316e-7 * x * x * x * x;
y := y - 1.636410795555003514e-10 * x * x * x * x * x;
y := y + 9.991367887452148791e-14 * x * x * x * x * x * x;
yplot[k] := y;
END;
{DRAW A GRID WITH 12 LINES IN THE X DIRECTION }
{AND 14 LINES IN THE Y DIRECTION also draw the axis }
GRID(12, 14);
{number the x axis at 4 points, y axis in 7 points in addition}
{to the min x and min y points}
numxy(4, 7);
{label x and y axis nx,letters for x NY letters for y}
LAB_XY_AXIS(XLAB, YLAB, nx, ny);
{draw the title, NT letters}
WRITE_TITLE(TITLE, NT);
{draw the data generated above, ndp points}
GRAF2(XPLOT, YPLOT, NDP);
READ(TITLE[1]);
END.


ARCCOS[function]
ARCSIN[function]
ATAN2  [function]



PURPOSE
 
ARCCOS and ARCSIN of x return the inverse of the sin and cos fucntions for x. It is assumed abs(x)  1.  ATAN2(y,x) returns the arctan(y/x) in the correct quadrant.  It is assumed x and y are not both zero.




TYPE REQUIREMENTS

Type
FLOAT = REAL or DOUBLE or EXTENDED;


CALLING PROCEDURE

var
X,Y,ASIN,ACOS:FLOAT;{Input for the functions}

Define the input and call the functions.

Z:=ATAN2(Y,X);
Z:=ARCCOS(ACOS);
Z:=ARCSIN(ASIN);


EXAMPLE

The example calculates and prints the various functions for x and y ranging form -5 to 5.  The input for ARCCOS is x/sqrt(x*x+y*y).  The input for ARCSIN is y/sqrt(x*x+y*y);


REFERENCE

VAX/VMS Runtime Library Reference Manual, Digital Equipment Corporation, 1982.

PROGRAM TEST_ACOS;
TYPE
FLOAT = REAL;
FIXXED = INTEGER;
VAR
I, J : FIXXED;
XIN, YIN, Z, AS, AC, AT : FLOAT;
PROCEDURE see;
VAR
R : Rect;
BEGIN
HideAll;
SetRect(R, 0, 35, 550, 330);
SettextRect(R);
Showtext;
END;
FUNCTION ARCCOS (U : FLOAT) : FLOAT;
BEGIN
IF ((0.0 < U) AND (U < 1.0)) THEN
ARCCOS := ARCTAN(SQRT(1.0 - U * U) / U)
ELSE IF ((-1.0 < U) AND (U < 0.0)) THEN
ARCCOS := ARCTAN(SQRT(1.0 - U * U) / U) + 3.141592653589793238462643
ELSE IF U = -1.0 THEN
ARCCOS := 3.141592653589793238462643
ELSE IF U = 1.0 THEN
ARCCOS := 0.0
ELSE IF U = 0.0 THEN
ARCCOS := 3.141592653589793238462643 / 2.0
ELSE
ARCCOS := 0.0 / 0.0;
END;
FUNCTION ARCSIN (U : FLOAT) : FLOAT;
BEGIN
IF ((ABS(U) > 0.0) AND (ABS(U) < 1.0)) THEN
ARCSIN := ARCTAN(U / SQRT(1.0 - U * U))
ELSE IF (U = 0.0) THEN
ARCSIN := 0.0
ELSE IF (U = 1.0) THEN
ARCSIN := +3.141592653589793238462643 / 2.0
ELSE IF (U = -1) THEN
ARCSIN := -3.141592653589793238462643 / 2.0
ELSE
ARCSIN := 0.0 / 0.0;
END;
FUNCTION ATAN2 (Y, X : FLOAT) : FLOAT;
BEGIN
IF ((X = 0.0) AND (Y > 0.0)) THEN
ATAN2 := 3.141592653589793238462643 / 2.0
ELSE IF ((X = 0.0) AND (Y < 0.0)) THEN
ATAN2 := -3.141592653589793238462643 / 2.0
ELSE IF (X > 0.0) THEN
ATAN2 := ARCTAN(Y / X)
ELSE IF ((X < 0.0) AND (Y > 0.0)) THEN
ATAN2 := ARCTAN(Y / X) + 3.141592653589793238462643
ELSE IF ((X < 0.0) AND (Y < 0.0)) THEN
ATAN2 := ARCTAN(Y / X) - 3.141592653589793238462643
ELSE IF ((X < 0.0) AND (Y = 0.0)) THEN
ATAN2 := 3.141592653589793238462643
ELSE
ATAN2 := 0.0 / 0.0;
END;
BEGIN
SEE;
FOR I := -5 TO 5 DO
BEGIN
FOR J := -5 TO 5 DO
IF (NOT ((I = 0) AND (J = 0))) THEN
BEGIN
XIN := I;
YIN := J;
Z := SQRT(XIN * XIN + YIN * YIN);
AS := ARCSIN(YIN / Z);
AC := ARCCOS(XIN / Z);
AT := ATAN2(YIN, XIN);
WRITELN(' (X,Y)  ', XIN : 10 : 5, YIN : 10 : 5);
WRITELN(' ARCSIN ', AS : 20 : 10, ' ARCCOS', AC : 20 : 10, ' ATAN2', AT : 20 : 10);
END;
END;
END.


This stack is the latest evolution in the MacMathPascal package.  Within this stack is  a collection of source listings for Pascal programs.  These progarms contain procedures and functions to solve many of the types of problems which the engineer, scientist, or student faces daily.  There are  procedures to perfrom numeric calculus, curve fitting, interpolation, 
plotting (the plots on the introduction cards were done with some of the routines), Fourier transforms, special functions, and solutions to equations.   
 

As an added bonus, there is a card in this stack which performs a Discrete Fourier Transform (DFT).  To get to this card hit the Mac Icon on the screen.

The programs are  examples.  You can cut and paste the procedures form the source listings to include in custom porgrams.   This can save you several hours writting your own routines to perform daily tasks.  
By a click of a button you can write the source for these progams to disk.  They are ready to compile and run with LightSpeed Pascal.  The documentaion for the programs is included in this stack.  This documention will explain what the routine does and how to use it.  There are also many references for the theory of operation of the routines given in the documentation.  

As always, there is a catch.  A return is expected for the hours  spent putting these routines together.  This stack is SHARE WARE!  The return  expect is a check for $25. 

As an incentive to have you send me money, a few things have been done.  First, the documentaion can not be printed.  If you want a copy, send in the money and you will be sent a printed and bound copy of the documentation and a disk with the latest version of this stack.

Second, if you are a registered owner of this stack and you give the stack to someone and they become registered you will be sent $5.  

To date, no one have ever been sued for using the routines in this stack without paying for them but the author once did grade a schoolarship paper of a person who did use the routines without paying.

You can print an order form using the button below. 
____________________________________________________

DISCLAIMER

This stack has been "PASSWORD PROTECTED" but that is not a guarentee. 
Neither Coherent Cognition nor Apple nor its software suppliers make any warranty or representation, either express or implied , with respect to the software in this stack, its quality, performance, mercantability or fitness for any particular purpose.  This software is sold "as is"; the purchaser assumes the entire risk as to it's quality and performance.

In on event will Coherent Cognition or Apple or its software suppliers be liabel for direct, indirect, special, incidental, or consequential damages resulting from any defect in the software in this stack, even if they have been advised of the possibility of such damages.  In particular, they shall have no liability for any programs or data stored in or used whith this stack.  

The warranty and remidies set forth above are exclusive and in lieu of all others, oral or written, express or implied.  No Coherent Cognition agent, dealer, author, or employee is authorized to make any modification, extension, or adddition to this warranty.

Some states do not allow the exclusion or limitaion of implied warranties or liability for incidential or consequential damages, so the above limitation or exclusion man not apply.  This warranty gives you specific legal rights, and you may also have other rights that vary from state to state.  

____________________________________________________ 
GENERAL NOTES APPLICABLE FOR ALL ROUTINES

LightSpeed Pascal has double and extended precision variables.  A Pascal procedure which uses a large number or floating point numbers will run quicker if all of the variables are extended precision.  For examble, with 3600 points the FFT routine in this package will run 20% faster using extended percision.  The reason for this is simple.  All math is done in extended precision independent of the type of variables being used in the calculation.  If Real or Double floating point numbers are used, the CPU must convert the numbers to extended precision, perform the math, and then convert back to the Real or Double precision numbers.  The disadvantage of extended precision numbers is a use of memory.  They each occupy 10 bytes.   If your application can spare the space, it is a good idea to use the extended precision numbers. 

LightSpeed Pascal supports both a 2 and 4 byte integer.  Most of the time these can be used interchangeably.  

To make the conversion from various precision easy, the routines in this package use the following type declarations. 

TYPE
FLOAT = EXTENDED or DOUBLE or REAL;
FIXXED = INTEGER or LONGINT;
DATA = ARRAY[1..MAXSIZE] of FLOAT;
SQUARE = ARRAY[1..MAXSIZE,1..MAXSIZE] of FLOAT;

DATA and SQUARE are the single and two dimensional arrays used in the routines.  FLOAT is of the type EXTENDED, DOUBLE or REAL.    One or more of these Type declarations must be part of any program which uses the routines of this package. 


A word about size.  The maximum amount of data that can be referenced in one VAR statement or program segment  in LightSpeed Pascal is 32K bytes.  This applies to version 1.1 of the compiler and this limit may be lifted in later versions.  


Version 2.2
 


BESSEL[procedure]


PURPOSE 

BESSEL finds the values of Bessel's functions of the first and second types (Jn, Yn) along with the modified Bessel's functions (In, Kn) of integral orders for x>0.0 All four functions are computed with a single call to the procedure.

TYPE REQUIREMENTS

Type
FLOAT = REAL or DOUBLE or EXTENDED;
FIXXED = INTEGER or LONGINT;

CALLING PROCEDURE

var
X:FLOAT;{Independent variable}
N:FIXXED;{Order of the Bessel function of  interest}




BJN:FLOAT;{Output value for Jn(x)}
BY:FLOAT;{Output value for Yn(x)}
BIN:FLOAT;{Output value for In(x)}
BK:FLOAT;{Output value for Kn(x)}
Define X and N and then call the procedure

Bessel(X,BJN,BY,BIN,BK,N);.


EXAMPLE
The example calculates and prints Jn, Yn, In, and Kn for n=0 to 5 and for x=0.5 to 5.


REFERENCES

McCormic, J. M., Salvadori M. G.: Numerical Methods in Fortran, Prentice-Hall, Englewood Cliffs, 1965.

PROGRAM TEST_BESSEL;
TYPE
FLOAT = REAL;
FIXXED = INTEGER;
VAR
X, BJN, BY, BIN, BK : FLOAT;
NORD, INPU : FIXXED;
PROCEDURE see;
VAR
R : Rect;
BEGIN
HideAll;
SetRect(R, 0, 35, 550, 330);
SettextRect(R);
Showtext;
END;
PROCEDURE BESSEL (VAR X, BJN, BY, BIN, BK : FLOAT;
NORD : FIXXED);
VAR
FINISHED, DONE_WRON : BOOLEAN;
AN, AI, AK, FN, GAM, T, S, XA, C, D, PHI, PN, QN, U, DEN, XB, PI, BI, XI : FLOAT;
BJ0, BY0, BI0, BK0, BK1, BY1, P : FLOAT;
K, N, M, I : FIXXED;
TEMP : FLOAT;
PROCEDURE J_N_I_N;
VAR
DONE : BOOLEAN;
BEGIN
AN := N;
T := 1.0;
S := -1.0;
WHILE (AN > 0.0) DO
BEGIN
T := T * XA / AN;
AN := AN - 1;
END;
BJN := T;
BIN := T;
DONE := FALSE;
K := 1;
REPEAT
DEN := K * (K + N);
T := T * XB / DEN;
IF ((BJN + T) - BJN <> 0.0) THEN
BEGIN
BJN := BJN + T * S;
BIN := BIN + T;
S := -S;
K := K + 1;
END
ELSE
DONE := TRUE;
UNTIL DONE;
END;
PROCEDURE ASYMPTOT;
BEGIN
C := 4 * NORD * NORD;
D := 8.0 * X;
AN := NORD;
PHI := X - ((2.0 * AN + 1.0) / 4.0) * PI;
M := TRUNC(X + 1 + SQRT(X * X + AN * AN));
T := (C - 1.0) / D;
S := 1.0;
U := 1.0;
PN := 1.0;
QN := T;
BK := 1.0 + T;
   {WRITELN('#1',BK:10:5);}
BI := 1.0 - T;
I := 2;
REPEAT
AI := I;
temp := (C - SQR(2.0 * AI - 1.0));
   { T := (C - SQR(2.0 * AI - 1.0)) / D * T / AI;}
T := TEMP / D * T / AI;
BK := BK + T;
  {WRITELN('#2',BK:10:5);}
BI := BI + T * S;
IF (S > 0.0) THEN
BEGIN
PN := PN - T * S * U;
U := -U;
END
ELSE
BEGIN
QN := QN - T * S * U;
END;
S := -S;
I := I + 1;
UNTIL ((I > M) OR (((QN + T) - QN) = 0.0));
TEMP := EXP(-X) * SQRT(PI / (2.0 * X));
BK := TEMP * BK;
 {WRITELN('#3',BK:10:5);}
  { BK:= EXP(-X) * BK * SQRT(PI / (2.0 * X));}
IF ((X - FN - 6.0) >= 0.0) THEN
BEGIN
BJN := (PN * COS(PHI) - QN * SIN(PHI)) * SQRT(2.0 / (PI * X));
BY := (PN * SIN(PHI) + QN * COS(PHI)) * SQRT(2.0 / (PI * X));
BIN := EXP(X) * BI / SQRT(2.0 * PI * X);
END;
END;
BEGIN
PI := 4.0 * ARCTAN(1.0);
GAM := 0.57721566;
FN := NORD;
IF ((X - FN - 6.0) < 0.0) THEN
BEGIN
XA := X / 2.0;
XB := SQR(XA);
N := 0;
J_N_I_N;
FINISHED := FALSE;
WHILE ((N <= 1) AND (NOT FINISHED)) DO
BEGIN
IF (N = 0) THEN
BEGIN
BY := 2.0 / PI * (GAM + LN(XA)) * BJN;
BK := -(GAM + LN(XA)) * BIN;
  {WRITELN('#4',BK:10:5);}
T := XB;
S := 1.0;
XI := 1.0;
K := 2;
AK := K;
WHILE ((BY + T * XI - BY) <> 0.0) DO
BEGIN
BK := BK + T * XI;
{WRITELN('#5',BK:10:5);}
BY := BY + 2.0 / PI * S * T * XI;
T := T * XB / (AK * AK);
XI := XI + 1.0 / AK;
S := -S;
K := K + 1;
AK := K;
END;
IF (NORD > 0) THEN
BEGIN
N := 1;
BJ0 := BJN;
BI0 := BIN;
BY0 := BY;
BK0 := BK;
J_N_I_N;
END
ELSE
FINISHED := TRUE;
END
ELSE IF (N = 1) THEN
BEGIN
BY := (BJN * BY0 - 2.0 / (PI * X)) / BJ0;
TEMP := BIN * BK0;
TEMP := (1.0 / X - TEMP);
BK := TEMP / BI0;
  {WRITELN('#6',BK:10:5);}
{BK:= (1.0 / X - BIN * BK0) / BI0;}
P := 1;
IF (NORD > 1) THEN
BEGIN
DONE_WRON := FALSE;
WHILE (NOT DONE_WRON) DO
BEGIN
BY1 := BY;
BK1 := BK;
BY := 2.0 * P / X * BY1 - BY0;
TEMP := 2.0 * P / X * BK1;
BK := TEMP + BK0;
 { WRITELN('#7',BK:10:5);}
   { BK:= 2.0 * P / X * BK1 + BK0;}
P := P + 1;
IF (NORD > 2) THEN
BEGIN
BY0 := BY1;
BK0 := BK1;
NORD := NORD - 1;
END
ELSE
DONE_WRON := TRUE;
END;
N := ROUND(P);
J_N_I_N;
END
ELSE
FINISHED := TRUE;
END
ELSE
FINISHED := TRUE;
END;
END
ELSE
ASYMPTOT;
NORD := ROUND(FN);
END;
BEGIN
see;
FOR NORD := 1 TO 5 DO
FOR INPU := 1 TO 20 DO
BEGIN
X := INPU / 2.0;
BESSEL(X, BJN, BY, BIN, BK, NORD);
WRITELN(' X= ', X : 20 : 10, '     ORDER =', NORD : 5);
WRITELN('            BJN             BY            BIN             BK');
WRITELN(BJN : 15 : 7, BY : 15 : 7, BIN : 15 : 7, BK : 15 : 12);
END;
END.



NEW_mac (INPUT, OUTPUT);
 type
  FLOAT = EXTENDED;
 var
  P, N, R, TEMP, I, Y, YEARS, PR, LOST : FLOAT;
  YEARINT : array[0..40] of FLOAT;
  J, K, MONTH, NYEAR : INTEGER;
  TABLE : BOOLEAN;
  TF : CHAR;
  MAC : TEXT;
  DATA : string;
procedure see;
   R : Rect;
 begin
  HideAll;
  SetRect(R, 0, 35, 550, 400);
  SettextRect(R);
  Showtext;
 end;
   PROCEDURE EXAMP;
 VAR 
 DUM:CHAR; 
 BEGIN
 TEXTSIZE(9);
 WRITELN('THIS EXAMPLE PROGRAM DEMONSTRATES SOME OF CAPABILITIES');
 WRITELN('THE MACMATHPASCAL PACKAGE FROM COHERET COGNITION');
 WRITELN('THE MACMATHPASCAL PACKAGE IS A COLLECTION OF SCIENTIFIC AND');
 WRITELN('ENGINEERING SUBROUTINES TO BE INCORPORATED INTO YOUR PASCAL PROGRAMS');
 WRITELN;
 WRITELN('INCLUDED IN THE PACKAGE IS A ROUTINE WHICH CALCULATES A STANDARD'); 
 WRITELN('LOAN AMORIZATION.  THIS PROGRAM WAS INCLUDED SO YOU COULD FIGURE');
 WRITELN('HOW TO PAY FOR YOUR MAC II.');
 WRITELN('TO QUIT ENTER PRINCIPAL=0.0  APPLE HAS STARTED TO GIVE THEM AWAY?');
 WRITELN;
 WRITELN('RETURN TO CONTIUE');
 READLN;
 TEXTSIZE(18);
 WRITELN;
 WRITELN;
 WRITELN('    MACMATHPASCAL');
 WRITELN('    COHERENT COGNITION');
 WRITELN('    BOX 24114');
 WRITELN('    SAN DIEGO, CA  92142');
 WRITELN('    619-278-4141');
 TEXTSIZE(9);
 WRITELN;
 WRITELN;
 WRITELN('RETURN TO CONTIUE');
 READLN;
 END;
 function x_to_Y (X, Y : FLOAT) : FLOAT;
  var
   XTEMP : FLOAT;
 begin
  XTEMP := LN(X);
  XTEMP := XTEMP * Y;
  X_TO_Y := EXP(XTEMP);
 end;
begin
see;
EXAMP;
 WRITELN(' INPUT PRINCIPAL');
 READLN(P);
 while (P > 0.0) do
  begin
   WRITELN(' ANNUAL INTEREST');
   READLN(I);
{N IS THE NUMBER OF PAYMENTS IN A YEAR}
   N := 12.0;
   WRITELN('YEARS');
   READLN(Y);
   Y := Y;
   YEARS := Y;
   TEMP := (I / 100.0) / N + 1.0;
   TEMP := X_TO_Y(TEMP, (N * Y));
   R := ((I / 100.0) * P / N) / (1.0 - 1.0 / TEMP);
   WRITELN( 'THE PAYMENT ON A LOAN OF $', P : 10 : 2);
   WRITELN( 'AT AN INTEREST RATE OF ', I : 4 : 2, '% FOR ', Y : 5 : 2, ' YEARS');
   WRITELN( 'IS $', R : 7 : 2);
   K := ROUND(12 * YEARS) - 1;
   WRITELN(MAC, 'PAYMENT      REMAINING PRINCIPAL      TOTAL INTEREST');
   for J := 1 to K do
    begin
     Y := YEARS - J * 1.0 / 12.0;
     TEMP := (I / 100.0) / N + 1.0;
     TEMP := X_TO_Y(TEMP, (N * Y));
     TEMP := ((I / 100.0) * 1.0 / N) / (1.0 - 1.0 / TEMP);
     PR := R / TEMP;
     LOST := (R * J) - (P - PR);
     WRITELN( J : 5, PR : 20 : 2, LOST : 20 : 2);
     IF(((J DIV 12)*12)=J)THEN
     BEGIN
     WRITELN('RETURN TO CONTINUE');
     READLN;
     END;
    end;
   LOST := LOST + (R - PR);
   PR := 0.0;
   J := K + 1;
   WRITELN( J : 5, PR : 20 : 2, LOST : 20 : 2);
   WRITELN(' INPUT PRINCIPAL');
   READLN(P);
  end;
end.  THIS ROUTINE CALCULATES A STANDARD LOAN AMORIZATION.  THIS PROGRAM WAS INCLUDED SO YOU COULD FIGURE HOW TO PAY FOR YOUR MAC II.  TO QUIT THE PROGRAM ENTER A PRINCIPAL OF 0.0 WHEN ASKED.




FFT512 , FFT128[procedure]



FFT512 and FFT128 perform a fast Fourier transform or inverse transform on an array of data.  This data can represent a single, double, or triple dimensional system.  


TYPE REQUIREMENTS

TYPE
FLOAT =  EXTENDED OR DOUBLE OR REAL;
FIXXED = INTEGER OR LONGINT;
DATA = ARRAY[1..NDP*2] OF FLOAT;


CALLING PROCEDURE

VAR
D:DATA;
NN:ARRAY[1..3] OF FIXXED;
NDIM,ISIGN,IFORM:FIXXED;

D, of type DATA, is the data to be transformed.   The first element of the array is the real part of the data to be transformed. The second element is a complex value. This alternation continues until the array is filled.  For a two dimensional, (a[i,j]), system the array may be filled as follows.

for j:=1 to number_of_columns   do
 for i:=1 to number_of_rows       do
  begin
    readln(realpart,imagpart)
    D[(i+(j-1)*number_of_rows)*2-1]=realpart
    D[(i+(j-1)*number_of_rows)*2]=imagpart
  end;

For a three dimensional system assume the system is defined as
a[1..kk,1..jj,1..ii].  The data would be read into d as follows.





for i:=1 to ii do
 for j:=1 to jj do
  for k:=1 to kk do
   begin
    readln(realpart,imagpart);
    d[(k+(j-1)*kk+(i-1)*kk*jj)*2-1]:=realpart;
    d[(k+(j-1)*kk+(i-1)*kk*jj)*2]:=imagpart;
   end;

On return from the routine, D contains the transformed data. One further note on D.  It should be dimensioned a size two times the number of points for which the transform is being taken to account for both the real and complex portion of the data.

NN of type Array[1..3] of FIXXED, contains the number of points in each dimension.  In the above example, NN[1]=number_of_rows, NN[2]=number_of_colomns. NN[3]=0.

NDIM of type FIXXED, the number of dimensions 1,2, or 3.

ISIGN of type FIXXED,  implies the direction of the transform. -1 implies forward, +1 implies the inverse transform.  

IFORM of type FIXXED,  if the complex portion of the data is zero, set IFORM:=0.  If the complex portion of the data is not zero, set IFORM:=1.  If IFORM is zero, the transform is much faster.  This applies only for FFT512.  For FFT128 IFORM is a dummy argument.

The calling procedure is simple
Define the variables as mentioned above and then

FFT512(D,ISIGN,IFORM,NDIM,NN);

When the routine finishes, D will contain the transformed data.

It is recomended when using this routine with LightSpeed Pascal, that only Longint integers should be used.  This will prevent an Integer overflow when large arrays are being transformed.  There is also an array in FFT512 and FFT128 called WORK.  It should be dimensioned twice as large as the the largest dimension of the data which is not a power of two.  If all dimensions are powers of two, then set the dimension of work to [1..2].  





EXAMPLE

The example program for  FFT512 reads in from the keyboard the number of real and complex data points for a single dimensional array.  It then reads in type data in pairs.  The data is transformed and printed back to the screen.

The example program for FFT128 transforms a four by four function in two dimensions after reading the data from the keyboard.  The transformed data is then printed to the screen.


A NOTE ON SPEED.  

As mentioned in the introductory remarks, LightSpeed Pascal is faster if extended precision numbers are used.  They can also use up space quickly.  In general, the more factorable the number of data values in each dimension is, the faster the routine will run.  For example a 3600 point transfer will run slightly faster than a 3500 point transform.  However, a 4098  (212) point transform is much quicker.  Also set IFORM equal to zero whenever possible.


REFERENCES

BRIGHAM, E. O.:  The Fast Fourier Transform, Prentice-Hall, Englewood Cliffs, 1970.

Baase, S.:  Computer Algorithms:  Introduction to Design and Analysis, Addison-Wesley, Menlo Park 1983.

Pavlidis,T.: Algorithms for Graphics and Image Processing, Computer Science Press, Rockville, 1982. 

PROGRAM FIND_2D_FFT;
CONST
MAXSIZE = 32;
TYPE
FLOAT = REAL;
FIXXED = INTEGER;
INTDATA = ARRAY[1..3] OF FIXXED;
DATA = ARRAY[1..MAXSIZE] OF FLOAT;
VAR
D : DATA;
REALPART, IMAGPART : FLOAT;
NDIM, ISIGN, IFORM, IOUT, COUNT, J, K : FIXXED;
NN : INTDATA;
PROCEDURE see;
VAR
R : Rect;
BEGIN
HideAll;
SetRect(R, 0, 35, 550, 330);
SettextRect(R);
Showtext;
END;
PROCEDURE FFT128 (VAR D : DATA;
ISIGN, IFORM, NDIM : FIXXED;
NN : INTDATA);
{*                         COPYWRITE COHERENT COGNITION 1985                        *}
VAR
I, I1, I1MAX, I1RNG, I2, I2MAX, I3, ICASE : FIXXED;
IDIM, IDIV, IF2, IFMIN : FIXXED;
IFP1, IFP2, IMAX, IMIN, INON2, IPAR, IQUOT, IREM : FIXXED;
J, J1, J2, J2MAX, J3, J3MAX, JMAX, JMIN : FIXXED;
K1, K2, K3, K4, KDIF, KMIN, KSTEP, L, LMAX : FIXXED;
M, MMAX, N, NHALF, NON2P, NP0 : FIXXED;
NP1, NP1TW, NP2, NP2HF, NPREV : FIXXED;
NTOT, NTWO, NWORK : FIXXED;
DIFI, DIFR : FLOAT;
OLDSI, OLDSR, RTHLF, SI, SR, STMPI, STMPR, SUMI, SUMR : FLOAT;
T2I, T2R, T3I, T3R, T4I, T4R, TI, TR, THETA, THETM : FLOAT;
TWOPI, TWOWR, U1I, U1R, U2I, U2R, U3I, U3R, U4I, U4R : FLOAT;
W2I, W2R, W3I, W3R, WI, WMINI, WMINR, WR : FLOAT;
WSTPI, WSTPR, WT : FLOAT;
W : DATA;
IFACT : ARRAY[1..16] OF FIXXED;
PROCEDURE zeroit;
VAR
izero : fixxed;
BEGIN
FOR izero := 1 TO 16 DO
IFACT[izero] := 0;
I := 0;
I1 := 0;
I1MAX := 0;
I1RNG := 0;
I2 := 0;
I2MAX := 0;
I3 := 0;
ICASE := 0;
IDIM := 0;
IDIV := 0;
IF2 := 0;
IFMIN := 0;
IFP1 := 0;
IFP2 := 0;
IMAX := 0;
IMIN := 0;
INON2 := 0;
IPAR := 0;
IQUOT := 0;
IREM := 0;
J := 0;
J1 := 0;
J2 := 0;
J2MAX := 0;
J3 := 0;
J3MAX := 0;
JMAX := 0;
JMIN := 0;
K1 := 0;
K2 := 0;
K3 := 0;
K4 := 0;
KDIF := 0;
KMIN := 0;
KSTEP := 0;
L := 0;
LMAX := 0;
M := 0;
MMAX := 0;
N := 0;
NHALF := 0;
NON2P := 0;
NP0 := 0;
NP1 := 0;
NP1TW := 0;
NP2 := 0;
NP2HF := 0;
NPREV := 0;
NTOT := 0;
NTWO := 0;
NWORK := 0;
END;
PROCEDURE Z_299;
VAR
J, IOUT : FIXXED;
FLAG1 : BOOLEAN;
BEGIN
N := NN[IDIM];
NP2 := NP1 * N;
M := N;
NTWO := NP1;
IF2 := 1;
IDIV := 2;
IQUOT := M DIV IDIV;
IREM := M - IDIV * IQUOT;
WHILE ((IQUOT - IDIV) >= 0) AND (IREM = 0) DO
BEGIN
NTWO := NTWO + NTWO;
IFACT[IF2] := IDIV;
IF2 := IF2 + 1;
M := IQUOT;
IQUOT := M DIV IDIV;
IREM := M - IDIV * IQUOT;
END;
IF ((IQUOT - IDIV) < 0) THEN
BEGIN
INON2 := IF2;
IF IREM = 0 THEN
NTWO := NTWO + NTWO
ELSE
IFACT[IF2] := M
END
ELSE
BEGIN
IDIV := 3;
INON2 := IF2;
IQUOT := M DIV IDIV;
IREM := M - IDIV * IQUOT;
WHILE (IQUOT - IDIV) >= 0 DO
BEGIN
IF IREM = 0 THEN
BEGIN
IFACT[IF2] := IDIV;
IF2 := IF2 + 1;
M := IQUOT;
IQUOT := M DIV IDIV;
IREM := M - IDIV * IQUOT;
END
ELSE
BEGIN
IDIV := IDIV + 2;
IQUOT := M DIV IDIV;
IREM := M - IDIV * IQUOT;
END
END;
IFACT[IF2] := M;
NON2P := NP2 DIV NTWO;
END;
ICASE := 1;
IFMIN := 1;
I1RNG := NP1;

IF ((NON2P - 1) <= 0) THEN
BEGIN
NP2HF := NP2 DIV 2;
J := 1;
I2 := 1;
REPEAT
IF ((J - I2) < 0) THEN
BEGIN
I1MAX := I2 + NP1 - 2;
I1 := I2;
REPEAT
I3 := I1;
REPEAT
J3 := J + I3 - I2;
TR := D[I3];
TI := D[I3 + 1];
D[I3] := D[J3];
D[I3 + 1] := D[J3 + 1];
D[J3] := TR;
D[J3 + 1] := TI;
I3 := I3 + NP2;
UNTIL (I3 > NTOT);
I1 := I1 + 2;
UNTIL (I1 > I1MAX);
END;
M := NP2HF;
IF ((J - M) > 0) THEN
BEGIN
REPEAT
J := J - M;
M := M DIV 2;
UNTIL (((M - NP1) < 0) OR ((J - M) <= 0));
END;
J := J + M;
I2 := I2 + NP1;
UNTIL (I2 > NP2);
END;
IF ((NON2P - 1) > 0) THEN
BEGIN
NWORK := 2 * N;
I1 := 1;
REPEAT
I3 := I1;
REPEAT
J := I3;
I := 1;
REPEAT
IF (ICASE <> 3) THEN
BEGIN
W[I] := D[J];
W[I + 1] := D[J + 1];
END
ELSE
BEGIN
W[I] := D[J];
W[I + 1] := 0.0;
END;
IFP2 := NP2;
IF2 := IFMIN;
IFP1 := IFP2 DIV IFACT[IF2];
J := J + IFP1;
FLAG1 := FALSE;
REPEAT
FLAG1 := FALSE;
IF ((J - I3 - IFP2) >= 0) THEN
BEGIN
FLAG1 := TRUE;
J := J - IFP2;
IFP2 := IFP1;
IF2 := IF2 + 1;
IF ((IFP2 - NP1) <= 0) THEN
FLAG1 := FALSE;
END;
IF FLAG1 THEN
BEGIN
IFP1 := IFP2 DIV IFACT[IF2];
J := J + IFP1;
END;
UNTIL (NOT (FLAG1));
I := I + 2;
UNTIL (I > NWORK);
I2MAX := I3 + NP2 - NP1;
I := 1;
I2 := I3;
REPEAT
D[I2] := W[I];
D[I2 + 1] := W[I + 1];
I := I + 2;
I2 := I2 + NP1;
UNTIL (I2 > I2MAX);
I3 := I3 + NP2;
UNTIL (I3 > NTOT);
I1 := I1 + 2;
UNTIL (I1 > NP1);
END;
END;
FUNCTION MAX0 (J, K : LONGINT) : LONGINT;
VAR
T : LONGINT;
BEGIN
T := J;
IF (K > J) THEN
T := K;
MAX0 := T;
END;
PROCEDURE THREE_599;
VAR
FLAG2 : BOOLEAN;
IOUT : LONGINT;
BEGIN
NP1TW := NP1 + NP1;
IPAR := NTWO DIV NP1;
WHILE ((IPAR - 2) > 0) DO
IPAR := IPAR DIV 4;
IF ((IPAR - 2) = 0) THEN
BEGIN
I1 := 1;
REPEAT
K1 := I1;
REPEAT
K2 := K1 + NP1;
TR := D[K2];
TI := D[K2 + 1];
D[K2] := D[K1] - TR;
D[K2 + 1] := D[K1 + 1] - TI;
D[K1] := D[K1] + TR;
D[K1 + 1] := D[K1 + 1] + TI;
K1 := K1 + NP1TW;
UNTIL (K1 > NTOT);
I1 := I1 + 2;
UNTIL (I1 > I1RNG);
END;
MMAX := NP1;
WHILE ((MMAX - NTWO DIV 2) < 0) DO
BEGIN
LMAX := MAX0(NP1TW, MMAX DIV 2);
L := NP1;
REPEAT
M := L;
FLAG2 := FALSE;
IF ((MMAX - NP1) > 0) THEN
BEGIN
FLAG2 := TRUE;
THETA := (-TWOPI * L) / (4.0 * MMAX);
IF (ISIGN >= 0) THEN
THETA := -THETA;
WR := COS(THETA);
WI := SIN(THETA);
END;
REPEAT
IF (FLAG2) THEN
BEGIN
W2R := WR * WR - WI * WI;
W2I := 2.0 * WR * WI;
W3R := W2R * WR - W2I * WI;
W3I := W2R * WI + W2I * WR;
END;
FLAG2 := TRUE;
I1 := 1;
REPEAT

KMIN := I1 + IPAR * M;
IF ((MMAX - NP1) <= 0) THEN
KMIN := I1;
KDIF := IPAR * MMAX;
KSTEP := 4 * KDIF;
WHILE ((KSTEP - NTWO) <= 0) DO
BEGIN
BEGIN
K1 := KMIN;
REPEAT
K2 := K1 + KDIF;
K3 := K2 + KDIF;
K4 := K3 + KDIF;
IF ((MMAX - NP1) <= 0) THEN
BEGIN
U1R := D[K1] + D[K2];
U1I := D[K1 + 1] + D[K2 + 1];
U2R := D[K3] + D[K4];
U2I := D[K3 + 1] + D[K4 + 1];
U3R := D[K1] - D[K2];
U3I := D[K1 + 1] - D[K2 + 1];
IF (ISIGN < 0) THEN
BEGIN
U4R := D[K3 + 1] - D[K4 + 1];
U4I := D[K4] - D[K3];
END
ELSE
BEGIN
U4R := D[K4 + 1] - D[K3 + 1];
U4I := D[K3] - D[K4];
END;
END
ELSE
BEGIN
T2R := W2R * D[K2] - W2I * D[K2 + 1];
T2I := W2R * D[K2 + 1] + W2I * D[K2];
T3R := WR * D[K3] - WI * D[K3 + 1];
T3I := WR * D[K3 + 1] + WI * D[K3];
T4R := W3R * D[K4] - W3I * D[K4 + 1];
T4I := W3R * D[K4 + 1] + W3I * D[K4];
U3R := D[K1] - T2R;
U1I := D[K1 + 1] + T2I;
U2R := T3R + T4R;
U3I := D[K1 + 1] - T2I;
U1R := D[K1] + T2R;
U2I := T3I + T4I;
IF (ISIGN < 0) THEN
BEGIN
U4R := T3I - T4I;
U4I := T4R - T3R;
END;
IF (ISIGN > 0) THEN
BEGIN
U4R := T4I - T3I;
U4I := T3R - T4R;
END;
END;
D[K1] := U1R + U2R;
D[K1 + 1] := U1I + U2I;
D[K2] := U3R + U4R;
D[K2 + 1] := U3I + U4I;
D[K3] := U1R - U2R;
D[K3 + 1] := U1I - U2I;
D[K4] := U3R - U4R;
D[K4 + 1] := U3I - U4I;
K1 := K1 + KSTEP;
UNTIL (K1 > NTOT);
END;
KDIF := KSTEP;
KMIN := 4 * (KMIN - I1) + I1;
KSTEP := 4 * KDIF;
END;
I1 := I1 + 2
UNTIL (I1 > I1RNG);
M := M + LMAX;
IF ((M - MMAX) <= 0) THEN
BEGIN
IF (ISIGN < 0) THEN
BEGIN
TR := WR;
WR := (WR + WI) * RTHLF;
WI := (WI - TR) * RTHLF
END
ELSE
BEGIN
TR := WR;
{CHANGED + TO - NEXT ONE LINE}
WR := (WR - WI) * RTHLF;
WI := (TR + WI) * RTHLF;
END;
END;
UNTIL ((M - MMAX) > 0);
L := L + NP1TW;
UNTIL (L > LMAX);
IPAR := 3 - IPAR;
MMAX := MMAX + MMAX;
END;
END;
PROCEDURE SIX_699;
VAR
TEST : BOOLEAN;
IOUT : LONGINT;
BEGIN
IFP1 := NTWO;
IF2 := INON2;
REPEAT
IFP2 := IFACT[IF2] * IFP1;
THETA := -TWOPI / IFACT[IF2];
IF (ISIGN > 0) THEN
THETA := -THETA;
WSTPR := COS(THETA);
WSTPI := SIN(THETA);
J1 := 1;
REPEAT
THETM := (J1 - 1) * (-TWOPI) / IFP2;
IF (ISIGN >= 0) THEN
THETM := -THETM;
WMINR := COS(THETM);
WMINI := SIN(THETM);
I1MAX := J1 + I1RNG - 2;
I1 := J1;
REPEAT
I3 := I1;
REPEAT
I := 1;
WR := WMINR;
WI := WMINI;
J2MAX := I3 + IFP2 - IFP1;
J2 := I3;
REPEAT
TWOWR := WR + WR;
J3MAX := J2 + NP2 - IFP2;
J3 := J2;
REPEAT
JMIN := J3 - J2 + I3;
J := JMIN + IFP2 - IFP1;
SR := D[J];
SI := D[J + 1];
OLDSR := 0.0;
OLDSI := 0.0;
J := J - IFP1;
REPEAT
STMPR := SR;
STMPI := SI;
SR := TWOWR * SR - OLDSR + D[J];
SI := TWOWR * SI - OLDSI + D[J + 1];
OLDSR := STMPR;
OLDSI := STMPI;
J := J - IFP1;
UNTIL ((J - JMIN) <= 0);
W[I] := WR * SR - WI * SI - OLDSR + D[J];
W[I + 1] := WI * SR + WR * SI - OLDSI + D[J + 1];
I := I + 2;
J3 := J3 + IFP2;
UNTIL (J3 > J3MAX);
WT := WR * WSTPI;
WR := WR * WSTPR - WI * WSTPI;
WI := WI * WSTPR + WT;
J2 := J2 + IFP1;
UNTIL (J2 > J2MAX);
I := 1;
J2 := I3;
REPEAT
J3MAX := J2 + NP2 - IFP2;
J3 := J2;
REPEAT
D[J3] := W[I];
D[J3 + 1] := W[I + 1];
I := I + 2;
J3 := J3 + IFP2;
UNTIL (J3 > J3MAX);
J2 := J2 + IFP1;
UNTIL (J2 > J2MAX);
I3 := I3 + NP2;
UNTIL (I3 > NTOT);
I1 := I1 + 2;
UNTIL (I1 > I1MAX);
J1 := J1 + NP1;
UNTIL (J1 > IFP1);
IF2 := IF2 + 1;
IFP1 := IFP2;
UNTIL ((IFP1 - NP2) >= 0);
END;
BEGIN
zeroit;
TWOPI := 8.0 * ARCTAN(1.0);
RTHLF := 1.0 / SQRT(2.0);
NTOT := 2;
FOR IDIM := 1 TO NDIM DO
NTOT := NTOT * NN[IDIM];
NP1 := 2;
FOR IDIM := 1 TO NDIM DO
BEGIN
Z_299;
IF ((NTWO - NP1) > 0) THEN
BEGIN
THREE_599;
IF ((NON2P - 1) > 0) THEN
SIX_699;
END
ELSE IF ((NON2P - 1) > 0) THEN
SIX_699;
NP0 := NP1;
NP1 := NP2;
NPREV := N;
END;
END;
BEGIN
see;
IFORM := 1;
ISIGN := -1;
NDIM := 2;
NN[1] := 4;
NN[2] := 4;
WRITELN('ENTER A 4 BY 4 GRID OF COMPLEX DATA');
FOR J := 1 TO NN[2] DO
FOR K := 1 TO NN[1] DO
BEGIN
WRITELN(' ENTER D[', K, ',', J, '] REAL AND IMAG PARTS');
READLN(REALPART, IMAGPART);
D[(K + (J - 1) * NN[1]) * 2 - 1] := REALPART;
D[(K + (J - 1) * NN[1]) * 2] := IMAGPART;
END;
WRITELN(' THE INPUT DATA IS');
FOR IOUT := 1 TO NN[1] * NN[2] DO
writeln(D[(IOUT - 1) * 2 + 1] : 20 : 10, D[IOUT * 2] : 20 : 10);
FFT128(D, ISIGN, IFORM, NDIM, NN);
WRITELN;
writeln('AND THE ANSWER IS');
writeln('***********************************************');
FOR IOUT := 1 TO NN[1] * NN[2] DO
writeln(D[(IOUT - 1) * 2 + 1] : 20 : 10, D[IOUT * 2] : 20 : 10);
writeln('***********************************************');
END. 



NEWTON_ZERO[procedure]


PURPOSE 

NEWTON_ZERO solves a user supplied transcendental equation F(x), by search and the Newton_Raphson method.  This procedure also requires a function DFUNC(x) which is dF(x)/dx.  If the function is dificult to differentiate, the function FIND_DYDY can be used to find dF(x)/dx.  This practice is discouraged because it can lead to instabilities.


TYPE REQUIREMENTS

TYPE
FLOAT = REAL or DOUBLE or EXTENDED;


CALLING PROCEDURE

var

X:FLOAT;{Starting value for the search for the solution}
DX:FLOAT;{Initial delta x}





MIN_FUNC:FLOAT;{If the absolute value of F   MIN_FUNC then it is assumed  the solution is found}
MIN_DX:FLOAT;{If succesive iterations do not  change x by more than MIN_DX  it is assumed the solution is   found}

Define the function F of type FLOAT with input XIN also of type FLOAT.
Define the function DFUNC of type FLOAT with input XIN also of type FLOAT.
Define X, DX, MIN_FUNC,  and MIN_DX.  Then

NEWTON_ZERO(X,DX,MIN_FUNC,MIN_DX);

On return, X is the zero of the function F.


EXAMPLE

The example calculates and prints the zero of the function F(X)=SIN(X)-COS(X).


REFERENCES

James, M. L., Smith, G. M., Wolford, J. C.:  Applied Numerical Methods for Digital Computation With Fortran and CSMP, Harper and Row, New York, 1977.


PROGRAM TEST_NEWTON_ZERO (INPUT, OUTPUT);
TYPE
FLOAT = EXTENDED;
VAR
K, DELK, MIN_FUNC, MIN_DK, PI : FLOAT;
PROCEDURE see;
VAR
R : Rect;
BEGIN
HideAll;
SetRect(R, 0, 35, 550, 330);
SettextRect(R);
Showtext;
END;
FUNCTION F (X : FLOAT) : FLOAT;
BEGIN
F := SIN(X) - COS(X);
END;
FUNCTION DFUNC (X : FLOAT) : FLOAT;
BEGIN
DFUNC := COS(X) + SIN(X);
END;
PROCEDURE NEWTON_ZERO (VAR K : FLOAT;
DELK, MIN_FUNC, MIN_DK : FLOAT);
VAR
KN : FLOAT;
TML1, TML2 : FLOAT;
BEGIN
KN := K;
WHILE (F(K) * F(KN)) > 0.0 DO
BEGIN
K := KN;
KN := K + DELK;
END;
 { KN := K - F(K) / DFUNC(K);}
TML1 := DFUNC(K);
TML2 := F(K);
KN := K - TML2 / TML1;
WHILE (ABS(K - KN)) > MIN_DK DO
BEGIN
K := KN;
IF ABS(F(K)) > MIN_FUNC THEN
BEGIN
{ KN := K - F(K) / DFUNC(K);}
TML1 := DFUNC(K);
TML2 := F(K);
KN := K - TML2 / TML1;
END;
IF ABS(F(K)) <= MIN_FUNC THEN
KN := K;
END;
K := KN;
END;
BEGIN
see;
PI := ARCTAN(1.0) * 4.0;
WRITELN(' FINDING THE ZERO OF SIN(X)-COS(X)');
WRITELN('START LOOKING FOR THE ROOT AT THETA= ?   "DEG"');
READLN(K);
K := K * PI / 180.0;
DELK := 0.01;
MIN_FUNC := 0.0001;
MIN_DK := 0.0001;
NEWTON_ZERO(K, DELK, MIN_FUNC, MIN_DK);
MIN_DK := SIN(K) - COS(K);
K := K * 180.0 / PI;
WRITELN(' THE NEXT ROOT OF  SIN(X)-COS(X) IS AT ', K : 10 : 5, ' DEG');
WRITELN('SIN(X)-COS(X)=', MIN_DK : 20 : 10);
READLN;
END.



FFT512 , FFT128[procedure]



FFT512 and FFT128 perform a fast Fourier transform or inverse transform on an array of data.  This data can represent a single, double, or triple dimensional system.  


TYPE REQUIREMENTS

TYPE
FLOAT =  EXTENDED OR DOUBLE OR REAL;
FIXXED = INTEGER OR LONGINT;
DATA = ARRAY[1..NDP*2] OF FLOAT;


CALLING PROCEDURE

VAR
D:DATA;
NN:ARRAY[1..3] OF FIXXED;
NDIM,ISIGN,IFORM:FIXXED;

D, of type DATA, is the data to be transformed.   The first element of the array is the real part of the data to be transformed. The second element is a complex value. This alternation continues until the array is filled.  For a two dimensional, (a[i,j]), system the array may be filled as follows.

for j:=1 to number_of_columns   do
 for i:=1 to number_of_rows       do
  begin
    readln(realpart,imagpart)
    D[(i+(j-1)*number_of_rows)*2-1]=realpart
    D[(i+(j-1)*number_of_rows)*2]=imagpart
  end;

For a three dimensional system assume the system is defined as
a[1..kk,1..jj,1..ii].  The data would be read into d as follows.





for i:=1 to ii do
 for j:=1 to jj do
  for k:=1 to kk do
   begin
    readln(realpart,imagpart);
    d[(k+(j-1)*kk+(i-1)*kk*jj)*2-1]:=realpart;
    d[(k+(j-1)*kk+(i-1)*kk*jj)*2]:=imagpart;
   end;

On return from the routine, D contains the transformed data. One further note on D.  It should be dimensioned a size two times the number of points for which the transform is being taken to account for both the real and complex portion of the data.

NN of type Array[1..3] of FIXXED, contains the number of points in each dimension.  In the above example, NN[1]=number_of_rows, NN[2]=number_of_colomns. NN[3]=0.

NDIM of type FIXXED, the number of dimensions 1,2, or 3.

ISIGN of type FIXXED,  implies the direction of the transform. -1 implies forward, +1 implies the inverse transform.  

IFORM of type FIXXED,  if the complex portion of the data is zero, set IFORM:=0.  If the complex portion of the data is not zero, set IFORM:=1.  If IFORM is zero, the transform is much faster.  This applies only for FFT512.  For FFT128 IFORM is a dummy argument.

The calling procedure is simple
Define the variables as mentioned above and then

FFT512(D,ISIGN,IFORM,NDIM,NN);

When the routine finishes, D will contain the transformed data.

It is recomended when using this routine with LightSpeed Pascal, that only Longint integers should be used.  This will prevent an Integer overflow when large arrays are being transformed.  There is also an array in FFT512 and FFT128 called WORK.  It should be dimensioned twice as large as the the largest dimension of the data which is not a power of two.  If all dimensions are powers of two, then set the dimension of work to [1..2].  





EXAMPLE

The example program for  FFT512 reads in from the keyboard the number of real and complex data points for a single dimensional array.  It then reads in type data in pairs.  The data is transformed and printed back to the screen.

The example program for FFT128 transforms a four by four function in two dimensions after reading the data from the keyboard.  The transformed data is then printed to the screen.


A NOTE ON SPEED.  

As mentioned in the introductory remarks, LightSpeed Pascal is faster if extended precision numbers are used.  They can also use up space quickly.  In general, the more factorable the number of data values in each dimension is, the faster the routine will run.  For example a 3600 point transfer will run slightly faster than a 3500 point transform.  However, a 4098  (212) point transform is much quicker.  Also set IFORM equal to zero whenever possible.


REFERENCES

BRIGHAM, E. O.:  The Fast Fourier Transform, Prentice-Hall, Englewood Cliffs, 1970.

Baase, S.:  Computer Algorithms:  Introduction to Design and Analysis, Addison-Wesley, Menlo Park 1983.

Pavlidis,T.: Algorithms for Graphics and Image Processing, Computer Science Press, Rockville, 1982. 

PROGRAM FOURIER_TRANS;
CONST
MAXSIZE = 1024;
TYPE
FLOAT = REAL;
FIXXED = INTEGER;
DATA = ARRAY[1..MAXSIZE] OF FLOAT;
INTDATA = ARRAY[1..3] OF FIXXED;
VAR
REAL_PART, COMPLEX_PART : FLOAT;
D : DATA;
NDIM, ISIGN, IFORM, IOUT, COUNT : FIXXED;
NN : INTDATA;
PROCEDURE see;
VAR
R : Rect;
BEGIN
HideAll;
SetRect(R, 0, 35, 550, 330);
SettextRect(R);
Showtext;
END;
PROCEDURE FFT512 (VAR D : DATA;
ISIGN, IFORM, NDIM : FIXXED;
NN : INTDATA);
{*                         COPYWRITE COHERENT COGNITION 1985                        *}
VAR
I, I1, I1MAX, I1RNG, I2, I2MAX, I3, ICASE : FIXXED;
IDIM, IDIV, IF2, IFMIN : FIXXED;
IFP1, IFP2, IMAX, IMIN, INON2, IPAR, IQUOT, IREM : FIXXED;
J, J1, J2, J2MAX, J3, J3MAX, JMAX, JMIN : FIXXED;
K1, K2, K3, K4, KDIF, KMIN, KSTEP, L, LMAX : FIXXED;
M, MMAX, N, NHALF, NON2P, NP0 : FIXXED;
NP1, NP1TW, NP2, NP2HF, NPREV : FIXXED;
NTOT, NTWO, NWORK : FIXXED;
DIFI, DIFR : FLOAT;
OLDSI, OLDSR, RTHLF, SI, SR, STMPI, STMPR, SUMI, SUMR : FLOAT;
T2I, T2R, T3I, T3R, T4I, T4R, TI, TR, THETA, THETM : FLOAT;
TWOPI, TWOWR, U1I, U1R, U2I, U2R, U3I, U3R, U4I, U4R : FLOAT;
W2I, W2R, W3I, W3R, WI, WMINI, WMINR, WR : FLOAT;
WSTPI, WSTPR, WT : FLOAT;
W : DATA;
IFACT : ARRAY[1..16] OF FIXXED;
PROCEDURE zeroit;
VAR
izero : fixxed;
BEGIN
FOR izero := 1 TO 16 DO
IFACT[izero] := 0;
I := 0;
I1 := 0;
I1MAX := 0;
I1RNG := 0;
I2 := 0;
I2MAX := 0;
I3 := 0;
ICASE := 0;
IDIM := 0;
IDIV := 0;
IF2 := 0;
IFMIN := 0;
IFP1 := 0;
IFP2 := 0;
IMAX := 0;
IMIN := 0;
INON2 := 0;
IPAR := 0;
IQUOT := 0;
IREM := 0;
J := 0;
J1 := 0;
J2 := 0;
J2MAX := 0;
J3 := 0;
J3MAX := 0;
JMAX := 0;
JMIN := 0;
K1 := 0;
K2 := 0;
K3 := 0;
K4 := 0;
KDIF := 0;
KMIN := 0;
KSTEP := 0;
L := 0;
LMAX := 0;
M := 0;
MMAX := 0;
N := 0;
NHALF := 0;
NON2P := 0;
NP0 := 0;
NP1 := 0;
NP1TW := 0;
NP2 := 0;
NP2HF := 0;
NPREV := 0;
NTOT := 0;
NTWO := 0;
NWORK := 0;
END;
PROCEDURE Z_299;
VAR
J, IOUT : INTEGER;
FLAG1 : BOOLEAN;
BEGIN
  {THE NEXT LINE WAS ADDED}
NON2P := 0;
N := NN[IDIM];
NP2 := NP1 * N;
M := N;
NTWO := NP1;
IF2 := 1;
IDIV := 2;
IQUOT := M DIV IDIV;
IREM := M - IDIV * IQUOT;
WHILE ((IQUOT - IDIV) >= 0) AND (IREM = 0) DO
BEGIN
NTWO := NTWO + NTWO;
IFACT[IF2] := IDIV;
IF2 := IF2 + 1;
M := IQUOT;
IQUOT := M DIV IDIV;
IREM := M - IDIV * IQUOT;
END;
IF ((IQUOT - IDIV) < 0) THEN
BEGIN
INON2 := IF2;
IF IREM = 0 THEN
NTWO := NTWO + NTWO
ELSE
IFACT[IF2] := M
END
ELSE
BEGIN
IDIV := 3;
INON2 := IF2;
IQUOT := M DIV IDIV;
IREM := M - IDIV * IQUOT;
WHILE (IQUOT - IDIV) >= 0 DO
BEGIN
IF IREM = 0 THEN
BEGIN
IFACT[IF2] := IDIV;
IF2 := IF2 + 1;
M := IQUOT;
IQUOT := M DIV IDIV;
IREM := M - IDIV * IQUOT;
END
ELSE
BEGIN
IDIV := IDIV + 2;
IQUOT := M DIV IDIV;
IREM := M - IDIV * IQUOT;
END
END;
IFACT[IF2] := M;
NON2P := NP2 DIV NTWO;
END;
ICASE := 1;
IFMIN := 1;
I1RNG := NP1;
IF (((IDIM - 4) < 0) AND (IFORM <= 0)) THEN
BEGIN
ICASE := 2;
I1RNG := NP0 * (1 + NPREV DIV 2);
IF ((IDIM - 1) <= 0) THEN
BEGIN
ICASE := 3;
I1RNG := NP1;
IF ((NTWO - NP1) > 0) THEN
BEGIN
ICASE := 4;
IFMIN := 2;
NTWO := NTWO DIV 2;
N := N DIV 2;
NP2 := NP2 DIV 2;
NTOT := NTOT DIV 2;
I := 1;
FOR J := 1 TO NTOT DO
BEGIN
D[J] := D[I];
I := I + 2;
END;
END;
END;
END;
IF ((NON2P - 1) <= 0) THEN
BEGIN
NP2HF := NP2 DIV 2;
J := 1;
I2 := 1;
REPEAT
IF ((J - I2) < 0) THEN
BEGIN
I1MAX := I2 + NP1 - 2;
I1 := I2;
REPEAT
I3 := I1;
REPEAT
J3 := J + I3 - I2;
TR := D[I3];
TI := D[I3 + 1];
D[I3] := D[J3];
D[I3 + 1] := D[J3 + 1];
D[J3] := TR;
D[J3 + 1] := TI;
I3 := I3 + NP2;
UNTIL (I3 > NTOT);
I1 := I1 + 2;
UNTIL (I1 > I1MAX);
END;
M := NP2HF;
IF ((J - M) > 0) THEN
BEGIN
REPEAT
J := J - M;
M := M DIV 2;
UNTIL (((M - NP1) < 0) OR ((J - M) <= 0));
END;
J := J + M;
I2 := I2 + NP1;
UNTIL (I2 > NP2);
END;
IF ((NON2P - 1) > 0) THEN
BEGIN
NWORK := 2 * N;
I1 := 1;
REPEAT
I3 := I1;
REPEAT
J := I3;
I := 1;
REPEAT
IF (ICASE <> 3) THEN
BEGIN
W[I] := D[J];
W[I + 1] := D[J + 1];
END
ELSE
BEGIN
W[I] := D[J];
W[I + 1] := 0.0;
END;
IFP2 := NP2;
IF2 := IFMIN;
IFP1 := IFP2 DIV IFACT[IF2];
J := J + IFP1;
FLAG1 := FALSE;
REPEAT
FLAG1 := FALSE;
IF ((J - I3 - IFP2) >= 0) THEN
BEGIN
FLAG1 := TRUE;
J := J - IFP2;
IFP2 := IFP1;
IF2 := IF2 + 1;
IF ((IFP2 - NP1) <= 0) THEN
FLAG1 := FALSE;
END;
IF FLAG1 THEN
BEGIN
IFP1 := IFP2 DIV IFACT[IF2];
J := J + IFP1;
END;
UNTIL (NOT (FLAG1));
I := I + 2;
UNTIL (I > NWORK);
I2MAX := I3 + NP2 - NP1;
I := 1;
I2 := I3;
REPEAT
D[I2] := W[I];
D[I2 + 1] := W[I + 1];
I := I + 2;
I2 := I2 + NP1;
UNTIL (I2 > I2MAX);
I3 := I3 + NP2;
UNTIL (I3 > NTOT);
I1 := I1 + 2;
UNTIL (I1 > NP1);
END;
END;
FUNCTION MAX0 (J, K : LONGINT) : LONGINT;
VAR
T : LONGINT;
BEGIN
T := J;
IF (K > J) THEN
T := K;
MAX0 := T;
END;
PROCEDURE THREE_599;
VAR
FLAG2 : BOOLEAN;
IOUT : LONGINT;
BEGIN
NP1TW := NP1 + NP1;
IPAR := NTWO DIV NP1;
WHILE ((IPAR - 2) > 0) DO
IPAR := IPAR DIV 4;
IF ((IPAR - 2) = 0) THEN
BEGIN
I1 := 1;
REPEAT
K1 := I1;
REPEAT
K2 := K1 + NP1;
TR := D[K2];
TI := D[K2 + 1];
D[K2] := D[K1] - TR;
D[K2 + 1] := D[K1 + 1] - TI;
D[K1] := D[K1] + TR;
D[K1 + 1] := D[K1 + 1] + TI;
K1 := K1 + NP1TW;
UNTIL (K1 > NTOT);
I1 := I1 + 2;
UNTIL (I1 > I1RNG);
END;
MMAX := NP1;
WHILE ((MMAX - NTWO DIV 2) < 0) DO
BEGIN
LMAX := MAX0(NP1TW, MMAX DIV 2);
L := NP1;
REPEAT
M := L;
FLAG2 := FALSE;
IF ((MMAX - NP1) > 0) THEN
BEGIN
FLAG2 := TRUE;
THETA := (-TWOPI * L) / (4.0 * MMAX);
IF (ISIGN >= 0) THEN
THETA := -THETA;
WR := COS(THETA);
WI := SIN(THETA);
END;
REPEAT
IF (FLAG2) THEN
BEGIN
W2R := WR * WR - WI * WI;
W2I := 2.0 * WR * WI;
W3R := W2R * WR - W2I * WI;
W3I := W2R * WI + W2I * WR;
END;
FLAG2 := TRUE;
I1 := 1;
REPEAT

KMIN := I1 + IPAR * M;
IF ((MMAX - NP1) <= 0) THEN
KMIN := I1;
KDIF := IPAR * MMAX;
KSTEP := 4 * KDIF;
WHILE ((KSTEP - NTWO) <= 0) DO
BEGIN
BEGIN
K1 := KMIN;
REPEAT
K2 := K1 + KDIF;
K3 := K2 + KDIF;
K4 := K3 + KDIF;
IF ((MMAX - NP1) <= 0) THEN
BEGIN
U1R := D[K1] + D[K2];
U1I := D[K1 + 1] + D[K2 + 1];
U2R := D[K3] + D[K4];
U2I := D[K3 + 1] + D[K4 + 1];
U3R := D[K1] - D[K2];
U3I := D[K1 + 1] - D[K2 + 1];
IF (ISIGN < 0) THEN
BEGIN
U4R := D[K3 + 1] - D[K4 + 1];
U4I := D[K4] - D[K3];
END
ELSE
BEGIN
U4R := D[K4 + 1] - D[K3 + 1];
U4I := D[K3] - D[K4];
END;
END
ELSE
BEGIN
T2R := W2R * D[K2] - W2I * D[K2 + 1];
T2I := W2R * D[K2 + 1] + W2I * D[K2];
T3R := WR * D[K3] - WI * D[K3 + 1];
T3I := WR * D[K3 + 1] + WI * D[K3];
T4R := W3R * D[K4] - W3I * D[K4 + 1];
T4I := W3R * D[K4 + 1] + W3I * D[K4];
U3R := D[K1] - T2R;
U1I := D[K1 + 1] + T2I;
U2R := T3R + T4R;
U3I := D[K1 + 1] - T2I;
U1R := D[K1] + T2R;
U2I := T3I + T4I;
IF (ISIGN < 0) THEN
BEGIN
U4R := T3I - T4I;
U4I := T4R - T3R;
END;
IF (ISIGN > 0) THEN
BEGIN
U4R := T4I - T3I;
U4I := T3R - T4R;
END;
END;
D[K1] := U1R + U2R;
D[K1 + 1] := U1I + U2I;
D[K2] := U3R + U4R;
D[K2 + 1] := U3I + U4I;
D[K3] := U1R - U2R;
D[K3 + 1] := U1I - U2I;
D[K4] := U3R - U4R;
D[K4 + 1] := U3I - U4I;
K1 := K1 + KSTEP;
UNTIL (K1 > NTOT);
END;
KDIF := KSTEP;
KMIN := 4 * (KMIN - I1) + I1;
KSTEP := 4 * KDIF;
END;
I1 := I1 + 2
UNTIL (I1 > I1RNG);
M := M + LMAX;
IF ((M - MMAX) <= 0) THEN
BEGIN
IF (ISIGN < 0) THEN
BEGIN
TR := WR;
WR := (WR + WI) * RTHLF;
WI := (WI - TR) * RTHLF
END
ELSE
BEGIN
TR := WR;
WR := (WR - WI) * RTHLF;
WI := (TR + WI) * RTHLF;
END;
END;
UNTIL ((M - MMAX) > 0);
L := L + NP1TW;
UNTIL (L > LMAX);
IPAR := 3 - IPAR;
MMAX := MMAX + MMAX;
END;
END;
PROCEDURE SIX_699;
VAR
TEST : BOOLEAN;
IOUT : LONGINT;
BEGIN
IFP1 := NTWO;
IF2 := INON2;
REPEAT
IFP2 := IFACT[IF2] * IFP1;
THETA := -TWOPI / IFACT[IF2];
IF (ISIGN > 0) THEN
THETA := -THETA;
WSTPR := COS(THETA);
WSTPI := SIN(THETA);
J1 := 1;
REPEAT
THETM := (J1 - 1) * (-TWOPI) / IFP2;
IF (ISIGN >= 0) THEN
THETM := -THETM;
WMINR := COS(THETM);
WMINI := SIN(THETM);
I1MAX := J1 + I1RNG - 2;
I1 := J1;
REPEAT
I3 := I1;
REPEAT
I := 1;
WR := WMINR;
WI := WMINI;
J2MAX := I3 + IFP2 - IFP1;
J2 := I3;
REPEAT
TWOWR := WR + WR;
J3MAX := J2 + NP2 - IFP2;
J3 := J2;
REPEAT
JMIN := J3 - J2 + I3;
J := JMIN + IFP2 - IFP1;
SR := D[J];
SI := D[J + 1];
OLDSR := 0.0;
OLDSI := 0.0;
J := J - IFP1;
REPEAT
STMPR := SR;
STMPI := SI;
SR := TWOWR * SR - OLDSR + D[J];
SI := TWOWR * SI - OLDSI + D[J + 1];
OLDSR := STMPR;
OLDSI := STMPI;
J := J - IFP1;
UNTIL ((J - JMIN) <= 0);
W[I] := WR * SR - WI * SI - OLDSR + D[J];
W[I + 1] := WI * SR + WR * SI - OLDSI + D[J + 1];
I := I + 2;
J3 := J3 + IFP2;
UNTIL (J3 > J3MAX);
WT := WR * WSTPI;
WR := WR * WSTPR - WI * WSTPI;
WI := WI * WSTPR + WT;
J2 := J2 + IFP1;
UNTIL (J2 > J2MAX);
I := 1;
J2 := I3;
REPEAT
J3MAX := J2 + NP2 - IFP2;
J3 := J2;
REPEAT
D[J3] := W[I];
D[J3 + 1] := W[I + 1];
I := I + 2;
J3 := J3 + IFP2;
UNTIL (J3 > J3MAX);
J2 := J2 + IFP1;
UNTIL (J2 > J2MAX);
I3 := I3 + NP2;
UNTIL (I3 > NTOT);
I1 := I1 + 2;
UNTIL (I1 > I1MAX);
J1 := J1 + NP1;
UNTIL (J1 > IFP1);
IF2 := IF2 + 1;
IFP1 := IFP2;
UNTIL ((IFP1 - NP2) >= 0);
END;
PROCEDURE SEVEN_799;
VAR
FLAG3 : BOOLEAN;
BEGIN
NHALF := N;
N := N + N;
THETA := -TWOPI / N;
IF (ISIGN >= 0) THEN
THETA := -THETA;
WSTPR := COS(THETA);
WSTPI := SIN(THETA);
WR := WSTPR;
WI := WSTPI;
IMIN := 3;
JMIN := 2 * NHALF - 1;
IF ((IMIN - JMIN) < 0) THEN
REPEAT
J := JMIN;
I := IMIN;
REPEAT
SUMR := (D[I] + D[J]) / 2.0;
SUMI := (D[I + 1] + D[J + 1]) / 2.0;
DIFR := (D[I] - D[J]) / 2.0;
DIFI := (D[I + 1] - D[J + 1]) / 2.0;
TR := WR * SUMI + WI * DIFR;
TI := WI * SUMI - WR * DIFR;
D[I] := SUMR + TR;
D[I + 1] := DIFI + TI;
D[J] := SUMR - TR;
D[J + 1] := -DIFI + TI;
J := J + NP2;
I := I + NP2;
UNTIL (I > NTOT);
IMIN := IMIN + 2;
JMIN := JMIN - 2;
WT := WR * WSTPI;
WR := WR * WSTPR - WI * WSTPI;
WI := WI * WSTPR + WT;
UNTIL ((IMIN - JMIN) >= 0);
IF (((IMIN - JMIN) = 0) AND (ISIGN < 0)) THEN
BEGIN
I := IMIN;
REPEAT
D[I + 1] := (-D[I + 1]);
I := I + NP2;
UNTIL (I > NTOT);
END;
NP2 := NP2 + NP2;
NTOT := NTOT + NTOT;
J := NTOT + 1;
IMAX := NTOT DIV 2 + 1;
REPEAT
IMIN := IMAX - 2 * NHALF;
I := IMIN;
I := I + 2;
J := J - 2;
WHILE ((I - IMAX) < 0) DO
BEGIN
D[J] := D[I];
D[J + 1] := -D[I + 1];
I := I + 2;
J := J - 2;
END;
D[J] := D[IMIN] - D[IMIN + 1];
D[J + 1] := 0.0;
FLAG3 := FALSE;
IF ((I - J) < 0) THEN
BEGIN
FLAG3 := TRUE;
I := I - 2;
J := J - 2;
WHILE ((I - IMIN) > 0) DO
BEGIN
D[J] := D[I];
D[J + 1] := D[I + 1];
I := I - 2;
J := J - 2;
END;
END;
UNTIL NOT FLAG3;
D[1] := D[1] + D[2];
D[2] := 0.0;
END;
PROCEDURE EIGHT_899;
BEGIN
I3 := 1;
REPEAT
I2MAX := I3 + NP2 - NP1;
I2 := I3;
REPEAT
IMIN := I2 + I1RNG;
IMAX := I2 + NP1 - 2;
JMAX := 2 * I3 + NP1 - IMIN;
IF ((I2 - I3) > 0) THEN
JMAX := JMAX + NP2;
IF ((IDIM - 2) > 0) THEN
BEGIN
J := JMAX + NP0;
I := IMIN;
REPEAT
D[I] := D[J];
D[I + 1] := -D[J + 1];
J := J - 2;
I := I + 2;
UNTIL (I > IMAX);
END;
J := JMAX;
I := IMIN;
REPEAT
D[I] := D[J];
D[I + 1] := -D[J + 1];
J := J - NP0;
I := I + NP0;
UNTIL (I > IMAX);
I2 := I2 + NP1;
UNTIL (I2 > I2MAX);
I3 := I3 + NP2;
UNTIL (I3 > NTOT);
END;
BEGIN
TWOPI := 8.0 * ARCTAN(1.0);
RTHLF := 1.0 / SQRT(2.0);
NTOT := 2;
FOR IDIM := 1 TO NDIM DO
NTOT := NTOT * NN[IDIM];
NP1 := 2;
FOR IDIM := 1 TO NDIM DO
BEGIN
Z_299;
IF ((NTWO - NP1) > 0) THEN
BEGIN
THREE_599;
IF ((NON2P - 1) > 0) THEN
SIX_699;
END
ELSE IF ((NON2P - 1) > 0) THEN
SIX_699;
IF (ICASE = 4) THEN
SEVEN_799;
IF (ICASE = 2) THEN
EIGHT_899;
NP0 := NP1;
NP1 := NP2;
NPREV := N;
END;
END;
BEGIN
see;
IFORM := 1;
ISIGN := -1;
NDIM := 1;
WRITELN('HOW MANY DATA POINTS FOR FFT');
READLN(NN[1]);
NN[2] := 0;
NN[3] := 0;
FOR IOUT := 1 TO NN[1] DO
BEGIN
WRITELN('ENTER REAL AND COMPLEX POINT');
READLN(REAL_PART, COMPLEX_PART);
D[((IOUT - 1) * 2 + 1)] := REAL_PART;
D[(IOUT * 2)] := COMPLEX_PART;
END;
FFT512(D, ISIGN, IFORM, NDIM, NN);
writeln('AND THE ANSWER IS');
writeln('***********************************************');
FOR IOUT := 1 TO NN[1] DO
writeln(D[(IOUT - 1) * 2 + 1] : 20 : 10, D[IOUT * 2] : 20 : 10);
writeln('***********************************************');
END.





EIGEN  [procedure]



PURPOSE
 
EIGEN finds eigenvalues and eignevectors for the eigenvalues from a matrix "AI".   An iteration procedure is used to find the smallest or largest eigenvalue and then the associated eigenvector.   Then "S" other eigenvalues are found sweeping.  (S is an input parameter.)  There is a boolean input parameter "Small" which determines if the smallest or largest eigenvalue is found first.  If Small is TRUE then the smallest eigenvalue is found followed by the rest of the "S" eigenvalues.  If small is TRUE the the matrix AI is inverted as a by product of the procedure to find the eigenvalues.


TYPE REQUIREMENTS
CONST
MAXSIZE = THE MAXIMUM SIZE OF THE INPUT ARRAY
MAXPLUS1 = MAXSIZE +1

Type
  FLOAT = extended or real;
  FIXXED = INTEGER;
  SQUARE = array[1..MAXSIZE, 1..MAXSIZE] of FLOAT;
  MYRECT = array[1..MAXPLUS1, 1..MAXSIZE] of FLOAT;
  RAY = array[1..MAXSIZE] of FLOAT;


CALLING PROCEDURE

var
AI:SQUARE;{matrix of interest}
Z:Square;{output matrix of eigenvectors};
Lambda:ray;{Array of eigenvalues}
N:fixxed;{size of the matrix}
S:FIXXED;{How many eigenvalues to find}
Epsi:float;{Accuracy requirement.  determines when to stop iteration in finding the first eigenvalues}
SMALL:boolean; {if small is true the smallest eigenvalue will be found first and AI will be inverted.  If small is false the the largest eigenvalue will be found first}
ITERATE:FIXXED;{Number of times MULTI_DIFEQ will iterate before returning}

Define the procedure Eigen.  The calling sequence to Eigen is 

 EIGEN(AI, LAMBDA, Z, N, S, EPSI, SMALL,ITERATE);

Define AI,N,S,EPSI and SMALL.   Then call the procedure.

 EIGEN(AI, LAMBDA, Z, N, S, EPSI, SMALL,ITERATE);

When the procerdure finishes, the matrix "Z" will contain the eigenvectors and "Lambda" will contain the eigenvalues.  The N components of the eigenvector of corresponding to the first eigenvalue are in Z[1,1],Z[1,2]..Z[1,N].


EXAMPLE

The example defines a 3 x 3 matrix and finds 3 eigenvalues and eigenvectors starting with the largest.  The eigenvalues and vectors are printed.


REFERENCES
James, M. L., Smith, G. M., Wolford, J. C.:  Applied Numerical Methods for 
Digital Computation With Fortran and CSMP, Harper and Row, New York, 
1977.
  PROGRAM DOEIGEN;
CONST
MAXSIZE = 10;
MAXPLUS1 = 11;
TYPE
FLOAT = extended;
FIXXED = INTEGER;
SQUARE = ARRAY[1..MAXSIZE, 1..MAXSIZE] OF FLOAT;
MYRECT = ARRAY[1..MAXPLUS1, 1..MAXSIZE] OF FLOAT;
data = ARRAY[1..MAXSIZE] OF FLOAT;
VAR
inmatrix, eigenvector : SQUARE;
N, S, SP1, I, L, ITMAX : FIXXED;
SMALL : BOOLEAN;
EPSI : FLOAT;
LAMBDA : data;
PROCEDURE see;
VAR
R : Rect;
BEGIN
HideAll;
SetRect(R, 0, 35, 550, 330);
SettextRect(R);
Showtext;
END;
PROCEDURE eigen (VAR inmatrix : SQUARE;
VAR LAMBDA : data;
VAR eigenvector : SQUARE;
N, SIN : FIXXED;
EPSI : FLOAT;
OVER : BOOLEAN;
ITMAX : FIXXED);
VAR
BACK : ARRAY[1..maxsize, 1..2] OF FIXXED;
K, JJ, KP1, I, J, L, KROW, IROW : FIXXED;
rot, TEMP : FLOAT;
S : INTEGER;
X, D, C, G, ADD, Y, XX : data;
REC1, REC2, REC3 : MYRECT;
SUM : FLOAT;
ITERATE, IP1, II, KK : FIXXED;
done10, done20, done30, done40 : BOOLEAN;
BEGIN
done10 := FALSE;
done20 := FALSE;
done30 := FALSE;
done40 := FALSE;
S := SIN - 1;
IF (OVER) THEN
BEGIN
FOR K := 1 TO n DO
BEGIN
JJ := K;
IF (K <> n) THEN
BEGIN
KP1 := K + 1;
rot := ABS(inmatrix[K, K]);
FOR I := KP1 TO n DO
BEGIN
TEMP := ABS(inmatrix[I, K]);
IF (rot < TEMP) THEN
BEGIN
rot := TEMP;
JJ := I;
END;
END;
END;
BACK[K, 1] := K;
BACK[K, 2] := JJ;
IF (JJ <> K) THEN
FOR J := 1 TO n DO
BEGIN
TEMP := inmatrix[JJ, J];
inmatrix[JJ, J] := inmatrix[K, J];
inmatrix[K, J] := TEMP;
END;
FOR J := 1 TO n DO
IF (J <> K) THEN
inmatrix[K, J] := inmatrix[K, J] / inmatrix[K, K];
inmatrix[K, K] := 1.0 / inmatrix[K, K];
FOR I := 1 TO n DO
IF (I <> K) THEN
FOR J := 1 TO n DO
IF (J <> K) THEN
inmatrix[I, J] := inmatrix[I, J] - inmatrix[K, J] * inmatrix[I, K];
FOR I := 1 TO n DO
IF (I <> K) THEN
inmatrix[I, K] := -inmatrix[I, K] * inmatrix[K, K];
END;
FOR L := 1 TO n DO
BEGIN
K := n - L + 1;
KROW := BACK[K, 1];
IROW := BACK[K, 2];
IF (KROW <> IROW) THEN
FOR I := 1 TO n DO
BEGIN
TEMP := inmatrix[I, KROW];
inmatrix[I, KROW] := inmatrix[I, IROW];
inmatrix[I, IROW] := TEMP;
END;
END;
END;
FOR I := 1 TO N DO
BEGIN
X[I] := 1.0 / SQR(1.0 * N) + 1.0 / (1.0 * I);
END;
ITERATE := 0;

REPEAT
FOR I := 1 TO N DO
BEGIN
D[I] := 0.0;
FOR J := 1 TO N DO
D[I] := D[I] + inmatrix[I, J] * X[J];
END;
ITERATE := ITERATE + 1;
FOR I := 1 TO N DO
eigenvector[1, I] := D[I] / D[1];
I := 1;
REPEAT

IF (ABS(X[I] - eigenvector[1, I]) - EPSI * eigenvector[1, I] >= 0) THEN
done10 := TRUE;
I := I + 1;
UNTIL (done10 OR (I > N));
IF (NOT (done10)) THEN
done20 := TRUE;

IF (NOT (done20)) THEN
BEGIN
FOR I := 1 TO N DO
X[I] := eigenvector[1, I]
END;
IF (ITERATE > ITMAX) THEN
done20 := TRUE;
UNTIL (done20);

LAMBDA[1] := 1.0 / D[1];
FOR I := 1 TO S DO   {100 START}
BEGIN
IP1 := I + 1;
FOR L := 1 TO N DO
REC1[IP1, L] := 1.0;
G[I] := 0.0;
FOR L := 1 TO N DO
G[I] := G[I] + eigenvector[I, L] * eigenvector[I, L];
ITERATE := 0;
REPEAT
done30 := FALSE;

FOR II := 1 TO I DO
BEGIN
SUM := 0.0;
FOR L := 1 TO N DO
SUM := SUM + eigenvector[II, L] * REC1[IP1, L];
C[II] := SUM / G[II];
END;
ITERATE := ITERATE + 1;
FOR L := 1 TO N DO
ADD[L] := 0.0;
FOR K := 1 TO I DO
BEGIN
FOR L := 1 TO N DO
BEGIN
Y[L] := C[K] * eigenvector[K, L];
ADD[L] := ADD[L] + Y[L];
END;
END;
FOR L := 1 TO N DO
REC2[IP1, L] := REC1[IP1, L] - ADD[L];
FOR II := 1 TO N DO
BEGIN
XX[II] := 0;
FOR KK := 1 TO N DO
XX[II] := XX[II] + inmatrix[II, KK] * REC2[IP1, KK];
END;
FOR L := 1 TO N DO
REC3[IP1, L] := XX[L] / XX[1];
L := 1;
done40 := FALSE;
REPEAT
IF ((ABS(REC1[IP1, L] - REC3[IP1, L]) - EPSI * REC3[IP1, L]) > 0.0) THEN
done40 := TRUE;
L := L + 1;
UNTIL ((L > N) OR (done40));
IF (done40) THEN
BEGIN  {24}
FOR L := 1 TO N DO
REC1[IP1, L] := REC3[IP1, L];
END;
IF (ITERATE > ITMAX) THEN
done30 := TRUE;
UNTIL (done30);
IF (over) THEN
LAMBDA[IP1] := 1.0 / XX[1]
ELSE
LAMBDA[IP1] := XX[1];

FOR L := 1 TO N DO
eigenvector[IP1, L] := REC3[IP1, L];
END;
IF (NOT over) THEN
lambda[1] := 1.0 / lambda[1];
END;
BEGIN
see;
inmatrix[1, 1] := 20.0;
inmatrix[1, 2] := -10.0;
inmatrix[1, 3] := 0.0;
inmatrix[2, 1] := -10.0;
inmatrix[2, 2] := 20.0;
inmatrix[2, 3] := -10.0;
inmatrix[3, 1] := 0.0;
inmatrix[3, 2] := -10.0;
inmatrix[3, 3] := 10.0;
N := 3;
S := 3;
EPSI := 1E-3;
ITMAX := 50;
writeln(' input matrix');
writeln(inmatrix[1, 1] : 10 : 5, inmatrix[1, 2] : 10 : 5, inmatrix[1, 3] : 10 : 5);
writeln(inmatrix[2, 1] : 10 : 5, inmatrix[2, 2] : 10 : 5, inmatrix[2, 3] : 10 : 5);
writeln(inmatrix[3, 1] : 10 : 5, inmatrix[3, 2] : 10 : 5, inmatrix[3, 3] : 10 : 5);
writeln;
SMALL := false;
EIGEN(inmatrix, LAMBDA, eigenvector, N, S, EPSI, SMALL, ITMAX);
FOR I := 1 TO S DO
BEGIN
WRITELN;
WRITELN('  LAMBDA(', I : 2, ')=', LAMBDA[I] : 20 : 10);
WRITELN(' THE  EIGENVECTOR IS ');
FOR L := 1 TO N DO
WRITELN(eigenvector[I, L] : 20 : 10);
END;
END.
