Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 9490f9e

Browse files
A WeightedHarmonicMean functions to Maths category
Added Double, Cardinal and Integer overloads of the functions. Added source code file for each function. Updated maths.ini with meta data for each function.
1 parent bf4cb04 commit 9490f9e

File tree

4 files changed

+86
-0
lines changed

4 files changed

+86
-0
lines changed

‎collection/687.dat‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
function WeightedHarmonicMean(const Values: array of Double;
2+
const Weights: array of Double): Double; overload;
3+
var
4+
Sum: Double;
5+
Idx: Integer;
6+
NormalisedWeights: Types.TDoubleDynArray;
7+
begin
8+
if System.Length(Values) = 0 then
9+
raise SysUtils.EArgumentException.Create('Array of values is empty');
10+
if System.Length(Values) <> System.Length(Weights) then
11+
raise SysUtils.EArgumentException.Create(
12+
'Number of values and number of weights must be the same'
13+
);
14+
NormalisedWeights := NormaliseByWeight(Weights);
15+
Sum := 0.0;
16+
for Idx := 0 to Pred(System.Length(Values)) do
17+
Sum := Sum + NormalisedWeights[Idx] / Values[Idx];
18+
Result := 1.0 / Sum;
19+
end;

‎collection/688.dat‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
function WeightedHarmonicMean(const Values: array of Cardinal;
2+
const Weights: array of Double): Double; overload;
3+
var
4+
Idx: Integer;
5+
FloatValues: Types.TDoubleDynArray;
6+
begin
7+
System.Setlength(FloatValues, System.Length(Values));
8+
for Idx := 0 to Pred(System.Length(Values)) do
9+
FloatValues[Idx] := Values[Idx];
10+
Result := WeightedHarmonicMean(FloatValues, Weights);
11+
end;

‎collection/689.dat‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
function WeightedHarmonicMean(const Values: array of Integer;
2+
const Weights: array of Double): Double; overload;
3+
var
4+
Idx: Integer;
5+
FloatValues: Types.TDoubleDynArray;
6+
begin
7+
System.Setlength(FloatValues, System.Length(Values));
8+
for Idx := 0 to Pred(System.Length(Values)) do
9+
FloatValues[Idx] := Values[Idx];
10+
Result := WeightedHarmonicMean(FloatValues, Weights);
11+
end;

‎collection/maths.ini‎

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2166,3 +2166,48 @@ AdvancedTest.URL="https://github.com/delphidabbler/code-snippets/tree/master/tes
21662166
Snip=686.dat
21672167
DelphiXE=Y
21682168
Delphi12A=Y
2169+
2170+
[WeightedHarmonicMean_Double]
2171+
DisplayName="WeightedHarmonicMean (Double overload)"
2172+
DescEx="<p>Calculates and returns the weighted harmonic mean of the array <var>Values</var> of positive <var>Double</var> values where each element is weighted by the corresponding element in the array <var>Weights</var>.</p><p>An <var>EArgumentException</var> exception is raised if any of the following pre-conditions are not met: <var>Values</var> must be non-empty; all elements of <var>Values</var> must be positive; <var>Values</var> &amp; <var>Weights</var> must have the same number of elements; all elements of <var>Weights</var> must be non-negative, with at least one element being non-zero.</p>"
2173+
Extra="<p>See <a href="https://en.wikipedia.org/wiki/Harmonic_mean#Weighted_harmonic_mean">Wikipedia</a> for information about the weighted harmonic mean.</p>"
2174+
Kind=routine
2175+
Units=SysUtils,Types
2176+
Depends=NormaliseByWeight_Double
2177+
SeeAlso=HarmonicMean_Double,WeightedHarmonicMean_Cardinal,WeightedHarmonicMean_Integer,WeightedArithMean_Double,WeightedGeoMean_Double
2178+
TestInfo=advanced
2179+
AdvancedTest.Level=unit-tests
2180+
AdvancedTest.URL="https://github.com/delphidabbler/code-snippets/tree/master/tests/Cat-Maths"
2181+
Snip=687.dat
2182+
DelphiXE=Y
2183+
Delphi12A=Y
2184+
2185+
[WeightedHarmonicMean_Cardinal]
2186+
DisplayName="WeightedHarmonicMean (Cardinal overload)"
2187+
DescEx="<p>Calculates and returns the weighted harmonic mean of the array <var>Values</var> of positive <var>Cardinal</var> values where each element is weighted by the corresponding element in the array <var>Weights</var>.</p><p>An <var>EArgumentException</var> exception is raised if any of the following pre-conditions are not met: <var>Values</var> must be non-empty; all elements of <var>Values</var> must be positive; <var>Values</var> &amp; <var>Weights</var> must have the same number of elements; all elements of <var>Weights</var> must be non-negative, with at least one element being non-zero.</p>"
2188+
Extra="<p>See <a href="https://en.wikipedia.org/wiki/Harmonic_mean#Weighted_harmonic_mean">Wikipedia</a> for information about the weighted harmonic mean.</p>"
2189+
Kind=routine
2190+
Units=Types
2191+
Depends=WeightedHarmonicMean_Double
2192+
SeeAlso=HarmonicMean_Cardinal,WeightedHarmonicMean_Double,WeightedHarmonicMean_Integer,WeightedArithMean_Cardinal,WeightedGeoMean_Cardinal
2193+
TestInfo=advanced
2194+
AdvancedTest.Level=unit-tests
2195+
AdvancedTest.URL="https://github.com/delphidabbler/code-snippets/tree/master/tests/Cat-Maths"
2196+
Snip=688.dat
2197+
DelphiXE=Y
2198+
Delphi12A=Y
2199+
2200+
[WeightedHarmonicMean_Integer]
2201+
DisplayName="WeightedHarmonicMean (Integer overload)"
2202+
DescEx="<p>Calculates and returns the weighted harmonic mean of the array <var>Values</var> of positive <var>Integer</var> values where each element is weighted by the corresponding element in the array <var>Weights</var>.</p><p>An <var>EArgumentException</var> exception is raised if any of the following pre-conditions are not met: <var>Values</var> must be non-empty; all elements of <var>Values</var> must be positive; <var>Values</var> &amp; <var>Weights</var> must have the same number of elements; all elements of <var>Weights</var> must be non-negative, with at least one element being non-zero.</p>"
2203+
Extra="<p>See <a href="https://en.wikipedia.org/wiki/Harmonic_mean#Weighted_harmonic_mean">Wikipedia</a> for information about the weighted harmonic mean.</p>"
2204+
Kind=routine
2205+
Units=Types
2206+
Depends=WeightedHarmonicMean_Double
2207+
SeeAlso=HarmonicMean_Integer,WeightedHarmonicMean_Double,WeightedHarmonicMean_Cardinal,WeightedArithMean_Integer,WeightedGeoMean_Integer
2208+
TestInfo=advanced
2209+
AdvancedTest.Level=unit-tests
2210+
AdvancedTest.URL="https://github.com/delphidabbler/code-snippets/tree/master/tests/Cat-Maths"
2211+
Snip=689.dat
2212+
DelphiXE=Y
2213+
Delphi12A=Y

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /