Skip to content

Commit 9620a9c

Browse files
committed
Merge branch 'datascience' of https://github.com/fslaborg/FSharp.Stats into datascience
2 parents 4a71736 + 789223f commit 9620a9c

File tree

3 files changed

+26
-4
lines changed

3 files changed

+26
-4
lines changed

src/FSharp.Stats/Array.fs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,9 @@ module Array =
260260
/// </code>
261261
/// </example>
262262
let inline weightedMean (weights:array<'T>) (items:array<'T>) =
263+
// throws an exception if the collection is empty and the file type is not float (returns nan if float collection)
264+
if items.Length = 0 && not (box items :? float[]) then
265+
failwithf "Array.weightedMean is undefined for an empty non float sequence!"
263266
// DimensionMismatchException
264267
if (items.Length <> weights.Length) then
265268
failwithf "The items and weights must have the same length"
@@ -305,6 +308,10 @@ module Array =
305308
/// </code>
306309
/// </example>
307310
let inline weightedVariance mean (weights:array<'T>) (items:array<'T>) =
311+
// throws an exception if the collection is empty and the file type is not float (returns nan if float collection)
312+
if items.Length = 0 && not (box items :? float[]) then
313+
failwithf "Array.weightedVariance is undefined for an empty non float sequence!"
314+
308315
// DimensionMismatchException
309316
if (items.Length <> weights.Length) then
310317
failwithf "The items and weights must have the same length"
@@ -334,7 +341,10 @@ module Array =
334341
/// <code>
335342
/// </code>
336343
/// </example>
337-
let inline median (items:array<'T>) =
344+
let inline median (items: array<'T>) =
345+
// throws an exception if the collection is empty and the file type is not float (returns nan if float collection)
346+
if items.Length = 0 && not (box items :? float[]) then
347+
failwithf "Array.median is undefined for an empty non float sequence!"
338348

339349
let zero = LanguagePrimitives.GenericZero< 'T >
340350
let one = LanguagePrimitives.GenericOne< 'T >

tests/FSharp.Stats.Tests/Array.fs

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ let testArrayNegInfinity = [|10000.;-0.1;14.;-10.;5.;Double.NegativeInfinity|]
1313

1414
let testArrayEvenCountsInt = [|10000;-50;14;-9|]
1515
let testArrayOddCountsInt = [|10000;-50;14;-10;5|]
16+
let testArrayEmptyInt : int array = [||]
17+
let testArrayEmptyFloat : float array = [||]
18+
let testArrayEmptyDec : decimal array = [||]
1619

1720
[<Tests>]
1821
let medianTests =
@@ -33,12 +36,21 @@ let medianTests =
3336
let median = Array.median testArrayNegInfinity
3437
Expect.floatClose Accuracy.high median 2.45 "Median should be 2.45"
3538

36-
testCase "testListEvenCountsInt" <| fun () ->
39+
testCase "testArrayEvenCountsInt" <| fun () ->
3740
let median = Array.median testArrayEvenCountsInt
3841
Expect.equal median 2 "Median should be 2"
39-
testCase "testListOddCountsInt" <| fun () ->
42+
testCase "testArrayOddCountsInt" <| fun () ->
4043
let median = Array.median testArrayOddCountsInt
4144
Expect.equal median 5 "Median should be 5"
45+
testCase "testArrayEmptyFloat" <| fun () ->
46+
let median = Array.median testArrayEmptyFloat
47+
Expect.isTrue (Ops.isNan median) "Median of empty float array should be nan"
48+
testCase "testArrayEmptyInt" <| fun () ->
49+
let median() = Array.median testArrayEmptyInt
50+
Expect.throws (fun () -> median () |> ignore) "Median for empty non-float sequences is not defined"
51+
testCase "testArrayEmptyDec" <| fun () ->
52+
let median() = Array.median testArrayEmptyDec
53+
Expect.throws (fun () -> median () |> ignore) "Median for empty non-float sequences is not defined"
4254
]
4355

4456
[<Tests>]

tests/FSharp.Stats.Tests/FSharp.Stats.Tests.fsproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@
6767
<ItemGroup>
6868
<PackageReference Include="altcover" Version="8.6.68" />
6969
<PackageReference Include="Expecto" Version="10.*" />
70-
<PackageReference Include="YoloDev.Expecto.TestSdk" Version="0.*" />
70+
<PackageReference Include="YoloDev.Expecto.TestSdk" Version="0.14.*" />
7171
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.6.3" />
7272
<PackageReference Include="OptimizedPriorityQueue" Version="5.1.0" />
7373
<PackageReference Include="Deedle" Version="3.0.0" />

0 commit comments

Comments
 (0)