lazarus Pascal and library about Mathematical Functions lazarus / 21-General ---------------------------------------------------------------------- sedsil · 2025-08-01 15:19 I wonder if some mathematical functionals exist in Lazarus Pascal such that Gamma and Hypergeometrical2F1 or , binomial coefficients. How can I learn what kind of mathematical routines it has. ---------------------------------------------------------------------- wp · 2025-08-01 15:45 NumLib comes with Free Pascal and contains some special functions: htt ps://wiki.lazarus.freepascal.org/NumLib_Documentation#Gamma_function Other (external) libraries are: * lmath (https://wiki.freepascal.org/LMath): e.g., special functions in folder lmGenMath * similar: DMath (available in Online-Package-Manager) * Wolfgang Ehrhard's lib: hypergeometric https://github.com/chadilukito/www.wolfgang- ehrhardt.de/blob/master/src/misc/amath/sfhyperg.pas * jpmMath https://github.com/wp- xyz/jpmMath/blob/main/original/README.md (old code, requires adaption for general use) * plus certainly some more... ---------------------------------------------------------------------- Thaddy · 2025-08-01 16:17 Quote from: Ed78z on August 01, 2025, 06:00:38 pmIf you are looking for the functions with complex numbers, there is no library available yet. What a nonsense. Freepascal has very good support for complex numbers in the unit ucomplex (standard unit in the rtl). That includes all basic operations, not only +-*/, for dealing with complex math. You can simply write out code for any formulae that uses complex math using familiar operators. And that support has been there for years, 26 years to be precise.... and well maintained. I used it for my own fft implementation, but many users use it. Shame on you. May I ask why you did not know that? ---------------------------------------------------------------------- Thaddy · 2025-08-01 16:53 Yes, all the basics. So? If you know your math it is very easy to write out any function relying on complex numbers. Have you seen they are by now actual operators? I mean I can write complex * complex, but also complex * real. etc. You missed that! ---------------------------------------------------------------------- LV · 2025-08-01 16:58 Quote from: Ed78z on August 01, 2025, 06:52:15 pmFor Gamma function, I wrote this function last year, however it's not a highly accuracy method. Why reinvent the wheel?  Have you checked out the links provided by @wp? ---------------------------------------------------------------------- LV · 2025-08-01 17:30 Quote from: Ed78z on August 01, 2025, 07:00:42 pmI didn't know! could you please share the link that works for the complex numbers? For example, Lmath: Declaration function CLnGamma(Z : Complex) : Complex; Description Logarithm of Gamma function If I'm not mistaken, you just need to take the complex exponent of the result Γ(z)=exp(CLnGamma(z)): function CGamma(z: Complex): Complex; begin   CGamma := CExp(CLnGamma(z)); end; ---------------------------------------------------------------------- gues1 · 2025-08-01 17:36 If you want there is the GPMATH (from sourceforge), I attach the documentation (zipped 'cause limit dimension) so you can check. ---------------------------------------------------------------------- LV · 2025-08-02 06:53 Quote from: Ed78z on August 01, 2025, 08:09:55 pmIt uses Stirling’s approximation, enhanced with Bernoulli-derived coefficients, accurate to several digits. that's good when Re(Z) >9 for 1 < Re(z) < 9 or Re(z) < 0.5   has limited accuracy and when Im(z) > 10 Might cause instability or underflow in some cases. So, that's not a really good and accurate and "universal" function. This isn't my sport, but let's give it a shot. Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i function CLnGamma(z: TComplex): TComplex;const  g = 7.0;  lanczosCoef: array[0..8] of Double = (    0.99999999999980993, 676.5203681218851, -1259.1392167224028,    771.32342877765313, -176.61502916214059, 12.507343278686905,    -0.13857109526572012, 9.9843695780195716e-6, 1.5056327351493116e-7  );var  i: Integer;  z1, sum, t, tmp: TComplex;begin  if z.Re < 0.5 then  begin    tmp := CMul(Complex(Pi, 0.0), z);    Result := CSub(                CSub(                  CLog(Complex(Pi, 0.0)),                  CLog(CSin(tmp))              ),                CLnGamma(CSub(Complex(1.0, 0.0), z))              );    Exit;  end;   z1 := CSub(z, Complex(1.0, 0.0));  sum := Complex(lanczosCoef[0], 0.0);  for i := 1 to 8 do    sum := CAdd(sum, CDiv(Complex(lanczosCoef[i], 0.0), CAdd(z1, Complex(i, 0.0))));   t := CAdd(z1, Complex(g + 0.5, 0.0));  Result := CAdd(              Complex(Ln(Sqrt(2 * Pi)), 0.0),              CSub(                CMul(CAdd(z1, Complex(0.5, 0.0)), CLog(t)),                t            )            );  Result := CAdd(Result, CLog(sum));end;  Some results: Code: Text  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i Test 1: lnГ(1) = 0  z = 1.000000 + 0.000000i  Computed: 0.000000000000 + 0.000000000000i  Expected: 0.000000000000 + 0.000000000000i  Abs Error: 0.00E+000  Passed Test 2: lnГ(3) = ln(2)  z = 3.000000 + 0.000000i  Computed: 0.693147180560 + 0.000000000000i  Expected: 0.693147180560 + 0.000000000000i  Abs Error: 5.55E-016  Passed Test 3: lnГ(0.5) = ln(sqrt(pi))  z = 0.500000 + 0.000000i  Computed: 0.572364942925 + 0.000000000000i  Expected: 0.572364942925 + 0.000000000000i  Abs Error: 5.55E-016  Passed Test 4: lnГ(0.1 - 15i)  z = 0.100000 + -15.000000i  Computed: -23.726199767408 + -24.989878306376i  Expected: -23.726199767408 + -24.989878306376i  Abs Error: 8.29E-014  Passed Test 5: lnГ(0.1 + 30i)  z = 0.100000 + 30.000000i  Computed: -47.565423555699 + 71.406325063462i  Expected: -47.565423555699 + 71.406325063462i  Abs Error: 1.54E-013  Passed  Expected values are obtained from Python Code: Python  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i from mpmath import loggammaz = 0.1 + 30jprint(loggamma(z)) ---------------------------------------------------------------------- srvaldez · 2025-08-02 13:26 this implementation is based on https://www.researchgate.net/publicati on/302892889_Chebychev_interpolations_of_the_Gamma_and_Polygamma_Funct ions_and_their_analytical_properties it took me some time to figure out how to use the tables because they are presented using the convolution operator which was new for me https://en.wikipedia.org/wiki/Convolution and https://youtu.be/KuXjwB4LzSA?t=346, I first converted the table to a power series and then used Maple to simplify the expression to make this functions work with complex numbers simply change some variables and functions to complex for some reason the accuracy is not as good as the FreeBasic version in free Pascal the relative error for gamma(100) is 3.1859E-14, in FreeBasic the relative error is 5.0702E-16 see my FreeBasic code here https://www.freebasic.net/forum/viewtopic.p hp?p=303286&hilit=gamma#p303286 Code: [Select]{$mode objfpc}{$H+} program test_gamma_function; uses     Math; function gamma( x : double) : double; var     f, sum, z : double;     a : array [1..20] of double = (         0.9999999999999999,         0.08333333333338228,         0.003472222216158396,        -0.002681326870868177,        -0.0002294790668608988,         0.0007841331256329749,         6.903992060449035e-05,        -0.0005907032612269776,        -2.877475047743023e-05,         0.0005566293593820988,         0.001799738210158344,        -0.008767670094590723,         0.01817828637250317,        -0.02452583787937907,         0.02361068245082701,        -0.01654210549755366,         0.008304315532029655,        -0.00284326571576103,         0.0005961678245858015,        -5.783378931872318e-05); begin     z:=2.506628274631001*x**(x-0.5)*exp(-x);     f:=x*x ; f:=f*f ; f:=f*f*x ; f:=f*f*x; //f := x^19     sum:=z*(a[20]+(a[19]+(a[18]+(a[17]+(a[16]+(a[15]+(a[14]+(a[13]     + (a[12]+(a[11]+(a[10]+(a[9]+(a[8]+(a[7]+(a[6]+(a[5]+(a[4]+(a[3]     + (x*a[1]+a[2])*x)*x)*x)*x)*x)*x)*x)*x)*x)*x)*x)*x)*x)*x)*x)*x)*x)*x)/f;     result := sum; end; Function lngamma( x : double) : Double; var     f, sum : double;     c : array [1..20] of double = (         0.9189385332046727,         0.08333333333338824,        -6.875523375600131e-12,        -0.002777777444245219,        -8.229108205635117e-09,         0.0007937666109114311,        -9.417521476898452e-07,        -0.0005917271548255839,         1.10417689045145e-05,         0.0006092585812576799,         0.001574019576696141,        -0.008450831265113272,         0.01796959267198273,        -0.02456297768317419,         0.0238450482919148,        -0.01680609543961501,         0.008475041030242971,        -0.002912109145921757,         0.000612384508536461,        -5.955145748126546e-05);         begin     f:=x*x ; f:=f*f ; f:=f*f*x ; f:=f*f*x; //f := x^19     sum:=(c[20]+(c[19]+(c[18]+(c[17]+(c[16]+(c[15]+(c[14]+(c[13]+(c[12]     +(c[11]+(c[10]+(c[9]+(c[8]+(c[7]+(c[6]+(c[5]+(c[4]+(c[3]+(c[2]     +(c[1]-0.5*ln(x)+(ln(x)- 1)*x)*x)*x)*x)*x)*x)*x)*x)*x)*x)*x)*x)*x)*x)*x)*x)*x)*x)*x)*x)/f;     Result := sum; end; var i : longint; begin     for i:=1 to 20 do writeln( i, gamma(i)); end. the problem seems to be with freePascal's power function and possibly others, for example x, y : double; x := 100; y:=x-0.5; x=x**y; result = 1.0000000000000318E+199 actual value should be 1E+199 ---------------------------------------------------------------------- marcov · 2025-08-02 17:28 Assuming that you are using 32-bit for both, it might be that some code stores a value to memory and reloads it, while in the other case it is kept in a register. If that rounding from 80-bit to 64-bit can cause small deviations.  You can try to mitigate this by making sure (floating point) optimisation is on. See also the discussion on the FB forum(with  Dodicat) about determining the macheps. And as said it might be target (win32/win64) and FB backend dependent. ---------------------------------------------------------------------- LV · 2025-08-02 21:45 @Ed78z. I don't know what you calculated. Here is the Pascal code and the results with a relative error of 1e-16...1e-15. The comparison was made with the reliable mpmath library, called from the Python code. Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i program LnGammaComplexDemo; {$codepage utf8} uses  Math, SysUtils; type  TComplex = record    Re, Im: Double;  end;   TRefTest = record    Z: TComplex;    Expected: TComplex;    LabelText: string;  end; function Complex(Re, Im: Double): TComplex;begin  Result.Re := Re;  Result.Im := Im;end; function CAdd(a, b: TComplex): TComplex;begin  Result.Re := a.Re + b.Re;  Result.Im := a.Im + b.Im;end; function CSub(a, b: TComplex): TComplex;begin  Result.Re := a.Re - b.Re;  Result.Im := a.Im - b.Im;end; function CMul(a, b: TComplex): TComplex;begin  Result.Re := a.Re * b.Re - a.Im * b.Im;  Result.Im := a.Re * b.Im + a.Im * b.Re;end; function CDiv(a, b: TComplex): TComplex;var  denom: Double;begin  denom := b.Re * b.Re + b.Im * b.Im;  Result.Re := (a.Re * b.Re + a.Im * b.Im) / denom;  Result.Im := (a.Im * b.Re - a.Re * b.Im) / denom;end; function CLog(z: TComplex): TComplex;begin  Result.Re := Ln(Hypot(z.Re, z.Im));  Result.Im := ArcTan2(z.Im, z.Re);end; function CExp(z: TComplex): TComplex;var  expRe: Double;begin  expRe := Exp(z.Re);  Result.Re := expRe * Cos(z.Im);  Result.Im := expRe * Sin(z.Im);end; function CSin(z: TComplex): TComplex;begin  Result.Re := Sin(z.Re) * Cosh(z.Im);  Result.Im := Cos(z.Re) * Sinh(z.Im);end; function CLnGamma(z: TComplex): TComplex;const  g = 7.0;  lanczosCoef: array[0..8] of Double = (    0.99999999999980993, 676.5203681218851, -1259.1392167224028,    771.32342877765313, -176.61502916214059, 12.507343278686905,    -0.13857109526572012, 9.9843695780195716e-6, 1.5056327351493116e-7  );var  i: Integer;  z1, sum, t, tmp: TComplex;begin  if z.Re < 0.5 then  begin    tmp := CMul(Complex(Pi, 0.0), z);    Result := CSub(              CSub(                  CLog(Complex(Pi, 0.0)),                CLog(CSin(tmp))                ),                CLnGamma(CSub(Complex(1.0, 0.0), z))              );    Exit;  end;   z1 := CSub(z, Complex(1.0, 0.0));  sum := Complex(lanczosCoef[0], 0.0);  for i := 1 to 8 do    sum := CAdd(sum, CDiv(Complex(lanczosCoef[i], 0.0), CAdd(z1, Complex(i, 0.0))));   t := CAdd(z1, Complex(g + 0.5, 0.0));  Result := CAdd(              Complex(Ln(Sqrt(2 * Pi)), 0.0),              CSub(                CMul(CAdd(z1, Complex(0.5, 0.0)), CLog(t)),                t            )            );  Result := CAdd(Result, CLog(sum));end; procedure RunTests;const  Eps = 1e-14;var  res, delta: TComplex;  err, normExp: Double;  i: Integer;  tests: array[1..7] of TRefTest = (    (Z: (Re: 1.0; Im: 0.0); Expected: (Re: 0.0; Im: 0.0); LabelText: 'lnГ(1) = 0'),    (Z: (Re: 0.5; Im: 0.0); Expected: (Re: 0.572364942924700087; Im: 0.0); LabelText: 'lnГ(0.5) = ln(sqrt(pi))'),    (Z: (Re: 30.0; Im: 0.0); Expected: (Re: 71.257038967168009; Im: 0.0); LabelText: 'lnГ(30)'),    (Z: (Re: 0.1; Im: -30.0); Expected: (Re: -47.5654235556991727; Im: -71.4063250634621394); LabelText: 'lnГ(0.1 - 30i)'),    (Z: (Re: 0.1; Im: 30); Expected: (Re: -47.5654235556991727; Im: 71.4063250634621394); LabelText: 'lnГ(0.1 + 30i)'),    (Z: (Re: 0.1; Im: -100.0); Expected: (Re: -158.002761620672605; Im: -359.888316732655004); LabelText: 'lnГ(0.1 - 100i)'),    (Z: (Re: 0.1; Im: 100.0); Expected: (Re: -158.002761620672605; Im: 359.888316732655004); LabelText: 'lnГ(0.1 + 100i)')  ); begin  for i := 1 to Length(tests) do  begin    WriteLn('Test ', i, ': ', tests[i].LabelText);    WriteLn(Format('  z = %.6f + %.6fi', [tests[i].Z.Re, tests[i].Z.Im]));    res := CLnGamma(tests[i].Z);    WriteLn(Format('  Computed: %.15f + %.15fi', [res.Re, res.Im]));    WriteLn(Format('  Expected: %.15f + %.15fi', [tests[i].Expected.Re, tests[i].Expected.Im]));    delta := CSub(res, tests[i].Expected);    normExp := Hypot(tests[i].Expected.Re, tests[i].Expected.Im);    if normExp = 0 then      err := Hypot(res.Re, res.Im)    else      err := Hypot(delta.Re, delta.Im) / normExp;    WriteLn(Format('  Rel Error: %.3e', [err]));    if err < Eps then      WriteLn('  Passed')    else      WriteLn('  Failed');    WriteLn;  end;end; begin  RunTests;  Readln;end.  Results: Code: Text  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i Test 1: lnГ(1) = 0  z = 1,000000 + 0,000000i  Computed: 0,000000000000000 + 0,000000000000000i  Expected: 0,000000000000000 + 0,000000000000000i  Rel Error: 0,00E+000  Passed Test 2: lnГ(0.5) = ln(sqrt(pi))  z = 0,500000 + 0,000000i  Computed: 0,572364942924700 + 0,000000000000000i  Expected: 0,572364942924700 + 0,000000000000000i  Rel Error: 9,70E-016  Passed Test 3: lnГ(30)  z = 30,000000 + 0,000000i  Computed: 71,257038967168015 + 0,000000000000000i  Expected: 71,257038967168015 + 0,000000000000000i  Rel Error: 0,00E+000  Passed Test 4: lnГ(0.1 - 30i)  z = 0,100000 + -30,000000i  Computed: -47,565423555699326 + -71,406325063462191i  Expected: -47,565423555699169 + -71,406325063462134i  Rel Error: 1,94E-015  Passed Test 5: lnГ(0.1 + 30i)  z = 0,100000 + 30,000000i  Computed: -47,565423555699326 + 71,406325063462191i  Expected: -47,565423555699169 + 71,406325063462134i  Rel Error: 1,94E-015  Passed Test 6: lnГ(0.1 - 100i)  z = 0,100000 + -100,000000i  Computed: -158,002761620672520 + -359,888316732654800i  Expected: -158,002761620672600 + -359,888316732655030i  Rel Error: 6,18E-016  Passed Test 7: lnГ(0.1 + 100i)  z = 0,100000 + 100,000000i  Computed: -158,002761620672520 + 359,888316732654800i  Expected: -158,002761620672600 + 359,888316732655030i  Rel Error: 6,18E-016  Passed ---------------------------------------------------------------------- LV · 2025-08-02 22:08 Code: Python  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i from mpmath import mp, loggammamp.dps = 17z = 0.1 + 30jprint(loggamma(z))  output: Code: Text  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i (-47.565423555699173 + 71.406325063462139j)  Good night! ---------------------------------------------------------------------- wp · 2025-08-03 09:06 Instead of lamenting that there is no advanced math library contribute to this community project: write a bug report/feature request and attach a patch with the complex gamma function for inclusion in the ucomplex unit. ---------------------------------------------------------------------- srvaldez · 2025-08-03 16:59 I tried my implementation, Mathematica, Maple and gp/pari they all give the same answer log(Gamma(0.1, -30) = -47.565423555699173 - 2.291286684486688 I but lnGamma(0.1, -30) = -47.565423555699173 - 71.406325063462139 I similar results for 0.1+30*I why are the imaginary values different ? ---------------------------------------------------------------------- LV · 2025-08-03 21:10 The imaginary parts differ because of a difference in the choice of branch of the complex logarithm when taking log(Γ(0.1,−30)). The principal branch takes arg(z)∈(−π,π]. The analytic continuation is outside this range (i.e., multiple of 2π): 71.4063−2.2913≈69.115≈11⋅2π. Code: Python  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i from mpmath import arg, log, gamma, mp, loggammamp.dps = 17 z = 0.1 - 30j print('The principal branch')print(log(gamma(z)))  # arg in (-π, π] print('The analytic continuation')print(loggamma(z))  output: Code: Text  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i The principal branch(-47.565423555699173 - 2.2912866844866882j)The analytic continuation(-47.565423555699173 - 71.406325063462139j)  Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i // The analytic continuationfunction CLnGamma(z: TComplex): TComplex;const  g = 7.0;  lanczosCoef: array[0..8] of Double = (    0.99999999999980993, 676.5203681218851, -1259.1392167224028,    771.32342877765313, -176.61502916214059, 12.507343278686905,    -0.13857109526572012, 9.9843695780195716e-6, 1.5056327351493116e-7  );var  i: Integer;  z1, sum, t, tmp: TComplex;begin  if z.Re < 0.5 then  begin    tmp := CMul(Complex(Pi, 0.0), z);    Result := CSub(                CSub(                CLog(Complex(Pi, 0.0)),                  CLog(CSin(tmp))                ),                CLnGamma(CSub(Complex(1.0, 0.0), z))            );    Exit;  end;   z1 := CSub(z, Complex(1.0, 0.0));  sum := Complex(lanczosCoef[0], 0.0);  for i := 1 to 8 do    sum := CAdd(sum, CDiv(Complex(lanczosCoef[i], 0.0), CAdd(z1, Complex(i, 0.0))));   t := CAdd(z1, Complex(g + 0.5, 0.0));  Result := CAdd(            Complex(Ln(Sqrt(2 * Pi)), 0.0),              CSub(              CMul(CAdd(z1, Complex(0.5, 0.0)), CLog(t)),                t            )            );  Result := CAdd(Result, CLog(sum));end; // The principal branchfunction CLnGammaPrincipal(z: TComplex): TComplex;begin  Result := CLnGamma(z);  Result.Im := ArcTan2(Sin(Result.Im), Cos(Result.Im));end;   output: Code: Text  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i Test 1: The principal branch  z = 0,10 + -30,00i  Computed: -47,5654235556993 + -2,29128668448674i  Expected: -47,5654235556992 + -2,29128668448669i  Rel Error: 3,31E-015  Passed Test 2: The analytic continuation  z = 0,10 + -30,00i  Computed: -47,5654235556993 + -71,4063250634622i  Expected: -47,5654235556992 + -71,4063250634621i  Rel Error: 1,86E-015  Passed ---------------------------------------------------------------------- srvaldez · 2025-08-03 23:06 thanks LV