Skip to content

Commit 28b2b6f

Browse files
committed
Merge branch 'feature/32-logarithmic-mean-function' into develop
Fixes #32
2 parents b50ec8a + b220524 commit 28b2b6f

File tree

4 files changed

+85
-1
lines changed

4 files changed

+85
-1
lines changed

Diff for: collection/690.dat

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
function LogarithmicMean(const X, Y: Double): Double;
2+
begin
3+
if (X <= 0) or (Y <= 0) then
4+
raise SysUtils.EArgumentException.Create(
5+
'Parameters X & Y must both be positive'
6+
);
7+
if Math.SameValue(X, Y) then
8+
Result := X
9+
else
10+
Result := (Y - X) / (System.Ln(Y) - System.Ln(X));
11+
end;

Diff for: collection/maths.ini

+14
Original file line numberDiff line numberDiff line change
@@ -2211,3 +2211,17 @@ AdvancedTest.URL="https://github.com/delphidabbler/code-snippets/tree/master/tes
22112211
Snip=689.dat
22122212
DelphiXE=Y
22132213
Delphi12A=Y
2214+
2215+
[LogarithmicMean]
2216+
DisplayName="LogarithmicMean"
2217+
DescEx="<p>Returns the logarithmic mean of two positive floating point values, <var>X</var> and <var>Y</var>.</p><p>Raises <var>EArgumentException</var> if either <var>X</var> or <var>Y</var> is not positive.</p>"
2218+
Extra="<p>See <a href="https://www.ryantoomey.org/wiki/Logarithmic_mean_average">Chemepedia</a> for information about the logarithmic mean.</p>"
2219+
Kind=routine
2220+
Units=SysUtils,Math
2221+
SeeAlso=ArithmeticMean_Double,GeometricMean_Double,HarmonicMean_Double
2222+
TestInfo=advanced
2223+
AdvancedTest.Level=unit-tests
2224+
AdvancedTest.URL="https://github.com/delphidabbler/code-snippets/tree/master/tests/Cat-Maths"
2225+
Snip=690.dat
2226+
DelphiXE=Y
2227+
Delphi12A=Y

Diff for: tests/Cat-Maths/TestUMathsCatSnippets.pas

+37
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,8 @@ TestMathsCatSnippets = class(TTestCase)
6464
procedure TestWeightedHarmonicMean_Double_ExceptDiffSizeArrays;
6565
procedure TestWeightedHarmonicMean_Double_ExceptNegativeWeights;
6666
procedure TestWeightedHarmonicMean_Double_ExceptZeroWeights;
67+
procedure TestLogarithmicMean_ExceptNonPositive;
68+
procedure TestLogarithmicMean_ExceptZero;
6769
function EqualArrays(const Left, Right: TBytes): Boolean; overload;
6870
function EqualArrays(const Left, Right: array of Double;
6971
Fudge: Double = 0.0): Boolean; overload;
@@ -153,6 +155,7 @@ TestMathsCatSnippets = class(TTestCase)
153155
procedure TestWeightedHarmonicMean_Double; // required by Integer & Cardinal overloads
154156
procedure TestWeightedHarmonicMean_Cardinal;
155157
procedure TestWeightedHarmonicMean_Integer;
158+
procedure TestLogarithmicMean;
156159
end;
157160

158161
implementation
@@ -1022,6 +1025,40 @@ procedure TestMathsCatSnippets.TestLCD;
10221025
CheckEquals(9, LCD(-9, -9), 'LCD(-9, -9)');
10231026
end;
10241027

