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 9c411d5

Browse files
Merge branch 'feature/49-rms-function' into develop
Fixes #49
2 parents 8196a2e + 3f003fb commit 9c411d5

File tree

5 files changed

+163
-1
lines changed

5 files changed

+163
-1
lines changed

‎collection/702.dat

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
function RMS(const A: array of Double): Double; overload;
2+
var
3+
Squares: array of Double;
4+
Idx: Integer;
5+
begin
6+
System.SetLength(Squares, System.Length(A));
7+
for Idx := 0 to Pred(System.Length(A)) do
8+
Squares[Idx] := A[Idx] * A[Idx];
9+
// Note: ArithmeticMean raises exception if A is empty
10+
Result := Math.Power(ArithmeticMean(Squares), 0.5);
11+
end;

‎collection/703.dat

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
function RMS(const A: array of Integer): Double; overload;
2+
var
3+
Squares: array of Double;
4+
Idx: Integer;
5+
Elem: Double;
6+
begin
7+
System.SetLength(Squares, System.Length(A));
8+
for Idx := 0 to Pred(System.Length(A)) do
9+
begin
10+
// convert Integer to Double before squaring to reduce change of overflow
11+
Elem := A[Idx];
12+
Squares[Idx] := Elem * Elem;
13+
end;
14+
// Note: ArithmeticMean raises exception if A is empty
15+
Result := Math.Power(ArithmeticMean(Squares), 0.5);
16+
end;

‎collection/maths.ini

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2387,3 +2387,31 @@ AdvancedTest.URL="https://github.com/delphidabbler/code-snippets/tree/master/tes
23872387
Snip=701.dat
23882388
DelphiXE=Y
23892389
Delphi12A=Y
2390+
2391+
[RMS_Double]
2392+
DisplayName="RMS (Double overload)"
2393+
DescEx="<p>Calculates the root mean square of the elements of <var>Double</var> floating point array <var>A</var>.</p><p>Raises <var>EArgumentException</var> if <var>A</var> is empty.</p>"
2394+
Kind=routine
2395+
Units=SysUtils,Math
2396+
Depends=ArithmeticMean_Double
2397+
SeeAlso=RMS_Integer
2398+
TestInfo=advanced
2399+
AdvancedTest.Level=unit-tests
2400+
AdvancedTest.URL="https://github.com/delphidabbler/code-snippets/tree/master/tests/Cat-Maths"
2401+
Snip=702.dat
2402+
DelphiXE=Y
2403+
Delphi12A=Y
2404+
2405+
[RMS_Integer]
2406+
DisplayName="RMS (Integer overload)"
2407+
DescEx="<p>Calculates the root mean square of the elements of <var>Integer</var> array <var>A</var>.</p><p>Raises <var>EArgumentException</var> if <var>A</var> is empty.</p>"
2408+
Kind=routine
2409+
Units=SysUtils,Math
2410+
Depends=ArithmeticMean_Double
2411+
SeeAlso=RMS_Double
2412+
TestInfo=advanced
2413+
AdvancedTest.Level=unit-tests
2414+
AdvancedTest.URL="https://github.com/delphidabbler/code-snippets/tree/master/tests/Cat-Maths"
2415+
Snip=703.dat
2416+
DelphiXE=Y
2417+
Delphi12A=Y

‎tests/Cat-Maths/TestUMathsCatSnippets.pas

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,8 @@ TestMathsCatSnippets = class(TTestCase)
8484
procedure TestHasMode_ExceptSingleElementArray;
8585
procedure TestModeCount_ExceptEmptyArray;
8686
procedure TestModeCount_ExceptSingleElementArray;
87+
procedure TestRMS_Double_ExceptEmptyArray;
88+
procedure TestRMS_Integer_ExceptEmptyArray;
8789
function EqualArrays(const Left, Right: TBytes): Boolean; overload;
8890
function EqualArrays(const Left, Right: array of Integer): Boolean;
8991
overload;
@@ -187,6 +189,8 @@ TestMathsCatSnippets = class(TTestCase)
187189
procedure TestModeAlt;
188190
procedure TestHasMode;
189191
procedure TestModeCount;
192+
procedure TestRMS_Double;
193+
procedure TestRMS_Integer;
190194
end;
191195

192196
implementation
@@ -2418,6 +2422,58 @@ procedure TestMathsCatSnippets.TestResizeRect_B;
24182422
CheckEquals(-4, RectHeight(R), '3: RectHeight');
24192423
end;
24202424

