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 273051d

Browse files
Add 8 new routines to Maths category
Added new MinMaxOfArray (Double & Integer overloads), RescaleRange (Double & Integer overloads), NormaliseByWeight (Double & Cardinal overloads) and RangeOf (Double & Integer overloads) routines to the Mathematics category. Added source code .dat files for all the new routines. Added meta data for all 8 new routines to maths.ini.
1 parent 0bbdeb5 commit 273051d

File tree

9 files changed

+229
-0
lines changed

9 files changed

+229
-0
lines changed

‎collection/667.dat

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
function RescaleRange(const A: array of Double): Types.TDoubleDynArray;
2+
overload;
3+
var
4+
Min, Max, RangeLength: Double;
5+
Idx: Integer;
6+
begin
7+
MinMaxOfArray(A, Min, Max); // raises exception if A is empty
8+
if Math.SameValue(Max, Min) then
9+
raise SysUtils.EArgumentException.Create('All array values are the same');
10+
System.SetLength(Result, System.Length(A));
11+
RangeLength := Max - Min;
12+
for Idx := 0 to Pred(System.Length(A)) do
13+
Result[Idx] := (A[Idx] - Min) / RangeLength;
14+
end;

‎collection/668.dat

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

‎collection/669.dat

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
procedure MinMaxOfArray(const A: array of Double;
2+
out MinValue, MaxValue: Double); overload;
3+
var
4+
Idx: Integer;
5+
Elem: Double;
6+
begin
7+
if Length(A) = 0 then
8+
raise SysUtils.EArgumentException.Create('Array is empty');
9+
MinValue := A[0];
10+
MaxValue := A[0];
11+
for Idx := 1 to Pred(Length(A)) do
12+
begin
13+
Elem := A[Idx];
14+
if Elem > MaxValue then
15+
MaxValue := Elem
16+
else if Elem < MinValue then
17+
MinValue := Elem;
18+
end;
19+
end;

‎collection/670.dat

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
procedure MinMaxOfArray(const A: array of Integer;
2+
out MinValue, MaxValue: Integer); overload;
3+
var
4+
Idx: Integer;
5+
Elem: Integer;
6+
begin
7+
if Length(A) = 0 then
8+
raise SysUtils.EArgumentException.Create('Array is empty');
9+
MinValue := A[0];
10+
MaxValue := A[0];
11+
for Idx := 1 to Pred(Length(A)) do
12+
begin
13+
Elem := A[Idx];
14+
if Elem > MaxValue then
15+
MaxValue := Elem
16+
else if Elem < MinValue then
17+
MinValue := Elem;
18+
end;
19+
end;

‎collection/671.dat

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
function NormaliseByWeight(const A: array of Cardinal): Types.TDoubleDynArray;
2+
overload;
3+
var
4+
Weight: Cardinal;
5+
WeightSum: UInt64;
6+
Idx: Integer;
7+
begin
8+
if (System.Length(A) = 0) then
9+
raise SysUtils.EArgumentException.Create('Array is empty');
10+
WeightSum := 0;
11+
for Weight in A do
12+
WeightSum := WeightSum + Weight;
13+
if WeightSum = 0 then
14+
raise SysUtils.EArgumentException.Create('Sum of array elements is zero');
15+
System.SetLength(Result, System.Length(A));
16+
for Idx := 0 to Pred(System.Length(A)) do
17+
Result[Idx] := A[Idx] / WeightSum;
18+
end;

‎collection/672.dat

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
function RangeOf(const A: array of Double): Double; overload;
2+
var
3+
MinValue, MaxValue: Double;
4+
begin
5+
MinMaxOfArray(A, MinValue, MaxValue); // exception raised if A is empty
6+
Result := MaxValue - MinValue;
7+
// Ensure that exactly zero is returned when MaxValue = MinValue
8+
if Math.IsZero(Result) then
9+
Result := 0.0;
10+
end;

‎collection/673.dat

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
function RangeOf(const A: array of Integer): Cardinal; overload;
2+
var
3+
MinValue, MaxValue: Integer;
4+
begin
5+
MinMaxOfArray(A, MinValue, MaxValue); // exception raised if A is empty
6+
// MaxValue >= MinValue is guaranteed => Result >= 0
7+
Result := Cardinal(MaxValue - MinValue);
8+
end;

