{
Find least squares polynom P(x)=c[0]+c[1]*x+...:
PolynomDump:=Dump(Power)+Dump(Center)+Dump(Scale)+Dump(c[0])+Dump(c[1])+...
}
function PolynomDump(crv,Power:Integer;Center,Scale,Eps:Real):String;
type
 TVector=array[0..9] of Real;
 TMatrix=array[0..9] of TVector;
 var A:TMatrix; P,Q:TVector; s:String; i:Integer;
 function Solve(N:Integer; var A:TMatrix; var X,Y:TVector; Eps:Real):Integer;
 var i,j,k,p,err:Integer; S:Real;
 begin
  err:=0;
  if (N<1) or (N>10) then err:=err+1 else
  if N=1 then begin
   if abs(A[0,0])<=Eps then err:=err+1 else X[0]:=Y[0]/A[0,0];
  end else
  for j:=0 to N-2 do begin
   p:=j;
   for i:=j+1 to N-1 do if ABS(A[i,j])>ABS(A[p,j]) then p:=i;
   if p <> j then begin
    for k:=0 to N-1 do begin
     S:=A[p,k];
     A[p,k]:=A[j,k];
     A[j,k]:=S;
    end;
    S:=Y[p];
    Y[p]:=Y[j];
    Y[j]:=S;
   end;
   S:=A[j,j];
   if abs(S)<=Eps then err:=err+1;
   for i:=j+1 to N-1 do begin
    S:=-A[i,j]/A[j,j];
    for k:=0 to N-1 do A[i,k]:=A[i,k]+S*A[j,k];
    Y[i]:=Y[i]+S*Y[j];
   end;
  end;
  S:=A[N-1,N-1];
  if abs(S)<Eps then err:=err+1;
  for i:=N-1 downto 0 do begin
   S:=0; for j:=i+1 to N-1 do S:=S+A[i,j]*X[j];
   X[i]:=(Y[i]-S)/A[i,i];
  end;
  Solve:=err;
 end;
 procedure CreateEquations(crv,N:Integer; Center,Scale:Real; var A:TMatrix; var P,Q:TVector);
 var i,j,k,NumPoints:Integer; Pw,t,Yi:Real; b:Boolean;
 begin
  b:=CrvLock(crv);
  NumPoints:=Round(CrvLen(crv));
  for j:=0 to N-1 do begin
   Q[j]:=0;
   for k:=0 to N-1 do A[j,k]:=0;
  end;
  for i:=0 to NumPoints-1 do begin
   t:=(CrvX(crv,1+i)-Center)/Scale;
   Pw:=1;
   for j:=0 to N-1 do begin
    P[j]:=Pw;
    Pw:=Pw*t;
   end;
   for j:=0 to N-1 do
   for k:=0 to N-1 do A[j,k]:=A[j,k]+P[j]*P[k];
   Yi:=CrvY(crv,1+i);
   for j:=0 to N-1 do Q[j]:=Q[j]+Yi*P[j];
  end;
  b:=CrvUnLock(crv);
 end;
begin
 s:='';
 if Scale>0 then
 if CrvLen(crv)>0 then
 if not IsNan(Scale) then
 if not IsInf(Scale) then
 if not IsNan(Center) then
 if not IsInf(Center) then
 if (Power>=0) and (Power<=9) then begin
  CreateEquations(crv,Power+1,Center,Scale,A,P,Q);
  if Solve(Power+1,A,P,Q,Eps)=0 then begin
   s:=Dump(Power)+Dump(Center)+Dump(Scale);
   for i:=0 to Power+1 do s:=s+Dump(P[i]);
  end;
 end;
 PolynomDump:=s;
 s:='';
end;
function PolyPower(Poly:String):Integer;
begin
 PolyPower:=Dump2i(Poly);
end;
function PolyCenter(Poly:String):Real;
begin
 PolyCenter:=Dump2r(Copy(Poly,5));
end;
function PolyScale(Poly:String):Real;
begin
 PolyScale:=Dump2r(Copy(Poly,13));
end;
function PolyCoeff(Poly:String;i:Integer):Real;
begin
 PolyCoeff:=Dump2r(Copy(Poly,21+i*8));
end;

