Back

Note: Only properties and public methods are displayed

{Math types and constants: Basic Math Types, Interfaces, Constants and Math utilities)
author: Nikolai Shokhirev
http://www.shokhirev.com/nikolai.html,
http://www.chem.arizona.edu/~shokhirn/nikolai.html
created: 2003.01.01
last modified: 2004.12.12
ŠNikolai V. Shokhirev, 2001-2003
January 2004:
  Incorporated some definitions, names and functions from Earl F. Glynn's
  ComplexMathlibrary unit (www.efg2.com/Lab/Mathematics/ComplexMath.htm)
  They alre labeled "From EFG Unit" below.
  NOTE: the type Complex differs from TComplex in EFG Unit}
unit  uMatTypes;

interface

uses
  SysUtils;

type
  TArrayType =(atGeneric,    // [Lo..Hi] - arbitrary limits
               atNatural,    // [1..Dim] - one-based
               atZeroBased,  // [0..Dim-1] - zero-based
               atCentered);  // [-((Dim-1) div 2)..Dim div 2]

const
{ for double:  }
  cMachEps = 2.22044604925031e-16; { cMachEps+1 > 1  }
  cSqrtMachEps = 1.49011611938476e-8;
  MinFloat = 1.7e-308;
  MaxFloat = 1.7e+308;
  NegMaxFloat = -1.7e+308;
  SqrtMinFloat = 1.3e-154;
  SqrtMaxFloat = 1.3e+154;
  MaxExp =  308.0;
  ExpArg = 709.0;
  SqrtSqrtMinFloat = 1.1e-77;
(*
  for Extended:
  MinFloat = 3.4e-4932;
  MaxFloat = 1.1e+4932;
  NegMaxFloat = -1.1e+4932;
  SqrtMinFloat = 1.9e-2466;
  SqrtMaxFloat = 1.0e+2466;
  MaxExp =  4932.3;
  ExpArg = 11356.3;   // 4932*ln(10)
*)
  HighInt = 2147483647;
  LowInt = -2147483647;
  SafeFactor = 1024.0;

  TwoPI = 6.283185307179586; // From EFG Unit
  HalfLn2PI = 0.9189385332046727;  // HalfLn2PI = 0.5*(Ln(TwoPI));  [From EFG Unit]
