1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212
|
{
*****************************************************************************
See the file COPYING.modifiedLGPL.txt, included in this distribution,
for details about the license.
*****************************************************************************
Authors: Alexander Klenin
}
unit TAFitUtils;
{$H+}
interface
type
TFitEquation = (
fePolynomial, // y = b0 + b1*x + b2*x^2 + ... bn*x^n
feLinear, // y = a + b*x
feExp, // y = a * exp(b * x)
fePower // y = a * x^b
);
IFitEquationText = interface
function Equation(AEquation: TFitEquation): IFitEquationText;
function X(AText: String): IFitEquationText;
function Y(AText: String): IFitEquationText;
function NumFormat(AFormat: String): IFitEquationText;
function NumFormats(const AFormats: array of String): IFitEquationText;
function Params(const AParams: array of Double): IFitEquationText;
function Get: String;
end;
TFitEmptyEquationText = class(TInterfacedObject, IFitEquationText)
public
function Equation(AEquation: TFitEquation): IFitEquationText;
function X(AText: String): IFitEquationText;
function Y(AText: String): IFitEquationText;
function NumFormat(AFormat: String): IFitEquationText;
function NumFormats(const AFormats: array of String): IFitEquationText;
function Params(const AParams: array of Double): IFitEquationText;
function Get: String;
end;
TFitEquationText = class(TInterfacedObject, IFitEquationText)
strict private
FEquation: TFitEquation;
FX: String;
FY: String;
FNumFormat: String;
FNumFormats: array of String;
FParams: array of Double;
function GetNumFormat(AIndex: Integer): String;
public
constructor Create;
function Equation(AEquation: TFitEquation): IFitEquationText;
function X(AText: String): IFitEquationText;
function Y(AText: String): IFitEquationText;
function NumFormat(AFormat: String): IFitEquationText;
function NumFormats(const AFormats: array of String): IFitEquationText;
function Params(const AParams: array of Double): IFitEquationText;
function Get: String;
end;
operator :=(AEq: IFitEquationText): String; inline;
implementation
uses
TAChartUtils, StrUtils, SysUtils;
operator := (AEq: IFitEquationText): String;
begin
Result := AEq.Get;
end;
{ TFitEmptyEquationText }
function TFitEmptyEquationText.Equation(
AEquation: TFitEquation): IFitEquationText;
begin
Unused(AEquation);
Result := Self;
end;
function TFitEmptyEquationText.Get: String;
begin
Result := '';
end;
function TFitEmptyEquationText.NumFormat(AFormat: String): IFitEquationText;
begin
Unused(AFormat);
Result := Self;
end;
function TFitEmptyEquationText.NumFormats(
const AFormats: array of String): IFitEquationText;
begin
Unused(AFormats);
Result := Self;
end;
function TFitEmptyEquationText.Params(
const AParams: array of Double): IFitEquationText;
begin
Unused(AParams);
Result := Self;
end;
function TFitEmptyEquationText.X(AText: String): IFitEquationText;
begin
Unused(AText);
Result := Self;
end;
function TFitEmptyEquationText.Y(AText: String): IFitEquationText;
begin
Unused(AText);
Result := Self;
end;
{ TFitEquationText }
constructor TFitEquationText.Create;
begin
FX := 'x';
FY := 'y';
FNumFormat := '%.9g';
end;
function TFitEquationText.Equation(AEquation: TFitEquation): IFitEquationText;
begin
FEquation := AEquation;
Result := Self;
end;
function TFitEquationText.Get: String;
var
ps: String = '';
i: Integer;
begin
if Length(FParams) = 0 then exit('');
Result := Format('%s = ' + GetNumFormat(0), [FY, FParams[0]]);
if FEquation in [fePolynomial, feLinear] then
for i := 1 to High(FParams) do begin
if FParams[i] = 0 then continue;
if i > 1 then ps := Format('^%d', [i]);
Result += Format(
' %s ' + GetNumFormat(i) + '*%s%s',
[IfThen(FParams[i] > 0, '+', '-'), Abs(FParams[i]), FX, ps]);
end
else if (Length(FParams) >= 2) and (FParams[0] <> 0) and (FParams[1] <> 0) then
case FEquation of
feExp:
Result += Format(' * exp(' + GetNumFormat(1) +' * %s)', [FParams[1], FX]);
fePower:
Result += Format(' * %s^' + GetNumFormat(1), [FX, FParams[1]]);
end;
end;
function TFitEquationText.GetNumFormat(AIndex: Integer): String;
begin
if AIndex < Length(FNumFormats) then
Result := FNumFormats[AIndex]
else
Result := FNumFormat;
end;
function TFitEquationText.NumFormat(AFormat: String): IFitEquationText;
begin
FNumFormat := AFormat;
Result := Self;
end;
function TFitEquationText.NumFormats(
const AFormats: array of String): IFitEquationText;
var
i: Integer;
begin
SetLength(FNumFormats, Length(AFormats));
for i := 0 to High(AFormats) do
FNumFormats[i] := AFormats[i];
Result := Self;
end;
function TFitEquationText.Params(
const AParams: array of Double): IFitEquationText;
var
i: Integer;
begin
SetLength(FParams, Length(AParams));
for i := 0 to High(AParams) do
FParams[i] := AParams[i];
Result := Self;
end;
function TFitEquationText.X(AText: String): IFitEquationText;
begin
FX := AText;
Result := Self;
end;
function TFitEquationText.Y(AText: String): IFitEquationText;
begin
FY := AText;
Result := Self;
end;
end.
|