Up \ObjAlg
{@abstract(Linear algebra utilities, basic routines and I/O routines )
author: Nikolai Shokhirev )
created: February 02, 2001)
last modified: June 06, 2003)
©Nikolai V. Shokhirev, 2001-2003
}
unit  uTensorUtils;

interface

uses
  uMatTypes, uTensors;

{ like in const arrays }
procedure StrToVector(const V: Itensor1D; ss: string);
{ comma-separated output }
procedure VectorToStr(const V: Itensor1D; var s: string);

{ like in const arrays }
procedure StrToMatrix(const M: Itensor2D; ss: string);
{ comma-separated output }
procedure MatrixToStr(const M: Itensor2D; var s: string; ds: string = #13#10);

{ err = 0 - dimensions match }
function DimDif(NL, NR: TInt; var err: TInt): TInt;
{ err = NL - NR }
function DimOK(NL, NR: TInt; var err: TInt): boolean;

{ result = ((M.Lo1 = M.Lo2) and (M.Hi1 = M.Hi2)) }
function IsSquareLim(const M: ILimits2D): boolean;
{ result = (M.Dim1 = M.Dim2) }
function IsSquareDim(const M: ILimits2D): boolean;

{ result = ((V1.Lo = V2.Lo) and (V1.Hi = V2.Hi)) }
function SameLimitsVt(const V1, V2: ILimits1D): boolean;
{ result = ((M1.Lo1 = M2.Lo1) and (M1.Lo2 = M2.Lo2) and <br>
            (M1.Hi1 = M2.Hi1) and (M1.Hi2 = M2.Hi2)) }
function SameLimitsMt(const M1, M2: ILimits2D): boolean;
{ result = ((M1.Lo1 = M2.Lo1) and (M1.Lo2 = M2.Lo2) and (M1.Lo3 = M2.Lo3) <br>
            (M1.Hi1 = M2.Hi1) and (M1.Hi2 = M2.Hi2) and (M1.Hi3 = M2.Hi3)) }
function SameLimitsMt3D(const M1, M2: ILimits3D): boolean;

{ result = ((M1.Lo2 = V2.Lo) and (M1.Hi2 = V2.Hi)) }
function SameLimitsMtVt(const M1: ILimits2D; const V2: ILimits1D): boolean;
{ result = ((V1.Lo = M2.Lo1) and (V1.Hi = M2.Hi1)) }
function SameLimitsVtMt(const V1: ILimits1D; const M2: ILimits2D): boolean;
{ result := (M1.Lo2 = M2.Lo1) and (M1.Hi2 = M2.Hi1)) }
function SameLimitsMtMt(const M1, M2: ILimits2D): boolean;

{ result = (V1.Dim = V2.Dim) }
function SameDimsVt(const V1, V2: ILimits1D): boolean;
{ result = ((M1.Dim1 = M2.Dim1) and (M1.Dim2 = M2.Dim22)) }
function SameDimsMt(const M1, M2: ILimits2D): boolean;
{ result = ((M1.Dim1 = M2.Dim1) and (M1.Dim2 = M2.Dim2) and (M1.Dim3 = M2.Dim3))}
function SameDimsMt3D(const M1, M2: ILimits3D): boolean;

{ result = (M1.Dim2 = V2.Dim) }
function SameDimsMtVt(const M1: ILimits2D; const V2: ILimits1D): boolean;
{ result = (V1.Dim = M2.Dim1) }
function SameDimsVtMt(const V1: ILimits1D; const M2: ILimits2D): boolean;
{ result := (M1.Dim2 = M2.Dim1) }
function SameDimsMtMt(const M1, M2: ILimits2D): boolean;

{ if SameLimitsVt(V1, V2) then result := 0   <br>
  else if SameDimsVt(V1, V2) then result := 1 else result := 2;}
function FitVt(const V1, V2: ILimits1D): integer;
{ if SameLimitsMt(M1, M2) then result := 0  <br>
  else if SameDimsMt(M1, M2) then result := 1 else result := 2;}
function FitMt(const M1, M2: ILimits2D): integer;
{ if SameLimitsMt(M1, M2) then result := 0  <br>
  else if SameDimsMt(M1, M2) then result := 1 else result := 2;}
function FitMt3D(const M1, M2: ILimits3D): integer;

