Skip to content

Commit 9cc6d86

Browse files
committed
Merge branch 'feature/51-total-sum-of-squares-fn' into develop
Fixes #51
2 parents 9c411d5 + b87d292 commit 9cc6d86

File tree

5 files changed

+173
-1
lines changed

5 files changed

+173
-1
lines changed

collection/704.dat

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
function TSS(const A: array of Double): Double; overload;
2+
var
3+
ElemOfA: Double;
4+
MeanOfA: Double;
5+
begin
6+
// Note: ArithmeticMean raises an exception if A is empty
7+
MeanOfA := ArithmeticMean(A);
8+
Result := 0.0;
9+
for ElemOfA in A do
10+
Result := Result + System.Sqr(ElemOfA - MeanOfA);
11+
end;

collection/705.dat

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
function TSS(const A: array of Integer): Double; overload;
2+
var
3+
ElemOfA: Double;
4+
MeanOfA: Double;
5+
begin
6+
// Note: ArithmeticMean raises an exception if A is empty
7+
MeanOfA := ArithmeticMean(A);
8+
Result := 0.0;
9+
for ElemOfA in A do
10+
Result := Result + System.Sqr(ElemOfA - MeanOfA);
11+
end;

collection/maths.ini

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2415,3 +2415,31 @@ AdvancedTest.URL="https://github.com/delphidabbler/code-snippets/tree/master/tes
24152415
Snip=703.dat
24162416
DelphiXE=Y
24172417
Delphi12A=Y
2418+
2419+
[TSS_Double]
2420+
DisplayName="TSS (Double overload)"
2421+
DescEx="<p>Calculates the statistical total sum of squares of the elements of <var>Double</var> array <var>A</var>.</p><p><var>EArgumentException</var> is raised if <var>A</var> is empty.</p>"
2422+
Extra="<p>See <a href="https://en.wikipedia.org/wiki/Total_sum_of_squares">Wikipedia</a> for an explanation of the <var>TSS</var> function.</p><p><strong>Note:</strong> <var>TSS</var> is not the same as the <var>SumOfSquares</var> function in the Delphi RTL.</p>"
2423+
Kind=routine
2424+
Depends=ArithmeticMean_Double
2425+
SeeAlso=TSS_Integer
2426+
TestInfo=advanced
2427+
AdvancedTest.Level=unit-tests
2428+
AdvancedTest.URL="https://github.com/delphidabbler/code-snippets/tree/master/tests/Cat-Maths"
2429+
Snip=704.dat
2430+
DelphiXE=Y
2431+
Delphi12A=Y
2432+
2433+
[TSS_Integer]
2434+
DisplayName="TSS (Integer overload)"
2435+
DescEx="<p>Calculates the statistical total sum of squares of the elements of <var>Integer</var> array <var>A</var>.</p><p><var>EArgumentException</var> is raised if <var>A</var> is empty.</p>"
2436+
Extra="<p>See <a href="https://en.wikipedia.org/wiki/Total_sum_of_squares">Wikipedia</a> for an explanation of the <var>TSS</var> function.</p><p><strong>Note:</strong> <var>TSS</var> is not the same as the <var>SumOfSquares</var> function in the Delphi RTL.</p>"
2437+
Kind=routine
2438+
Depends=ArithmeticMean_Integer
2439+
SeeAlso=TSS_Double
2440+
TestInfo=advanced
2441+
AdvancedTest.Level=unit-tests
2442+
AdvancedTest.URL="https://github.com/delphidabbler/code-snippets/tree/master/tests/Cat-Maths"
2443+
Snip=705.dat
2444+
DelphiXE=Y
2445+
Delphi12A=Y

tests/Cat-Maths/TestUMathsCatSnippets.pas

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,8 @@ TestMathsCatSnippets = class(TTestCase)
8686
procedure TestModeCount_ExceptSingleElementArray;
8787
procedure TestRMS_Double_ExceptEmptyArray;
8888
procedure TestRMS_Integer_ExceptEmptyArray;
89+
procedure TestTSS_Double_ExceptEmptyArray;
90+
procedure TestTSS_Integer_ExceptEmptyArray;
8991
function EqualArrays(const Left, Right: TBytes): Boolean; overload;
9092
function EqualArrays(const Left, Right: array of Integer): Boolean;
9193
overload;
@@ -191,6 +193,8 @@ TestMathsCatSnippets = class(TTestCase)
191193
procedure TestModeCount;
192194
procedure TestRMS_Double;
193195
procedure TestRMS_Integer;
196+
procedure TestTSS_Double;
197+
procedure TestTSS_Integer;
194198
end;
195199

196200
implementation
@@ -2791,6 +2795,76 @@ procedure TestMathsCatSnippets.TestSumOfReciprocals_Integer_ExceptNonPositive;
27912795
SumOfReciprocals(A);
27922796
end;
27932797

