Up \MatTypes
{@abstract(Math types and constants.
 Basic Math Types, Interfaces, Constants and Math utilities<br>
 ILimits1D, ILimits2D, ILimits1D, INames, IRestore1D, IRestore2D<br>
 TNames, TRestore1D, TRestore2D)
author: Nikolai Shokhirev  )
author: Eugene B. Krissinel http://www.fh.huji.ac.il/~krissinel/)
created: September 09, 1991)
last modified: June 06, 2003)
©Nikolai V. Shokhirev, 2001-2003 }
unit  uMatTypes;

interface

const
  { for double = 2.22044604925031e-16}
  cMachEps = 2.22044604925031e-16;
  { for double = 1.7e-308}
  MinFloat = 1.7e-308;
  { for double = 1.7e+308 }
  MaxFloat = 1.7e+308;
  { for double = -1.7e+308}
  NegMaxFloat = -1.7e+308;
  { for double = 1.3e-154}
  SqrtMinFloat = 1.3e-154;
  { for double = 1.3e+154}
  SqrtMaxFloat = 1.3e+154;
  { for double = 308.0}
  MaxExp =  308.0;
  { for double = 709.0 ~ 308*ln(10) }
  ExpArg = 709.0;
  { for double = 1.1e-77}
  SqrtSqrtMinFloat = 1.1e-77;
(*
  { for Extended = 3.4e-4932 ; actual 3.6 x 10^–4951 for Extended}
  MinFloat = 3.4e-4932;
  { for Extended = 1.1e+4932 ; actual 1.1 x 10^4932 for Extended}
  MaxFloat = 1.1e+4932;
  NegMaxFloat = -1.1e+4932;
  { for Extended = 1.9e-2466}
  SqrtMinFloat = 1.9e-2466;
  { for Extended = 1.0e+2466}
  SqrtMaxFloat = 1.0e+2466;
  { for Extended = 4932.3}
  MaxExp =  4932.3;
  { for Extended = 11356.3 = 4932*ln(10) }
  ExpArg = 11356.3;   // 4932*ln(10)
*)
  { SegmentSize = $FFFF }
  SegmentSize  =  $FFFF;
  { HighInt = 2147483647}
  HighInt = 2147483647;
  { LowInt = -2147483647}
  LowInt = -2147483647;
  { Safe Factor = 1024.0}
  SafeFactor = 1024.0;
type
  { currently = longint }
  TInt   = longint;
  { currently = double }
  TFloat  =  double;

  { function types }
  { function of x }
  FloatFunction1D = function(const x: TFloat): TFloat;
  { function of x, y }
  FloatFunction2D = function(x, y: TFloat): TFloat;
  { function of x, y, z}
  FloatFunction3D = function(x, y, z: TFloat): TFloat;
  { function of n, x}
  BasisFunction = function(n: TInt; x: TFloat): TFloat;

  { record (min, max: TFloat) - used in Data }
  TInterval = record
    min: TFloat;
    max: TFloat;
  end;

  { record (min, max: TInt) - used in Data }
  TIntInterval = record
    min: TInt;
    max: TInt;
  end;

  { =  array [1.. ] of TFloat }
  Vector    =  array [1..999] of TFloat;
  { =  ^Vector }
  VcPtr     =  ^Vector;
  { =  array [1.. ] of TInt }
  IVector_  =  array [1..999] of TInt;
  { =  ^IVector }
  IVcPtr    =  ^IVector_;
  { =  array [1.. ] of word }
  WVector   =  array [1..999] of word;
  { =  ^WVector }
  WVcPtr    =  ^WVector;
  { =  array [1.. ] of byte }
  BtVector  =  array [1..999] of byte;
  { =  ^BtVector }
  BtVcPtr   =  ^BtVector;
  { =  array [1.. ] of boolean }
  BVector   =  array [1..999] of boolean;
  { =  ^BVector }
  BVcPtr    =  ^BVector;
  { =  array [1.. ] of VcPtr }
  Matrix    =  array [1..999] of VcPtr;
  { =  ^Matrix }
  MtPtr     =  ^Matrix;
  { =  array [1.. ] of IVcPtr }
  IMatrix_   =  array [1..999] of IVcPtr;
  { =  ^IMatrix_ }
  IMtPtr    =  ^IMatrix_;
  { =  array [1.. ] of BVcPtr }
  BMatrix   =  array [1..999] of BVcPtr;
  { =  ^BMatrix }
  BMtPtr    =  ^BMatrix;
  { =  array [1.. ] of BtVcPtr }
  BtMatrix  =  array [1..999] of BtVcPtr;
  { =  ^BtMatrix }
  BtMtPtr   =  ^BtMatrix;
  { =  array [1.. ] of MtPtr }
  Matrix3   =  array [1..999] of MtPtr;
  { =  ^Matrix3 }
  Mt3Ptr    =  ^Matrix3;

