Skip to content

Commit

Permalink
Merge pull request #316 from LibraChris/setColFix
Browse files Browse the repository at this point in the history
Fix for `FSharp.Stats` Matrix Functionality**
  • Loading branch information
bvenn authored Feb 9, 2024
2 parents 6c97a2b + 42914c3 commit 065fefd
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/FSharp.Stats/AlgTypes.fs
Original file line number Diff line number Diff line change
Expand Up @@ -2099,7 +2099,7 @@ namespace FSharp.Stats
/// </code>
/// </example>
let setColM (a:Matrix<_>) j (v:Vector<_>) =
if a.NumCols = v.Length then
if a.NumRows = v.Length then
let l = v.Length-1
for i = 0 to l do
a.[i,j] <- v.[i]
Expand Down
38 changes: 36 additions & 2 deletions tests/FSharp.Stats.Tests/Matrix.fs
Original file line number Diff line number Diff line change
Expand Up @@ -1133,10 +1133,44 @@ let floatImplementationDenseTests =
Expect.equal actual expected "Matrix.setCol did not return the correct vector"

testCase "Setting column out of col range using Matrix.setCol should fail" <| fun () ->
Expect.throws (fun () -> Matrix.setRow testSquareMatrixA 1337 testVectorA |> ignore) "Setting column out of col range using Matrix.setCol did not fail although it should"
Expect.throws (fun () -> Matrix.setCol testSquareMatrixA 1337 testVectorA |> ignore) "Setting column out of col range using Matrix.setCol did not fail although it should"

testCase "Setting column with vector of wrong length using Matrix.setCol should fail" <| fun () ->
Expect.throws (fun () -> Matrix.setRow testSquareMatrixA 1 testVector1LowerDiag |> ignore) "Setting row with vector of wrong length using Matrix.setRow did not fail although it should"
Expect.throws (fun () -> Matrix.setCol testSquareMatrixA 1 testVector1LowerDiag |> ignore) "Setting row with vector of wrong length using Matrix.setRow did not fail although it should"

testCase "Set Column non square" <| fun () ->

let test2x3MatrixNonSquare : Matrix<float> =
let values =
Array2D.init
2
3
(fun i j ->
0.
)
Matrix.DenseRepr
(DenseMatrix<float>(Some (Instances.FloatNumerics :> INumeric<float>),values))

Matrix.setCol test2x3MatrixNonSquare 0 ([1.;1.]|>Vector.ofList)

let expected =
let rows =
[|
[|1.;0.;0.|]
[|1.;0.;0.|]
|]
let values =
Array2D.init
2
3
(fun i j ->
rows.[i].[j]
)
Matrix.DenseRepr
(DenseMatrix<float>(Some (Instances.FloatNumerics :> INumeric<float>),values))

Expect.equal test2x3MatrixNonSquare expected "Matrix.setCol did not return the correct vector for non square Matrix"


]
testList "getCols" [
Expand Down

0 comments on commit 065fefd

Please sign in to comment.