Skip to content

Commit

Permalink
fix Matrix.sumColumns output type
Browse files Browse the repository at this point in the history
fix Matrix unit tests #150
  • Loading branch information
bvenn committed Aug 7, 2021
1 parent b22df6f commit ba7ac89
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 15 deletions.
20 changes: 10 additions & 10 deletions src/FSharp.Stats/Matrix.fs
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ module Matrix =
o.Inverse

type DataSource =
|Sample
|Population
|Sample
|Population

module Generic =

Expand Down Expand Up @@ -567,15 +567,15 @@ module Matrix =
getDiag a
|> diag

/// Computes the row wise sum of a Matrix
/// Computes the row wise sums of a Matrix
let sumRows (a:matrix) =
a
|> foldByRow (fun acc r -> acc + r ) (Vector.zeroCreate a.NumRows)

/// Computes the Column wise sum of a Matrix
/// Computes the column wise sums of a Matrix
let sumColumns (a:matrix) =
a.Transpose
|> foldByRow (fun acc r -> acc + r ) (Vector.zeroCreate a.NumCols)
a
|> foldByCol (fun acc r -> acc + r ) (RowVector.zero a.NumCols)

/// Computes the row wise mean of a Matrix
let meanRowWise (a:matrix) =
Expand All @@ -587,14 +587,14 @@ module Matrix =
let meanColumnWise (a:matrix) =
a
|> sumColumns
|> Vector.map (fun sum -> sum / (a.NumRows |> float))
|> RowVector.map (fun sum -> sum / (a.NumRows |> float))

///Computes mean in the specified orientation
/// orientation - "RowWise" or "ColWise"
let mean (orientation:Orientation) (a:matrix) =
let meanAsSeq (orientation:Orientation) (a:matrix) =
match orientation with
|RowWise -> meanRowWise a
|ColWise -> meanColumnWise a
| RowWise -> meanRowWise a |> seq
| ColWise -> meanColumnWise a |> seq

/// computes the column specific covariance matrix of a data matrix as described at:
// http://stattrek.com/matrix-algebra/covariance-matrix.aspx
Expand Down
2 changes: 1 addition & 1 deletion src/FSharp.Stats/Vector.fs
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ module Vector =
let ofScalar value = VecDS.scalarVecDS value
///Builds a new vector whose elements are the results of adding the corresponding elements of the two vectors pairwise. The two input vectors must have the same lengths, otherwise ArgumentException is raised.
let add vector1 vector2 = VecDS.addVecDS vector1 vector2
///Builds a new vector whose elements are the results of substracting the corresponding elements of vector1 from vector2. The two input vectors must have the same lengths, otherwise ArgumentException is raised.
///Builds a new vector whose elements are the results of substracting the corresponding elements of vector2 from vector1. The two input vectors must have the same lengths, otherwise ArgumentException is raised.
let sub vector1 vector2 = VecDS.subVecDS vector1 vector2
let mulRVV vector1 vector2 = VecDS.mulRowVecVecDS vector1 vector2
let mulVRV vector1 vector2 = VecDS.mulVecRowVecDS vector1 vector2
Expand Down
8 changes: 4 additions & 4 deletions tests/FSharp.Stats.Tests/Matrix.fs
Original file line number Diff line number Diff line change
Expand Up @@ -258,21 +258,21 @@ let floatImplementationDenseTests =

testCase "ofRows" <| fun () ->
let actual =
testValuesArrRows
testValues2x3
|> Array.map rowvec
|> Vector.Generic.ofSeq
|> Matrix.ofRows

Expect.equal actual testSquareMatrixA "Matrix was not initialized correctly using Matrix.ofRows"
Expect.equal actual test2x3Matrix "Matrix was not initialized correctly using Matrix.ofRows"

testCase "ofCols" <| fun () ->
let actual =
testValuesArrCols
testValues2x3Transposed
|> Array.map vector
|> RowVector.Generic.ofSeq
|> Matrix.ofCols

Expect.equal actual testSquareMatrixA "Matrix was not initialized correctly using Matrix.ofCols"
Expect.equal actual test2x3Matrix "Matrix was not initialized correctly using Matrix.ofCols"

testCase "ofJaggedList" <| fun () ->

Expand Down

0 comments on commit ba7ac89

Please sign in to comment.