Browse Source

added unit tests for mapCols, mapRows, sumColsBy and sumRowsBy

pull/36/head
Gustavo Guerra 15 years ago
committed by Christoph Ruegg
parent
commit
934df455f3
  1. 2
      src/FSharp/Matrix.fs
  2. 8
      src/FSharpUnitTests/Program.fs

2
src/FSharp/Matrix.fs

@ -128,7 +128,7 @@ module Matrix =
/// Map every matrix column into an element in a sequence using the given position dependent function.
let inline mapCols (f: int -> Vector<float> -> 'a) (A: #Matrix<float>) =
A.ColumnEnumerator() |> Seq.map (fun (j, row) -> f j row)
A.ColumnEnumerator() |> Seq.map (fun (j, col) -> f j col)
/// Map every matrix row into an element in a sequence using the given position dependent function.
let inline mapRows (f: int -> Vector<float> -> 'a) (A: #Matrix<float>) =

8
src/FSharpUnitTests/Program.fs

@ -125,6 +125,10 @@ let MatrixTests =
(Matrix.map (fun x -> 2.0 * x) smallM |> should equal (2.0 * smallM))
spec "Matrix.mapi"
(Matrix.mapi (fun i j x -> float i * 100.0 + float j + x) largeM |> should equal (2.0 * largeM))
spec "Matrix.mapCols"
(Matrix.mapCols (fun j col -> col.[0] + col.[1]) largeM |> Seq.toArray |> should array_equal [| for j in 0.0 .. 99.0 -> 2.0 * j + 100.0 |])
spec "Matrix.mapRows"
(Matrix.mapRows (fun i row -> row.[0] + row.[1]) largeM |> Seq.toArray |> should array_equal [| for i in 0.0 .. 99.0 -> 2.0 * 100.0 * i + 1.0 |])
spec "Matrix.inplaceAssign"
( let N = smallM.Clone()
Matrix.inplaceAssign (fun i j -> 0.0) N
@ -137,6 +141,10 @@ let MatrixTests =
(Seq.length (Matrix.nonZeroEntries smallM) |> should equal 4)
spec "Matrix.sum"
(Matrix.sum smallM |> should equal 1.2)
spec "Matrix.sumColsBy"
(Matrix.sumColsBy (fun j col -> col.[0] * col.[1]) (matrix [[1.0; 2.0]; [3.0; 4.0]]) |> should equal 11.0)
spec "Matrix.sumRowsBy"
(Matrix.sumRowsBy (fun i row -> row.[0] * row.[1]) (matrix [[1.0; 2.0]; [3.0; 4.0]]) |> should equal 14.0)
spec "Matrix.foldCol"
(Matrix.foldCol (+) 0.0 largeM 0 |> should equal 495000.0)
spec "Matrix.foldRow"

Loading…
Cancel
Save