{ if SameLimitsVt(V1, V2) then result := 0  <br>
  else if SameDimsVt(V1, V2) then result := 1 else result := 2;}
function FitVtxVt(const V1, V2: ILimits1D): integer;

{ if (DL.Lo=MR.Lo1) and (DL.Hi=MR.Hi1) then result := 0 else <br>
  if (DL.Dim=MR.Dim1) then result := 1 else result := 2; }
function FitDxMt(const DL: ILimits1D;  const MR:ILimits2D): integer;
{  if (ML.Lo2=DR.Lo) and (ML.Hi2=DR.Hi) then result := 0 else  <br>
  if (ML.Dim2=DR.Dim) then result := 1 else result := 2; }
function FitMtxD(const ML: ILimits2D; const DR: ILimits1D): integer;

{ Fit Row : if SameLimitsMtVt(M1, V2) then result := 0  <br>
  else if SameDimsMtVt(M1, V2) then result := 1 else result := 2; }
function FitMtxVt(const M1: ILimits2D; const V2: ILimits1D): integer;
{ Fit Col : if SameLimitsVtMt(V1, M2) then result := 0 <br>
  else if SameDimsVtMt(V1, M2) then result := 1 else result := 2; }
function FitVtxMt(const V1: ILimits1D; const M2: ILimits2D): integer;

{ if SameLimitsMtMt(M1, M2) then result := 0  <br>
  else if SameDimsMtMt(M1, M2) then result := 1 else result := 2; }
function FitMtxMt(const M1, M2: ILimits2D): integer;
{ if (M1.Lo1=M2.Lo1) and (M1.Hi1=M2.Hi1) then result := 0 else <br>
  if (M1.Dim1=M2.Dim1) then result := 1  else result := 2; }
function FitMtTxMt(const M1, M2: ILimits2D): integer;
{ if (M1.Lo2=M2.Lo2) and (M1.Hi2=M2.Hi2) then result := 0 else <br>
  if (M1.Dim2=M2.Dim2) then result := 1 else result := 2; }
function FitMtxMtT(const M1, M2: ILimits2D): integer;
{ if (M1.Lo2=M2.Lo2) and (M1.Hi2=M2.Hi2) then result := 0 else <br>
  if (M1.Dim2=M2.Dim2) then result := 1 else result := 2; }
function FitMtTxMtT(const M1, M2: ILimits2D): integer;

{ random boolean vector }
procedure RandBVt(const V: IBTensor1D);
{ random boolean matrix }
procedure RandBMt(const M: IBTensor2D);

{ fills with r iMin <= r <= iMax }
procedure RandIVt(iMin, iMax: TInt; const V: IIntTensor1D);
{<pre>fills with r iMin <= r <= iMax <br>
  if sym = -1 then M[i,j] = - M[j,i] <br>
  if sym =  1 then M[i,j] = M[j,i]   <br>
  if sym =  0 - no symmetry </pre>}
procedure RandIMt(iMin, iMax: TInt; const M: IIntTensor2D; sym: TInt);

{ random vector: cMin <= V[i] <= cMax }
procedure RandVt(cMin, cMax: TFloat; const V: ITensor1D);
{<pre>fills with r cMin <= r < cMax  <br>
 if sym = -1 then M[i,j] = - M[j,i] <br>
 if sym =  1 then M[i,j] = M[j,i]   <br>
 if sym =  0 - no symmetry  </pre>}
procedure RandMt(cMin, cMax: TFloat; const M: ITensor2D; sym: TInt);

{ fills with r cMin <= r < cMax  }
procedure Rand3Mt(cMin, cMax: TFloat; const M: ITensor3D);

{<pre>
| b1  c1  0   0  ...               |<br>
| a2  b2  c2  0  ...               |<br>
|                ...               |<br>
|                ... aN-1 bN-1 cN-1|<br>
|                ...  0   aN   bN  |</pre>}
procedure TriDiagMt(const a, b, c: ITensor1D; const M: ITensor2D; var err: TInt);

{ V := CxV }
procedure CxVt(C: TFloat; const V: ITensor1D);
{ M := CxM }
procedure CxMt(C: TFloat; const M: ITensor2D);

