Skip to content

Commit 1a3e56e

Browse files
committed
Add 6 new functions to Maths category
Added Double, Cardinal and Integer overloads of new SumOfReciprocals and HarmonicMean routines. Added a source code file for each routine. Updated maths.ini with meta data for each new routine.
1 parent 5c7623f commit 1a3e56e

File tree

7 files changed

+129
-0
lines changed

7 files changed

+129
-0
lines changed

collection/681.dat

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
function SumOfReciprocals(const A: array of Double): Double; overload;
2+
var
3+
Elem: Double;
4+
begin
5+
if System.Length(A) = 0 then
6+
raise SysUtils.EArgumentException.Create('Array is empty');
7+
Result := 0.0;
8+
for Elem in A do
9+
begin
10+
if Math.Sign(Elem) <> Math.PositiveValue then
11+
raise SysUtils.EArgumentException.Create('Array values must be > 0');
12+
Result := Result + 1 / Elem;
13+
end;
14+
end;

collection/682.dat

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
function SumOfReciprocals(const A: array of Integer): Double; overload;
2+
var
3+
Elem: Integer;
4+
begin
5+
if System.Length(A) = 0 then
6+
raise SysUtils.EArgumentException.Create('Array is empty');
7+
Result := 0.0;
8+
for Elem in A do
9+
begin
10+
if Elem <= 0 then
11+
raise SysUtils.EArgumentException.Create('Array values must be > 0');
12+
Result := Result + 1 / Elem;
13+
end;
14+
end;

collection/683.dat

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
function SumOfReciprocals(const A: array of Cardinal): Double; overload;
2+
var
3+
Elem: Cardinal;
4+
begin
5+
if System.Length(A) = 0 then
6+
raise SysUtils.EArgumentException.Create('Array is empty');
7+
Result := 0.0;
8+
for Elem in A do
9+
begin
10+
if Elem = 0 then
11+
raise SysUtils.EArgumentException.Create('Array values must be > 0');
12+
Result := Result + 1 / Elem;
13+
end;
14+
end;

collection/684.dat

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
function HarmonicMean(const A: array of Double): Double; overload;
2+
begin
3+
Result := System.Length(A) / SumOfReciprocals(A);
4+
end;

collection/685.dat

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
function HarmonicMean(const A: array of Cardinal): Double; overload;
2+
begin
3+
Result := System.Length(A) / SumOfReciprocals(A);
4+
end;

collection/686.dat

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
function HarmonicMean(const A: array of Integer): Double; overload;
2+
begin
3+
Result := System.Length(A) / SumOfReciprocals(A);
4+
end;