type

  { Interface for the Restore objects }
  IRestore = interface
  end;

  { Interface for Comment: string }
  IComment = interface
    property Comment: string read GetComment write SetComment;
  end;

  { Object Comment: string }
  TComment = class(TInterfacedObject, IComment)
  protected
    function GetComment: string;
    procedure SetComment(const Value: string);
  public
    property Comment: string read GetComment write SetComment;
  end;

  { currently = longint }
  TInt   = longint;
  { currently = double }
  TFloat  =  double;

  { complex number }
  Complex = record
              Re,Im: TFloat;
            end;

  { Float function of x }
  TFloatFunction1D = function(const x: TFloat): TFloat;
  { Float function of x, y }
  TFloatFunction2D = function(x, y: TFloat): TFloat;
  { Float function of x, y, z}
  TFloatFunction3D = function(x, y, z: TFloat): TFloat;
  { Float function of n, x}
  TBasisFunction = function(n: TInt; x: TFloat): TFloat;

  TComplexfunction = function (const z: Complex): Complex; // From EFG Unit

  EComplexLnZero     = class(exception);  // From EFG Unit
  EComplexZerodivide = class(exception);  // From EFG Unit
  EComplexinvalidop  = class(exception);  // From EFG Unit
  EComplexZerotoZero = class(exception);  // From EFG Unit

  { Conversion functions for Complex }

  { Basic Conversion functions for Complex }
  function ComplexToStr(const z: Complex): string;
  { Conversion: zero Im is not diaplayed }
  function CmplxToStr(const z: Complex; Width: TInt; Decimals: TInt): string;
  { Conversion: zero Im is diaplayed }
  function CmplxToStr0(const z: Complex; Width: TInt; Decimals: TInt): string;
  { Conversion }
  function StrToCmplx(const s: string): Complex;

  { Polar To Complex Conversion: (r, angle) -> (Re, Im) }
  function PolarToComplex(r, angle: TFloat): Complex;
  { Complex To Polar Conversion: (Re, Im) -> (r, angle) }
  procedure ComplexToPolar(const z: Complex; var r, angle: TFloat);

  { Basic functions for Complex Calculations }

  { result = max(|z1.re-z2.re|,|z1.im-z2.im|) }
  function Diff0(const z1, z2: Complex): TFloat;
  { abs(c1.Re-c2.Re) < eps And abs(c1.Im-c2.Im) < eps }
  function SameComplex(const c1, c2: Complex; eps: TFloat): boolean;
  { result = x + i*y}
  function cmplx(x: TFloat; y: TFloat = 0.0): Complex;
  { result = x + i*y - same as cmplx [From EFG Unit] }
  function Cset (x: TFloat; y: TFloat = 0.0): Complex;
  { result = sqrt(sqr(z.Re)+sqr(x.Im)) = |z|  }
  function Cabs (const z: Complex): TFloat;
  { max(abs(z.re),abs(z.im)) }
  function Cabs0(const z: Complex): TFloat;
  { result = abs(z.Re) + abs(z.Im)    }
  function Cabs1(const z: Complex): TFloat;
  { result = sqr(z.Re)+ sqr(z.Im) = |z|^2  }
  function Cabs2(const z: Complex): TFloat;
  { result = sqr(z.Re)+ sqr(z.Im) = |z|^2 same as Cabs2 [From EFG Unit] }
  function CAbsSqr (const z: Complex): TFloat;
  { result = -z }
  function Cneg(const z: Complex): Complex;
  { result = 1/z }
  function Cinv(const z: Complex): Complex;
  { result = z.Re -i*z.Im }
  function conjug(const z: Complex): Complex;
  { result = z.Re -i*z.Im same as conjug [From EFG Unit]}
  function Cconjugate(const z: Complex): Complex;
  { result = z.Re }
  function Re(const z: Complex): TFloat;
  { result = z.Im }
  function Im(const z: Complex): TFloat;
  { result = z/r r := Cabs (z) }
  function Cunit(const z: Complex; var r: TFloat): Complex;
  { result = z + w }
  function Cadd(const z: Complex;  w: Complex): Complex; overload;
  { result = z + x }
  function Cadd(const z: Complex;  x: TFloat): Complex; overload;
  { result = x + w }
  function Cadd(x: TFloat; const w: Complex): Complex; overload;
  { result = z - w }
  function Csub(const z, w: Complex): Complex; overload;
  { result = z - x }
  function Csub(const z: Complex;  x: TFloat): Complex; overload;
  { result = x - w }
  function Csub(x: TFloat; const w: Complex): Complex; overload;
  { result = z * w }
  function Cmul(const z, w: Complex): Complex; overload;
  { result = x * w }
  function Cmul(x: TFloat; const w: Complex): Complex; overload;
  { result = z * x }
  function Cmul(const z: Complex;  x: TFloat): Complex; overload;
  { result = z / w }
  function Cdiv(const z, w: Complex): Complex; overload;
  { result = x / w }
  function Cdiv(x: TFloat; const w: Complex): Complex; overload;
  { result = z / x }
  function Cdiv(const z: Complex;  x: TFloat): Complex; overload;
  { result = exp(v)  }
  function Cexp(const z: Complex): Complex;
  { result = exp(i*f)  }
  function CexpIm(f: TFloat): Complex;
  { result = sqrt(z), result.Re > 0 }
  function Csqrt(const z: Complex): Complex; overload;
  { result = sqrt(z), result.Re > 0 }
  function Csqrt(const x: TFloat): Complex; overload;
  // result = SQR(a)  [From EFG Unit]
  function CSqr  (const z: Complex): Complex;

  { result = sqrt(x) }

  // -PI < theta <= PI     [From EFG Unit]
  function FixAngle (const theta: TFloat): TFloat;
  { complex natural log: result = ln(a)
    NOTE: principal value only [From EFG Unit] }
  function CLn (const z: Complex): Complex;

  { s := s + z }
  procedure Csum(var s: Complex; const z: Complex); overload;
  { s := s + x }
  procedure Csum(var s: Complex; x: TFloat); overload;

  { r1 <= re < r2, i1 <= im < i2 }
  function RndComplex(r1, r2, i1, i2: TFloat): Complex;

  function cer(const z: Complex): Complex;

