-
Notifications
You must be signed in to change notification settings - Fork 38
/
fpexprpars_addon.pas
485 lines (412 loc) · 14 KB
/
fpexprpars_addon.pas
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
unit fpexprpars_addon;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, fpexprpars;
Procedure ExprDegToRad(Var Result: TFPExpressionResult; Const Args: TExprParameterArray);
Procedure ExprRadToDeg(Var Result: TFPExpressionResult; Const Args: TExprParameterArray);
Procedure ExprTan(Var Result: TFPExpressionResult; Const Args: TExprParameterArray);
Procedure ExprCot(Var Result: TFPExpressionResult; Const Args: TExprParameterArray);
Procedure ExprArcsin(Var Result: TFPExpressionResult; Const Args: TExprParameterArray);
Procedure ExprArccos(Var Result: TFPExpressionResult; Const Args: TExprParameterArray);
Procedure ExprArccot(Var Result: TFPExpressionResult; Const Args: TExprParameterArray);
Procedure ExprCosh(Var Result: TFPExpressionResult; Const Args: TExprParameterArray);
Procedure ExprCoth(Var Result: TFPExpressionResult; Const Args: TExprParameterArray);
Procedure ExprSinh(Var Result: TFPExpressionResult; Const Args: TExprParameterArray);
Procedure ExprTanh(Var Result: TFPExpressionResult; Const Args: TExprParameterArray);
Procedure ExprArcosh(Var Result: TFPExpressionResult; Const Args: TExprParameterArray);
Procedure ExprArsinh(Var Result: TFPExpressionResult; Const Args: TExprParameterArray);
Procedure ExprArtanh(Var Result: TFPExpressionResult; Const Args: TExprParameterArray);
Procedure ExprArcoth(Var Result: TFPExpressionResult; Const Args: TExprParameterArray);
Procedure ExprSinc(Var Result: TFPExpressionResult; Const Args: TExprParameterArray);
Procedure ExprPower(Var Result: TFPExpressionResult; Const Args: TExprParameterArray);
Procedure ExprHypot(Var Result: TFPExpressionResult; Const Args: TExprParameterArray);
Procedure ExprLog10(Var Result: TFPExpressionResult; Const Args: TExprParameterArray);
Procedure ExprLog2(Var Result: TFPExpressionResult; Const Args: TExprParameterArray);
Procedure ExprErf(Var Result: TFPExpressionResult; Const Args: TExprParameterArray);
Procedure ExprErfc(Var Result: TFPExpressionResult; Const Args: TExprParameterArray);
Procedure ExprGammaP(var Result: TFPExpressionResult; Const Args: TExprParameterArray);
Procedure ExprGammaQ(var Result: TFPExpressionResult; Const Args: TExprParameterArray);
Procedure ExprBetaI(var Result: TFPExpressionResult; Const Args: TExprParameterArray);
Procedure ExprChi2Dist(var Result: TFPExpressionResult; Const Args: TExprParameterArray);
Procedure ExprtDist(var Result: TFPExpressionResult; const Args: TExprParameterArray);
Procedure ExprFDist(var Result: TFPExpressionResult; const Args: TExprParameterArray);
Procedure ExprI0(Var Result: TFPExpressionResult; Const Args: TExprParameterArray);
Procedure ExprI1(Var Result: TFPExpressionResult; Const Args: TExprParameterArray);
Procedure ExprJ0(Var Result: TFPExpressionResult; Const Args: TExprParameterArray);
Procedure ExprJ1(Var Result: TFPExpressionResult; Const Args: TExprParameterArray);
Procedure ExprK0(Var Result: TFPExpressionResult; Const Args: TExprParameterArray);
Procedure ExprK1(Var Result: TFPExpressionResult; Const Args: TExprParameterArray);
Procedure ExprY0(Var Result: TFPExpressionResult; Const Args: TExprParameterArray);
Procedure ExprY1(Var Result: TFPExpressionResult; Const Args: TExprParameterArray);
function FixDecSep(const AExpression: String): String;
implementation
uses
Math,
typ, spe; // numlib
{ Additional functions for the parser }
procedure ExprDegToRad(var Result: TFPExpressionResult; const Args: TExprParameterArray);
var
x: Double;
begin
x := ArgToFloat(Args[0]);
Result.resFloat := degtorad(x);
end;
procedure ExprRadToDeg(var Result: TFPExpressionResult; const Args: TExprParameterArray);
var
x: Double;
begin
x := ArgToFloat(Args[0]);
Result.resFloat := radtodeg(x);
end;
procedure ExprTan(var Result: TFPExpressionResult; const Args: TExprParameterArray);
var
x: Double;
begin
x := ArgToFloat(Args[0]);
Result.resFloat := tan(x);
end;
procedure ExprCot(var Result: TFPExpressionResult; const Args: TExprParameterArray);
var
x: Double;
begin
x := ArgToFloat(Args[0]);
Result.resFloat := cot(x);
end;
procedure ExprArcsin(var Result: TFPExpressionResult; const Args: TExprParameterArray);
var
x: Double;
begin
x := ArgToFloat(Args[0]);
Result.resFloat := arcsin(x);
end;
procedure ExprArccos(var Result: TFPExpressionResult; const Args: TExprParameterArray);
var
x: Double;
begin
x := ArgToFloat(Args[0]);
Result.resFloat := arccos(x);
end;
procedure ExprArccot(Var Result: TFPExpressionResult; const Args: TExprParameterArray);
var
x: Double;
begin
x := ArgToFloat(Args[0]);
Result.resFloat := pi/2 - arctan(x);
end;
procedure ExprCosh(var Result: TFPExpressionResult; const Args: TExprParameterArray);
var
x: Double;
begin
x := ArgToFloat(Args[0]);
Result.resFloat := cosh(x);
end;
{ Hyperbolic cotangent coth(x); x <> 0 }
procedure ExprCoth(var Result: TFPExpressionResult; const Args: TExprParameterArray);
var
x: Double;
begin
x := ArgToFloat(Args[0]);
Result.resFloat := 1/tanh(x);
end;
procedure ExprSinh(var Result: TFPExpressionResult; const Args: TExprParameterArray);
var
x: Double;
begin
x := ArgToFloat(Args[0]);
Result.resFloat := sinh(x);
end;
procedure ExprTanh(var Result: TFPExpressionResult; const Args: TExprParameterArray);
var
x: Double;
begin
x := ArgToFloat(Args[0]);
Result.resFloat := tanh(x);
end;
procedure ExprArcosh(var Result: TFPExpressionResult; const Args: TExprParameterArray);
var
x: Double;
begin
x := ArgToFloat(Args[0]);
Result.resFloat := arcosh(x);
end;
procedure ExprArsinh(var Result: TFPExpressionResult; const Args: TExprParameterArray);
var
x: Double;
begin
x := ArgtoFloat(Args[0]);
Result.resFloat := arsinh(x);
end;
procedure ExprArtanh(var Result: TFPExpressionResult; const Args: TExprParameterArray);
var
x: Double;
begin
x := ArgToFloat(Args[0]);
Result.resFloat := artanh(x);
end;
procedure ExprArcoth(var Result: TFPExpressionResult; const Args: TExprParameterArray);
var
x: Double;
begin
x := ArgToFloat(Args[0]);
Result.resFloat := artanh(1.0/x);
end;
procedure ExprSinc(var Result: TFPExpressionResult; const Args: TExprParameterArray);
var
x: Double;
begin
x := ArgToFloat(Args[0]);
if x = 0 then
Result.ResFloat := 1.0
else
Result.resFloat := sin(x)/x;
end;
procedure ExprPower(var Result: TFPExpressionResult; const Args: TExprParameterArray);
var
x,y: Double;
begin
x := ArgToFloat(Args[0]);
y := ArgToFloat(Args[1]);
Result.resFloat := Power(x, y);
end;
procedure ExprLg(var Result: TFPExpressionResult; const Args: TExprParameterArray);
var
x: Double;
begin
x := ArgToFloat(Args[0]);
Result.resFloat := log10(x);
end;
procedure ExprLog10(var Result: TFPExpressionResult; const Args: TExprParameterArray);
var
x: Double;
begin
x := ArgToFloat(Args[0]);
Result.resFloat := log10(x);
end;
procedure ExprLog2(var Result: TFPExpressionResult; const Args: TExprParameterArray);
var
x: Double;
begin
x := ArgToFloat(Args[0]);
Result.resFloat := log2(x);
end;
procedure ExprMax(var Result: TFPExpressionResult; const Args: TExprParameterArray);
var
x1, x2: Double;
begin
x1 := ArgToFloat(Args[0]);
x2 := ArgToFloat(Args[1]);
Result.resFloat := Max(x1, x2);
end;
procedure ExprMin(var Result: TFPExpressionResult; const Args: TExprParameterArray);
var
x1, x2: Double;
begin
x1 := ArgToFloat(Args[0]);
x2 := ArgToFloat(Args[1]);
Result.resFloat := Min(x1, x2);
end;
Procedure ExprHypot(Var Result: TFPExpressionResult; Const Args: TExprParameterArray);
var
x,y: Double;
begin
x := ArgToFloat(Args[0]);
y := ArgToFloat(Args[1]);
Result.resFloat := Hypot(x,y);
end;
Procedure ExprErf(Var Result: TFPExpressionResult; const Args: TExprParameterArray);
// Error function
var
x: Double;
begin
x := ArgToFloat(Args[0]);
Result.resFloat := speerf(x);
end;
Procedure ExprErfc(Var Result: TFPExpressionResult; const Args: TExprParameterArray);
// Error function complement
var
x: Double;
begin
x := ArgToFloat(Args[0]);
Result.resFloat := speefc(x);
end;
// Incomplete gamma function P
Procedure ExprGammaP(var Result: TFPExpressionResult; Const Args: TExprParameterArray);
var
x, s: Double;
begin
s := ArgToFloat(Args[0]);
x := ArgToFloat(Args[1]);
Result.resFloat := gammap(s, x);
end;
// Incomplete gamma function Q
Procedure ExprGammaQ(var Result: TFPExpressionResult; Const Args: TExprParameterArray);
var
x, s: Double;
begin
s := ArgToFloat(Args[0]);
x := ArgToFloat(Args[1]);
Result.resFloat := gammaq(s, x);
end;
// Incomplete beta function
Procedure ExprBetaI(var Result: TFPExpressionResult; Const Args: TExprParameterArray);
var
a, b, x: Double;
begin
a := ArgToFloat(Args[0]);
b := ArgToFloat(Args[1]);
x := ArgToFloat(Args[2]);
Result.resFloat := betai(a, b, x);
end;
Procedure ExprChi2Dist(var Result: TFPExpressionResult; const Args: TExprParameterArray);
var
x: Double;
n: Double;
begin
x := ArgToFloat(Args[0]);
n := ArgToFloat(Args[1]);
Result.resFloat := chi2dist(x, round(n));
end;
Procedure ExprtDist(var Result: TFPExpressionResult; const Args: TExprParameterArray);
var
x: Double;
n: Double;
begin
x := ArgToFloat(Args[0]);
n := ArgToFloat(Args[1]);
Result.resFloat := tdist(x, round(n), 2);
end;
Procedure ExprFDist(var Result: TFPExpressionResult; const Args: TExprParameterArray);
var
x: Double;
n1, n2: Double;
begin
x := ArgToFloat(Args[0]);
n1 := ArgToFloat(Args[1]);
n2 := ArgToFloat(Args[2]);
Result.resFloat := Fdist(x, round(n1), round(n2));
end;
Procedure ExprI0(Var Result: TFPExpressionResult; Const Args: TExprParameterArray);
// Bessel function of the first kind I0(x)
var
x: Double;
begin
x := ArgToFloat(Args[0]);
Result.resFloat := spebi0(x);
end;
Procedure ExprI1(Var Result: TFPExpressionResult; Const Args: TExprParameterArray);
// Bessel function of the first kind I1(x)
var
x: Double;
begin
x := ArgToFloat(Args[0]);
Result.resFloat := spebi1(x);
end;
Procedure ExprJ0(Var Result: TFPExpressionResult; Const Args: TExprParameterArray);
// Bessel function of the first kind J0(x)
var
x: Double;
begin
x := ArgToFloat(Args[0]);
Result.resFloat := spebj0(x);
end;
Procedure ExprJ1(Var Result: TFPExpressionResult; Const Args: TExprParameterArray);
// Bessel function of the first kind J1(x)
var
x: Double;
begin
x := ArgToFloat(Args[0]);
Result.resFloat := spebj1(x);
end;
Procedure ExprK0(Var Result: TFPExpressionResult; Const Args: TExprParameterArray);
// Bessel function of the second kind K0(x)
var
x: Double;
begin
x := ArgToFloat(Args[0]);
Result.resFloat := spebk0(x);
end;
Procedure ExprK1(Var Result: TFPExpressionResult; Const Args: TExprParameterArray);
// Bessel function of the second kind K1(x)
var
x: Double;
begin
x := ArgToFloat(Args[0]);
Result.resFloat := spebk1(x);
end;
Procedure ExprY0(Var Result: TFPExpressionResult; Const Args: TExprParameterArray);
// Bessel function of the second kind Y0(x)
var
x: Double;
begin
x := ArgToFloat(Args[0]);
Result.resFloat := speby0(x);
end;
Procedure ExprY1(Var Result: TFPExpressionResult; Const Args: TExprParameterArray);
// Bessel function of the second kind Y1(x)
var
x: Double;
begin
x := ArgToFloat(Args[0]);
Result.resFloat := speby1(x);
end;
function FixDecSep(const AExpression: String): String;
var
i: Integer;
begin
Result := AExpression;
for i:=1 to Length(Result) do begin
if Result[i] = ',' then Result[i] := '.';
end;
end;
//---------------------
procedure RegisterExprParserAddons;
begin
with BuiltinIdentifiers do begin
AddFunction(bcMath, 'degtorad', 'F', 'F', @ExprDegtorad);
AddFunction(bcMath, 'radtodeg', 'F', 'F', @ExprRadtodeg);
AddFunction(bcMath, 'tan', 'F', 'F', @ExprTan);
AddFunction(bcMath, 'cot', 'F', 'F', @ExprCot);
AddFunction(bcMath, 'arcsin', 'F', 'F', @ExprArcSin);
AddFunction(bcMath, 'arccos', 'F', 'F', @ExprArcCos);
AddFunction(bcMath, 'arccot', 'F', 'F', @ExprArcCot);
AddFunction(bcMath, 'cosh', 'F', 'F', @ExprCosh);
AddFunction(bcMath, 'coth', 'F', 'F', @ExprCoth);
AddFunction(bcMath, 'sinh', 'F', 'F', @ExprSinh);
AddFunction(bcMath, 'tanh', 'F', 'F', @ExprTanh);
AddFunction(bcMath, 'arcosh', 'F', 'F', @ExprArcosh);
AddFunction(bcMath, 'arsinh', 'F', 'F', @ExprArsinh);
AddFunction(bcMath, 'artanh', 'F', 'F', @ExprArtanh);
AddFunction(bcMath, 'arcoth', 'F', 'F', @ExprArcoth);
AddFunction(bcMath, 'sinc', 'F', 'F', @ExprSinc);
AddFunction(bcMath, 'power', 'F', 'FF', @ExprPower);
AddFunction(bcMath, 'hypot', 'F', 'FF', @ExprHypot);
AddFunction(bcMath, 'lg', 'F', 'F', @ExprLog10);
AddFunction(bcMath, 'log10', 'F', 'F', @ExprLog10);
AddFunction(bcMath, 'log2', 'F', 'F', @ExprLog2);
// Error function
AddFunction(bcMath, 'erf', 'F', 'F', @ExprErf);
AddFunction(bcMath, 'erfc', 'F', 'F', @ExprErfc);
// Incomplete gamma and beta functions
AddFunction(bcMath, 'gammap', 'F', 'FF', @ExprGammaP);
AddFunction(bcMath, 'gammaq', 'F', 'FF', @ExprGammaQ);
AddFunction(bcMath, 'betai', 'F', 'FFF', @ExprBetaI);
// Probability distributions
AddFunction(bcMath, 'chi2dist', 'F', 'FI', @ExprChi2Dist);
AddFunction(bcMath, 'tdist', 'F', 'FI', @Exprtdist);
AddFunction(bcMath, 'Fdist', 'F', 'FII', @ExprFDist);
// Bessel functions of the first kind
AddFunction(bcMath, 'I0', 'F', 'F', @ExprI0);
AddFunction(bcMath, 'I1', 'F', 'F', @ExprI1);
AddFunction(bcMath, 'J0', 'F', 'F', @ExprJ0);
AddFunction(bcMath, 'J1', 'F', 'F', @ExprJ1);
// Bessel functions of the second kind
AddFunction(bcMath, 'K0', 'F', 'F', @ExprK0);
AddFunction(bcMath, 'K1', 'F', 'F', @ExprK1);
AddFunction(bcMath, 'Y0', 'F', 'F', @ExprY0);
AddFunction(bcMath, 'Y1', 'F', 'F', @ExprY1);
end;
end;
initialization
RegisterExprParserAddons;
end.