collection/maths.ini

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2091,3 +2091,78 @@ AdvancedTest.URL="https://github.com/delphidabbler/code-snippets/tree/master/tes
20912091
Snip=680.dat
20922092
DelphiXE=Y
20932093
Delphi12A=Y
2094+
2095+
[SumOfReciprocals_Double]
2096+
DisplayName="SumOfReciprocals (Double overload)"
2097+
DescEx="<p>Calculates the sum of the reciprocal values of all elements of <var>Double</var> array <var>A</var>.</p><p><var>A</var> must not be empty and all its elements must be positive. <var>EArgumentException</var> is raised if either of these conditions is not satisfied.</p>"
2098+
Units=SysUtils,Math
2099+
SeeAlso=SumOfReciprocals_Integer,SumOfReciprocals_Cardinal
2100+
TestInfo=advanced
2101+
AdvancedTest.Level=unit-tests
2102+
AdvancedTest.URL="https://github.com/delphidabbler/code-snippets/tree/master/tests/Cat-Maths"
2103+
Snip=681.dat
2104+
DelphiXE=Y
2105+
Delphi12A=Y
2106+
2107+
[SumOfReciprocals_Integer]
2108+
DisplayName="SumOfReciprocals (Integer overload)"
2109+
DescEx="<p>Calculates the sum of the reciprocal values of all elements of <var>Integer</var> array <var>A</var>.</p><p><var>A</var> must not be empty and all its elements must be positive. <var>EArgumentException</var> is raised if either of these conditions is not satisfied.</p>"
2110+
Units=SysUtils
2111+
SeeAlso=SumOfReciprocals_Integer,SumOfReciprocals_Double
2112+
TestInfo=advanced
2113+
AdvancedTest.Level=unit-tests
2114+
AdvancedTest.URL="https://github.com/delphidabbler/code-snippets/tree/master/tests/Cat-Maths"
2115+
Snip=682.dat
2116+
DelphiXE=Y
2117+
Delphi12A=Y
2118+
2119+
[SumOfReciprocals_Cardinal]
2120+
DisplayName="SumOfReciprocals (Cardinal overload)"
2121+
DescEx="<p>Calculates the sum of the reciprocal values of all elements of <var>Cardinal</var> array <var>A</var>.</p><p><var>A</var> must not be empty and all its elements must be positive. <var>EArgumentException</var> is raised if either of these conditions is not satisfied.</p>"
2122+
Units=SysUtils
2123+
SeeAlso=SumOfReciprocals_Double,SumOfReciprocals_Cardinal
2124+
TestInfo=advanced
2125+
AdvancedTest.Level=unit-tests
2126+
AdvancedTest.URL="https://github.com/delphidabbler/code-snippets/tree/master/tests/Cat-Maths"
2127+
Snip=683.dat
2128+
DelphiXE=Y
2129+
Delphi12A=Y
2130+
2131+
[HarmonicMean_Double]
2132+
DisplayName="HarmonicMean (Double overload)"
2133+
DescEx="<p>Returns the harmonic mean of an array of positive <var>Double</var> values.</p><p><var>EArgumentException</var> is raised if the array is empty or if any array element is not positive.</p>"
2134+
Extra="<p>See <a href="https://en.m.wikipedia.org/wiki/Harmonic_mean">Wikipedia</a> for information about the harmonic mean.</p>"
2135+
Depends=SumOfReciprocals_Double
2136+
SeeAlso=HarmonicMean_Integer,HarmonicMean_Cardinal,ArithMean_Double,GeoMean_Double
2137+
TestInfo=advanced
2138+
AdvancedTest.Level=unit-tests
2139+
AdvancedTest.URL="https://github.com/delphidabbler/code-snippets/tree/master/tests/Cat-Maths"
2140+
Snip=684.dat
2141+
DelphiXE=Y
2142+
Delphi12A=Y
2143+
2144+
[HarmonicMean_Cardinal]
2145+
DisplayName="HarmonicMean (Cardinal overload)"
2146+
DescEx="<p>Returns the harmonic mean of an array of positive <var>Cardinal</var> values.</p><p><var>EArgumentException</var> is raised if the array is empty or if any array element is not positive.</p>"
2147+
Extra="<p>See <a href="https://en.m.wikipedia.org/wiki/Harmonic_mean">Wikipedia</a> for information about the harmonic mean.</p>"
2148+
Depends=SumOfReciprocals_Cardinal
2149+
SeeAlso=HarmonicMean_Double,HarmonicMean_Integer,ArithMean_Cardinal,GeoMean_Cardinal
2150+
TestInfo=advanced
2151+
AdvancedTest.Level=unit-tests
2152+
AdvancedTest.URL="https://github.com/delphidabbler/code-snippets/tree/master/tests/Cat-Maths"
2153+
Snip=685.dat
2154+
DelphiXE=Y
2155+
Delphi12A=Y
2156+
2157+
[HarmonicMean_Integer]
2158+
DisplayName="HarmonicMean (Integer overload)"
2159+
DescEx="<p>Returns the harmonic mean of an array of positive <var>Integer</var> values.</p><p><var>EArgumentException</var> is raised if the array is empty or if any array element is not positive.</p>"
2160+
Extra="<p>See <a href="https://en.m.wikipedia.org/wiki/Harmonic_mean">Wikipedia</a> for information about the harmonic mean.</p>"
2161+
Depends=SumOfReciprocals_Integer
2162+
SeeAlso=HarmonicMean_Cardinal,HarmonicMean_Double,ArithMean_Integer,GeoMean_Integer
2163+
TestInfo=advanced
2164+
AdvancedTest.Level=unit-tests
2165+
AdvancedTest.URL="https://github.com/delphidabbler/code-snippets/tree/master/tests/Cat-Maths"
2166+
Snip=686.dat
2167+
DelphiXE=Y
2168+
Delphi12A=Y

0 commit comments

Comments
 (0)