
{$R-}

unit uFunctGeneration;

{$MODE Delphi}

interface

uses uVecMat;

procedure linear(n:integer; var x,y:datamatrix);
procedure Xshape(n:integer; var x,y:datamatrix);
procedure LinPeriod1(n:integer; var x,y:datamatrix);
procedure RandBox(n:integer; var x,y:datamatrix);
procedure CircleFull(n:integer; var x,y:datamatrix);
procedure paraboloid(n:integer; var z,xy:datamatrix);

procedure CmplxLinear(n:integer; are,aim:real; var xre,xim,yre,yim:datamatrix);
procedure CmplxQuad(n:integer; are,aim:real; var xre,xim,yre,yim:datamatrix);
procedure CmplxPrincipleSqrt(n:integer; are,aim:real; var xre,xim,yre,yim:datamatrix);
procedure CmplxLog(n:integer; are,aim:real; var xre,xim,yre,yim:datamatrix);

implementation

uses math;




procedure Xvalues(n:integer; xmin,xmax:real; var x:datamatrix);
// creates x vector, with n values uniform in interval xmin to xmax
var i:integer; a,b:extended;
begin
  b:=(xmin-xmax)/(1.0-n);
  a:=xmin-b;
  for i:=1 to n do
    x[i,1]:=a+b*i;
end;




procedure linear(n:integer; var x,y:datamatrix);
var i:integer;
begin
  Xvalues(n,0,+1,x);
  for i:=1 to n do
    y[i,1]:=x[i,1];
end;




procedure Xshape(n:integer; var x,y:datamatrix);
var i,n1:integer; a,b,xmin,xmax:extended;
begin
  n1:=n div 2;
  xmin:=0;
  xmax:=1;
  b:=(xmin-xmax)/(1.0-n1);
  a:=xmin-b;
  for i:=1 to n1 do begin
    x[i,1]:=a+b*i;
    y[i,1]:=x[i,1];
    x[i+n1,1]:=a+b*i;
    y[i+n1,1]:=1-x[i,1];
  end;
end;




procedure RandBox(n:integer; var x,y:datamatrix);
var i:integer;
begin
  randomize;
  for i:=1 to n do begin
    x[i,1]:=random;
    y[i,1]:=random;
  end;
end;




procedure LinPeriod1(n:integer; var x,y:datamatrix);
// creates x,y:n*1
var i:integer;
begin
  Xvalues(n,0,1,x);
  for i:=1 to n do
    y[i,1]:=sin(7*pi*x[i,1])+x[i,1];
end;




procedure CircleFull(n:integer; var x,y:datamatrix);
var i:integer; r,xmin,xmax,a,b:real;
begin
  xmin:=-1;
  xmax:=1;
  b:=(xmin-xmax)/(1.0-n);
  a:=xmin-b;
  for i:=1 to n do begin
    x[i,1]:=a+b*i;
    r:=1-sqr(x[i,1]);
    if r<0 then r:=0;
    if odd(i) then
      y[i,1]:=sqrt(r)
    else
      y[i,1]:=-sqrt(r);
  end;
end;




procedure Xvalues2(n:integer; xmin,xmax:real; var x:datamatrix);
// puts n2=n*n values equi-spaced square grid xmin to xmax
// into x[1...n2,k], k=1,2
var i,j,k:integer; a,b,r1,r2:extended;
begin
  b:=(xmin-xmax)/(1.0-n);
  a:=xmin-b;
  k:=0;
  for i:=1 to n do begin
    r1:=a+b*i;
    for j:=1 to n do begin
      k:=k+1;
      r2:=a+b*j;
      x[k,1]:=r1;
      x[k,2]:=r2;
    end;
  end;
end;




procedure paraboloid(n:integer; var z,xy:datamatrix);
// creates z:(n^2)*1 and xy:(n^2)*2
var n2,i:integer;
begin
  n2:=round(sqrt(n));
  Xvalues2(n2,-1.0,+1.0,xy);
  for i:=1 to n do
    z[i,1]:=sqr(xy[i,1])+sqr(xy[i,2]);
end;




procedure Xvalues3(n:integer; xmin,xmax:real; var xre,xim:datamatrix); overload;
// pre-create outside matrix x: n2*2;
// puts n2=n*n values equi-spaced square grid xmin to xmax
// into x[1...n2,k], k=1,2
var i,j,k:integer; a,b,r1,r2:extended;
begin
  b:=(xmin-xmax)/(1.0-n);
  a:=xmin-b;
  k:=0;
  for i:=1 to n do begin
    r1:=a+b*i;
    for j:=1 to n do begin
      k:=k+1;
      r2:=a+b*j;
      xre[k,1]:=r1;
      xim[k,1]:=r2;
    end;
  end;
end;




procedure CmplxLinear(n:integer; are,aim:real; var xre,xim,yre,yim:datamatrix);
// creates x and y of size 2n; for i=1..n, y=x, x in [0..1]
// for next n points y=(1-x), with repeated x in [0..1]
var n2,i:integer;
begin
  n2:=round(sqrt(n));
  Xvalues3(n2,-1.0,+1.0,xre,xim);
  for i:=1 to n do begin
    yre[i,1]:=are*xre[i,1]-aim*xim[i,1];
    yim[i,1]:=aim*xre[i,1]+are*xim[i,1];
  end;
end;




procedure CmplxQuad(n:integer; are,aim:real; var xre,xim,yre,yim:datamatrix);
// creates x and y of size 2n; for i=1..n, y=x, x in [0..1]
// for next n points y=(1-x), with repeated x in [0..1]
var n2,i:integer; ar,ai:extended;
begin
  n2:=round(sqrt(n));
  Xvalues3(n2,-1.0,+1.0,xre,xim);
  for i:=1 to n do begin
    ar:=sqr(xre[i,1])-sqr(xim[i,1]);
    ai:=2*xre[i,1]*xim[i,1];
    yre[i,1]:=are*ar-aim*ai;
    yim[i,1]:=aim*ar+are*ai;
  end;
end;




procedure CmplxPrincipleSqrt(n:integer; are,aim:real; var xre,xim,yre,yim:datamatrix);
// creates x and y of size 2n; for i=1..n, y=x, x in [0..1]
// for next n points y=(1-x), with repeated x in [0..1]
var n2,i:integer; ar,ai,r:extended;
begin
  n2:=round(sqrt(n));
  Xvalues3(n2,-1.0,+1.0,xre,xim);
  for i:=1 to n do begin

    r:=sqrt(sqr(xre[i,1])+sqr(xim[i,1]));
    ar:=sqrt((r+xre[i,1])/2);
    ai:=sign(xim[i,1])*sqrt((r-xre[i,1])/2);

    yre[i,1]:=are*ar-aim*ai;
    yim[i,1]:=aim*ar+are*ai;
  end;
end;




procedure CmplxLog(n:integer; are,aim:real; var xre,xim,yre,yim:datamatrix);
var n2,i:integer; ar,ai:extended;
begin
  n2:=round(sqrt(n));
  Xvalues3(n2,-1.0,+1.0,xre,xim);
  for i:=1 to n do begin
    ar:=ln(sqrt(sqr(xre[i,1])+sqr(xim[i,1])));
    ai:=Arctan(xim[i,1]/xre[i,1]);
    yre[i,1]:=are*ar-aim*ai;
    yim[i,1]:=aim*ar+are*ai;
  end;
end;




end.