{ Dot (scalar) product of two vectors VtxVt = VL*VR without limit checks}
function VxV(const VL, VR: ITensor1D): TFloat;
{ Dot (scalar) product of two vectors VtxVt = VL*VR }
function DotProd(const VL, VR: ITensor1D; var err: TInt;
                             imin: TInt = LowInt; imax: TInt = HighInt): TFloat;
{ max|V1-V2| }
function MaxDifVt(const V1, V2: ITensor1D; var err: TInt): TFloat;
{ max|M1-M2| }
function MaxDifMt(const M1, M2: ITensor2D; var err: TInt): TFloat;
{ max|M1-M2| }
function MaxDif3Mt(const M1, M2: ITensor3D; var err: TInt): TFloat;

{ result := V1 = V2 , err = CheckVtFit(V1, V2) }
function EqualBVt(const V1, V2: IBTensor1D; var err: TInt): boolean;
{ result := M1 = M2 , err = CheckMtFit(M1, M2)  }
function EqualBMt(const M1, M2: IBTensor2D; var err: TInt): boolean;

{ result := V1 = V2 , err = CheckVtFit(V1, V2) }
function EqualIVt(const V1, V2: IIntTensor1D; var err: TInt): boolean;
{ result := M1 = M2 , err = CheckMtFit(M1, M2)  }
function EqualIMt(const M1, M2: IIntTensor2D; var err: TInt): boolean;

{ Ascending 1-Darray sorting <br>
 Based on: Sebastian Boßung, Sorting Algorithms in Pascal<br>
 @link(http://privat.schlund.de/b/bossung/prog/delphi/sorting.html) }
procedure SortTensor1D(const A: ITensor1D);

{ Ascending 1D-array sorting with sorting of the associated 2D-array <br>
  by columns (SortColumns = true) or  by rows (SortColumns = false) <br>
  NOTE: SwapColumns is fast pointer operation  <br>
  Based on: Sebastian Boßung, Sorting Algorithms in Pascal <br>
  http://privat.schlund.de/b/bossung/prog/delphi/sorting.html }
procedure SortTensor2D(const A: ITensor1D; const B: ITensor2D;
                                                   SortColumns: boolean = true);

{ MT = Transpose(M) }
procedure MtT(const M, MT: ITensor2D; var err: TInt);

// Fake XML I/O routines ---------------------------------------------------

{ Fake XML I/O routines }
procedure ReadStr(var aFile : TextFile; var str: string; var tag: string);
{ Fake XML I/O routines }
procedure WriteStr(var aFile : TextFile; const str: string; const tag: string);

{ Fake XML I/O routines }
procedure ReadInt(var aFile : TextFile; var i: TInt; var tag: string);
{ Fake XML I/O routines }
procedure WriteInt(var aFile : TextFile; i: TInt; const tag: string);

{ Fake XML I/O routines }
procedure ReadFloat(var aFile : TextFile; var v: TFloat; var tag: string);
{ Fake XML I/O routines }
procedure WriteFloat(var aFile : TextFile; v: TFloat; const tag: string);

{ Fake XML I/O routines }
procedure ReadIntTensor1D(var aFile : TextFile; const aVector: IIntTensor1D;
                          Resize: boolean = true);
{ Fake XML I/O routines }
procedure WriteIntTensor1D(var aFile : TextFile; const aVector: IIntTensor1D);

{ Fake XML I/O routines }
procedure ReadTensor1D(var aFile : TextFile; const aVector: ITensor1D;
                                                        Resize: boolean = true);

{ Fake XML I/O routines }
procedure WriteTensor1D(var aFile : TextFile; const aVector: ITensor1D);
{ Fake XML I/O routines }
procedure ReadIntTensor2D(var aFile : TextFile; const aMatrx: IIntTensor2D;
                                                        Resize: boolean = true);
{ Fake XML I/O routines }
procedure WriteIntTensor2D(var aFile : TextFile; const aMatrx: IIntTensor2D);
{ Fake XML I/O routines }
procedure ReadTensor2D(var aFile : TextFile; const aMatrx: ITensor2D;
                                                        Resize: boolean = true);
{ Fake XML I/O routines }
procedure WriteTensor2D(var aFile : TextFile; const aMatrx: ITensor2D);
{ Fake XML I/O routines }
procedure ReadTensor3D(var aFile : TextFile; const aMatrx3D: ITensor3D;
                                                        Resize: boolean = true);
{ Fake XML I/O routines }
procedure WriteTensor3D(var aFile : TextFile; const aMatrx3D: ITensor3D);

implementation


end.
Up \ObjAlg