fslaborg / FSharp.Stats

statistical testing, linear algebra, machine learning, fitting and signal processing in F#
https://fslab.org/FSharp.Stats/
Other
206 stars 54 forks source link

Matrix.ofCol fails for Seq and Array #150

Closed bvenn closed 2 years ago

bvenn commented 2 years ago

Describe the bug

When transforming jagged sequences to column oriented matrices there are three specialized functions for sequences, lists, and arrays respectively.

Matrix.ofJaggedColList [[1. .. 4.];[2. .. 5.]] generates the correct result:

val it : Matrix<float> = 
    matrix [[1.0; 2.0]
            [2.0; 3.0]
            [3.0; 4.0]
            [4.0; 5.0]]

Matrix.ofJaggedColSeq and Matrix.ofJaggedColArray fail with System.IndexOutOfRangeException: Index was outside the bounds of the array.

To Reproduce

Matrix.ofJaggedColSeq [|[|1. .. 4.|];[|2. .. 5.|]|]
Matrix.ofJaggedColArray [|[|1. .. 4.|];[|2. .. 5.|]|]

Additional context

I'm pretty sure, the error is in the following and I'm going to fix it and provide tests:

https://github.com/fslaborg/FSharp.Stats/blob/6874435eeee928a9cd79c58bf3fd108d5a46db90/src/FSharp.Stats/AlgTypes.fs#L476

bvenn commented 2 years ago

I just saw that there are already tests for these functions, but they are based on square matrices, which unintentionally covers the problem.

https://github.com/fslaborg/FSharp.Stats/blob/6874435eeee928a9cd79c58bf3fd108d5a46db90/tests/FSharp.Stats.Tests/Matrix.fs#L84