2798+
procedure TestMathsCatSnippets.TestTSS_Double;
2799+
const
2800+
// Expected results calculated using
2801+
// https://www.socscistatistics.com/tests/sumofsquares/default.aspx
2802+
Fudge = 0.00001;
2803+
A: array[1..4] of Double = (49.7, -89.3, 9.36, 10.0);
2804+
EA = 10529.7752;
2805+
B: array[1..1] of Double = (7483.84);
2806+
EB = 0.0;
2807+
C: array[1..3] of Double = (7483.84, 7483.84, 7483.84);
2808+
EC = 0.0;
2809+
D: array[1..3] of Double = (-7483.84, -7483.84, -7483.84);
2810+
ED = 0.0;
2811+
E: array[1..3] of Double = (-7483.84, 7483.84, -7483.84);
2812+
EE = 149354296.38827;
2813+
F: array[1..6] of Double = (-42.0, 42.0, -56.0, 56.0, -4256.42, 4256.42);
2814+
EF = 36244022.4328;
2815+
begin
2816+
CheckEquals(EA, TSS(A), Fudge, 'A');
2817+
CheckEquals(EB, TSS(B), Fudge, 'B');
2818+
CheckEquals(EC, TSS(C), Fudge, 'C');
2819+
CheckEquals(ED, TSS(D), Fudge, 'D');
2820+
CheckEquals(EE, TSS(E), Fudge, 'E');
2821+
CheckEquals(EF, TSS(F), Fudge, 'F');
2822+
CheckException(TestTSS_Double_ExceptEmptyArray, EArgumentException, 'Empty array');
2823+
end;
2824+
2825+
procedure TestMathsCatSnippets.TestTSS_Double_ExceptEmptyArray;
2826+
var
2827+
A: array of Double;
2828+
begin
2829+
SetLength(A, 0);
2830+
TSS(A);
2831+
end;
2832+
2833+
procedure TestMathsCatSnippets.TestTSS_Integer;
2834+
const
2835+
// Expected results calculated using
2836+
// https://www.socscistatistics.com/tests/sumofsquares/default.aspx
2837+
Fudge = 0.00001;
2838+
A: array[1..4] of Integer = (50, -89, 9, 10);
2839+
EA = 10502.0;
2840+
B: array[1..1] of Integer = (7484);
2841+
EB = 0.0;
2842+
C: array[1..3] of Integer = (7484, 7484, 7484);
2843+
EC = 0.0;
2844+
D: array[1..3] of Integer = (-7484, -7484, -7484);
2845+
ED = 0.0;
2846+
E: array[1..3] of Integer = (-7484, 7484, -7484);
2847+
EE = 149360682.66667;
2848+
F: array[1..6] of Integer = (-42, 42, -56, 56, -4256, 4256);
2849+
EF = 36236872.0;
2850+
begin
2851+
CheckEquals(EA, TSS(A), Fudge, 'A');
2852+
CheckEquals(EB, TSS(B), Fudge, 'B');
2853+
CheckEquals(EC, TSS(C), Fudge, 'C');
2854+
CheckEquals(ED, TSS(D), Fudge, 'D');
2855+
CheckEquals(EE, TSS(E), Fudge, 'E');
2856+
CheckEquals(EF, TSS(F), Fudge, 'F');
2857+
CheckException(TestTSS_Integer_ExceptEmptyArray, EArgumentException, 'Empty array');
2858+
end;
2859+
2860+
procedure TestMathsCatSnippets.TestTSS_Integer_ExceptEmptyArray;
2861+
var
2862+
A: array of Integer;
2863+
begin
2864+
SetLength(A, 0);
2865+
TSS(A);
2866+
end;
2867+
27942868
procedure TestMathsCatSnippets.TestWeightedArithmeticMean_Cardinal;
27952869
const
27962870
A: array[1..3] of Cardinal = (12, 6, 3);

tests/Cat-Maths/UMathsCatSnippets.pas

Lines changed: 49 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 : Sat, 18 Jan 2025 09:49:49 GMT.
9+
* Generated on : Sat, 18 Jan 2025 10:35:45 GMT.
1010
* Generated by : DelphiDabbler CodeSnip Release 4.24.0.
1111
*
1212
* The latest version of CodeSnip is available from the CodeSnip GitHub project
@@ -815,6 +815,20 @@ function SumOfReciprocals(const A: array of Double): Double; overload;
815815
}
816816
function SumOfReciprocals(const A: array of Integer): Double; overload;
817817

818+
{
819+
Calculates the statistical total sum of squares of the elements of Double
820+
array A.
821+
EArgumentException is raised if A is empty.
822+
}
823+
function TSS(const A: array of Double): Double; overload;
824+
825+
{
826+
Calculates the statistical total sum of squares of the elements of Integer
827+
array A.
828+
EArgumentException is raised if A is empty.
829+
}
830+
function TSS(const A: array of Integer): Double; overload;
831+
818832
{
819833
Calculates and returns the weighted average of the Cardinal elements of array
820834
Values where each element is weighted by the corresponding element in the
@@ -3249,6 +3263,40 @@ function SumOfReciprocals(const A: array of Integer): Double; overload;
32493263
end;
32503264
end;
32513265

3266+
{
3267+
Calculates the statistical total sum of squares of the elements of Double
3268+
array A.
3269+
EArgumentException is raised if A is empty.
3270+
}
3271+
function TSS(const A: array of Double): Double; overload;
3272+
var
3273+
ElemOfA: Double;
3274+
MeanOfA: Double;
3275+
begin
3276+
// Note: ArithmeticMean raises an exception if A is empty
3277+
MeanOfA := ArithmeticMean(A);
3278+
Result := 0.0;
3279+
for ElemOfA in A do
3280+
Result := Result + System.Sqr(ElemOfA - MeanOfA);
3281+
end;
3282+
3283+
{
3284+
Calculates the statistical total sum of squares of the elements of Integer
3285+
array A.
3286+
EArgumentException is raised if A is empty.
3287+
}
3288+
function TSS(const A: array of Integer): Double; overload;
3289+
var
3290+
ElemOfA: Double;
3291+
MeanOfA: Double;
3292+
begin
3293+
// Note: ArithmeticMean raises an exception if A is empty
3294+
MeanOfA := ArithmeticMean(A);
3295+
Result := 0.0;
3296+
for ElemOfA in A do
3297+
Result := Result + System.Sqr(ElemOfA - MeanOfA);
3298+
end;
3299+
32523300
{
32533301
Calculates and returns the weighted average of the Cardinal elements of array
32543302
Values where each element is weighted by the corresponding element in the

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy