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 ada95bc

Browse files
Add various power mean functions to Maths category
Added Double, Cardinal & Integer overloads of PowerMean & WeightedPowerMean functions. Added source code files for each function. Added meta data for each function to maths.ini.
1 parent 8396dd8 commit ada95bc

File tree

7 files changed

+190
-0
lines changed

7 files changed

+190
-0
lines changed

‎collection/691.dat

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
function PowerMean(const A: array of Double; const Lambda: Double): Double;
2+
overload;
3+
var
4+
Sum: Double;
5+
X: Double;
6+
begin
7+
if System.Length(A) = 0 then
8+
raise SysUtils.EArgumentException.Create('Array is empty');
9+
if Math.IsZero(Lambda) then
10+
raise SysUtils.EArgumentException.Create('Lambda must not be zero');
11+
Sum := 0.0;
12+
for X in A do
13+
begin
14+
if Math.Sign(X) = NegativeValue then
15+
raise SysUtils.EArgumentException.Create(
16+
'All array elements must be non-negative'
17+
);
18+
if not Math.IsZero(X) then
19+
Sum := Sum + Math.Power(X, Lambda);
20+
end;
21+
Result := Math.Power(Sum / System.Length(A), 1 / Lambda);
22+
end;

‎collection/692.dat

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

‎collection/693.dat

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

‎collection/694.dat

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
function WeightedPowerMean(const Values, Weights: array of Double;
2+
const Lambda: Double): Double; overload;
3+
var
4+
NormalisedWeights: Types.TDoubleDynArray;
5+
PowerSum: Double;
6+
Idx: Integer;
7+
Value: Double;
8+
Weight: Double;
9+
begin
10+
if System.Length(Values) = 0 then
11+
raise SysUtils.EArgumentException.Create('Array of values is empty');
12+
if System.Length(Values) <> System.Length(Weights) then
13+
raise SysUtils.EArgumentException.Create(
14+
'Number of values and number of weights must be the same'
15+
);
16+
if Math.IsZero(Lambda) then
17+
raise SysUtils.EArgumentException.Create('Lambda must not be zero');
18+
NormalisedWeights := NormaliseByWeight(Weights);
19+
PowerSum := 0.0;
20+
for Idx := 0 to Pred(System.Length(Values)) do
21+
begin
22+
Value := Values[Idx];
23+
Weight := NormalisedWeights[Idx];
24+
if Math.Sign(Value) = Math.NegativeValue then
25+
raise SysUtils.EArgumentException.Create(
26+
'All values must be non-negative'
27+
);
28+
if not Math.IsZero(Value) and not Math.IsZero(Weight) then
29+
PowerSum := PowerSum + Weight * Math.Power(Value, Lambda);
30+
end;
31+
if not Math.IsZero(PowerSum) then
32+
Result := Math.Power(PowerSum, 1 / Lambda)
33+
else
34+
Result := 0.0;
35+
end;

‎collection/695.dat

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
function WeightedPowerMean(const Values: array of Integer;
2+
const Weights: array of Double; const Lambda: Double): Double; overload;
3+
var
4+
FloatValues: Types.TDoubleDynArray;
5+
Idx: Integer;
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 := WeightedPowerMean(FloatValues, Weights, Lambda);
11+
end;

‎collection/696.dat

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
function WeightedPowerMean(const Values: array of Cardinal;
2+
const Weights: array of Double; const Lambda: Double): Double; overload;
3+
var
4+
FloatValues: Types.TDoubleDynArray;
5+
Idx: Integer;
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 := WeightedPowerMean(FloatValues, Weights, Lambda);
11+
end;

‎collection/maths.ini

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2225,3 +2225,92 @@ AdvancedTest.URL="https://github.com/delphidabbler/code-snippets/tree/master/tes
22252225
Snip=690.dat
22262226
DelphiXE=Y
22272227
Delphi12A=Y
2228+
2229+
[PowerMean_Double]
2230+
DisplayName="PowerMean (Double overload)"
2231+
DescEx="<p>Returns the power mean of the elements of <var>Double</var> array <var>Values</var>, with exponent <var>Lambda</var>.</p><p>An <var>EArgumentException</var> is raised if the array is empty, if any array element is negative or if <var>Lambda</var> is zero.</p>"
2232+
Extra="<p>The <em>power mean</em> is also known as the <em>generalised mean</em> and the <em>Holder mean</em>.</p><p>See <a href="https://en.m.wikipedia.org/wiki/Generalized_mean">Wikipedia</a> for further information.</p>"
2233+
Kind=routine
2234+
Units=SysUtils,Math
2235+
SeeAlso=ArithmeticMean_Double,GeometricMean_Double,HarmonicMean_Double,PowerMean_Cardinal,PowerMean_Integer,LogarithmicMean,WeightedPowerMean_Double
2236+
TestInfo=advanced
2237+
AdvancedTest.Level=unit-tests
2238+
AdvancedTest.URL="https://github.com/delphidabbler/code-snippets/tree/master/tests/Cat-Maths"
2239+
Snip=691.dat
2240+
DelphiXE=Y
2241+
Delphi12A=Y
2242+
2243+
[PowerMean_Integer]
2244+
DisplayName="PowerMean (Integer overload)"
2245+
DescEx="<p>Returns the power mean of the elements of <var>Integer</var> array <var>Values</var>, with exponent <var>Lambda</var>.</p><p>An <var>EArgumentException</var> is raised if the array is empty, if any array element is negative or if <var>Lambda</var> is zero.</p>"
2246+
Extra="<p>The <em>power mean</em> is also known as the <em>generalised mean</em> and the <em>Holder mean</em>.</p><p>See <a href="https://en.m.wikipedia.org/wiki/Generalized_mean">Wikipedia</a> for further information.</p>"
2247+
Kind=routine
2248+
Units=Types
2249+
Depends=PowerMean_Double
2250+
SeeAlso=ArithmeticMean_Integer,GeometricMean_Integer,HarmonicMean_Integer,PowerMean_Double,PowerMean_Cardinal,LogarithmicMean,WeightedPowerMean_Integer
2251+
TestInfo=advanced
2252+
AdvancedTest.Level=unit-tests
2253+
AdvancedTest.URL="https://github.com/delphidabbler/code-snippets/tree/master/tests/Cat-Maths"
2254+
Snip=692.dat
2255+
DelphiXE=Y
2256+
Delphi12A=Y
2257+
2258+
[PowerMean_Cardinal]
2259+
DisplayName="PowerMean (Cardinal overload)"
2260+
DescEx="<p>Returns the power mean of the elements of <var>Cardinal</var> array <var>Values</var>, with exponent <var>Lambda</var>.</p><p>An <var>EArgumentException</var> is raised if the array is empty or if <var>Lambda</var> is zero.</p>"
2261+
Extra="<p>The <em>power mean</em> is also known as the <em>generalised mean</em> and the <em>Holder mean</em>.</p><p>See <a href="https://en.m.wikipedia.org/wiki/Generalized_mean">Wikipedia</a> for further information.</p>"
2262+
Kind=routine
2263+
Units=Types
2264+
Depends=PowerMean_Double
2265+
SeeAlso=ArithmeticMean_Cardinal,GeometricMean_Cardinal,HarmonicMean_Cardinal,PowerMean_Double,PowerMean_Integer,LogarithmicMean,WeightedPowerMean_Cardinal
2266+
TestInfo=advanced
2267+
AdvancedTest.Level=unit-tests
2268+
AdvancedTest.URL="https://github.com/delphidabbler/code-snippets/tree/master/tests/Cat-Maths"
2269+
Snip=693.dat
2270+
DelphiXE=Y
2271+
Delphi12A=Y
2272+
2273+
[WeightedPowerMean_Double]
2274+
DisplayName="WeightedPowerMean (Double overload)"
2275+
DescEx="<p>Returns the weighted power mean of the elements of <var>Double</var> array <var>Values</var>, with exponent <var>Lambda</var>. Each term 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; no element of <var>Values</var> may be negative; <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; <var>Lambda</var> must not be zero.</p>"
2276+
Extra="<p>The <em>power mean</em> is also known as the <em>generalised mean</em> and the <em>Holder mean</em>.</p><p>See <a href="https://www.statisticshowto.com/power-mean-generalized-mean/">Statistics How To</a> for further information.</p>"
2277+
Kind=routine
2278+
Units=SysUtils,Types,Math
2279+
Depends=NormaliseByWeight_Double
2280+
SeeAlso=WeightedArithmeticMean_Double,WeightedGeometricMean_Double,WeightedHarmonicMean_Double,PowerMean_Double,WeightedPowerMean_Cardinal,WeightedPowerMean_Integer
2281+
TestInfo=advanced
2282+
AdvancedTest.Level=unit-tests
2283+
AdvancedTest.URL="https://github.com/delphidabbler/code-snippets/tree/master/tests/Cat-Maths"
2284+
Snip=694.dat
2285+
DelphiXE=Y
2286+
Delphi12A=Y
2287+
2288+
[WeightedPowerMean_Integer]
2289+
DisplayName="WeightedPowerMean (Integer overload)"
2290+
DescEx="<p>Returns the weighted power mean of the elements of <var>Integer</var> array <var>Values</var>, with exponent <var>Lambda</var>. Each term 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; no element of <var>Values</var> may be negative; <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; <var>Lambda</var> must not be zero.</p>"
2291+
Extra="<p>The <em>power mean</em> is also known as the <em>generalised mean</em> and the <em>Holder mean</em>.</p><p>See <a href="https://www.statisticshowto.com/power-mean-generalized-mean/">Statistics How To</a> for further information.</p>"
2292+
Kind=routine
2293+
Units=Types
2294+
Depends=WeightedPowerMean_Double
2295+
SeeAlso=WeightedArithmeticMean_Integer,WeightedGeometricMean_Integer,WeightedHarmonicMean_Integer,PowerMean_Integer,WeightedPowerMean_Cardinal,WeightedPowerMean_Double
2296+
TestInfo=advanced
2297+
AdvancedTest.Level=unit-tests
2298+
AdvancedTest.URL="https://github.com/delphidabbler/code-snippets/tree/master/tests/Cat-Maths"
2299+
Snip=695.dat
2300+
DelphiXE=Y
2301+
Delphi12A=Y
2302+
2303+
[WeightedPowerMean_Cardinal]
2304+
DisplayName="WeightedPowerMean (Cardinal overload)"
2305+
DescEx="<p>Returns the weighted power mean of the elements of <var>Cardinal</var> array <var>Values</var>, with exponent <var>Lambda</var>. Each term 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; <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; <var>Lambda</var> must not be zero.</p>"
2306+
Extra="<p>The <em>power mean</em> is also known as the <em>generalised mean</em> and the <em>Holder mean</em>.</p><p>See <a href="https://www.statisticshowto.com/power-mean-generalized-mean/">Statistics How To</a> for further information.</p>"
2307+
Kind=routine
2308+
Units=Types
2309+
Depends=WeightedPowerMean_Double
2310+
SeeAlso=WeightedArithmeticMean_Cardinal,WeightedGeometricMean_Cardinal,WeightedHarmonicMean_Cardinal,PowerMean_Cardinal,WeightedPowerMean_Double,WeightedPowerMean_Integer
2311+
TestInfo=advanced
2312+
AdvancedTest.Level=unit-tests
2313+
AdvancedTest.URL="https://github.com/delphidabbler/code-snippets/tree/master/tests/Cat-Maths"
2314+
Snip=696.dat
2315+
DelphiXE=Y
2316+
Delphi12A=Y

0 commit comments

Comments
(0)

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