1028+
procedure TestMathsCatSnippets.TestLogarithmicMean;
1029+
const
1030+
Fudge = 0.000000001;
1031+
// Expected values calculated with Windows Calc
1032+
XA = 42.456; EA = XA;
1033+
XB = 42.456; YB = 56.847; EB = 49.3019407666718697;
1034+
XC = 0.000001; YC = 0.000002; EC = 1.4426950408889634e-6;
1035+
XD = 0.000001; ED = XD;
1036+
XE = 18374983.0; YE = 2768293.9362; EE = 8245471.247628288866;
1037+
XF = 18.374983; YF = 2768293.9362; EF = 232184.284293825682;
1038+
XG = 0.00002356; YG = 2768293.9362; EG = 108604.405745470878;
1039+
begin
1040+
CheckTrue(SameValue(EA, LogarithmicMean(XA, XA), Fudge), 'A (x,x)');
1041+
CheckTrue(SameValue(EB, LogarithmicMean(XB, YB), Fudge), 'B (x,y)');
1042+
CheckTrue(SameValue(EB, LogarithmicMean(YB, XB), Fudge), 'B (y,x)');
1043+
CheckTrue(SameValue(EC, LogarithmicMean(XC, YC), Fudge), 'C (x,y)');
1044+
CheckTrue(SameValue(ED, LogarithmicMean(XD, XD), Fudge), 'D (x,x)');
1045+
CheckTrue(SameValue(EE, LogarithmicMean(XE, YE), Fudge), 'E (x,y)');
1046+
CheckTrue(SameValue(EF, LogarithmicMean(XF, YF), Fudge), 'F (x,y)');
1047+
CheckTrue(SameValue(EG, LogarithmicMean(XG, YG), Fudge), 'G (x,y)');
1048+
CheckException(TestLogarithmicMean_ExceptNonPositive, EArgumentException, 'Not positive exception');
1049+
CheckException(TestLogarithmicMean_ExceptZero, EArgumentException, 'Zero exception');
1050+
end;
1051+
1052+
procedure TestMathsCatSnippets.TestLogarithmicMean_ExceptNonPositive;
1053+
begin
1054+
LogarithmicMean(-2.4, 1.0);
1055+
end;
1056+
1057+
procedure TestMathsCatSnippets.TestLogarithmicMean_ExceptZero;
1058+
begin
1059+
LogarithmicMean(2.4, 0.0);
1060+
end;
1061+
10251062
procedure TestMathsCatSnippets.TestLSE;
10261063
const
10271064
Fudge = 0.000001;

Diff for: tests/Cat-Maths/UMathsCatSnippets.pas

+23-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
* The unit is copyright © 2005-2024 by Peter Johnson & Contributors and is
77
* licensed under the MIT License (https://opensource.org/licenses/MIT).
88
*
9-
* Generated on : Tue, 14 Jan 2025 10:14:17 GMT.
9+
* Generated on : Tue, 14 Jan 2025 11:37:09 GMT.
1010
* Generated by : DelphiDabbler CodeSnip Release 4.24.0.
1111
*
1212
* The latest version of CodeSnip is available from the CodeSnip GitHub project
@@ -269,6 +269,12 @@ function IsRectNormal(const R: Windows.TRect): Boolean;
269269
}
270270
function LCD(A, B: Integer): Integer;
271271

272+
{
273+
Returns the logarithmic mean of two positive floating point values, X and Y.
274+
Raises EArgumentException if either X or Y is not positive.
275+
}
276+
function LogarithmicMean(const X, Y: Double): Double;
277+
272278
{
273279
Returns the logarithm of the sum of the exponentials of the given array of
274280
floating pointing point numbers.
@@ -1505,6 +1511,22 @@ function LCD(A, B: Integer): Integer;
15051511
Result := Abs((A * B)) div GCD(A, B);
15061512
end;
15071513

1514+
{
1515+
Returns the logarithmic mean of two positive floating point values, X and Y.
1516+
Raises EArgumentException if either X or Y is not positive.
1517+
}
1518+
function LogarithmicMean(const X, Y: Double): Double;
1519+
begin
1520+
if (X <= 0) or (Y <= 0) then
1521+
raise SysUtils.EArgumentException.Create(
1522+
'Parameters X & Y must both be positive'
1523+
);
1524+
if Math.SameValue(X, Y) then
1525+
Result := X
1526+
else
1527+
Result := (Y - X) / (System.Ln(Y) - System.Ln(X));
1528+
end;
1529+
15081530
{
15091531
Returns the logarithm of the sum of the exponentials of the given array of
15101532
floating pointing point numbers.

0 commit comments

Comments
 (0)