From 934df455f3ecdfd0384e19822e505045f0472d77 Mon Sep 17 00:00:00 2001 From: Gustavo Guerra Date: Tue, 18 Oct 2011 23:01:50 +0100 Subject: [PATCH] added unit tests for mapCols, mapRows, sumColsBy and sumRowsBy --- src/FSharp/Matrix.fs | 2 +- src/FSharpUnitTests/Program.fs | 8 ++++++++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/src/FSharp/Matrix.fs b/src/FSharp/Matrix.fs index 0bb74012..58a6e8a1 100644 --- a/src/FSharp/Matrix.fs +++ b/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 -> 'a) (A: #Matrix) = - 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 -> 'a) (A: #Matrix) = diff --git a/src/FSharpUnitTests/Program.fs b/src/FSharpUnitTests/Program.fs index c6a32fae..c7d5dfcd 100644 --- a/src/FSharpUnitTests/Program.fs +++ b/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"