const
  // cmplx(0.0, 0.0)
  Cmplx0: Complex = (Re: 0.0; Im: 0.0);
  // cmplx(1.0, 0.0)
  Cmplx1: Complex = (Re: 1.0; Im: 0.0);
  // cmplx(0.0, 1.0)
  CmplxIm1: Complex = (Re: 0.0; Im: 1.0);

  // cmplx(0.0, 0.0) [From EFG Unit]
  ComplexZero: Complex = (Re: 0.0; Im: 0.0);
  // cmplx(1.0, 0.0)  [From EFG Unit]
  ComplexOne: Complex = (Re: 1.0; Im: 0.0);

var
  MachEps, SqrtMachEps: TFloat;

  { boolean to string conversion }
  function BoolToStr(Value: boolean): string;
  { string to boolean conversion }
  function StrToBool(Value: string): boolean;

  { Calculation of the machine epsilon  }
  procedure MachinEps ( var MachEps  : TFloat );
  { Swap of float numbers }
  procedure Swap(var f1, f2: TFloat);
  { Swap of integer numbers }
  procedure iSwap(var i1, i2: integer);
  { random true/false }
  function RndBool: boolean;
  { random number r1 <= r < r2 }
  function RndFloat(r1, r2: TFloat): TFloat;
  { random number N1 <= result <= N2 }
  function RndNum(N1, N2: integer): Integer;
  { random number N1 <= r <= N2, r <> r1 }
  function RndNumEx(N1, N2, r1: integer): Integer;
  { if x > 0 then result := 1 else if x = 0 then result := 0 else result := -1;}
  function iSign(x: TInt)  : TInt;
  { Kroneker delta : if x1 = x2 then result := 1 else result := 0 }
  function delta(x1: TInt; x2: TInt = 0): TInt;
  { if x > 0.0 then result := 1.0 <br>
    else if x < 0.0 then result := -1.0 else result := 0.0; }
  function Sign0(x: TFloat): TFloat;
  {   if  X >= 0.0  then  result := 1.0 else  result := -1.0;}
  function Sign1(x: TFloat) : TFloat;
  { FORTRAN signum: |a|*signum(b) }
  function Sign2( a, b : TFloat) : TFloat;
  { n!/(m!(n-m)! }
  function Combinations(m, n: TInt): TInt;
  { n! }
  function Fac(m: TInt): TInt;
  { obsolete }
  function exp1(X: TFloat): TFloat;
  { 10**x or 10^x = Power(10.0, x) [Math]}
  function power10(x: TFloat)   : TFloat;
  { lg(x) = log10(x) [Math] }
  function lg(x: TFloat)        : TFloat;
  { FORTRAN amin2(x1, x2) = min(x1, x2) [Math] }
  function amin2(x1, x2: TFloat): TFloat;
  { FORTRAN amax2(x1, x2) = max(x1, x2) [Math] }
  function amax2(x1, x2: TFloat): TFloat;
  { safe sqrt(x + y); it is assumed that (x+y) > 0 }
  function sqroot1(x, y: TFloat): TFloat;
  { safe sqrt(x*x+y*y); functionally the same as pythag}
  function sqroot2(x, y: TFloat): TFloat;
  { finds dsqrt(a**2+b**2) without overflow or destructive underflow;
    functionally the same as sqroot2}
  function pythag(a, b: TFloat): TFloat;

implementation

uses
  Math;
end.

Back

Generated by Lore's Source to HTML Converter(http://www.newty.de/lsc/index.html)