// zero-based arrays ( Data support )
  { =  array [0.. ] of TFloat }
  Vector0    =  array [0..9998] of TFloat;
  { =  ^Vector0 }
  VcPtr0     =  ^Vector0;
  { =  array [0.. ] of VcPtr0 }
  Matrix0    =  array [0..9998] of VcPtr0;
  { =  ^Matrix0 }
  MtPtr0     =  ^Matrix0;

{ Interfaces and Objects ----------------------------------------------------- }

{ Interface for 1D-Array Limits }
  ILimits1D = interface
  ['{65EC84EB-26C0-4E6E-BE5A-FF8313297B73}']
    { Set/Reset dimension }
    procedure SetSize(aDim: TInt);
    { index base }
    property Base: TInt read GetBase write SetBase;
    { index low limit (= base) }
    property Lo: TInt read GetLo;
    { index high limit }
    property Hi: TInt read GetHi;
    { Dimension }
    property Dim: TInt read GetDim;
   end;

{ Interface for 2D-Array Limits }
  ILimits2D = interface
  ['{AAF3C542-93A9-4D94-BC9A-BEF609E7FC1F}']
    { Set/Reset dimensions }
    procedure SetSize(aDim1, aDim2: TInt);
    { Low Limit for index 1 }
    property Lo1: TInt read GetLo1;
    { High Limit for index 1}
    property Hi1: TInt read GetHi1;
    { Low Limit for index 2 }
    property Lo2: TInt read GetLo2;
    { High Limit for index 2}
    property Hi2: TInt read GetHi2;
    { base for index 1 }
    property Base1: TInt read GetBase1 write SetBase1;
    { base for index 2 }
    property Base2: TInt read GetBase2 write SetBase2;
    { 1st Dimension }
    property Dim1: TInt read GetDim1;
    { 2nd Dimension }
    property Dim2: TInt read GetDim2;
  end;

{ Interface for 3D-Array Limits }
//  ILimits3D = interface(ILimits2D)
  ILimits3D = interface
  ['{8E00B153-F77F-4606-9FBE-6E0C157ABE01}']
    { Set/Reset dimensions }
    procedure SetSize(aDim1, aDim2, aDim3: TInt);
    { Low Limit for index 1 }
    property Lo1: TInt read GetLo1;
    { High Limit for index 1}
    property Hi1: TInt read GetHi1;
    { Low Limit for index 2 }
    property Lo2: TInt read GetLo2;
    { High Limit for index 2}
    property Hi2: TInt read GetHi2;
    { Low Limit for index 3 }
    property Lo3: TInt read GetLo3;
    { High Limit for index 3}
    property Hi3: TInt read GetHi3;
    { base for index 1 }
    property Base1: TInt read GetBase1 write SetBase1;
    { base for index 2 }
    property Base2: TInt read GetBase2 write SetBase2;
    { base for index 3 }
    property Base3: TInt read GetBase3 write SetBase3;
    { 1st Dimension }
    property Dim1: TInt read GetDim1;
    { 2nd Dimension }
    property Dim2: TInt read GetDim2;
    { 3rd Dimension }
    property Dim3: TInt read GetDim3;
  end;

  { Interface for the Restore objects }
  IRestore = interface
  ['{DF81E0EC-94AC-4DBA-933F-2471667C2796}']
  end;

  { Limits1D Restore object }
  TRestore1D = class(TinterfacedObject, Irestore)
  public
    constructor Create(Lim: array of ILimits1D);
    destructor Destroy; override;
  end;

  { Limits2D Restore object }
  TRestore2D = class(TinterfacedObject, Irestore)
  public
    constructor Create(Lim: array of ILimits2D);
    destructor Destroy; override;
  end;

  { interface for dynamic Array [Lo..Hi] of string }
  INames = interface(ILimits1D)
  ['{B19C1500-295A-11D6-970E-0800690F8402}']
    { string array }
    property Names[i1: TInt]: string read GetNames write SetNames; default;
  end;

  { implementation of INames: dynamic Array [1..Dim] of string }
  TNames = class(TInterfacedObject, INames)
  public
    constructor Create; overload;
    constructor Create(aDim: TInt); overload;
    destructor Destroy; override;
    function GetDim: TInt;
    { Set/Reset dimension }
    procedure SetSize(aDim: TInt);
    function GetNames(i1: TInt): string;
    procedure SetNames(i1: TInt; s: string);
    property Base: TInt read GetBase write SetBase;
    property Lo: TInt read GetLo;
    property Hi: TInt read GetHi;
    property Dim: TInt read GetDim;
    property Names[i1: TInt]: string read GetNames write SetNames; default;
  end;

  { Fake XML I/O routines for Names}
  procedure ReadNames(var aFile : TextFile; const aNames: INames;
                                                        Resize: boolean = true);
  { Fake XML I/O routines for Names}
  procedure WriteNames(var aFile : TextFile; const aNames: INames);