‎collection/674.dat

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
function RescaleRange(const A: array of Integer): Types.TDoubleDynArray;
2+
overload;
3+
var
4+
Min, Max, RangeLength: Integer;
5+
Idx: Integer;
6+
begin
7+
MinMaxOfArray(A, Min, Max); // raises exception if A is empty
8+
if Max = Min then
9+
raise SysUtils.EArgumentException.Create('All array values are the same');
10+
System.SetLength(Result, System.Length(A));
11+
RangeLength := Max - Min;
12+
for Idx := 0 to Pred(System.Length(A)) do
13+
Result[Idx] := (A[Idx] - Min) / RangeLength;
14+
end;

‎collection/maths.ini

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1897,3 +1897,106 @@ AdvancedTest.URL="https://github.com/delphidabbler/code-snippets/tree/master/tes
18971897
Snip=666.dat
18981898
DelphiXE=Y
18991899
Delphi12A=Y
1900+
1901+
[MinMaxOfArray_Double]
1902+
DisplayName="MinMaxOfArray (Double overload)"
1903+
DescEx="<p>Finds the minimum and maximum values contained in the non-empty array, <var>A</var>, of double precision floating point values. <var>MinValue</var> and <var>MaxValue</var> are set to the minimum and maximum values, respectively.</p><p><var>EArgumentException</var> is raised if <var>A</var> is empty.</p>"
1904+
Kind=routine
1905+
Units=SysUtils
1906+
SeeAlso=MinMaxOfArray_Integer
1907+
TestInfo=advanced
1908+
AdvancedTest.Level=unit-tests
1909+
AdvancedTest.URL="https://github.com/delphidabbler/code-snippets/tree/master/tests/Cat-Maths"
1910+
Snip=669.dat
1911+
DelphiXE=Y
1912+
Delphi12A=Y
1913+
1914+
[MinMaxOfArray_Integer]
1915+
DisplayName="MinMaxOfArray (Integer overload)"
1916+
DescEx="<p>Finds the minimum and maximum values contained in the non-empty <var>Integer</var> array <var>A</var>. <var>MinValue</var> and <var>MaxValue</var> are set to the minimum and maximum values, respectively.</p><p><var>EArgumentException</var> is raised if <var>A</var> is empty.</p>"
1917+
Kind=routine
1918+
Units=SysUtils
1919+
SeeAlso=MinMaxOfArray_Double
1920+
TestInfo=advanced
1921+
AdvancedTest.Level=unit-tests
1922+
AdvancedTest.URL="https://github.com/delphidabbler/code-snippets/tree/master/tests/Cat-Maths"
1923+
Snip=670.dat
1924+
DelphiXE=Y
1925+
Delphi12A=Y
1926+
1927+
[RescaleRange_Double]
1928+
DisplayName="RescaleRange (Double overload)"
1929+
DescEx="<p>Rescales the elements of array <var>A</var> containing a range of <var>Double</var> values so that each value of <var>A</var> is mapped to a value in the range [0..1].</p><p>An array of the same size as <var>A</var> is returned where each element contains the rescaled value of the corresponding element of <var>A</var>.</p><p><var>A</var> must not be empty and not all elements may be the same value. <var>EArgumentException</var> is raised if either condition is not met.</p>"
1930+
Kind=routine
1931+
Units=SysUtils,Types,Math
1932+
Depends=MinMaxOfArray_Double
1933+
SeeAlso=NormaliseByWeight_Double,RescaleRange_Integer
1934+
TestInfo=advanced
1935+
AdvancedTest.Level=unit-tests
1936+
AdvancedTest.URL="https://github.com/delphidabbler/code-snippets/tree/master/tests/Cat-Maths"
1937+
Snip=667.dat
1938+
DelphiXE=Y
1939+
Delphi12A=Y
1940+
1941+
[RescaleRange_Integer]
1942+
DisplayName="RescaleRange (Integer overload)"
1943+
DescEx="<p>Rescales the elements of array <var>A</var> containing a range of <var>Integer</var> values so that each value of <var>A</var> is mapped to a value in the range [0..1].</p><p>An array of the same size as <var>A</var> is returned where each element contains the rescaled value of the corresponding element of <var>A</var>.</p><p><var>A</var> must not be empty and not all elements may be the same value. <var>EArgumentException</var> is raised if either condition is not met.</p>"
1944+
Kind=routine
1945+
Units=SysUtils,Types
1946+
Depends=MinMaxOfArray_Integer
1947+
SeeAlso=NormaliseByWeight_Cardinal,RescaleRange_Double
1948+
TestInfo=advanced
1949+
AdvancedTest.Level=unit-tests
1950+
AdvancedTest.URL="https://github.com/delphidabbler/code-snippets/tree/master/tests/Cat-Maths"
1951+
Snip=674.dat
1952+
DelphiXE=Y
1953+
Delphi12A=Y
1954+
1955+
[NormaliseByWeight_Double]
1956+
DisplayName="NormaliseByWeight (Double overload)"
1957+
DescEx="<p>Normalises the values in floating point array <var>A</var> so that each value of <var>A</var> is mapped to a value in the range [0..1], where the total of all the values is 1. The relative weights of the values are preserved.</p><p>An array of the same size as <var>A</var> is returned where each element contains the normalised value of the corresponding element of <var>A</var>.</p><p><var>A</var> must not be empty. All elements of <var>A</var> must be &gt;= 0, with at least one element &gt; 0. <var>EArgumentException</var> is raised if these conditions are not met.</p>"
1958+
Kind=routine
1959+
Units=SysUtils,Types,Math
1960+
SeeAlso=RescaleRange_Double,WeightedArithMean_Double,NormaliseByWeight_Cardinal
1961+
AdvancedTest.Level=unit-tests
1962+
AdvancedTest.URL="https://github.com/delphidabbler/code-snippets/tree/master/tests/Cat-Maths"
1963+
Snip=668.dat
1964+
DelphiXE=Y
1965+
Delphi12A=Y
1966+
1967+
[NormaliseByWeight_Cardinal]
1968+
DisplayName="NormaliseByWeight (Cardinal overload)"
1969+
DescEx="<p>Normalises the values in unsigned integer array <var>A</var> so that each value of <var>A</var> is mapped to a value in the range [0..1], where the total of all the values is 1. The relative weights of the values are preserved.</p><p>An array of the same size as <var>A</var> is returned where each element contains the normalised value of the corresponding element of <var>A</var>.</p><p><var>A</var> must not be empty and must have at least one non-zero element. <var>EArgumentException</var> is raised if these conditions are not met.</p>"
1970+
Kind=routine
1971+
Units=SysUtils,Types
1972+
SeeAlso=RescaleRange_Integer,WeightedArithMean_Integer,NormaliseByWeight_Double
1973+
AdvancedTest.Level=unit-tests
1974+
AdvancedTest.URL="https://github.com/delphidabbler/code-snippets/tree/master/tests/Cat-Maths"
1975+
Snip=671.dat
1976+
DelphiXE=Y
1977+
Delphi12A=Y
1978+
1979+
[RangeOf_Double]
1980+
DisplayName="RangeOf (Double overload)"
1981+
DescEx="<p>Returns the length of the range of values in non-empty <var>Double</var> array <var>A</var>.</p><p><var>EArgumentException</var> is raised if <var>A</var> is empty.</p>"
1982+
Kind=routine
1983+
Units=Math
1984+
Depends=MinMaxOfArray_Double
1985+
SeeAlso=RangeOf_Integer,RescaleRange_Double
1986+
AdvancedTest.Level=unit-tests
1987+
AdvancedTest.URL="https://github.com/delphidabbler/code-snippets/tree/master/tests/Cat-Maths"
1988+
Snip=672.dat
1989+
DelphiXE=Y
1990+
Delphi12A=Y
1991+
1992+
[RangeOf_Integer]
1993+
DisplayName="RangeOf (Integer overload)"
1994+
DescEx="<p>Returns the length of the range of values in non-empty <var>Integer</var> array <var>A</var>.</p><p><var>EArgumentException</var> is raised if <var>A</var> is empty.</p>"
1995+
Kind=routine
1996+
Depends=MinMaxOfArray_Integer
1997+
SeeAlso=RangeOf_Double,RescaleRange_Integer
1998+
AdvancedTest.Level=unit-tests
1999+
AdvancedTest.URL="https://github.com/delphidabbler/code-snippets/tree/master/tests/Cat-Maths"
2000+
Snip=673.dat
2001+
DelphiXE=Y
2002+
Delphi12A=Y

0 commit comments

Comments
(0)

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