From b1aee2aa74cc4a9069a0445ea03cb2a3268e2aec Mon Sep 17 00:00:00 2001 From: bstrausser Date: Sat, 15 Sep 2012 00:31:42 -0400 Subject: [PATCH] Fixes incorrect matrix foldback FoldBack was incorrect because of incorrect indexing. Tests did not detect this because the matrix was square. --- src/FSharp/Matrix.fs | 2 +- src/FSharpUnitTests/Program.fs | 4 ++++ 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/src/FSharp/Matrix.fs b/src/FSharp/Matrix.fs index 0708e769..e25a71e9 100644 --- a/src/FSharp/Matrix.fs +++ b/src/FSharp/Matrix.fs @@ -52,7 +52,7 @@ module Matrix = let m = A.ColumnCount let mutable acc = acc0 for i in n-1 .. -1 .. 0 do - for j in n-1 .. -1 .. 0 do + for j in m-1 .. -1 .. 0 do acc <- f (A.Item(i,j)) acc acc diff --git a/src/FSharpUnitTests/Program.fs b/src/FSharpUnitTests/Program.fs index e15e6049..04d43d03 100644 --- a/src/FSharpUnitTests/Program.fs +++ b/src/FSharpUnitTests/Program.fs @@ -106,6 +106,8 @@ let MatrixTests = /// A small uniform vector. let smallM = new DenseMatrix( Array2D.create 2 2 0.3 ) + let failingFoldBackM = DenseMatrix.init 2 3 (fun i j -> 1.0) + /// A large vector with increasingly large entries let largeM = new DenseMatrix( Array2D.init 100 100 (fun i j -> float i * 100.0 + float j) ) @@ -115,6 +117,8 @@ let MatrixTests = (Matrix.fold (fun a b -> a - b) 0.0 smallM |> should equal -1.2) spec "Matrix.foldBack" (Matrix.foldBack (fun a b -> a - b) 0.0 smallM |> should equal 0.0) + spec "Matrix.foldBackSummation" + (Matrix.foldBack( fun a b -> a + b) 0.0 failingFoldBackM |> should equal 6.0) spec "Matrix.foldi" (Matrix.foldi (fun i j acc x -> acc + x + float (i+j)) 0.0 smallM |> should equal 5.2) spec "Matrix.toArray2"