var
  MachEps, SqrtMachEps: TFloat;

{ Boolean 1D array memory allocation }
procedure GetBVectorMemory(  var BVc : BVcPtr;  N : TInt );
{ Boolean 1D array memory release }
procedure FreeBVectorMemory( var BVc : BVcPtr;  N : TInt );

{ Byte 1D array memory allocation }
procedure GetBtVectorMemory(  var BtVc : BtVcPtr;  N : TInt );
{ Byte 1D array memory release }
procedure FreeBtVectorMemory( var BtVc : BtVcPtr;  N : TInt );

{ Boolean 2D array memory allocation }
procedure GetBMatrixMemory( var BMt : BMtPtr;  M,N  : TInt );
{ Boolean 2D array memory release }
procedure FreeBMatrixMemory(  var BMt : BMtPtr;  M,N  : TInt );

{ Float 1D array memory allocation }
procedure GetVectorMemory(  var  Vc : VcPtr;  N : TInt );
{ Float 1D array memory release }
procedure FreeVectorMemory(  var  Vc : VcPtr;  N : TInt );

{ Integer 1D array memory allocation }
procedure GetIVectorMemory(  var IVc : IVcPtr;  N : TInt );
{ Integer 1D array memory release }
procedure FreeIVectorMemory( var IVc : IVcPtr;  N : TInt );

{ Word 1D array memory allocation }
procedure GetWVectorMemory(  var WVc : WVcPtr;  N : TInt );
{ Word 1D array memory release }
procedure FreeWVectorMemory( var WVc : WVcPtr;  N : TInt );

{ Float 2D array memory allocation }
procedure GetMatrixMemory(  var  Mt : MtPtr;  M,N  : TInt );
{ Float 2D array memory release }
procedure FreeMatrixMemory(  var  Mt : MtPtr;  M,N  : TInt );

{ Integer 2D array memory allocation }
procedure GetIMatrixMemory(  var Mt : IMtPtr; M,N : TInt );
{ Integer 2D array memory release }
procedure FreeIMatrixMemory( var Mt : IMtPtr; M, N : TInt );

{ Byte 2D array memory allocation }
procedure GetBtMatrixMemory( var BtMt : BtMtPtr; M, N : TInt );
{ Byte 2D array memory release }
procedure FreeBtMatrixMemory( var BtMt : BtMtPtr; M, N : TInt );

{ Float 3D array memory allocation }
procedure GetMatrix3Memory(  var  Mt3 : Mt3Ptr; M,N,L  : TInt );
{ Float 3D array memory release }
procedure FreeMatrix3Memory(  var  Mt3 : Mt3Ptr; M,N,L  : TInt );

{ Float [0..N] array memory allocation }
procedure GetVector0Memory(  var Vc : VcPtr0;  N : TInt );
{ Float [0..N] array memory release }
procedure FreeVector0Memory( var Vc : VcPtr0;  N : TInt );

{ Float [0..M,0..N] array memory allocation }
procedure GetMatrix0Memory( var  Mt : MtPtr0; M,N : TInt );
{ Float [0..M,0..N] array memory release }
procedure FreeMatrix0Memory(var Mt : MtPtr0; M,N : TInt );

{ 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 { ============================================================= }

end.
Up \MatTypes