2425+
procedure TestMathsCatSnippets.TestRMS_Double;
2426+
const
2427+
Fudge = 0.0001;
2428+
A: array[1..4] of Double = (23.45, 35.786, 87326.948, 13);
2429+
B: array[1..8] of Double = (-19.0, 27.890, -42.83729, 56.73829, 100.0, -100.0, 0.0, 666.6);
2430+
C: array[1..3] of Double = (0.0, 0.0, 0.0);
2431+
D: array[1..1] of Double = (2345.67889);
2432+
E: array[1..2] of Double = (-999.99, +999.99);
2433+
begin
2434+
// Some expected results from https://miniwebtool.com/root-mean-square-calculator/
2435+
CheckEquals(43663.47973, RMS(A), Fudge, 'A');
2436+
CheckEquals(242.5254314, RMS(B), Fudge, 'B');
2437+
CheckEquals(0.0, RMS(C), Fudge, 'C');
2438+
CheckEquals(2345.67889, RMS(D), Fudge, 'D');
2439+
CheckEquals(999.99, RMS(E), Fudge, 'E');
2440+
CheckException(TestRMS_Double_ExceptEmptyArray, EArgumentException, 'Empty array');
2441+
end;
2442+
2443+
procedure TestMathsCatSnippets.TestRMS_Double_ExceptEmptyArray;
2444+
var
2445+
A: array of Double;
2446+
begin
2447+
SetLength(A, 0);
2448+
RMS(A);
2449+
end;
2450+
2451+
procedure TestMathsCatSnippets.TestRMS_Integer;
2452+
const
2453+
Fudge = 0.0001;
2454+
A: array[1..4] of Integer = (23, 36, 87327, 13);
2455+
B: array[1..8] of Integer = (-19, 28, -43, 57, 100, -100, 0, 666);
2456+
C: array[1..3] of Integer = (0, 0, 0);
2457+
D: array[1..1] of Integer = (2346);
2458+
E: array[1..2] of Integer = (-999, +999);
2459+
begin
2460+
// Some expected results from https://miniwebtool.com/root-mean-square-calculator/
2461+
CheckEquals(43663.505708428864, RMS(A), Fudge, 'A');
2462+
CheckEquals(242.3321584, RMS(B), Fudge, 'B');
2463+
CheckEquals(0.0, RMS(C), Fudge, 'C');
2464+
CheckEquals(2346.0, RMS(D), Fudge, 'D');
2465+
CheckEquals(999.0, RMS(E), Fudge, 'E');
2466+
CheckException(TestRMS_Integer_ExceptEmptyArray, EArgumentException, 'Empty array');
2467+
end;
2468+
2469+
procedure TestMathsCatSnippets.TestRMS_Integer_ExceptEmptyArray;
2470+
var
2471+
A: array of Integer;
2472+
begin
2473+
SetLength(A, 0);
2474+
RMS(A);
2475+
end;
2476+
24212477
procedure TestMathsCatSnippets.TestSoftMax;
24222478

24232479
function ArraysEqual(const Left, Right: array of Double): Boolean;

‎tests/Cat-Maths/UMathsCatSnippets.pas

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
* The unit is copyright © 2005-2025 by Peter Johnson & Contributors and is
77
* licensed under the MIT License (https://opensource.org/licenses/MIT).
88
*
9-
* Generated on : Fri, 17 Jan 2025 09:41:35 GMT.
9+
* Generated on : Sat, 18 Jan 2025 09:49:49 GMT.
1010
* Generated by : DelphiDabbler CodeSnip Release 4.24.0.
1111
*
1212
* The latest version of CodeSnip is available from the CodeSnip GitHub project
@@ -669,6 +669,19 @@ function ReverseNumber(AValue: Int64): Int64;
669669
}
670670
function ReverseNumberR(AValue: Int64): Int64;
671671

672+
{
673+
Calculates the root mean square of the elements of Double floating point array
674+
A.
675+
Raises EArgumentException if A is empty.
676+
}
677+
function RMS(const A: array of Double): Double; overload;
678+
679+
{
680+
Calculates the root mean square of the elements of Integer array A.
681+
Raises EArgumentException if A is empty.
682+
}
683+
function RMS(const A: array of Integer): Double; overload;
684+
672685
{
673686
Performs an arithmetic right shift operation on the given value and returns
674687
the result. Value is shifted right by Shift bits.
@@ -2838,6 +2851,44 @@ function ReverseNumberR(AValue: Int64): Int64;
28382851
+ ReverseNumberR(AValue div 10)
28392852
end;
28402853

2854+
{
2855+
Calculates the root mean square of the elements of Double floating point array
2856+
A.
2857+
Raises EArgumentException if A is empty.
2858+
}
2859+
function RMS(const A: array of Double): Double; overload;
2860+
var
2861+
Squares: array of Double;
2862+
Idx: Integer;
2863+
begin
2864+
System.SetLength(Squares, System.Length(A));
2865+
for Idx := 0 to Pred(System.Length(A)) do
2866+
Squares[Idx] := A[Idx] * A[Idx];
2867+
// Note: ArithmeticMean raises exception if A is empty
2868+
Result := Math.Power(ArithmeticMean(Squares), 0.5);
2869+
end;
2870+
2871+
{
2872+
Calculates the root mean square of the elements of Integer array A.
2873+
Raises EArgumentException if A is empty.
2874+
}
2875+
function RMS(const A: array of Integer): Double; overload;
2876+
var
2877+
Squares: array of Double;
2878+
Idx: Integer;
2879+
Elem: Double;
2880+
begin
2881+
System.SetLength(Squares, System.Length(A));
2882+
for Idx := 0 to Pred(System.Length(A)) do
2883+
begin
2884+
// convert Integer to Double before squaring to reduce change of overflow
2885+
Elem := A[Idx];
2886+
Squares[Idx] := Elem * Elem;
2887+
end;
2888+
// Note: ArithmeticMean raises exception if A is empty
2889+
Result := Math.Power(ArithmeticMean(Squares), 0.5);
2890+
end;
2891+
28412892
{
28422893
Performs an arithmetic right shift operation on the given value and returns
28432894
the result. Value is shifted right by Shift bits.

0 commit comments

Comments
(0)

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