@@ -23,6 +23,7 @@ TestMathsCatSnippets = class(TTestCase)
2323 procedure TestPowNZN_EOverflow ;
2424 procedure TestDigitPowerSum_EOverflow ;
2525 procedure TestDigitPowerSum_EArgumentException ;
26+ procedure TestLSE_EArgumentException ;
2627 function EqualArrays (const Left, Right: TBytes): Boolean;
2728 function ReverseArray (const A: TBytes): TBytes;
2829 published
@@ -52,7 +53,7 @@ TestMathsCatSnippets = class(TTestCase)
5253 procedure TestMaxOfArray_Integer ;
5354 procedure TestMaxOfArray_Int64 ;
5455 procedure TestMaxOfArray_Single ;
55- procedure TestMaxOfArray_Double ;
56+ procedure TestMaxOfArray_Double ; // required by LSE
5657 procedure TestMaxOfArray_Extended ;
5758 procedure TestPowNZN ; // required by DigitPowerSum
5859 procedure TestPowNZZ ;
@@ -83,6 +84,8 @@ TestMathsCatSnippets = class(TTestCase)
8384 procedure TestDigitPowerSum ; // required by IsNarcissistic
8485 procedure TestIsPalindromic ;
8586 procedure TestIsNarcissistic ;
87+ procedure TestLSE ; // required by SoftMax
88+ procedure TestSoftMax ;
8689 end ;
8790
8891implementation
@@ -753,6 +756,31 @@ procedure TestMathsCatSnippets.TestLCD;
753756 CheckEquals(9 , LCD(-9 , -9 ), ' LCD(-9, -9)' );
754757end ;
755758
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+
756784procedure TestMathsCatSnippets.TestMaxOfArray_Double ;
757785var
758786 A: TDoubleDynArray;
@@ -1203,6 +1231,42 @@ procedure TestMathsCatSnippets.TestResizeRect_B;
12031231 CheckEquals(-4 , RectHeight(R), ' 3: RectHeight' );
12041232end ;
12051233
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+
12061270procedure TestMathsCatSnippets.TestStretchRect_A ;
12071271var
12081272 R0, R1, R2: TRect;
0 commit comments