@@ -23,6 +23,7 @@ TestMathsCatSnippets = class(TTestCase)
23
23
procedure TestPowNZN_EOverflow ;
24
24
procedure TestDigitPowerSum_EOverflow ;
25
25
procedure TestDigitPowerSum_EArgumentException ;
26
+ procedure TestLSE_EArgumentException ;
26
27
function EqualArrays (const Left, Right: TBytes): Boolean;
27
28
function ReverseArray (const A: TBytes): TBytes;
28
29
published
@@ -52,7 +53,7 @@ TestMathsCatSnippets = class(TTestCase)
52
53
procedure TestMaxOfArray_Integer ;
53
54
procedure TestMaxOfArray_Int64 ;
54
55
procedure TestMaxOfArray_Single ;
55
- procedure TestMaxOfArray_Double ;
56
+ procedure TestMaxOfArray_Double ; // required by LSE
56
57
procedure TestMaxOfArray_Extended ;
57
58
procedure TestPowNZN ; // required by DigitPowerSum
58
59
procedure TestPowNZZ ;
@@ -83,6 +84,8 @@ TestMathsCatSnippets = class(TTestCase)
83
84
procedure TestDigitPowerSum ; // required by IsNarcissistic
84
85
procedure TestIsPalindromic ;
85
86
procedure TestIsNarcissistic ;
87
+ procedure TestLSE ; // required by SoftMax
88
+ procedure TestSoftMax ;
86
89
end ;
87
90
88
91
implementation
@@ -753,6 +756,31 @@ procedure TestMathsCatSnippets.TestLCD;
753
756
CheckEquals(9 , LCD(-9 , -9 ), ' LCD(-9, -9)' );
754
757
end ;
755
758
759
+ procedure TestMathsCatSnippets.TestLSE ;
760
+ const
761
+ Fudge = 0.000001 ;
762
+ A1: array [1 ..7 ] of Double = (-35.0 , 20.78 , 42.56 , -27.8 , 41.576 , 0.0 , 57.945 );
763
+ A2: array [1 ..7 ] of Double = (-35.0 , 20.78 , 42.56 , -27.8 , 41.576 , 0.0 , 20.78 );
764
+ A5: array [1 ..3 ] of Double = (-430.0 , -399.83 , -300.00 );
765
+ A6: array [1 ..10 ] of Double = (-12.0 , 4.0 , -6.0 , 11.0 , 10.0 , 3.0 , -3.0 , 9.0 , -8.0 , 7.0 );
766
+ begin
767
+ // Hand calculated
768
+ CheckTrue(SameValue(57.945000285961067157769252279369 , LSE(A1)), ' #1' );
769
+ // Calculated using http://mycalcsolutions.com/calculator?mathematics;stat_prob;softmax
770
+ CheckTrue(SameValue(42.87759 , LSE(A2), Fudge), ' #2' );
771
+ CheckTrue(SameValue(-35.0 , LSE([-35.0 ]), Fudge), ' #3' );
772
+ CheckTrue(SameValue(0.0 , LSE([0.0 ]), Fudge), ' #4' );
773
+ CheckTrue(SameValue(-300.0 , LSE(A5), Fudge), ' #5' );
774
+ CheckTrue(SameValue(11.420537 , LSE(A6), Fudge), ' #6' );
775
+ // Check empty array exception
776
+ CheckException(TestLSE_EArgumentException, EArgumentException, ' EArgumentException' );
777
+ end ;
778
+
779
+ procedure TestMathsCatSnippets.TestLSE_EArgumentException ;
780
+ begin
781
+ LSE([]);
782
+ end ;
783
+
756
784
procedure TestMathsCatSnippets.TestMaxOfArray_Double ;
757
785
var
758
786
A: TDoubleDynArray;
@@ -1203,6 +1231,42 @@ procedure TestMathsCatSnippets.TestResizeRect_B;
1203
1231
CheckEquals(-4 , RectHeight(R), ' 3: RectHeight' );
1204
1232
end ;
1205
1233
1234
+ procedure TestMathsCatSnippets.TestSoftMax ;
1235
+
1236
+ function ArraysEqual (const Left, Right: array of Double): Boolean;
1237
+ const
1238
+ Fudge = 0.000001 ;
1239
+ var
1240
+ Idx: Integer;
1241
+ begin
1242
+ Result := True;
1243
+ if Length(Left) <> Length(Right) then
1244
+ Exit(False);
1245
+ for Idx := Low(Left) to High(Left) do
1246
+ if not SameValue(Left[Idx], Right[Idx], Fudge) then
1247
+ Exit(False);
1248
+ end ;
1249
+ const
1250
+ A1: array [1 ..7 ] of Double = (-35.0 , 20.78 , 42.56 , -27.8 , 41.576 , 0.0 , 57.945 );
1251
+ E1: array [1 ..7 ] of Double = (0.0 , 0.0 , 0.0 , 0.0 , 0.0 , 0.0 , 1.0 );
1252
+ A2: array [1 ..7 ] of Double = (-35.0 , 20.78 , 42.56 , -27.8 , 41.576 , 0.0 , 20.78 );
1253
+ E2: array [1 ..7 ] of Double = (0.0 , 0.0 , 0.727901 , 0.0 , 0.272099 , 0.0 , 0.0 );
1254
+ A5: array [1 ..3 ] of Double = (-430.0 , -399.83 , -300.0 );
1255
+ E5: array [1 ..3 ] of Double = (0.0 , 0.0 , 1.0 );
1256
+ A6: array [1 ..10 ] of Double = (-12.0 , 4.0 , -6.0 , 11.0 , 10.0 , 3.0 , -3.0 , 9.0 , -8.0 , 7.0 );
1257
+ E6: array [1 ..10 ] of Double = (0.0 , 0.000599 , 0.0 , 0.656694 , 0.241584 , 0.00022 , 0.000001 , 0.088874 , 0 , 0.012028 );
1258
+ A7: array [1 ..3 ] of Double = (1430.0 , 1430.83 , 1440.47 );
1259
+ E7: array [1 ..3 ] of Double = (0.000028 , 0.000065 , 0.999907 );
1260
+ begin
1261
+ CheckTrue(ArraysEqual(E1, SoftMax(A1)), ' #1' );
1262
+ CheckTrue(ArraysEqual(E2, SoftMax(A2)), ' #2' );
1263
+ CheckTrue(ArraysEqual([1.0 ], SoftMax([-35.0 ])), ' #3' );
1264
+ CheckTrue(ArraysEqual([1.0 ], SoftMax([0.0 ])), ' #4' );
1265
+ CheckTrue(ArraysEqual(E5, SoftMax(A5)), ' #6' );
1266
+ CheckTrue(ArraysEqual(E6, SoftMax(A6)), ' #6' );
1267
+ CheckTrue(ArraysEqual(E7, SoftMax(A7)), ' #7' );
1268
+ end ;
1269
+
1206
1270
procedure TestMathsCatSnippets.TestStretchRect_A ;
1207
1271
var
1208
1272
R0, R1, R2: TRect;
0 commit comments