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 fa2e762

Browse files
Add 6 new routines to Maths category
Added Integer, Cardinal and Double overloads of each of the new ArithMean and WeightedArithMean routines.
1 parent 18a672e commit fa2e762

File tree

7 files changed

+158
-0
lines changed

7 files changed

+158
-0
lines changed

‎collection/650.dat‎

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

‎collection/651.dat‎

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

‎collection/652.dat‎

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

‎collection/653.dat‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
function WeightedArithMean(const Values: array of Double;
2+
const Weights: array of Double): Double; overload;
3+
var
4+
WeightSum: Double;
5+
Weight: Double;
6+
Idx: Integer;
7+
begin
8+
if Length(Values) = 0 then
9+
raise SysUtils.EArgumentException.Create('Array of values is empty');
10+
if Length(Values) <> Length(Weights) then
11+
raise SysUtils.EArgumentException.Create(
12+
'Number of values and number of weights must be the same'
13+
);
14+
WeightSum := 0.0;
15+
for Weight in Weights do
16+
begin
17+
if Math.Sign(Weight) = Math.NegativeValue then
18+
raise SysUtils.EArgumentException.Create('Weights must all be >= 0');
19+
WeightSum := WeightSum + Weight;
20+
end;
21+
if Math.IsZero(WeightSum) then
22+
raise SysUtils.EArgumentException.Create('All weights are 0');
23+
Result := 0.0;
24+
for Idx := Low(Values) to High(Values) do
25+
Result := Result + Weights[Idx] * Values[Idx] / WeightSum;
26+
end;

‎collection/654.dat‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
function WeightedArithMean(const Values: array of Integer;
2+
const Weights: array of Double): Double; overload;
3+
var
4+
Idx: Integer;
5+
DblVals: array of Double;
6+
begin
7+
SetLength(DblVals, Length(Values));
8+
for Idx := Low(Values) to High(Values) do
9+
DblVals[Idx] := Values[Idx];
10+
Result := WeightedArithMean(DblVals, Weights);
11+
end;

‎collection/655.dat‎

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

‎collection/maths.ini‎

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1681,3 +1681,77 @@ DelphiXE3=Y
16811681
DelphiXE4=Y
16821682
Delphi10S=Y
16831683
FPC=Y
1684+
1685+
[ArithMean_Double]
1686+
DisplayName="ArithMean (Double overload)"
1687+
DescEx="<p>Returns the arithmetic mean of an array of <var>Double</var> values.</p><p><var>EArgumentException</var> is raised if the array is empty.</p>"
1688+
Units=SysUtils
1689+
SeeAlso=ArithMean_Integer,ArithMean_Cardinal
1690+
TestInfo=advanced
1691+
AdvancedTest.Level=unit-tests
1692+
AdvancedTest.URL="https://github.com/delphidabbler/code-snippets/tree/master/tests/Cat-Maths"
1693+
Snip=650.dat
1694+
DelphiXE=Y
1695+
Delphi12A=Y
1696+
1697+
[ArithMean_Integer]
1698+
DisplayName="ArithMean (Integer overload)"
1699+
DescEx="<p>Returns the arithmetic mean of an array of <var>Integer</var> values.</p><p><var>EArgumentException</var> is raised if the array is empty.</p>"
1700+
Units=SysUtils
1701+
SeeAlso=ArithMean_Double,ArithMean_Cardinal
1702+
TestInfo=advanced
1703+
AdvancedTest.Level=unit-tests
1704+
AdvancedTest.URL="https://github.com/delphidabbler/code-snippets/tree/master/tests/Cat-Maths"
1705+
Snip=651.dat
1706+
DelphiXE=Y
1707+
Delphi12A=Y
1708+
1709+
[ArithMean_Cardinal]
1710+
DisplayName="ArithMean (Cardinal overload)"
1711+
DescEx="<p>Returns the arithmetic mean of an array of <var>Cardinal</var> values.</p><p><var>EArgumentException</var> is raised if the array is empty.</p>"
1712+
Units=SysUtils
1713+
SeeAlso=ArithMean_Double,ArithMean_Integer
1714+
TestInfo=advanced
1715+
AdvancedTest.Level=unit-tests
1716+
AdvancedTest.URL="https://github.com/delphidabbler/code-snippets/tree/master/tests/Cat-Maths"
1717+
Snip=652.dat
1718+
DelphiXE=Y
1719+
Delphi12A=Y
1720+
1721+
[WeightedArithMean_Double]
1722+
DisplayName="WeightedArithMean (Double overload)"
1723+
DescEx="<p>Calculates and returns the weighted average of the <var>Double</var> elements of array <var>Values</var> 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; <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>"
1724+
Units=SysUtils,Math
1725+
SeeAlso=ArithMean_Double,WeightedArithMean_Integer,WeightedArithMean_Cardinal
1726+
TestInfo=advanced
1727+
AdvancedTest.Level=unit-tests
1728+
AdvancedTest.URL="https://github.com/delphidabbler/code-snippets/tree/master/tests/Cat-Maths"
1729+
Snip=653.dat
1730+
DelphiXE=Y
1731+
Delphi12A=Y
1732+
1733+
[WeightedArithMean_Integer]
1734+
DisplayName="WeightedArithMean (Integer overload)"
1735+
DescEx="<p>Calculates and returns the weighted average of the <var>Integer</var> elements of array <var>Values</var> 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; <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>"
1736+
Units=
1737+
Depends=WeightedArithMean_Double
1738+
SeeAlso=ArithMean_Integer,WeightedArithMean_Double,WeightedArithMean_Cardinal
1739+
TestInfo=advanced
1740+
AdvancedTest.Level=unit-tests
1741+
AdvancedTest.URL="https://github.com/delphidabbler/code-snippets/tree/master/tests/Cat-Maths"
1742+
Snip=654.dat
1743+
DelphiXE=Y
1744+
Delphi12A=Y
1745+
1746+
[WeightedArithMean_Cardinal]
1747+
DisplayName="WeightedArithMean (Cardinal overload)"
1748+
DescEx="<p>Calculates and returns the weighted average of the <var>Cardinal</var> elements of array <var>Values</var> 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; <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>"
1749+
Units=
1750+
Depends=WeightedArithMean_Double
1751+
SeeAlso=ArithMean_Cardinal,WeightedArithMean_Double,WeightedArithMean_Integer
1752+
TestInfo=advanced
1753+
AdvancedTest.Level=unit-tests
1754+
AdvancedTest.URL="https://github.com/delphidabbler/code-snippets/tree/master/tests/Cat-Maths"
1755+
Snip=655.dat
1756+
DelphiXE=Y
1757+
Delphi12A=Y

0 commit comments

Comments
(0)

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