MODULE CalcD3;
IMPORT NbrInt, NbrRe, NbrCplx, MathRe, CalcFn;
CONST
Forward* = 9; Central* = 10; Backward* = 11;
VAR
epsilon, zero: NbrRe.Real;
PROCEDURE DoNothing( x: NbrRe.Real );
END DoNothing;
PROCEDURE DoCplxNothing( z: NbrCplx.Complex );
END DoCplxNothing;
PROCEDURE Solve*( f: CalcFn.ReArg; atX: NbrRe.Real; differencing: NbrInt.Integer ): NbrRe.Real;
VAR h, h2, h3, hOpt, hMin, power, result, temp: NbrRe.Real;
BEGIN
power := 4 / 5; hMin := MathRe.Power( NbrRe.Epsilon, power ); power := 1 / 5;
hOpt := ABS( atX ) * MathRe.Power( epsilon, power ); h := NbrRe.Max( hOpt, hMin );
temp := atX + h; DoNothing( temp ); h := temp - atX; h2 := h * h; h3 := h * h2;
IF differencing = Forward THEN
result := f( atX + 3 * h );
result := result - 3 * f( atX + 2 * h );
result := result + 3 * f( atX + h );
result := (result - f( atX )) / h3
ELSIF differencing = Backward THEN
result := f( atX );
result := result - 3 * f( atX - h );
result := result + 3 * f( atX - 2 * h );
result := (result - f( atX - 3 * h )) / h3
ELSE
result := f( atX + 2 * h );
result := result - 2 * f( atX + h );
result := result + 2 * f( atX - h );
result := (result - f( atX - 2 * h )) / (2 * h3)
END;
RETURN result
END Solve;
PROCEDURE SolveCplx*( f: CalcFn.CplxArg; atZ: NbrCplx.Complex; differencing: NbrInt.Integer ): NbrCplx.Complex;
VAR h, hOpt, hMin, power: NbrRe.Real; ch, ch2, ch3, result, temp: NbrCplx.Complex;
BEGIN
power := 4 / 5; hMin := MathRe.Power( NbrRe.Epsilon, power ); power := 1 / 5;
hOpt := NbrCplx.Abs( atZ ) * MathRe.Power( epsilon, power ); h := NbrRe.Max( hOpt, hMin );
NbrCplx.Set( h, h, ch );
temp := atZ + ch; DoCplxNothing( temp ); ch := temp - atZ; ch2 := ch * ch; ch3 := ch2 * ch;
IF differencing = Forward THEN
result := f( atZ + 3 * ch );
result := result - 3 * f( atZ + 2 * ch );
result := result + 3 * f( atZ + ch );
result := (result - f( atZ )) / ch3
ELSIF differencing = Backward THEN
result := f( atZ );
result := result - 3 * f( atZ - ch );
result := result + 3 * f( atZ - 2 * ch );
result := (result - f( atZ - 3 * ch )) / ch3
ELSE
result := f( atZ + 2 * ch );
result := result - 2 * f( atZ + ch );
result := result + 2 * f( atZ - ch );
result := (result - f( atZ - 2 * ch )) / (2 * ch3)
END;
RETURN result
END SolveCplx;
PROCEDURE SolveCplxRe*( f: CalcFn.CplxArg; atZ: NbrCplx.Complex; differencing: NbrInt.Integer ): NbrCplx.Complex;
VAR h, hOpt, hMin, power: NbrRe.Real; ch, ch2, ch3, result, temp: NbrCplx.Complex;
BEGIN
power := 4 / 5; hMin := MathRe.Power( NbrRe.Epsilon, power ); power := 1 / 5;
hOpt := NbrCplx.Abs( atZ ) * MathRe.Power( epsilon, power ); h := NbrRe.Max( hOpt, hMin );
NbrCplx.Set( h, zero, ch );
temp := atZ + ch; DoCplxNothing( temp ); ch := temp - atZ; ch2 := ch * ch; ch3 := ch2 * ch;
IF differencing = Forward THEN
result := f( atZ + 3 * ch );
result := result - 3 * f( atZ + 2 * ch );
result := result + 3 * f( atZ + ch );
result := (result - f( atZ )) / ch3
ELSIF differencing = Backward THEN
result := f( atZ );
result := result - 3 * f( atZ - ch );
result := result + 3 * f( atZ - 2 * ch );
result := (result - f( atZ - 3 * ch )) / ch3
ELSE
result := f( atZ + 2 * ch );
result := result - 2 * f( atZ + ch );
result := result + 2 * f( atZ - ch );
result := (result - f( atZ - 2 * ch )) / (2 * ch3)
END;
RETURN result
END SolveCplxRe;
PROCEDURE SolveCplxIm*( f: CalcFn.CplxArg; atZ: NbrCplx.Complex; differencing: NbrInt.Integer ): NbrCplx.Complex;
VAR h, hOpt, hMin, power: NbrRe.Real; ch, ch2, ch3, result, temp: NbrCplx.Complex;
BEGIN
power := 4 / 5; hMin := MathRe.Power( NbrRe.Epsilon, power ); power := 1 / 5;
hOpt := NbrCplx.Abs( atZ ) * MathRe.Power( epsilon, power ); h := NbrRe.Max( hOpt, hMin );
NbrCplx.Set( zero, h, ch );
temp := atZ + ch; DoCplxNothing( temp ); ch := temp - atZ; ch2 := ch * ch; ch3 := ch2 * ch;
IF differencing = Forward THEN
result := f( atZ + 3 * ch );
result := result - 3 * f( atZ + 2 * ch );
result := result + 3 * f( atZ + ch );
result := (result - f( atZ )) / ch3
ELSIF differencing = Backward THEN
result := f( atZ );
result := result - 3 * f( atZ - ch );
result := result + 3 * f( atZ - 2 * ch );
result := (result - f( atZ - 3 * ch )) / ch3
ELSE
result := f( atZ + 2 * ch );
result := result - 2 * f( atZ + ch );
result := result + 2 * f( atZ - ch );
result := (result - f( atZ - 2 * ch )) / (2 * ch3)
END;
RETURN result
END SolveCplxIm;
PROCEDURE SolveCplxAbs*( f: CalcFn.CplxArg; atZ: NbrCplx.Complex; differencing: NbrInt.Integer ): NbrCplx.Complex;
VAR h, hOpt, hMin, power: NbrRe.Real; ch, ch2, ch3, result, temp: NbrCplx.Complex;
BEGIN
power := 4 / 5; hMin := MathRe.Power( NbrRe.Epsilon, power ); power := 1 / 5;
hOpt := NbrCplx.Abs( atZ ) * MathRe.Power( epsilon, power ); h := NbrRe.Max( hOpt, hMin );
NbrCplx.SetPolar( h, zero, ch );
temp := atZ + ch; DoCplxNothing( temp ); ch := temp - atZ; ch2 := ch * ch; ch3 := ch2 * ch;
IF differencing = Forward THEN
result := f( atZ + 3 * ch );
result := result - 3 * f( atZ + 2 * ch );
result := result + 3 * f( atZ + ch );
result := (result - f( atZ )) / ch3
ELSIF differencing = Backward THEN
result := f( atZ );
result := result - 3 * f( atZ - ch );
result := result + 3 * f( atZ - 2 * ch );
result := (result - f( atZ - 3 * ch )) / ch3
ELSE
result := f( atZ + 2 * ch );
result := result - 2 * f( atZ + ch );
result := result + 2 * f( atZ - ch );
result := (result - f( atZ - 2 * ch )) / (2 * ch3)
END;
RETURN result
END SolveCplxAbs;
PROCEDURE SolveCplxArg*( f: CalcFn.CplxArg; atZ: NbrCplx.Complex; differencing: NbrInt.Integer ): NbrCplx.Complex;
VAR h, hOpt, hMin, power: NbrRe.Real; ch, ch2, ch3, result, temp: NbrCplx.Complex;
BEGIN
power := 4 / 5; hMin := MathRe.Power( NbrRe.Epsilon, power ); power := 1 / 5;
hOpt := NbrCplx.Arg( atZ ) * MathRe.Power( epsilon, power ); h := NbrRe.Max( hOpt, hMin );
NbrCplx.SetPolar( zero, h, ch );
temp := atZ + ch; DoCplxNothing( temp ); ch := temp - atZ; ch2 := ch * ch; ch3 := ch2 * ch;
IF differencing = Forward THEN
result := f( atZ + 3 * ch );
result := result - 3 * f( atZ + 2 * ch );
result := result + 3 * f( atZ + ch );
result := (result - f( atZ )) / ch3
ELSIF differencing = Backward THEN
result := f( atZ );
result := result - 3 * f( atZ - ch );
result := result + 3 * f( atZ - 2 * ch );
result := (result - f( atZ - 3 * ch )) / ch3
ELSE
result := f( atZ + 2 * ch );
result := result - 2 * f( atZ + ch );
result := result + 2 * f( atZ - ch );
result := (result - f( atZ - 2 * ch )) / (2 * ch3)
END;
RETURN result
END SolveCplxArg;
BEGIN
epsilon := 100 * NbrRe.Epsilon; zero := 0
END CalcD3.