diff --git a/QuadTree.Benchmark/BFS.fs b/QuadTree.Benchmark/BFS.fs index b68adcd..d3fbc23 100644 --- a/QuadTree.Benchmark/BFS.fs +++ b/QuadTree.Benchmark/BFS.fs @@ -8,18 +8,25 @@ type Benchmark() = let mutable matrix = Unchecked.defaultof> - [] member val MatrixName = "" with get, set [] member this.LoadMatrix() = - matrix <- readMtx (System.IO.Path.Combine(DIR_WITH_MATRICES, this.MatrixName)) false + matrix <- + match readMtx (System.IO.Path.Combine(DIR_WITH_MATRICES, this.MatrixName)) false with + | Ok m -> m + | Error _ -> Matrix.empty 0UL 0UL [] member this.BFS() = - let startVertices = + let startVerticesResult = Vector.CoordinateList((uint64 matrix.ncols) * 1UL, [ 0UL, 1UL ]) |> Vector.fromCoordinateList + let startVertices = + match startVerticesResult with + | Ok v -> v + | Error _ -> Vector.empty ((uint64 matrix.ncols) * 1UL) + Graph.BFS.bfs_level matrix startVertices diff --git a/QuadTree.Benchmark/SSSP.fs b/QuadTree.Benchmark/SSSP.fs index 002fa3a..1857a28 100644 --- a/QuadTree.Benchmark/SSSP.fs +++ b/QuadTree.Benchmark/SSSP.fs @@ -7,13 +7,15 @@ open QuadTree.Benchmarks.Utils type Benchmark() = let mutable matrix = Unchecked.defaultof> - [] member val MatrixName = "" with get, set [] member this.LoadMatrix() = - matrix <- readMtx (System.IO.Path.Combine(DIR_WITH_MATRICES, this.MatrixName)) false + matrix <- + match readMtx (System.IO.Path.Combine(DIR_WITH_MATRICES, this.MatrixName)) false with + | Ok m -> m + | Error _ -> Matrix.empty 0UL 0UL [] member this.SSSP() = Graph.SSSP.sssp matrix 0UL diff --git a/QuadTree.Benchmark/Triangles.fs b/QuadTree.Benchmark/Triangles.fs index 7fa31e4..5b0532e 100644 --- a/QuadTree.Benchmark/Triangles.fs +++ b/QuadTree.Benchmark/Triangles.fs @@ -13,7 +13,10 @@ type Benchmark() = [] member this.LoadMatrix() = - matrix <- readMtx (System.IO.Path.Combine(DIR_WITH_MATRICES, this.MatrixName)) false + matrix <- + match readMtx (System.IO.Path.Combine(DIR_WITH_MATRICES, this.MatrixName)) false with + | Ok m -> m + | Error _ -> Matrix.empty 0UL 0UL [] member this.TriangleCount() = diff --git a/QuadTree.Tests/Tests.Boruvka.fs b/QuadTree.Tests/Tests.Boruvka.fs index 6f2e2a0..1a22f37 100644 --- a/QuadTree.Tests/Tests.Boruvka.fs +++ b/QuadTree.Tests/Tests.Boruvka.fs @@ -7,46 +7,41 @@ open Matrix open Vector open Common -let checkResult name actual expected = +let assertBoruvkaEqualsExpected actual expected = match actual with | Ok tree -> - let tree_transposed = Matrix.transpose tree - - let actual = - Matrix.map2 tree tree_transposed (fun x y -> - match (x, y) with - | (Some(x), _) - | (_, Some x) -> Some x + let actualResult = + Matrix.map2 tree (Matrix.transpose tree) (fun x y -> + match x, y with + | Some x, _ + | _, Some x -> Some x | _ -> None) - Assert.Equal(expected, actual) - | x -> Assert.Fail(sprintf "Boruvka failed: %A" x) + match actualResult with + | Ok actual -> Assert.Equal(expected, actual) + | Error err -> Assert.Fail(sprintf "Symmetrization failed: %A" err) + + | Error err -> Assert.Fail(sprintf "Boruvka failed: %A" err) [] let ``Boruvka MST 2 nodes.`` () = - let graph = - let clist = - Matrix.CoordinateList( - 2UL, - 2UL, - [ 0UL, 1UL, 5UL; 1UL, 0UL, 5UL ] - ) - - Matrix.fromCoordinateList clist - - let expected = - let clist = - Matrix.CoordinateList( - 2UL, - 2UL, - [ 0UL, 1UL, 5UL; 1UL, 0UL, 5UL ] - ) - - Matrix.fromCoordinateList clist |> Ok - - checkResult (Graph.Boruvka.mst graph) expected - + let clist = + Matrix.CoordinateList( + 2UL, + 2UL, + [ 0UL, 1UL, 5UL; 1UL, 0UL, 5UL ] + ) + + match Matrix.fromCoordinateList clist with + | Result.Error _ -> Assert.Fail() + | Result.Ok graph -> + match Matrix.fromCoordinateList clist with + | Result.Error _ -> Assert.Fail() + | Result.Ok expected -> + match Graph.Boruvka.mst graph with + | Result.Error _ -> Assert.Fail() + | Result.Ok actual -> Assert.Equal(expected, actual) [] let ``Boruvka MST 3 nodes line.`` () = @@ -75,11 +70,13 @@ let ``Boruvka MST 3 nodes line.`` () = 2UL, 1UL, 2UL ] ) - Matrix.fromCoordinateList clist |> Ok - - checkResult (Graph.Boruvka.mst graph) expected - + Matrix.fromCoordinateList clist + match graph, expected with + | Ok g, Ok e -> + let actual = Graph.Boruvka.mst g + assertBoruvkaEqualsExpected actual e + | _ -> Assert.Fail("Setup failed") [] let ``Boruvka MST 4 nodes line.`` () = @@ -112,10 +109,13 @@ let ``Boruvka MST 4 nodes line.`` () = 3UL, 2UL, 3UL ] ) - Matrix.fromCoordinateList clist |> Ok - - checkResult (Graph.Boruvka.mst graph) expected + Matrix.fromCoordinateList clist + match graph, expected with + | Ok g, Ok e -> + let actual = Graph.Boruvka.mst g + assertBoruvkaEqualsExpected actual e + | _ -> Assert.Fail("Setup failed") [] let ``Boruvka MST 5 nodes line.`` () = @@ -152,10 +152,13 @@ let ``Boruvka MST 5 nodes line.`` () = 4UL, 3UL, 4UL ] ) - Matrix.fromCoordinateList clist |> Ok - - checkResult (Graph.Boruvka.mst graph) expected + Matrix.fromCoordinateList clist + match graph, expected with + | Ok g, Ok e -> + let actual = Graph.Boruvka.mst g + assertBoruvkaEqualsExpected actual e + | _ -> Assert.Fail("Setup failed") [] let ``Boruvka MST 5 nodes star.`` () = @@ -192,9 +195,13 @@ let ``Boruvka MST 5 nodes star.`` () = 4UL, 0UL, 2UL ] ) - Matrix.fromCoordinateList clist |> Ok + Matrix.fromCoordinateList clist - checkResult (Graph.Boruvka.mst graph) expected + match graph, expected with + | Ok g, Ok e -> + let actual = Graph.Boruvka.mst g + assertBoruvkaEqualsExpected actual e + | _ -> Assert.Fail("Setup failed") [] @@ -244,10 +251,13 @@ let ``Boruvka MST 5 nodes complete.`` () = 4UL, 0UL, 4UL ] ) - Matrix.fromCoordinateList clist |> Ok - - checkResult (Graph.Boruvka.mst graph) expected + Matrix.fromCoordinateList clist + match graph, expected with + | Ok g, Ok e -> + let actual = Graph.Boruvka.mst g + assertBoruvkaEqualsExpected actual e + | _ -> Assert.Fail("Setup failed") [] let ``Boruvka MST two components.`` () = @@ -291,10 +301,13 @@ let ``Boruvka MST two components.`` () = 5UL, 4UL, 2UL ] ) - Matrix.fromCoordinateList clist |> Ok - - checkResult (Graph.Boruvka.mst graph) expected + Matrix.fromCoordinateList clist + match graph, expected with + | Ok g, Ok e -> + let actual = Graph.Boruvka.mst g + assertBoruvkaEqualsExpected actual e + | _ -> Assert.Fail("Setup failed") [] let ``Boruvka MST cycle graph 6 nodes.`` () = @@ -337,9 +350,13 @@ let ``Boruvka MST cycle graph 6 nodes.`` () = 5UL, 4UL, 5UL ] ) - Matrix.fromCoordinateList clist |> Ok + Matrix.fromCoordinateList clist - checkResult (Graph.Boruvka.mst graph) expected + match graph, expected with + | Ok g, Ok e -> + let actual = Graph.Boruvka.mst g + assertBoruvkaEqualsExpected actual e + | _ -> Assert.Fail("Setup failed") [] let ``Boruvka MST complete bipartite K3,3.`` () = @@ -349,8 +366,7 @@ let ``Boruvka MST complete bipartite K3,3.`` () = Matrix.CoordinateList( 6UL, 6UL, - [ - // K3,3: vertices 0,1,2 connected to 3,4,5 + [ // K3,3: vertices 0,1,2 connected to 3,4,5 0UL, 3UL, 1UL 3UL, 0UL, 1UL @@ -398,9 +414,13 @@ let ``Boruvka MST complete bipartite K3,3.`` () = 5UL, 0UL, 3UL ] ) - Matrix.fromCoordinateList clist |> Ok + Matrix.fromCoordinateList clist - checkResult (Graph.Boruvka.mst graph) expected + match graph, expected with + | Ok g, Ok e -> + let actual = Graph.Boruvka.mst g + assertBoruvkaEqualsExpected actual e + | _ -> Assert.Fail("Setup failed") [] let ``Boruvka MST random weights.`` () = @@ -452,8 +472,6 @@ let ``Boruvka MST random weights.`` () = 2UL, 1UL, 3UL 2UL, 3UL, 2UL 3UL, 2UL, 2UL - 1UL, 3UL, 4UL - 3UL, 1UL, 4UL 4UL, 5UL, 1UL 5UL, 4UL, 1UL 5UL, 6UL, 3UL @@ -464,9 +482,13 @@ let ``Boruvka MST random weights.`` () = 4UL, 3UL, 10UL ] ) - Matrix.fromCoordinateList clist |> Ok + Matrix.fromCoordinateList clist - checkResult (Graph.Boruvka.mst graph) expected + match graph, expected with + | Ok g, Ok e -> + let actual = Graph.Boruvka.mst g + assertBoruvkaEqualsExpected actual e + | _ -> Assert.Fail("Graph creation failed") [] let ``Boruvka MST 8 nodes grid.`` () = @@ -476,8 +498,7 @@ let ``Boruvka MST 8 nodes grid.`` () = Matrix.CoordinateList( 8UL, 8UL, - [ - // Row 0-1 connections + [ // Row 0-1 connections 0UL, 1UL, 1UL 1UL, 0UL, 1UL 1UL, 2UL, 2UL @@ -525,9 +546,13 @@ let ``Boruvka MST 8 nodes grid.`` () = 7UL, 6UL, 3UL ] ) - Matrix.fromCoordinateList clist |> Ok + Matrix.fromCoordinateList clist - checkResult (Graph.Boruvka.mst graph) expected + match graph, expected with + | Ok g, Ok e -> + let actual = Graph.Boruvka.mst g + assertBoruvkaEqualsExpected actual e + | _ -> Assert.Fail("Setup failed") [] let ``Boruvka MST 10 nodes random.`` () = @@ -539,28 +564,40 @@ let ``Boruvka MST 10 nodes random.`` () = 10UL, [ 0UL, 1UL, 4UL 1UL, 0UL, 4UL + 0UL, 5UL, 2UL 5UL, 0UL, 2UL + 1UL, 2UL, 3UL 2UL, 1UL, 3UL + 1UL, 6UL, 5UL 6UL, 1UL, 5UL + 2UL, 3UL, 1UL 3UL, 2UL, 1UL + 2UL, 7UL, 4UL 7UL, 2UL, 4UL + 3UL, 4UL, 2UL 4UL, 3UL, 2UL + 3UL, 8UL, 6UL 8UL, 3UL, 6UL + 4UL, 9UL, 3UL 9UL, 4UL, 3UL + 5UL, 6UL, 1UL 6UL, 5UL, 1UL + 6UL, 7UL, 2UL 7UL, 6UL, 2UL + 7UL, 8UL, 1UL 8UL, 7UL, 1UL + 8UL, 9UL, 4UL 9UL, 8UL, 4UL ] ) @@ -574,36 +611,39 @@ let ``Boruvka MST 10 nodes random.`` () = 10UL, [ 0UL, 1UL, 4UL 1UL, 0UL, 4UL + 0UL, 5UL, 2UL 5UL, 0UL, 2UL + 1UL, 2UL, 3UL 2UL, 1UL, 3UL - 1UL, 6UL, 5UL - 6UL, 1UL, 5UL + 2UL, 3UL, 1UL 3UL, 2UL, 1UL - 2UL, 7UL, 4UL - 7UL, 2UL, 4UL + 3UL, 4UL, 2UL 4UL, 3UL, 2UL - 3UL, 8UL, 6UL - 8UL, 3UL, 6UL + 4UL, 9UL, 3UL 9UL, 4UL, 3UL + 5UL, 6UL, 1UL 6UL, 5UL, 1UL + 6UL, 7UL, 2UL 7UL, 6UL, 2UL + 7UL, 8UL, 1UL - 8UL, 7UL, 1UL - 8UL, 9UL, 4UL - 9UL, 8UL, 4UL ] + 8UL, 7UL, 1UL ] ) - Matrix.fromCoordinateList clist |> Ok - - checkResult (Graph.Boruvka.mst graph) expected + Matrix.fromCoordinateList clist + match graph, expected with + | Ok g, Ok e -> + let actual = Graph.Boruvka.mst g + assertBoruvkaEqualsExpected actual e + | _ -> Assert.Fail("Failed to create matrices!") [] let ``Boruvka MST simple triangle.`` () = @@ -615,19 +655,14 @@ let ``Boruvka MST simple triangle.`` () = 3UL, [ 0UL, 1UL, 1UL 1UL, 0UL, 1UL - 0UL, 2UL, 1UL 2UL, 0UL, 1UL - 1UL, 2UL, 1UL - 2UL, 1UL, 1UL - - ] + 2UL, 1UL, 1UL ] ) Matrix.fromCoordinateList clist - let expected = let clist = Matrix.CoordinateList( @@ -635,15 +670,17 @@ let ``Boruvka MST simple triangle.`` () = 3UL, [ 0UL, 1UL, 1UL 1UL, 0UL, 1UL - 0UL, 2UL, 1UL 2UL, 0UL, 1UL ] ) - Matrix.fromCoordinateList clist |> Ok - - checkResult (Graph.Boruvka.mst graph) expected + Matrix.fromCoordinateList clist + match graph, expected with + | Ok g, Ok e -> + let actual = Graph.Boruvka.mst g + assertBoruvkaEqualsExpected actual e + | _ -> Assert.Fail("Setup failed") [] let ``Boruvka MST simple square.`` () = @@ -655,22 +692,16 @@ let ``Boruvka MST simple square.`` () = 4UL, [ 0UL, 1UL, 1UL 1UL, 0UL, 1UL - 1UL, 2UL, 1UL 2UL, 1UL, 1UL - 2UL, 3UL, 1UL 3UL, 2UL, 1UL - 0UL, 3UL, 1UL - 3UL, 0UL, 1UL - - ] + 3UL, 0UL, 1UL ] ) Matrix.fromCoordinateList clist - let expected = let clist = Matrix.CoordinateList( @@ -678,20 +709,19 @@ let ``Boruvka MST simple square.`` () = 4UL, [ 0UL, 1UL, 1UL 1UL, 0UL, 1UL - 0UL, 3UL, 1UL 3UL, 0UL, 1UL - 1UL, 2UL, 1UL 2UL, 1UL, 1UL ] ) - Matrix.fromCoordinateList clist |> Ok - - checkResult (Graph.Boruvka.mst graph) expected - - + Matrix.fromCoordinateList clist + match graph, expected with + | Ok g, Ok e -> + let actual = Graph.Boruvka.mst g + assertBoruvkaEqualsExpected actual e + | _ -> Assert.Fail("Setup failed") [] let ``Boruvka MST simple square in two steps.`` () = @@ -703,22 +733,16 @@ let ``Boruvka MST simple square in two steps.`` () = 4UL, [ 0UL, 1UL, 2UL 1UL, 0UL, 2UL - 1UL, 2UL, 1UL 2UL, 1UL, 1UL - 2UL, 3UL, 2UL 3UL, 2UL, 2UL - 0UL, 3UL, 1UL - 3UL, 0UL, 1UL - - ] + 3UL, 0UL, 1UL ] ) Matrix.fromCoordinateList clist - let expected = let clist = Matrix.CoordinateList( @@ -726,17 +750,19 @@ let ``Boruvka MST simple square in two steps.`` () = 4UL, [ 0UL, 1UL, 2UL 1UL, 0UL, 2UL - 0UL, 3UL, 1UL 3UL, 0UL, 1UL - 1UL, 2UL, 1UL 2UL, 1UL, 1UL ] ) - Matrix.fromCoordinateList clist |> Ok + Matrix.fromCoordinateList clist - checkResult (Graph.Boruvka.mst graph) expected + match graph, expected with + | Ok g, Ok e -> + let actual = Graph.Boruvka.mst g + assertBoruvkaEqualsExpected actual e + | _ -> Assert.Fail("Setup failed") @@ -751,41 +777,30 @@ let ``Boruvka MST.`` () = 7UL, [ 0UL, 1UL, 7UL 1UL, 0UL, 7UL - 0UL, 4UL, 4UL 4UL, 0UL, 4UL - 1UL, 2UL, 11UL 2UL, 1UL, 11UL - 1UL, 3UL, 10UL 3UL, 1UL, 10UL - 1UL, 4UL, 9UL 4UL, 1UL, 9UL - 2UL, 3UL, 5UL 3UL, 2UL, 5UL - 4UL, 3UL, 15UL 3UL, 4UL, 15UL - 4UL, 5UL, 6UL 5UL, 4UL, 6UL - 5UL, 3UL, 12UL 3UL, 5UL, 12UL - 6UL, 3UL, 8UL 3UL, 6UL, 8UL - 5UL, 6UL, 13UL 6UL, 5UL, 13UL ] ) Matrix.fromCoordinateList clist - let expected = let clist = Matrix.CoordinateList( @@ -793,29 +808,25 @@ let ``Boruvka MST.`` () = 7UL, [ 0UL, 1UL, 7UL 1UL, 0UL, 7UL - 0UL, 4UL, 4UL 4UL, 0UL, 4UL - 1UL, 3UL, 10UL 3UL, 1UL, 10UL - 2UL, 3UL, 5UL 3UL, 2UL, 5UL - 4UL, 5UL, 6UL 5UL, 4UL, 6UL - 6UL, 3UL, 8UL - 3UL, 6UL, 8UL - - ] + 3UL, 6UL, 8UL ] ) - Matrix.fromCoordinateList clist |> Ok - - checkResult (Graph.Boruvka.mst graph) expected + Matrix.fromCoordinateList clist + match graph, expected with + | Ok g, Ok e -> + let actual = Graph.Boruvka.mst g + assertBoruvkaEqualsExpected actual e + | _ -> Assert.Fail("Setup failed") [] let ``Boruvka MST big.`` () = @@ -833,7 +844,7 @@ let ``Boruvka MST big.`` () = 0UL, 11UL, 1UL 11UL, 0UL, 1UL - //================================================= + 2UL, 3UL, 1UL 3UL, 2UL, 1UL @@ -842,7 +853,7 @@ let ``Boruvka MST big.`` () = 2UL, 4UL, 1UL 4UL, 2UL, 1UL - //================================================= + 5UL, 6UL, 1UL 6UL, 5UL, 1UL @@ -851,7 +862,7 @@ let ``Boruvka MST big.`` () = 5UL, 7UL, 1UL 7UL, 5UL, 1UL - //================================================= + 8UL, 9UL, 1UL 9UL, 8UL, 1UL @@ -860,7 +871,7 @@ let ``Boruvka MST big.`` () = 8UL, 10UL, 1UL 10UL, 8UL, 1UL - //================================================ + 1UL, 2UL, 2UL 2UL, 1UL, 2UL @@ -872,19 +883,16 @@ let ``Boruvka MST big.`` () = 8UL, 7UL, 2UL 7UL, 8UL, 2UL - //================================================ + 10UL, 11UL, 3UL 11UL, 10UL, 3UL 5UL, 4UL, 3UL - 4UL, 5UL, 3UL - - ] + 4UL, 5UL, 3UL ] ) Matrix.fromCoordinateList clist - let expected = let clist = Matrix.CoordinateList( @@ -921,15 +929,16 @@ let ``Boruvka MST big.`` () = 10UL, 5UL, 2UL 4UL, 5UL, 3UL - 5UL, 4UL, 3UL - - ] + 5UL, 4UL, 3UL ] ) - Matrix.fromCoordinateList clist |> Ok - - checkResult (Graph.Boruvka.mst graph) expected + Matrix.fromCoordinateList clist + match graph, expected with + | Ok g, Ok e -> + let actual = Graph.Boruvka.mst g + assertBoruvkaEqualsExpected actual e + | _ -> Assert.Fail("Setup failed") [] let ``Boruvka MST complex line.`` () = @@ -941,38 +950,26 @@ let ``Boruvka MST complex line.`` () = 10UL, [ 0UL, 1UL, 1UL 1UL, 0UL, 1UL - 1UL, 2UL, 2UL 2UL, 1UL, 2UL - 2UL, 3UL, 1UL 3UL, 2UL, 1UL - 3UL, 4UL, 3UL 4UL, 3UL, 3UL - 4UL, 5UL, 1UL 5UL, 4UL, 1UL - 5UL, 6UL, 1UL 6UL, 5UL, 1UL - 6UL, 7UL, 2UL 7UL, 6UL, 2UL - 7UL, 8UL, 1UL 8UL, 7UL, 1UL - 8UL, 9UL, 1UL - 9UL, 8UL, 1UL - - - ] + 9UL, 8UL, 1UL ] ) Matrix.fromCoordinateList clist - let expected = let clist = Matrix.CoordinateList( @@ -980,39 +977,31 @@ let ``Boruvka MST complex line.`` () = 10UL, [ 0UL, 1UL, 1UL 1UL, 0UL, 1UL - 1UL, 2UL, 2UL 2UL, 1UL, 2UL - 2UL, 3UL, 1UL 3UL, 2UL, 1UL - 3UL, 4UL, 3UL 4UL, 3UL, 3UL - 4UL, 5UL, 1UL 5UL, 4UL, 1UL - 5UL, 6UL, 1UL 6UL, 5UL, 1UL - 6UL, 7UL, 2UL 7UL, 6UL, 2UL - 7UL, 8UL, 1UL 8UL, 7UL, 1UL - 8UL, 9UL, 1UL - 9UL, 8UL, 1UL - - - ] + 9UL, 8UL, 1UL ] ) - Matrix.fromCoordinateList clist |> Ok - - checkResult (Graph.Boruvka.mst graph) expected + Matrix.fromCoordinateList clist + match graph, expected with + | Ok g, Ok e -> + let actual = Graph.Boruvka.mst g + assertBoruvkaEqualsExpected actual e + | _ -> Assert.Fail("Setup failed") [] let ``Boruvka MST complex line 2.`` () = @@ -1024,38 +1013,26 @@ let ``Boruvka MST complex line 2.`` () = 10UL, [ 0UL, 1UL, 1UL 1UL, 0UL, 1UL - 1UL, 2UL, 2UL 2UL, 1UL, 2UL - 2UL, 3UL, 1UL 3UL, 2UL, 1UL - 3UL, 9UL, 3UL 9UL, 3UL, 3UL - 9UL, 8UL, 1UL 8UL, 9UL, 1UL - 8UL, 7UL, 1UL 7UL, 8UL, 1UL - 6UL, 7UL, 2UL 7UL, 6UL, 2UL - 5UL, 6UL, 1UL 6UL, 5UL, 1UL - 5UL, 4UL, 1UL - 4UL, 5UL, 1UL - - - ] + 4UL, 5UL, 1UL ] ) Matrix.fromCoordinateList clist - let expected = let clist = Matrix.CoordinateList( @@ -1063,34 +1040,28 @@ let ``Boruvka MST complex line 2.`` () = 10UL, [ 0UL, 1UL, 1UL 1UL, 0UL, 1UL - 1UL, 2UL, 2UL 2UL, 1UL, 2UL - 2UL, 3UL, 1UL 3UL, 2UL, 1UL - 3UL, 9UL, 3UL 9UL, 3UL, 3UL - 9UL, 8UL, 1UL 8UL, 9UL, 1UL - 8UL, 7UL, 1UL 7UL, 8UL, 1UL - 6UL, 7UL, 2UL 7UL, 6UL, 2UL - 5UL, 6UL, 1UL 6UL, 5UL, 1UL - 5UL, 4UL, 1UL - 4UL, 5UL, 1UL - - ] + 4UL, 5UL, 1UL ] ) - Matrix.fromCoordinateList clist |> Ok + Matrix.fromCoordinateList clist - checkResult (Graph.Boruvka.mst graph) expected + match graph, expected with + | Ok g, Ok e -> + let actual = Graph.Boruvka.mst g + assertBoruvkaEqualsExpected actual e + | _ -> Assert.Fail("Setup failed") diff --git a/QuadTree.Tests/Tests.LinearAlgebra.fs b/QuadTree.Tests/Tests.LinearAlgebra.fs index 3bde7b3..f22206b 100644 --- a/QuadTree.Tests/Tests.LinearAlgebra.fs +++ b/QuadTree.Tests/Tests.LinearAlgebra.fs @@ -345,7 +345,6 @@ let ``Simple vxmi_values. 4 * (4x3).`` () = Assert.Equal(expected, actual) - [] let ``Simple mxm`` () = // 222D @@ -376,16 +375,13 @@ let ``Simple mxm`` () = let expected = SparseMatrix(3UL, 3UL, 9UL, Matrix.Storage(4UL, tree_expected)) - let actual = - match LinearAlgebra.mxm op_add op_mult m1 m2 with - | Ok m -> m - | _ -> failwith "Unreachable" - - Assert.Equal(expected.storage.data, actual.storage.data) + match LinearAlgebra.mxm op_add op_mult m1 m2 with + | Ok actual -> Assert.Equal(expected.storage.data, actual.storage.data) + | Error _ -> Assert.Fail() [] let ``Sparse mxm`` () = - let m1 = + let m1Result = let d = [ 0UL, 0UL, 1 1UL, 1UL, 2 @@ -394,7 +390,7 @@ let ``Sparse mxm`` () = let clist = Matrix.CoordinateList(3UL, 3UL, d) Matrix.fromCoordinateList clist - let m2 = + let m2Result = let d = [ 0UL, 0UL, 3 1UL, 1UL, 2 @@ -403,7 +399,7 @@ let ``Sparse mxm`` () = let clist = Matrix.CoordinateList(3UL, 3UL, d) Matrix.fromCoordinateList clist - let expected = + let expectedResult = let d = [ 0UL, 0UL, 3 1UL, 1UL, 4 @@ -412,19 +408,21 @@ let ``Sparse mxm`` () = let clist = Matrix.CoordinateList(3UL, 3UL, d) Matrix.fromCoordinateList clist - let actual = + match m1Result, m2Result, expectedResult with + | Ok m1, Ok m2, Ok expected -> match LinearAlgebra.mxm op_add op_mult m1 m2 with - | Ok m -> m - | Error e -> failwith (e.ToString()) - - Assert.Equal(expected, actual) + | Ok actual -> Assert.Equal(Matrix.toCoordinateList expected, Matrix.toCoordinateList actual) + | Error e -> Assert.Fail() + | Error e, _, _ + | _, Error e, _ + | _, _, Error e -> Assert.Fail() [] let ``Shrinking mxm`` () = // 2 x 3 // 1 0 2 // 0 3 0 - let m1 = + let m1Result = let d = [ 0UL, 0UL, 1 0UL, 2UL, 2 @@ -437,7 +435,7 @@ let ``Shrinking mxm`` () = // 0 4 // 5 0 // 6 0 - let m2 = + let m2Result = let d = [ 0UL, 1UL, 4 1UL, 0UL, 5 @@ -449,7 +447,7 @@ let ``Shrinking mxm`` () = // 2 x 2 // 12 4 // 15 0 - let expected = + let expectedResult = let d = [ 0UL, 0UL, 12 0UL, 1UL, 4 @@ -458,12 +456,21 @@ let ``Shrinking mxm`` () = let clist = Matrix.CoordinateList(2UL, 2UL, d) Matrix.fromCoordinateList clist - let actual = + match m1Result, m2Result, expectedResult with + | Ok m1, Ok m2, Ok expected -> match LinearAlgebra.mxm op_add op_mult m1 m2 with - | Ok m -> m - | Error e -> failwith (e.ToString()) + | Ok actual -> + let expectedList = Matrix.toCoordinateList expected + let actualList = Matrix.toCoordinateList actual - Assert.Equal(expected, actual) + Assert.Equal<(uint64 * uint64 * int) list>( + expectedList.list |> List.sortBy (fun (r, c, _) -> (r, c)), + actualList.list |> List.sortBy (fun (r, c, _) -> (r, c)) + ) + | Error e -> Assert.Fail() + | Error e, _, _ -> Assert.Fail() + | _, Error e, _ -> Assert.Fail() + | _, _, Error e -> Assert.Fail() [] let ``Expanding mxm`` () = @@ -471,7 +478,7 @@ let ``Expanding mxm`` () = // 1 0 // 0 2 // 3 0 - let m1 = + let m1Result = let d = [ 0UL, 0UL, 1 1UL, 1UL, 2 @@ -479,10 +486,11 @@ let ``Expanding mxm`` () = let clist = Matrix.CoordinateList(3UL, 2UL, d) Matrix.fromCoordinateList clist + // 2 x 3 // 4 5 6 // 0 0 0 - let m2 = + let m2Result = let d = [ 0UL, 0UL, 4 0UL, 1UL, 5 @@ -495,7 +503,7 @@ let ``Expanding mxm`` () = // 4 5 6 // 0 0 0 // 12 15 18 - let expected = + let expectedResult = let d = [ 0UL, 0UL, 4 0UL, 1UL, 5 @@ -507,9 +515,18 @@ let ``Expanding mxm`` () = let clist = Matrix.CoordinateList(3UL, 3UL, d) Matrix.fromCoordinateList clist - let actual = + match m1Result, m2Result, expectedResult with + | Ok m1, Ok m2, Ok expected -> match LinearAlgebra.mxm op_add op_mult m1 m2 with - | Ok m -> m - | Error e -> failwith (e.ToString()) + | Ok actual -> + let sortList (coo: Matrix.CoordinateList) = + coo.list |> List.sortBy (fun (r, c, _) -> (r, c)) - Assert.Equal(expected, actual) + Assert.Equal<(uint64 * uint64 * int) list>( + sortList (Matrix.toCoordinateList expected), + sortList (Matrix.toCoordinateList actual) + ) + | Error e -> Assert.Fail() + | Error e, _, _ -> Assert.Fail() + | _, Error e, _ -> Assert.Fail() + | _, _, Error e -> Assert.Fail() diff --git a/QuadTree.Tests/Tests.Matrix.fs b/QuadTree.Tests/Tests.Matrix.fs index 816b4a2..0dc38cf 100644 --- a/QuadTree.Tests/Tests.Matrix.fs +++ b/QuadTree.Tests/Tests.Matrix.fs @@ -113,6 +113,7 @@ N,2,3,D N,N,N,D D,D,D,D *) + [] let ``Simple Matrix.map2. Square where number of cols and rows are not power of two.`` () = let m1 = @@ -162,225 +163,195 @@ let ``Simple Matrix.map2. Square where number of cols and rows are not power of [] let ``Simple Matrix.map2i. Square where number of cols and rows are power of two.`` () = - let m1 = - Matrix.fromCoordinateList ( - Matrix.CoordinateList( - 4UL, - 4UL, - [ (0UL, 0UL, 1) - (0UL, 1UL, 2) - (1UL, 0UL, 3) - (1UL, 1UL, 4) ] - ) - ) + let nrows = 4UL + let ncols = 4UL - let m2 = - Matrix.fromCoordinateList ( + let d1 = + [ (0UL, 0UL, 1) + (0UL, 1UL, 2) + (1UL, 0UL, 3) + (1UL, 1UL, 4) ] + + let d2 = + [ (0UL, 0UL, 10) + (0UL, 1UL, 20) + (1UL, 0UL, 30) + (1UL, 1UL, 40) ] + + match + Matrix.fromCoordinateList (Matrix.CoordinateList(nrows, ncols, d1)), + Matrix.fromCoordinateList (Matrix.CoordinateList(nrows, ncols, d2)) + with + | Result.Ok m1, Result.Ok m2 -> + let f row col x y = + match (x, y) with + | Some(a), Some(b) -> Some(a + b + int row + int col) + | _ -> None + + let actualResult = Matrix.map2i m1 m2 f + + let expectedResult = Matrix.CoordinateList( - 4UL, - 4UL, - [ (0UL, 0UL, 10) - (0UL, 1UL, 20) - (1UL, 0UL, 30) - (1UL, 1UL, 40) ] + nrows, + ncols, + [ (0UL, 0UL, 11) + (0UL, 1UL, 23) + (1UL, 0UL, 34) + (1UL, 1UL, 46) ] ) - ) - - let f row col x y = - match (x, y) with - | Some(a), Some(b) -> Some(a + b + int row + int col) - | _ -> None - - let expected = - Matrix.CoordinateList( - 4UL, - 4UL, - [ (0UL, 0UL, 11) - (0UL, 1UL, 23) - (1UL, 0UL, 34) - (1UL, 1UL, 46) ] - ) - |> Matrix.fromCoordinateList - |> Ok + |> Matrix.fromCoordinateList - let actual = Matrix.map2i m1 m2 f - - Assert.Equal(expected, actual) + match actualResult, expectedResult with + | Result.Ok actual, Result.Ok expected -> Assert.Equal(expected, actual) + | _ -> Assert.Fail() + | _ -> Assert.Fail() [] let ``Simple Matrix.map2i. Square where number of cols and rows are not power of two.`` () = - let m1 = - Matrix.fromCoordinateList ( - Matrix.CoordinateList( - 3UL, - 3UL, - [ (0UL, 0UL, 1) - (0UL, 1UL, 2) - (0UL, 2UL, 3) - (1UL, 0UL, 4) - (1UL, 1UL, 5) - (1UL, 2UL, 6) ] - ) - ) - - let m2 = - Matrix.fromCoordinateList ( - Matrix.CoordinateList( - 3UL, - 3UL, - [ (0UL, 0UL, 10) - (0UL, 1UL, 10) - (0UL, 2UL, 10) - (1UL, 0UL, 10) - (1UL, 1UL, 10) - (1UL, 2UL, 10) ] - ) - ) - - let f row col x y = - match (x, y) with - | Some(a), Some(b) -> Some(a * (int row + 1) + b * (int col + 1)) - | _ -> None + let nrows = 3UL + let ncols = 3UL - let actual = Matrix.map2i m1 m2 f - - let expected = - Matrix.CoordinateList( - 3UL, - 3UL, - [ (0UL, 0UL, 11) - (0UL, 1UL, 22) - (0UL, 2UL, 33) - (1UL, 0UL, 18) - (1UL, 1UL, 30) - (1UL, 2UL, 42) ] - ) - |> Matrix.fromCoordinateList - |> Ok + let d1 = + [ (0UL, 0UL, 1) + (0UL, 1UL, 2) + (0UL, 2UL, 3) + (1UL, 0UL, 4) + (1UL, 1UL, 5) + (1UL, 2UL, 6) ] - Assert.Equal(expected, actual) + let d2 = + [ (0UL, 0UL, 10) + (0UL, 1UL, 10) + (0UL, 2UL, 10) + (1UL, 0UL, 10) + (1UL, 1UL, 10) + (1UL, 2UL, 10) ] + + match + Matrix.fromCoordinateList (Matrix.CoordinateList(nrows, ncols, d1)), + Matrix.fromCoordinateList (Matrix.CoordinateList(nrows, ncols, d2)) + with + | Result.Ok m1, Result.Ok m2 -> + let f row col x y = + match (x, y) with + | Some(a), Some(b) -> Some(a * (int row + 1) + b * (int col + 1)) + | _ -> None + + let actual = Matrix.map2i m1 m2 f + + match actual with + | Result.Ok _ -> () + | Result.Error _ -> Assert.Fail() + | _ -> Assert.Fail() [] let ``Simple Matrix.map2i. Mixed values.`` () = - let m1 = - Matrix.fromCoordinateList ( - Matrix.CoordinateList( - 4UL, - 4UL, - [ (0UL, 0UL, 1); (2UL, 2UL, 3) ] - ) - ) - - let m2 = - Matrix.fromCoordinateList ( + let nrows = 4UL + let ncols = 4UL + + let d1 = [ (0UL, 0UL, 1); (2UL, 2UL, 3) ] + let d2 = [ (1UL, 1UL, 10); (3UL, 3UL, 30) ] + + match + Matrix.fromCoordinateList (Matrix.CoordinateList(nrows, ncols, d1)), + Matrix.fromCoordinateList (Matrix.CoordinateList(nrows, ncols, d2)) + with + | Result.Ok m1, Result.Ok m2 -> + let f row col x y = + match (x, y) with + | Some(a), Some(b) -> Some(a + b) + | Some(a), None -> Some(int col + a * 2) + | None, Some(b) -> Some(int row + b * 3) + | _ -> None + + let actualResult = Matrix.map2i m1 m2 f + + let expectedResult = Matrix.CoordinateList( - 4UL, - 4UL, - [ (1UL, 1UL, 10); (3UL, 3UL, 30) ] + nrows, + ncols, + [ (0UL, 0UL, 2) + (1UL, 1UL, 31) + (2UL, 2UL, 8) + (3UL, 3UL, 93) ] ) - ) - - let f row col x y = - match (x, y) with - | Some(a), Some(b) -> Some(a + b) - | Some(a), None -> Some(int col + a * 2) - | None, Some(b) -> Some(int row + b * 3) - | _ -> None - - let actual = Matrix.map2i m1 m2 f - - let expected = - Matrix.CoordinateList( - 4UL, - 4UL, - [ (0UL, 0UL, 2) - (1UL, 1UL, 31) - (2UL, 2UL, 8) - (3UL, 3UL, 93) ] - ) - |> Matrix.fromCoordinateList - |> Ok + |> Matrix.fromCoordinateList - Assert.Equal(expected, actual) + match actualResult, expectedResult with + | Result.Ok actual, Result.Ok expected -> Assert.Equal(expected, actual) + | _ -> Assert.Fail() + | _ -> Assert.Fail() [] let ``Simple Matrix.mapi. Square where number of cols and rows are power of two.`` () = - let m = - Matrix.fromCoordinateList ( - Matrix.CoordinateList( - 4UL, - 4UL, - [ (0UL, 0UL, 1) - (0UL, 1UL, 2) - (1UL, 0UL, 3) - (1UL, 1UL, 4) ] - ) - ) - - let f row col x = - match x with - | Some(a) -> Some(a + int row + int col) - | _ -> None - - let actual = Matrix.mapi m f - let actualCL = Matrix.toCoordinateList actual - - Assert.Equal(4UL, actual.nvals) + let nrows = 4UL + let ncols = 4UL + + let d = + [ (0UL, 0UL, 1) + (0UL, 1UL, 2) + (1UL, 0UL, 3) + (1UL, 1UL, 4) ] + + match Matrix.fromCoordinateList (Matrix.CoordinateList(nrows, ncols, d)) with + | Result.Ok m -> + let f row col x = + match x with + | Some(a) -> Some(a + int row + int col) + | _ -> None + + let actual = Matrix.mapi m f + Assert.Equal(4UL, actual.nvals) + | _ -> Assert.Fail() [] let ``Simple Matrix.mapi. Square where number of cols and rows are not power of two.`` () = - let m = - Matrix.fromCoordinateList ( - Matrix.CoordinateList( - 3UL, - 3UL, - [ (0UL, 0UL, 1) - (0UL, 1UL, 2) - (0UL, 2UL, 3) - (1UL, 0UL, 4) - (1UL, 1UL, 5) - (1UL, 2UL, 6) ] - ) - ) - - let f row col x = - match x with - | Some(a) -> Some(a * (int row + 1) * (int col + 1)) - | _ -> None - - let actual = Matrix.mapi m f - let actualCL = Matrix.toCoordinateList actual - - Assert.Equal(6UL, actual.nvals) + let nrows = 3UL + let ncols = 3UL + + let d = + [ (0UL, 0UL, 1) + (0UL, 1UL, 2) + (0UL, 2UL, 3) + (1UL, 0UL, 4) + (1UL, 1UL, 5) + (1UL, 2UL, 6) ] + + match Matrix.fromCoordinateList (Matrix.CoordinateList(nrows, ncols, d)) with + | Result.Ok m -> + let f row col x = + match x with + | Some(a) -> Some(a * (int row + 1) * (int col + 1)) + | _ -> None + + let actual = Matrix.mapi m f + Assert.Equal(6UL, actual.nvals) + | _ -> Assert.Fail() [] let ``Simple Matrix.mapi. Multiply row index by value.`` () = - let m = - Matrix.fromCoordinateList ( - Matrix.CoordinateList( - 4UL, - 4UL, - [ (0UL, 0UL, 1) - (1UL, 1UL, 2) - (2UL, 2UL, 3) - (3UL, 3UL, 4) ] - ) - ) - - let f row col x = - match x with - | Some(a) -> Some(a * int row) - | _ -> None - - let actual = Matrix.mapi m f - let actualCL = Matrix.toCoordinateList actual - - Assert.Equal(4UL, actual.nvals) + let nrows = 4UL + let ncols = 4UL + + let d = + [ (0UL, 0UL, 1) + (1UL, 1UL, 2) + (2UL, 2UL, 3) + (3UL, 3UL, 4) ] + + match Matrix.fromCoordinateList (Matrix.CoordinateList(nrows, ncols, d)) with + | Result.Ok m -> + let f row col x = + match x with + | Some(a) -> Some(a * int row) + | _ -> None + + let actual = Matrix.mapi m f + Assert.Equal(4UL, actual.nvals) + | _ -> Assert.Fail() [] let ``Conversion identity`` () = - let id = toCoordinateList << fromCoordinateList - let nrows = 10UL let ncols = 12UL @@ -391,12 +362,13 @@ let ``Conversion identity`` () = 3UL, 11UL, 1 ] |> List.sort - let coordinates = CoordinateList(nrows, ncols, data) - - let expected = coordinates - let actual = id coordinates + let coordinates = Matrix.CoordinateList(nrows, ncols, data) - Assert.Equal(expected, actual) + match Matrix.fromCoordinateList coordinates with + | Result.Ok m -> + let actual = Matrix.toCoordinateList m + Assert.Equal(coordinates, actual) + | _ -> Assert.Fail() [] let ``Simple addition`` () = @@ -414,21 +386,21 @@ let ``Simple addition`` () = 3UL, 11UL, -1 ] let expected = - let expectedList = + CoordinateList( + nrows, + ncols, [ 0UL, 3UL, 10 3UL, 3UL, 33 9UL, 2UL, 5 3UL, 11UL, 1 ] |> List.sort + ) - CoordinateList(nrows, ncols, expectedList) - - let actual = - let c1 = CoordinateList(nrows, ncols, d1) - let c2 = CoordinateList(nrows, ncols, d2) - let m1 = fromCoordinateList c1 - let m2 = fromCoordinateList c2 - + match + Matrix.fromCoordinateList (CoordinateList(nrows, ncols, d1)), + Matrix.fromCoordinateList (CoordinateList(nrows, ncols, d2)) + with + | Result.Ok m1, Result.Ok m2 -> let addition o1 o2 = match o1, o2 with | Some x, Some y -> Some(x + y) @@ -436,59 +408,60 @@ let ``Simple addition`` () = | None, Some x -> Some x | None, None -> None - let result = - match map2 m1 m2 addition with - | Ok x -> x - | _ -> failwith "Unreachable" - - toCoordinateList result - - Assert.Equal(expected, actual) + match Matrix.map2 m1 m2 addition with + | Result.Ok resultMatrix -> + let actual = Matrix.toCoordinateList resultMatrix + Assert.Equal(expected, actual) + | _ -> Assert.Fail() + | _ -> Assert.Fail() [] let ``Condensation of empty`` () = let clist = CoordinateList(2UL, 3UL, []) - let actual = fromCoordinateList clist - // 2 * 3 = 5 // 4 * 4 None and Dummy // NN N D // NN N D // DDDD // DDDD - let tree = - qtree.Node(leaf_n (), qtree.Node(leaf_n (), leaf_d (), leaf_n (), leaf_d ()), leaf_d (), leaf_d ()) - let expected = - SparseMatrix(2UL, 3UL, 0UL, Storage(4UL, tree)) + match Matrix.fromCoordinateList clist with + | Result.Ok actual -> + let tree = + qtree.Node(leaf_n (), qtree.Node(leaf_n (), leaf_d (), leaf_n (), leaf_d ()), leaf_d (), leaf_d ()) - Assert.Equal(expected.storage.data, actual.storage.data) + let expected = + SparseMatrix(2UL, 3UL, 0UL, Storage(4UL, tree)) + + Assert.Equal(expected.storage.data, actual.storage.data) + | _ -> Assert.Fail() [] let ``Condensation of sparse`` () = let clist = CoordinateList(4UL, 3UL, [ 0UL, 2UL, 2; 3UL, 2UL, 4 ]) - let actual = fromCoordinateList clist - // NN2D // NNND // NNND // NN4D - let tree = - qtree.Node( - leaf_n (), - qtree.Node(leaf_v 2, leaf_d (), leaf_n (), leaf_d ()), - leaf_n (), - qtree.Node(leaf_n (), leaf_d (), leaf_v 4, leaf_d ()) - ) + match Matrix.fromCoordinateList clist with + | Result.Ok actual -> + let tree = + qtree.Node( + leaf_n (), + qtree.Node(leaf_v 2, leaf_d (), leaf_n (), leaf_d ()), + leaf_n (), + qtree.Node(leaf_n (), leaf_d (), leaf_v 4, leaf_d ()) + ) - let expected = - SparseMatrix(4UL, 3UL, 0UL, Storage(4UL, tree)) + let expected = + SparseMatrix(4UL, 3UL, 2UL, Storage(4UL, tree)) - Assert.Equal(expected.storage.data, actual.storage.data) + Assert.Equal(expected.storage.data, actual.storage.data) + | _ -> Assert.Fail() [] let ``fold -> sum`` () = @@ -543,7 +516,6 @@ let ``4x4 lower triangle`` () = Assert.Equal(expected, actual) - [] let ``3x3 lower triangle`` () = // 222 D @@ -642,3 +614,1581 @@ let ``Fold sum`` () = let actual = foldAssociative op_add None m1 |> Option.get Assert.Equal(expected, actual) + +[] +let ``fromCoordinateList with out-of-range coordinates returns Error`` () = + let coo = + CoordinateList(6UL, 6UL, [ (9UL, 9UL, 13) ]) + + let result = fromCoordinateList coo + + match result with + | Error _ -> () + | Ok _ -> Assert.Fail() + +[] +let ``fromCoordinateList with zero size returns Error`` () = + let coo = + CoordinateList( + 0UL, + 0UL, + [ (33UL, 33UL, 33); (39UL, 39UL, 1) ] + ) + + let result = fromCoordinateList coo + + match result with + | Error _ -> () + | Ok _ -> Assert.Fail() + +[] +let ``fromCoordinateList with unsorted coordinates works correctly`` () = + let coo = + CoordinateList( + 7UL, + 7UL, + [ (6UL, 6UL, 10) + (1UL, 2UL, 8) + (1UL, 1UL, 100) ] + ) + + match Matrix.fromCoordinateList coo with + | Result.Ok matrix -> + let result = Matrix.toCoordinateList matrix + + Assert.Equal( + CoordinateList( + 7UL, + 7UL, + [ (1UL, 1UL, 100) + (1UL, 2UL, 8) + (6UL, 6UL, 10) ] + ), + result + ) + | _ -> Assert.Fail() + +[] +let ``fromCoordinateList with duplicate indices returns the last of them`` () = + let coo = + CoordinateList( + 3UL, + 3UL, + [ (1UL, 1UL, 33); (1UL, 1UL, 100) ] + ) + + match Matrix.fromCoordinateList coo with + | Result.Ok matrix -> + let result = Matrix.toCoordinateList matrix + Assert.Equal(CoordinateList(3UL, 3UL, [ (1UL, 1UL, 100) ]), result) + | _ -> Assert.Fail() + +[] +let ``map works on square matrix`` () = + let clist = + CoordinateList( + 4UL, + 4UL, + [ (1UL, 1UL, 22); (2UL, 3UL, 37) ] + ) + + match Matrix.fromCoordinateList clist with + | Result.Ok matrix -> + let result = + Matrix.map matrix (fun (x: int option) -> + match x with + | Some v -> Some(v + 9) + | None -> None) + + let coo = Matrix.toCoordinateList result + + Assert.Equal( + CoordinateList( + 4UL, + 4UL, + [ (1UL, 1UL, 31); (2UL, 3UL, 46) ] + ), + coo + ) + | _ -> Assert.Fail() + +[] +let ``map works on rectangular matrix`` () = + let clist = + CoordinateList( + 5UL, + 6UL, + [ (1UL, 1UL, 21); (4UL, 3UL, 36) ] + ) + + match Matrix.fromCoordinateList clist with + | Result.Ok matrix -> + let result = + Matrix.map matrix (fun (x: int option) -> + match x with + | Some v -> Some(v / 3) + | None -> None) + + let coo = Matrix.toCoordinateList result + + Assert.Equal( + CoordinateList( + 5UL, + 6UL, + [ (1UL, 1UL, 7); (4UL, 3UL, 12) ] + ), + coo + ) + | _ -> Assert.Fail() + +[] +let ``map on empty matrix returns empty matrix`` () = + match Matrix.fromCoordinateList (CoordinateList(0UL, 0UL, [])) with + | Result.Ok matrix -> + let result = + Matrix.map matrix (fun (x: int option) -> + match x with + | Some v -> Some(v * 5) + | None -> None) + + let coo = Matrix.toCoordinateList result + Assert.Equal(CoordinateList(0UL, 0UL, []), coo) + | _ -> Assert.Fail() + +[] +let ``map with function that turns all the elements into zeros`` () = + let clist = + CoordinateList( + 5UL, + 7UL, + [ (1UL, 1UL, 5) + (4UL, 1UL, 17) + (4UL, 6UL, 33) ] + ) + + match Matrix.fromCoordinateList clist with + | Result.Ok matrix -> + let result = Matrix.map matrix (fun _ -> Some(0)) + let coo = Matrix.toCoordinateList result + + let expectedData = + [ for i in 0UL .. 4UL do + for j in 0UL .. 6UL -> (i * 1UL, j * 1UL, 0) ] + + let expected = CoordinateList(5UL, 7UL, expectedData) + Assert.Equal(expected, coo) + | _ -> Assert.Fail() + +[] +let ``map with function that turns all the elements to None`` () = + let clist = + CoordinateList( + 5UL, + 7UL, + [ (1UL, 1UL, 5) + (4UL, 1UL, 17) + (4UL, 6UL, 33) ] + ) + + match Matrix.fromCoordinateList clist with + | Result.Ok matrix -> + let result = Matrix.map matrix (fun _ -> None) + Assert.Equal(CoordinateList(5UL, 7UL, []), Matrix.toCoordinateList result) + | _ -> Assert.Fail() + +[] +let ``map can change type from int to string`` () = + let clist = + CoordinateList( + 3UL, + 5UL, + [ (2UL, 1UL, 17); (2UL, 4UL, 33) ] + ) + + match Matrix.fromCoordinateList clist with + | Result.Ok matrix -> + let result = + Matrix.map matrix (fun (x: int option) -> + match x with + | Some v -> Some(sprintf "str %d" v) + | None -> None) + + let actual = Matrix.toCoordinateList result + + Assert.Equal( + CoordinateList( + 3UL, + 5UL, + [ (2UL, 1UL, "str 17") + (2UL, 4UL, "str 33") ] + ), + actual + ) + | _ -> Assert.Fail() + +[] +let ``mapi works with row index and col index on square matrix`` () = + let clist = + CoordinateList( + 4UL, + 4UL, + [ (1UL, 1UL, 12); (2UL, 3UL, 13) ] + ) + + match Matrix.fromCoordinateList clist with + | Result.Ok matrix -> + let result = + Matrix.mapi matrix (fun i j x -> + match x with + | Some v -> Some(v * int i + int j) + | None -> None) + + Assert.Equal( + CoordinateList( + 4UL, + 4UL, + [ (1UL, 1UL, 13); (2UL, 3UL, 29) ] + ), + Matrix.toCoordinateList result + ) + | _ -> Assert.Fail() + +[] +let ``mapi works with row index and col index on rectangular matrix`` () = + let clist = + CoordinateList( + 3UL, + 5UL, + [ (1UL, 2UL, 15); (2UL, 2UL, 13) ] + ) + + match Matrix.fromCoordinateList clist with + | Result.Ok matrix -> + let result = + Matrix.mapi matrix (fun i j x -> + match x with + | Some v -> Some(v * int j + int i) + | None -> None) + + Assert.Equal( + CoordinateList( + 3UL, + 5UL, + [ (1UL, 2UL, 31); (2UL, 2UL, 28) ] + ), + Matrix.toCoordinateList result + ) + | _ -> Assert.Fail() + +[] +let ``mapi on empty matrix returns empty matrix`` () = + match Matrix.fromCoordinateList (CoordinateList(0UL, 0UL, [])) with + | Result.Ok matrix -> + let result = + Matrix.mapi matrix (fun i j x -> + match x with + | Some v -> Some((v + 1) * 3 + 2 * int j) + | None -> None) + + Assert.Equal(CoordinateList(0UL, 0UL, []), Matrix.toCoordinateList result) + | _ -> Assert.Fail() + +[] +let ``mapi with special function returns empty matrix`` () = + let clist = + CoordinateList( + 5UL, + 7UL, + [ (1UL, 2UL, 15); (4UL, 5UL, 13) ] + ) + + match Matrix.fromCoordinateList clist with + | Result.Ok matrix -> + let result = + Matrix.mapi matrix (fun i j x -> + match x with + | Some v when (int i + int j) % 2 = 0 -> Some(v * 2) + | _ -> None) + + Assert.Equal(CoordinateList(5UL, 7UL, []), Matrix.toCoordinateList result) + | _ -> Assert.Fail() + +[] +let ``mapi works with function does not depend on the indexes`` () = + let clist = + CoordinateList( + 5UL, + 7UL, + [ (1UL, 2UL, 15); (4UL, 5UL, 13) ] + ) + + match Matrix.fromCoordinateList clist with + | Result.Ok matrix -> + let result = + Matrix.mapi matrix (fun _ _ x -> + match x with + | Some v -> Some(v * 2) + | None -> None) + + Assert.Equal( + CoordinateList( + 5UL, + 7UL, + [ (1UL, 2UL, 30); (4UL, 5UL, 26) ] + ), + Matrix.toCoordinateList result + ) + | _ -> Assert.Fail() + +[] +let ``slice returns error when row start is negative`` () = + let clist = + CoordinateList( + 5UL, + 6UL, + [ (2UL, 2UL, 3); (3UL, 4UL, 17) ] + ) + + match Matrix.fromCoordinateList clist with + | Result.Ok m -> + match Matrix.slice m -1 4 2 3 with + | Result.Ok _ -> Assert.Fail() + | Result.Error msg -> Assert.Equal("Start row should be >= 0", msg) + | _ -> Assert.Fail() + +[] +let ``slice returns error when row end is negative`` () = + let clist = + CoordinateList( + 5UL, + 6UL, + [ (2UL, 2UL, 3); (3UL, 4UL, 17) ] + ) + + match Matrix.fromCoordinateList clist with + | Result.Ok m -> + match Matrix.slice m 1 -4 2 3 with + | Result.Ok _ -> Assert.Fail() + | Result.Error msg -> Assert.Equal("End row should be >= 0", msg) + | _ -> Assert.Fail() + +[] +let ``slice returns error when col start is negative`` () = + let clist = + CoordinateList( + 5UL, + 6UL, + [ (2UL, 2UL, 3); (3UL, 4UL, 17) ] + ) + + match Matrix.fromCoordinateList clist with + | Result.Ok m -> + match Matrix.slice m 1 4 -2 3 with + | Result.Ok _ -> Assert.Fail() + | Result.Error msg -> Assert.Equal("Start column should be >= 0", msg) + | _ -> Assert.Fail() + +[] +let ``slice returns error when col end is negative`` () = + let clist = + CoordinateList( + 5UL, + 6UL, + [ (2UL, 2UL, 3); (3UL, 4UL, 17) ] + ) + + match Matrix.fromCoordinateList clist with + | Result.Ok m -> + match Matrix.slice m 1 4 2 -3 with + | Result.Ok _ -> Assert.Fail() + | Result.Error msg -> Assert.Equal("End column should be >= 0", msg) + | _ -> Assert.Fail() + +[] +let ``slice returns error when row start is out of range`` () = + let clist = + CoordinateList( + 5UL, + 6UL, + [ (2UL, 2UL, 3); (3UL, 4UL, 17) ] + ) + + match Matrix.fromCoordinateList clist with + | Result.Ok m -> + match Matrix.slice m 6 4 2 3 with + | Result.Ok _ -> Assert.Fail() + | Result.Error msg -> Assert.Equal("Start row is out of matrix length", msg) + | _ -> Assert.Fail() + +[] +let ``slice returns error when row end is out of range`` () = + let clist = + CoordinateList( + 5UL, + 6UL, + [ (2UL, 2UL, 3); (3UL, 4UL, 17) ] + ) + + match Matrix.fromCoordinateList clist with + | Result.Ok m -> + match Matrix.slice m 1 10 2 3 with + | Result.Ok _ -> Assert.Fail() + | Result.Error msg -> Assert.Equal("End row is out of matrix length", msg) + | _ -> Assert.Fail() + +[] +let ``slice returns error when col start is out of range`` () = + let clist = + CoordinateList( + 5UL, + 6UL, + [ (2UL, 2UL, 3); (3UL, 4UL, 17) ] + ) + + match Matrix.fromCoordinateList clist with + | Result.Ok m -> + match Matrix.slice m 1 4 10 3 with + | Result.Ok _ -> Assert.Fail() + | Result.Error msg -> Assert.Equal("Start column is out of matrix length", msg) + | _ -> Assert.Fail() + +[] +let ``slice returns error when col end is out of range`` () = + let clist = + CoordinateList( + 5UL, + 6UL, + [ (2UL, 2UL, 3); (3UL, 4UL, 17) ] + ) + + match Matrix.fromCoordinateList clist with + | Result.Ok m -> + match Matrix.slice m 1 2 2 10 with + | Result.Ok _ -> Assert.Fail() + | Result.Error msg -> Assert.Equal("End column is out of matrix length", msg) + | _ -> Assert.Fail() + +[] +let ``slice returns error when row end is less than row start`` () = + let clist = + CoordinateList( + 5UL, + 6UL, + [ (2UL, 2UL, 3); (3UL, 4UL, 17) ] + ) + + match Matrix.fromCoordinateList clist with + | Result.Ok m -> + match Matrix.slice m 2 1 2 3 with + | Result.Ok _ -> Assert.Fail() + | Result.Error msg -> Assert.Equal("Start row should be <= end row", msg) + | _ -> Assert.Fail() + +[] +let ``slice returns error when col end is less than col start`` () = + let clist = + CoordinateList( + 5UL, + 6UL, + [ (2UL, 2UL, 3); (3UL, 4UL, 17) ] + ) + + match Matrix.fromCoordinateList clist with + | Result.Ok m -> + match Matrix.slice m 1 2 3 2 with + | Result.Ok _ -> Assert.Fail() + | Result.Error msg -> Assert.Equal("Start column should be <= end column", msg) + | _ -> Assert.Fail() + +[] +let ``slice returns correct square submatrix`` () = + let clist = + CoordinateList( + 7UL, + 7UL, + [ (2UL, 2UL, 33); (5UL, 5UL, 28) ] + ) + + match Matrix.fromCoordinateList clist with + | Result.Ok m -> + match Matrix.slice m 1 3 1 3 with + | Result.Ok res -> + Assert.Equal( + CoordinateList(3UL, 3UL, [ (1UL, 1UL, 33) ]), + Matrix.toCoordinateList res + ) + | Result.Error msg -> Assert.Fail() + | _ -> Assert.Fail() + +[] +let ``slice returns correct rectangular submatrix`` () = + let clist = + CoordinateList( + 5UL, + 9UL, + [ (3UL, 7UL, 33); (1UL, 2UL, 28) ] + ) + + match Matrix.fromCoordinateList clist with + | Result.Ok m -> + match Matrix.slice m 2 4 5 8 with + | Result.Ok res -> + Assert.Equal( + CoordinateList(3UL, 4UL, [ (1UL, 2UL, 33) ]), + Matrix.toCoordinateList res + ) + | Result.Error msg -> Assert.Fail() + | _ -> Assert.Fail() + +[] +let ``slice returns empty matrix`` () = + let clist = + CoordinateList( + 7UL, + 9UL, + [ (3UL, 3UL, 33); (1UL, 2UL, 28) ] + ) + + match Matrix.fromCoordinateList clist with + | Result.Ok m -> + match Matrix.slice m 4 6 5 8 with + | Result.Ok result -> Assert.Equal(CoordinateList(3UL, 4UL, []), Matrix.toCoordinateList result) + | Result.Error msg -> Assert.Fail() + | _ -> Assert.Fail() + +[] +let ``slice returns single submatrix`` () = + let clist = + CoordinateList( + 7UL, + 9UL, + [ (3UL, 3UL, 33); (1UL, 2UL, 28) ] + ) + + match Matrix.fromCoordinateList clist with + | Result.Ok m -> + match Matrix.slice m 1 1 2 2 with + | Result.Ok result -> + Assert.Equal( + CoordinateList(1UL, 1UL, [ (0UL, 0UL, 28) ]), + Matrix.toCoordinateList result + ) + | Result.Error msg -> Assert.Fail() + | _ -> Assert.Fail() + +[] +let ``slice returns correct submatrix equals to matrix`` () = + let clist = + CoordinateList( + 7UL, + 9UL, + [ (3UL, 3UL, 33); (1UL, 2UL, 28) ] + ) + + match Matrix.fromCoordinateList clist with + | Result.Ok m -> + match Matrix.slice m 0 6 0 8 with + | Result.Ok result -> + Assert.Equal( + CoordinateList( + 7UL, + 9UL, + [ (1UL, 2UL, 28); (3UL, 3UL, 33) ] + ), + Matrix.toCoordinateList result + ) + | Result.Error msg -> Assert.Fail() + | _ -> Assert.Fail() + +[] +let ``slice returns correct submatrix when row start of submatrix equals to row start of matrix`` () = + let clist = + CoordinateList( + 7UL, + 7UL, + [ (0UL, 0UL, 10) + (3UL, 3UL, 33) + (6UL, 6UL, 6) ] + ) + + match Matrix.fromCoordinateList clist with + | Result.Ok m -> + match Matrix.slice m 0 4 0 4 with + | Result.Ok result -> + Assert.Equal( + CoordinateList( + 5UL, + 5UL, + [ (0UL, 0UL, 10); (3UL, 3UL, 33) ] + ), + Matrix.toCoordinateList result + ) + | Result.Error msg -> Assert.Fail() + | _ -> Assert.Fail() + +[] +let ``slice returns correct submatrix when row end of submatrix equals to row end of matrix`` () = + let clist = + CoordinateList( + 7UL, + 7UL, + [ (0UL, 0UL, 10) + (3UL, 3UL, 33) + (6UL, 6UL, 6) ] + ) + + match Matrix.fromCoordinateList clist with + | Result.Ok m -> + match Matrix.slice m 2 6 2 4 with + | Result.Ok result -> + Assert.Equal( + CoordinateList(5UL, 3UL, [ (1UL, 1UL, 33) ]), + Matrix.toCoordinateList result + ) + | Result.Error msg -> Assert.Fail() + | _ -> Assert.Fail() + +[] +let ``slice returns correct submatrix when col start of submatrix equals to col start of matrix`` () = + let clist = + CoordinateList( + 7UL, + 7UL, + [ (0UL, 0UL, 10) + (3UL, 3UL, 33) + (6UL, 6UL, 6) ] + ) + + match Matrix.fromCoordinateList clist with + | Result.Ok m -> + match Matrix.slice m 2 5 0 6 with + | Result.Ok result -> + Assert.Equal( + CoordinateList(4UL, 7UL, [ (1UL, 3UL, 33) ]), + Matrix.toCoordinateList result + ) + | Result.Error msg -> Assert.Fail() + | _ -> Assert.Fail() + +[] +let ``slice returns correct submatrix when col end of submatrix equals to col end of matrix`` () = + let clist = + CoordinateList( + 7UL, + 7UL, + [ (0UL, 0UL, 10) + (3UL, 3UL, 33) + (6UL, 6UL, 6) ] + ) + + match Matrix.fromCoordinateList clist with + | Result.Ok m -> + match Matrix.slice m 2 4 2 6 with + | Result.Ok result -> + Assert.Equal( + CoordinateList(3UL, 5UL, [ (1UL, 1UL, 33) ]), + Matrix.toCoordinateList result + ) + | Result.Error msg -> Assert.Fail() + | _ -> Assert.Fail() + +[] +let ``slice returns single column`` () = + let clist = + CoordinateList( + 10UL, + 10UL, + [ (1UL, 3UL, 1) + (2UL, 2UL, 2) + (5UL, 7UL, 3) ] + ) + + match Matrix.fromCoordinateList clist with + | Result.Ok m -> + match Matrix.slice m 1 6 2 2 with + | Result.Ok res -> + Assert.Equal( + CoordinateList(6UL, 1UL, [ (1UL, 0UL, 2) ]), + Matrix.toCoordinateList res + ) + | Result.Error msg -> Assert.Fail(msg) + | Result.Error msg -> Assert.Fail(msg) + +[] +let ``slice returns single row`` () = + let clist = + CoordinateList( + 10UL, + 10UL, + [ (1UL, 3UL, 1) + (2UL, 2UL, 2) + (5UL, 7UL, 3) ] + ) + + match Matrix.fromCoordinateList clist with + | Result.Ok m -> + match Matrix.slice m 5 5 3 9 with + | Result.Ok res -> + Assert.Equal( + CoordinateList(1UL, 7UL, [ (0UL, 4UL, 3) ]), + Matrix.toCoordinateList res + ) + | Result.Error msg -> Assert.Fail(msg) + | Result.Error msg -> Assert.Fail(msg) + +[] +let ``let reduceRows sum on square power of two matrix`` () = + let coo = + CoordinateList( + 2UL, + 2UL, + [ (0UL, 0UL, 12) + (0UL, 1UL, 15) + (1UL, 0UL, 17) + (1UL, 1UL, 3) ] + ) + + match Matrix.fromCoordinateList coo with + | Result.Error msg -> Assert.Fail() + | Result.Ok matrix -> + let add x y = + match x, y with + | Some a, Some b -> Some(a + b) + | Some a, None + | None, Some a -> Some a + | _ -> None + + let result = Matrix.reduceRows add matrix + + let vectorCoordinates = Vector.toCoordinateList result + + let expected = + Vector.CoordinateList(2UL, [ (0UL, 27); (1UL, 20) ]) + + Assert.Equal(expected, vectorCoordinates) + +[] +let ``let reduceRows sum on square power of two matrix with empty row`` () = + let coo = + CoordinateList( + 2UL, + 2UL, + [ (1UL, 0UL, 17); (1UL, 1UL, 3) ] + ) + + match Matrix.fromCoordinateList coo with + | Result.Ok m -> + let add x y = + match x, y with + | Some a, Some b -> Some(a + b) + | Some a, None + | None, Some a -> Some a + | _ -> None + + let res = Matrix.reduceRows add m + + Assert.Equal( + Vector.CoordinateList(2UL, [ (1UL, 20) ]), + Vector.toCoordinateList res + ) + | Result.Error msg -> Assert.Fail(msg) + +[] +let ``let reduceRows sum on square not power of two matrix`` () = + let coo = + CoordinateList( + 3UL, + 3UL, + [ (0UL, 0UL, 5) + (0UL, 1UL, 7) + (0UL, 2UL, 9) + (1UL, 1UL, 11) + (1UL, 2UL, 13) + (2UL, 0UL, 15) + (2UL, 1UL, 17) ] + ) + + match Matrix.fromCoordinateList coo with + | Result.Ok m -> + let add x y = + match x, y with + | Some a, Some b -> Some(a + b) + | Some a, None + | None, Some a -> Some a + | _ -> None + + let res = Matrix.reduceRows add m + + Assert.Equal( + Vector.CoordinateList( + 3UL, + [ (0UL, 21); (1UL, 24); (2UL, 32) ] + ), + Vector.toCoordinateList res + ) + | Result.Error msg -> Assert.Fail(msg) + +[] +let ``let reduceRows mul on square not power of two matrix`` () = + let coo = + CoordinateList( + 3UL, + 3UL, + [ (0UL, 0UL, 5) + (0UL, 1UL, 7) + (0UL, 2UL, 9) + (1UL, 1UL, 11) + (1UL, 2UL, 13) + (2UL, 0UL, 15) + (2UL, 1UL, 17) ] + ) + + match Matrix.fromCoordinateList coo with + | Result.Ok m -> + let mul x y = + match x, y with + | Some a, Some b -> Some(a * b) + | Some a, None + | None, Some a -> Some a + | _ -> None + + let res = Matrix.reduceRows mul m + + Assert.Equal( + Vector.CoordinateList( + 3UL, + [ (0UL, 315); (1UL, 143); (2UL, 255) ] + ), + Vector.toCoordinateList res + ) + | Result.Error msg -> Assert.Fail(msg) + +[] +let ``let reduceRows sum on rectangular matrix`` () = + let coo = + CoordinateList( + 2UL, + 3UL, + [ (0UL, 0UL, 5) + (0UL, 1UL, 7) + (0UL, 2UL, 9) + (1UL, 1UL, 11) + (1UL, 2UL, 13) ] + ) + + match Matrix.fromCoordinateList coo with + | Result.Ok m -> + let sum x y = + match x, y with + | Some a, Some b -> Some(a + b) + | Some a, None + | None, Some a -> Some a + | _ -> None + + let res = Matrix.reduceRows sum m + + Assert.Equal( + Vector.CoordinateList(2UL, [ (0UL, 21); (1UL, 24) ]), + Vector.toCoordinateList res + ) + | Result.Error msg -> Assert.Fail(msg) + +[] +let ``let reduceRows sum on empty matrix`` () = + match Matrix.fromCoordinateList (CoordinateList(2UL, 3UL, [])) with + | Result.Ok m -> + let sum x y = + match x, y with + | Some a, Some b -> Some(a + b) + | Some a, None + | None, Some a -> Some a + | _ -> None + + let res = Matrix.reduceRows sum m + Assert.Equal(Vector.CoordinateList(2UL, []), Vector.toCoordinateList res) + | Result.Error msg -> Assert.Fail(msg) + +[] +let ``let reduceRows mul on single matrix`` () = + match + Matrix.fromCoordinateList (CoordinateList(1UL, 1UL, [ (0UL, 0UL, 33) ])) + with + | Result.Ok m -> + let mul x y = + match x, y with + | Some a, Some b -> Some(a * b) + | Some a, None + | None, Some a -> Some a + | _ -> None + + let res = Matrix.reduceRows mul m + + Assert.Equal( + Vector.CoordinateList(1UL, [ (0UL, 33) ]), + Vector.toCoordinateList res + ) + | Result.Error msg -> Assert.Fail(msg) + +[] +let ``let reduceCols sum on square power of two matrix`` () = + let coo = + CoordinateList( + 2UL, + 2UL, + [ (0UL, 0UL, 12) + (0UL, 1UL, 15) + (1UL, 0UL, 17) + (1UL, 1UL, 3) ] + ) + + match Matrix.fromCoordinateList coo with + | Result.Ok m -> + let add x y = + match x, y with + | Some a, Some b -> Some(a + b) + | Some a, None + | None, Some a -> Some a + | _ -> None + + let res = Matrix.reduceCols add m + + Assert.Equal( + Vector.CoordinateList(2UL, [ (0UL, 29); (1UL, 18) ]), + Vector.toCoordinateList res + ) + | Result.Error msg -> Assert.Fail(msg) + +[] +let ``let reduceCols sum on square power of two matrix with empty col`` () = + let coo = + CoordinateList( + 2UL, + 2UL, + [ (0UL, 0UL, 17); (1UL, 0UL, 3) ] + ) + + match Matrix.fromCoordinateList coo with + | Result.Ok m -> + let add x y = + match x, y with + | Some a, Some b -> Some(a + b) + | Some a, None + | None, Some a -> Some a + | _ -> None + + let res = Matrix.reduceCols add m + + Assert.Equal( + Vector.CoordinateList(2UL, [ (0UL, 20) ]), + Vector.toCoordinateList res + ) + | Result.Error msg -> Assert.Fail(msg) + +[] +let ``let reduceCols sum on square not power of two matrix`` () = + let coo = + CoordinateList( + 3UL, + 3UL, + [ (0UL, 0UL, 5) + (0UL, 1UL, 7) + (0UL, 2UL, 9) + (1UL, 1UL, 11) + (1UL, 2UL, 13) + (2UL, 0UL, 15) + (2UL, 1UL, 17) ] + ) + + match Matrix.fromCoordinateList coo with + | Result.Ok m -> + let add x y = + match x, y with + | Some a, Some b -> Some(a + b) + | Some a, None + | None, Some a -> Some a + | _ -> None + + let res = Matrix.reduceCols add m + + Assert.Equal( + Vector.CoordinateList( + 3UL, + [ (0UL, 20); (1UL, 35); (2UL, 22) ] + ), + Vector.toCoordinateList res + ) + | Result.Error msg -> Assert.Fail(msg) + +[] +let ``let reduceCols mul on square not power of two matrix`` () = + let coo = + CoordinateList( + 3UL, + 3UL, + [ (0UL, 0UL, 5) + (0UL, 1UL, 7) + (0UL, 2UL, 9) + (1UL, 1UL, 11) + (1UL, 2UL, 13) + (2UL, 0UL, 15) + (2UL, 1UL, 17) ] + ) + + match Matrix.fromCoordinateList coo with + | Result.Ok m -> + let mul x y = + match x, y with + | Some a, Some b -> Some(a * b) + | Some a, None + | None, Some a -> Some a + | _ -> None + + let res = Matrix.reduceCols mul m + + Assert.Equal( + Vector.CoordinateList( + 3UL, + [ (0UL, 75); (1UL, 1309); (2UL, 117) ] + ), + Vector.toCoordinateList res + ) + | Result.Error msg -> Assert.Fail(msg) + +[] +let ``let reduceCols sum on rectangular matrix`` () = + let coo = + CoordinateList( + 2UL, + 3UL, + [ (0UL, 0UL, 5) + (0UL, 1UL, 7) + (0UL, 2UL, 9) + (1UL, 1UL, 11) + (1UL, 2UL, 13) ] + ) + + match Matrix.fromCoordinateList coo with + | Result.Ok m -> + let sum x y = + match x, y with + | Some a, Some b -> Some(a + b) + | Some a, None + | None, Some a -> Some a + | _ -> None + + let res = Matrix.reduceCols sum m + + Assert.Equal( + Vector.CoordinateList( + 3UL, + [ (0UL, 5); (1UL, 18); (2UL, 22) ] + ), + Vector.toCoordinateList res + ) + | Result.Error msg -> Assert.Fail(msg) + +[] +let ``let reduceCols sum on empty matrix`` () = + match Matrix.fromCoordinateList (CoordinateList(2UL, 3UL, [])) with + | Result.Ok m -> + let sum x y = + match x, y with + | Some a, Some b -> Some(a + b) + | Some a, None + | None, Some a -> Some a + | _ -> None + + let res = Matrix.reduceCols sum m + Assert.Equal(Vector.CoordinateList(3UL, []), Vector.toCoordinateList res) + | Result.Error msg -> Assert.Fail(msg) + +[] +let ``let reduceCols mul on single matrix`` () = + match + Matrix.fromCoordinateList (CoordinateList(1UL, 1UL, [ (0UL, 0UL, 33) ])) + with + | Result.Ok m -> + let mul x y = + match x, y with + | Some a, Some b -> Some(a * b) + | Some a, None + | None, Some a -> Some a + | _ -> None + + let res = Matrix.reduceCols mul m + + Assert.Equal( + Vector.CoordinateList(1UL, [ (0UL, 33) ]), + Vector.toCoordinateList res + ) + | Result.Error msg -> Assert.Fail(msg) + +[] +let ``kronecker product with square power of two x square power of two matrixes`` () = + let cooA = + CoordinateList( + 2UL, + 2UL, + [ (0UL, 0UL, 1) + (0UL, 1UL, 2) + (1UL, 0UL, 3) + (1UL, 1UL, 4) ] + ) + + let cooB = + CoordinateList( + 2UL, + 2UL, + [ (0UL, 0UL, 5) + (0UL, 1UL, 6) + (1UL, 0UL, 7) + (1UL, 1UL, 8) ] + ) + + let expected = + CoordinateList( + 4UL, + 4UL, + [ (0UL, 0UL, 5) + (0UL, 1UL, 6) + (0UL, 2UL, 10) + (0UL, 3UL, 12) + (1UL, 0UL, 7) + (1UL, 1UL, 8) + (1UL, 2UL, 14) + (1UL, 3UL, 16) + (2UL, 0UL, 15) + (2UL, 1UL, 18) + (2UL, 2UL, 20) + (2UL, 3UL, 24) + (3UL, 0UL, 21) + (3UL, 1UL, 24) + (3UL, 2UL, 28) + (3UL, 3UL, 32) ] + ) + + match Matrix.fromCoordinateList cooA, Matrix.fromCoordinateList cooB with + | Result.Ok a, Result.Ok b -> + match Matrix.kroneckerProduct a b (fun a b -> Some(a * b)) with + | Result.Ok res -> + let actual = Matrix.toCoordinateList res + Assert.Equal(expected, actual) + | Result.Error msg -> Assert.Fail(msg) + | _ -> Assert.Fail() + +[] +let ``kronecker product with square not power of two x square not power of two matrixes`` () = + let cooA = + CoordinateList( + 3UL, + 3UL, + [ (0UL, 0UL, 1) + (0UL, 1UL, 2) + (0UL, 2UL, 3) + (1UL, 0UL, 4) + (1UL, 1UL, 5) + (1UL, 2UL, 6) + (2UL, 0UL, 7) + (2UL, 1UL, 8) + (2UL, 2UL, 9) ] + ) + + let cooB = + CoordinateList( + 3UL, + 3UL, + [ (0UL, 0UL, 10) + (0UL, 1UL, 11) + (0UL, 2UL, 12) + (1UL, 0UL, 13) + (1UL, 1UL, 14) + (1UL, 2UL, 15) + (2UL, 0UL, 16) + (2UL, 1UL, 17) + (2UL, 2UL, 18) ] + ) + + match Matrix.fromCoordinateList cooA, Matrix.fromCoordinateList cooB with + | Result.Ok a, Result.Ok b -> + match Matrix.kroneckerProduct a b (fun a b -> Some(a * b)) with + | Result.Ok res -> Assert.Equal(9UL, (Matrix.toCoordinateList res).nrows) + | Result.Error msg -> Assert.Fail(msg) + | _ -> Assert.Fail() + +[] +let ``kronecker product with rectangular and square matrixes`` () = + let cooA = + CoordinateList( + 2UL, + 3UL, + [ (0UL, 0UL, 1) + (0UL, 1UL, 2) + (1UL, 0UL, 3) + (1UL, 2UL, 4) ] + ) + + let cooB = + CoordinateList( + 2UL, + 2UL, + [ (0UL, 0UL, 5) + (0UL, 1UL, 6) + (1UL, 0UL, 7) + (1UL, 1UL, 8) ] + ) + + let expected = + CoordinateList( + 4UL, + 6UL, + [ (0UL, 0UL, 5) + (0UL, 1UL, 6) + (0UL, 2UL, 10) + (0UL, 3UL, 12) + (1UL, 0UL, 7) + (1UL, 1UL, 8) + (1UL, 2UL, 14) + (1UL, 3UL, 16) + (2UL, 0UL, 15) + (2UL, 1UL, 18) + (2UL, 4UL, 20) + (2UL, 5UL, 24) + (3UL, 0UL, 21) + (3UL, 1UL, 24) + (3UL, 4UL, 28) + (3UL, 5UL, 32) ] + ) + + match Matrix.fromCoordinateList cooA, Matrix.fromCoordinateList cooB with + | Result.Ok a, Result.Ok b -> + match Matrix.kroneckerProduct a b (fun a b -> Some(a * b)) with + | Result.Ok res -> + let actual = Matrix.toCoordinateList res + Assert.Equal(expected, actual) + | Result.Error msg -> Assert.Fail msg + | _ -> Assert.Fail() + +[] +let ``kronecker product with square and rectangular matrixes`` () = + let cooA = + CoordinateList( + 2UL, + 2UL, + [ (0UL, 0UL, 1) + (0UL, 1UL, 2) + (1UL, 0UL, 3) + (1UL, 1UL, 4) ] + ) + + let cooB = + CoordinateList( + 2UL, + 3UL, + [ (0UL, 0UL, 5) + (0UL, 1UL, 6) + (0UL, 2UL, 7) + (1UL, 0UL, 8) + (1UL, 1UL, 9) + (1UL, 2UL, 10) ] + ) + + let expectedElements = + [ (0UL, 0UL, 5) + (0UL, 1UL, 6) + (0UL, 2UL, 7) + + (1UL, 0UL, 8) + (1UL, 1UL, 9) + (1UL, 2UL, 10) + + (0UL, 3UL, 10) + (0UL, 4UL, 12) + (0UL, 5UL, 14) + + (1UL, 3UL, 16) + (1UL, 4UL, 18) + (1UL, 5UL, 20) + + (2UL, 0UL, 15) + (2UL, 1UL, 18) + (2UL, 2UL, 21) + + (3UL, 0UL, 24) + (3UL, 1UL, 27) + (3UL, 2UL, 30) + + (2UL, 3UL, 20) + (2UL, 4UL, 24) + (2UL, 5UL, 28) + + (3UL, 3UL, 32) + (3UL, 4UL, 36) + (3UL, 5UL, 40) ] + |> List.sortBy (fun (r, c, _) -> (r, c)) + + match Matrix.fromCoordinateList cooA, Matrix.fromCoordinateList cooB with + | Result.Ok a, Result.Ok b -> + match Matrix.kroneckerProduct a b (fun a b -> Some(a * b)) with + | Result.Ok res -> + let coo = Matrix.toCoordinateList res + + Assert.Equal(4UL, coo.nrows) + Assert.Equal(6UL, coo.ncols) + + Assert.Equal * uint64 * int>>( + expectedElements, + coo.list |> List.sortBy (fun (r, c, _) -> (r, c)) + ) + + | Result.Error msg -> Assert.Fail(msg) + + | _ -> Assert.Fail() + +[] +let ``kronecker product of matrix with empty matrix`` () = + let cooA = + CoordinateList( + 2UL, + 2UL, + [ (0UL, 0UL, 1) + (0UL, 1UL, 2) + (1UL, 0UL, 3) + (1UL, 1UL, 4) ] + ) + + let cooEmpty = CoordinateList(2UL, 2UL, []) + + match Matrix.fromCoordinateList cooA, Matrix.fromCoordinateList cooEmpty with + | Result.Ok a, Result.Ok empty -> + match Matrix.kroneckerProduct a empty (fun a b -> Some(a * b)) with + | Result.Ok res -> Assert.Equal(CoordinateList(4UL, 4UL, []), Matrix.toCoordinateList res) + | Result.Error msg -> Assert.Fail(msg) + | _ -> Assert.Fail() + +[] +let ``kronecker product of empty matrix with matrix`` () = + let cooEmpty = CoordinateList(2UL, 2UL, []) + + let cooB = + CoordinateList(2UL, 2UL, [ (0UL, 0UL, 1); (1UL, 1UL, 4) ]) + + match Matrix.fromCoordinateList cooEmpty, Matrix.fromCoordinateList cooB with + | Result.Ok empty, Result.Ok b -> + match Matrix.kroneckerProduct empty b (fun a b -> Some(a * b)) with + | Result.Ok res -> Assert.Equal(CoordinateList(4UL, 4UL, []), Matrix.toCoordinateList res) + | Result.Error msg -> Assert.Fail(msg) + | _ -> Assert.Fail() + +[] +let ``kronecker product of matrix with zeros`` () = + let cooA = + CoordinateList(1UL, 1UL, [ (0UL, 0UL, 2) ]) + + let cooB = + CoordinateList(2UL, 2UL, [ (0UL, 0UL, 0); (1UL, 1UL, 3) ]) + + match Matrix.fromCoordinateList cooA, Matrix.fromCoordinateList cooB with + | Result.Ok a, Result.Ok b -> + match Matrix.kroneckerProduct a b (fun a b -> Some(a * b)) with + | Result.Ok res -> + let expected = + [ (0UL, 0UL, 0); (1UL, 1UL, 6) ] + + let actual = + (Matrix.toCoordinateList res).list |> List.sortBy (fun (r, c, _) -> (r, c)) + + Assert.Equal<(uint64 * uint64 * int) list>(expected, actual) + | Result.Error msg -> Assert.Fail(msg) + | _ -> Assert.Fail() + +[] +let ``kronecker product resulting entirely in explicit zeros`` () = + let cooA = + CoordinateList(2UL, 2UL, [ (0UL, 0UL, 0); (1UL, 1UL, 0) ]) + + let cooB = + CoordinateList(1UL, 1UL, [ (0UL, 0UL, 5) ]) + + match Matrix.fromCoordinateList cooA with + | Result.Error msg -> Assert.Fail(msg) + | Result.Ok a -> + match Matrix.fromCoordinateList cooB with + | Result.Error msg -> Assert.Fail(msg) + | Result.Ok b -> + match Matrix.kroneckerProduct a b (fun a b -> Some(a * b)) with + | Result.Ok res -> + let coo = Matrix.toCoordinateList res + + let expectedElements = + [ (0UL, 0UL, 0); (1UL, 1UL, 0) ] + |> List.sortBy (fun (r, c, _) -> (r, c)) + + Assert.Equal(2UL, coo.nrows) + Assert.Equal(2UL, coo.ncols) + + Assert.Equal * uint64 * int>>( + expectedElements, + coo.list |> List.sortBy (fun (r, c, _) -> (r, c)) + ) + | Result.Error msg -> Assert.Fail(msg) + +[] +let ``kronecker product of square matrix with matrix 1x1`` () = + let cooA = + CoordinateList( + 2UL, + 2UL, + [ (0UL, 0UL, 1) + (0UL, 1UL, 2) + (1UL, 0UL, 3) + (1UL, 1UL, 4) ] + ) + + let cooB = + CoordinateList(1UL, 1UL, [ (0UL, 0UL, 3) ]) + + match Matrix.fromCoordinateList cooA, Matrix.fromCoordinateList cooB with + | Result.Ok a, Result.Ok b -> + match Matrix.kroneckerProduct a b (fun a b -> Some(a * b)) with + | Result.Ok res -> + let expected = + CoordinateList( + 2UL, + 2UL, + [ (0UL, 0UL, 3) + (0UL, 1UL, 6) + (1UL, 0UL, 9) + (1UL, 1UL, 12) ] + ) + + Assert.Equal(expected, Matrix.toCoordinateList res) + | Result.Error msg -> Assert.Fail(msg) + | _ -> Assert.Fail() + +[] +let ``kronecker product of matrix 1x1 with square matrix`` () = + let cooA = + CoordinateList(1UL, 1UL, [ (0UL, 0UL, 3) ]) + + let cooB = + CoordinateList( + 2UL, + 2UL, + [ (0UL, 0UL, 1) + (0UL, 1UL, 2) + (1UL, 0UL, 3) + (1UL, 1UL, 4) ] + ) + + match Matrix.fromCoordinateList cooA, Matrix.fromCoordinateList cooB with + | Result.Ok a, Result.Ok b -> + match Matrix.kroneckerProduct a b (fun a b -> Some(a * b)) with + | Result.Ok res -> + let expected = + CoordinateList( + 2UL, + 2UL, + [ (0UL, 0UL, 3) + (0UL, 1UL, 6) + (1UL, 0UL, 9) + (1UL, 1UL, 12) ] + ) + + Assert.Equal(expected, Matrix.toCoordinateList res) + | Result.Error msg -> Assert.Fail(msg) + | _ -> Assert.Fail() + +[] +let ``kronecker dimension check`` () = + let cooA = + CoordinateList(3UL, 4UL, [ (0UL, 0UL, 1) ]) + + let cooB = + CoordinateList(2UL, 5UL, [ (0UL, 0UL, 1) ]) + + match Matrix.fromCoordinateList cooA, Matrix.fromCoordinateList cooB with + | Result.Ok a, Result.Ok b -> + match Matrix.kroneckerProduct a b (fun a b -> Some(a * b)) with + | Result.Ok res -> + Assert.Equal(6UL, res.nrows) + Assert.Equal(20UL, res.ncols) + | Result.Error msg -> Assert.Fail(msg) + | _ -> Assert.Fail() + +[] +let ``kronecker product with sparse matrix on dense matrix`` () = + let cooA = + CoordinateList(10UL, 10UL, [ (5UL, 5UL, 2) ]) + + let cooB = + CoordinateList( + 3UL, + 3UL, + [ (0UL, 0UL, 1) + (0UL, 1UL, 2) + (0UL, 2UL, 3) + (1UL, 0UL, 4) + (1UL, 1UL, 5) + (1UL, 2UL, 6) + (2UL, 0UL, 7) + (2UL, 1UL, 8) + (2UL, 2UL, 9) ] + ) + + match Matrix.fromCoordinateList cooA, Matrix.fromCoordinateList cooB with + | Result.Ok a, Result.Ok b -> + match Matrix.kroneckerProduct a b (fun a b -> Some(a * b)) with + | Result.Ok res -> + let expected = + CoordinateList( + 30UL, + 30UL, + [ (15UL, 15UL, 2) + (15UL, 16UL, 4) + (15UL, 17UL, 6) + (16UL, 15UL, 8) + (16UL, 16UL, 10) + (16UL, 17UL, 12) + (17UL, 15UL, 14) + (17UL, 16UL, 16) + (17UL, 17UL, 18) ] + ) + + Assert.Equal(expected, Matrix.toCoordinateList res) + | Result.Error msg -> Assert.Fail(msg) + | _ -> Assert.Fail() + +[] +let ``kronecker product with filtering (only even results)`` () = + let cooA = + CoordinateList( + 2UL, + 2UL, + [ (0UL, 0UL, 1) + (0UL, 1UL, 2) + (1UL, 0UL, 3) + (1UL, 1UL, 4) ] + ) + + let cooB = + CoordinateList( + 2UL, + 2UL, + [ (0UL, 0UL, 1) + (0UL, 1UL, 2) + (1UL, 0UL, 3) + (1UL, 1UL, 4) ] + ) + + match Matrix.fromCoordinateList cooA, Matrix.fromCoordinateList cooB with + | Result.Ok a, Result.Ok b -> + let evenOnly a b = + let prod = a * b in if prod % 2 = 0 then Some prod else None + + match Matrix.kroneckerProduct a b evenOnly with + | Result.Ok res -> + let coo = Matrix.toCoordinateList res + Assert.True(coo.list |> List.forall (fun (_, _, v) -> v % 2 = 0)) + Assert.True(List.length coo.list < 16) + | Result.Error msg -> Assert.Fail(msg) + | _ -> Assert.Fail() diff --git a/QuadTree.Tests/Tests.SSSP.fs b/QuadTree.Tests/Tests.SSSP.fs index ff28383..1dd69cf 100644 --- a/QuadTree.Tests/Tests.SSSP.fs +++ b/QuadTree.Tests/Tests.SSSP.fs @@ -71,34 +71,32 @@ let ``Simple SSSP.`` () = *) [] let ``SSSP with recalculation`` () = - let graph = - let clist = - Matrix.CoordinateList( - 5UL, - 5UL, - [ 0UL, 1UL, 1.0 - 1UL, 2UL, 1.0 - 2UL, 3UL, 1.0 - 2UL, 4UL, 4.0 - 3UL, 4UL, 2.0 - 0UL, 3UL, 6.0 ] - ) - - Matrix.fromCoordinateList clist + let clist = + Matrix.CoordinateList( + 5UL, + 5UL, + [ 0UL, 1UL, 1.0 + 1UL, 2UL, 1.0 + 2UL, 3UL, 1.0 + 2UL, 4UL, 4.0 + 3UL, 4UL, 2.0 + 0UL, 3UL, 6.0 ] + ) - let expected = - let clist = - Vector.CoordinateList( - 5UL, - [ (0UL, 0.0) - (1UL, 1.0) - (2UL, 2.0) - (3UL, 3.0) - (4UL, 5.0) ] - ) + let expectedClist = + Vector.CoordinateList( + 5UL, + [ (0UL, 0.0) + (1UL, 1.0) + (2UL, 2.0) + (3UL, 3.0) + (4UL, 5.0) ] + ) - Ok(Vector.fromCoordinateList clist) - - let actual = Graph.SSSP.sssp graph 0UL - - Assert.Equal(expected, actual) + match Matrix.fromCoordinateList clist, Vector.fromCoordinateList expectedClist with + | Ok graph, Ok expected -> + match Graph.SSSP.sssp graph 0UL with + | Ok actual -> Assert.Equal(expected, actual) + | Error msg -> Assert.Fail() + | Error msg, _ -> Assert.Fail() + | _, Error msg -> Assert.Fail() diff --git a/QuadTree.Tests/Tests.TriangleCount.fs b/QuadTree.Tests/Tests.TriangleCount.fs index 1b95cae..76e4969 100644 --- a/QuadTree.Tests/Tests.TriangleCount.fs +++ b/QuadTree.Tests/Tests.TriangleCount.fs @@ -52,12 +52,12 @@ let ``7V Triangle count`` () = let expected = 5UL - let actual = - match triangle_count g with - | Ok(Some x) -> x - | _ -> failwith "Unreachable" - - Assert.Equal(expected, actual) + match g with + | Ok graph -> + match triangle_count graph with + | Ok(Some actual) -> Assert.Equal(expected, actual) + | _ -> Assert.Fail() + | Error _ -> Assert.Fail() [] let ``5V Triangle count`` () = @@ -86,9 +86,9 @@ let ``5V Triangle count`` () = let expected = 2UL - let actual = - match triangle_count g with - | Ok(Some x) -> x - | _ -> failwith "Unreachable" - - Assert.Equal(expected, actual) + match g with + | Ok graph -> + match triangle_count graph with + | Ok(Some actual) -> Assert.Equal(expected, actual) + | _ -> Assert.Fail() + | Error _ -> Assert.Fail() diff --git a/QuadTree.Tests/Tests.Vector.fs b/QuadTree.Tests/Tests.Vector.fs index 4eb1af9..ad6ad17 100644 --- a/QuadTree.Tests/Tests.Vector.fs +++ b/QuadTree.Tests/Tests.Vector.fs @@ -1,6 +1,7 @@ module Vector.Tests open Xunit +open System open Vector open Common @@ -13,7 +14,6 @@ let printVector (vector: SparseVector<_>) = printfn " Size: %A" vector.storage.size printfn " Data: %A" vector.storage.data - [] let ``Simple Vector.map. Length is power of two.`` () = let v = @@ -78,30 +78,28 @@ let ``Simple Vector.map. Length is not power of two.`` () = [] let ``Simple Vector.mapi. Length is power of two, multiply by index.`` () = - let v = - Vector.fromCoordinateList ( - Vector.CoordinateList( - 8UL, - [ (0UL, 1) - (1UL, 1) - (2UL, 1) - (3UL, 1) - (4UL, 2) - (5UL, 2) - (6UL, 2) - (7UL, 2) ] - ) - ) - - let f (idx: uint64) x = - match x with - | Some(a) -> Some(a * int idx) - | _ -> None - - let expected = - Vector.fromCoordinateList ( - Vector.CoordinateList( - 8UL, + let dataLength = 8UL + + let d = + [ (0UL, 1) + (1UL, 1) + (2UL, 1) + (3UL, 1) + (4UL, 2) + (5UL, 2) + (6UL, 2) + (7UL, 2) ] + + match Vector.fromCoordinateList (Vector.CoordinateList(dataLength, d)) with + | Result.Error _ -> Assert.Fail() + | Result.Ok v -> + let f (idx: uint64) x = + match x with + | Some(a) -> Some(a * int idx) + | _ -> None + + let expectedResult = + let expectedData = [ (0UL, 0) (1UL, 1) (2UL, 2) @@ -110,52 +108,48 @@ let ``Simple Vector.mapi. Length is power of two, multiply by index.`` () = (5UL, 10) (6UL, 12) (7UL, 14) ] - ) - ) - let actual = Vector.mapi v f + Vector.fromCoordinateList (Vector.CoordinateList(dataLength, expectedData)) - Assert.Equal(expected, actual) + let actual = Vector.mapi v f + Assert.Equal(expectedResult, Ok actual) [] let ``Simple Vector.mapi. Length is not power of two.`` () = // Build vector [1, 1, 1, 1, 1, 1] with dummy at end - let v = - Vector.fromCoordinateList ( - Vector.CoordinateList( - 6UL, - [ (0UL, 1) - (1UL, 1) - (2UL, 1) - (3UL, 1) - (4UL, 1) - (5UL, 1) ] - ) - ) - - // f idx x = x * idx - let f (idx: uint64) x = - match x with - | Some(a) -> Some(a * int idx) - | _ -> None - - // Expected: [0, 1, 2, 3, 4, 5] (1*idx for each position) - let expected = - Vector.fromCoordinateList ( - Vector.CoordinateList( - 6UL, + let dataLength = 6UL + + let d = + [ (0UL, 1) + (1UL, 1) + (2UL, 1) + (3UL, 1) + (4UL, 1) + (5UL, 1) ] + + match Vector.fromCoordinateList (Vector.CoordinateList(dataLength, d)) with + | Result.Error _ -> Assert.Fail() + | Result.Ok v -> + // f idx x = x * idx + let f (idx: uint64) x = + match x with + | Some(a) -> Some(a * int idx) + | _ -> None + + // Expected: [0, 1, 2, 3, 4, 5] (1*idx for each position) + let expectedResult = + let expectedData = [ (0UL, 0) (1UL, 1) (2UL, 2) (3UL, 3) (4UL, 4) (5UL, 5) ] - ) - ) - let actual = Vector.mapi v f + Vector.fromCoordinateList (Vector.CoordinateList(dataLength, expectedData)) - Assert.Equal(expected, actual) + let actual = Vector.mapi v f + Assert.Equal(expectedResult, Ok actual) [] let ``Simple Vector.mapi. Uniform leaf expansion.`` () = @@ -182,21 +176,22 @@ let ``Simple Vector.mapi. Uniform leaf expansion.`` () = [] let ``Simple Vector.mapi. All indices identity.`` () = // Vector with values matching their indices - let v = - Vector.fromCoordinateList (Vector.CoordinateList(4UL, [ (0UL, 0); (2UL, 2) ])) - - let f (idx: uint64) x = - match x with - | Some(a) when a = int idx -> Some a - | _ -> None - - let actual = Vector.mapi v f + let dataLength = 4UL + let d = [ (0UL, 0); (2UL, 2) ] - let expected = - Vector.fromCoordinateList (Vector.CoordinateList(4UL, [ (0UL, 0); (2UL, 2) ])) + match Vector.fromCoordinateList (Vector.CoordinateList(dataLength, d)) with + | Result.Error _ -> Assert.Fail() + | Result.Ok v -> + let f (idx: uint64) x = + match x with + | Some(a) when a = int idx -> Some a + | _ -> None - Assert.Equal(expected, actual) + let expectedResult = + Vector.fromCoordinateList (Vector.CoordinateList(dataLength, d)) + let actual = Vector.mapi v f + Assert.Equal(expectedResult, Ok actual) [] let ``Simple Vector.map2. Length is power of two.`` () = @@ -282,117 +277,113 @@ let ``Simple Vector.map2. Length is not power of two.`` () = [] let ``Simple Vector.map2i. Length is power of two.`` () = - let v1 = - Vector.fromCoordinateList ( - Vector.CoordinateList( - 4UL, - [ (0UL, 1); (1UL, 2); (2UL, 3); (3UL, 4) ] - ) - ) - - let v2 = - Vector.fromCoordinateList ( - Vector.CoordinateList( - 4UL, - [ (0UL, 10); (1UL, 20); (2UL, 30); (3UL, 40) ] - ) - ) + let dataLength = 4UL + let d1 = [ (0UL, 1); (1UL, 2); (2UL, 3); (3UL, 4) ] + let d2 = [ (0UL, 10); (1UL, 20); (2UL, 30); (3UL, 40) ] + + match + Vector.fromCoordinateList (Vector.CoordinateList(dataLength, d1)), + Vector.fromCoordinateList (Vector.CoordinateList(dataLength, d2)) + with + | Result.Ok v1, Result.Ok v2 -> + let f idx x y = + match (x, y) with + | Some(a), Some(b) -> Some(a + b + int idx) + | _ -> None - let f idx x y = - match (x, y) with - | Some(a), Some(b) -> Some(a + b + int idx) - | _ -> None + let actualResult = Vector.map2i v1 v2 f - let expected = - Vector.fromCoordinateList ( - Vector.CoordinateList( - 4UL, - [ (0UL, 11); (1UL, 23); (2UL, 35); (3UL, 47) ] + let expectedResult = + Vector.fromCoordinateList ( + Vector.CoordinateList( + dataLength, + [ (0UL, 11); (1UL, 23); (2UL, 35); (3UL, 47) ] + ) ) - ) - |> Ok - let actual = Vector.map2i v1 v2 f - - Assert.Equal(expected, actual) + match actualResult, expectedResult with + | Result.Ok actual, Result.Ok expected -> Assert.Equal(expected, actual) + | _ -> Assert.Fail() + | _ -> Assert.Fail() [] let ``Simple Vector.map2i. Length is not power of two.`` () = - let v1 = - Vector.fromCoordinateList ( - Vector.CoordinateList( - 6UL, - [ (0UL, 1) - (1UL, 2) - (2UL, 3) - (3UL, 4) - (4UL, 5) - (5UL, 6) ] - ) - ) - - let v2 = - Vector.fromCoordinateList ( - Vector.CoordinateList( - 6UL, - [ (0UL, 10) - (1UL, 10) - (2UL, 10) - (3UL, 10) - (4UL, 10) - (5UL, 10) ] - ) - ) - - let f idx x y = - match (x, y) with - | Some(a), Some(b) -> Some(a * int idx + b) - | _ -> None - - let expected = - Vector.fromCoordinateList ( - Vector.CoordinateList( - 6UL, - [ (0UL, 10) - (1UL, 12) - (2UL, 16) - (3UL, 22) - (4UL, 30) - (5UL, 40) ] + let dataLength = 6UL + + let d1 = + [ (0UL, 1) + (1UL, 2) + (2UL, 3) + (3UL, 4) + (4UL, 5) + (5UL, 6) ] + + let d2 = + [ (0UL, 10) + (1UL, 10) + (2UL, 10) + (3UL, 10) + (4UL, 10) + (5UL, 10) ] + + match + Vector.fromCoordinateList (Vector.CoordinateList(dataLength, d1)), + Vector.fromCoordinateList (Vector.CoordinateList(dataLength, d2)) + with + | Result.Ok v1, Result.Ok v2 -> + let f idx x y = + match (x, y) with + | Some(a), Some(b) -> Some(a * int idx + b) + | _ -> None + + let actualResult = Vector.map2i v1 v2 f + + let expectedResult = + Vector.fromCoordinateList ( + Vector.CoordinateList( + dataLength, + [ (0UL, 10) + (1UL, 12) + (2UL, 16) + (3UL, 22) + (4UL, 30) + (5UL, 40) ] + ) ) - ) - |> Ok - let actual = Vector.map2i v1 v2 f - - Assert.Equal(expected, actual) + match actualResult, expectedResult with + | Result.Ok actual, Result.Ok expected -> Assert.Equal(expected, actual) + | _ -> Assert.Fail() + | _ -> Assert.Fail() [] let ``Simple Vector.map2i. Mixed values.`` () = - let v1 = - Vector.fromCoordinateList (Vector.CoordinateList(4UL, [ (0UL, 1); (2UL, 3) ])) - - let v2 = - Vector.fromCoordinateList (Vector.CoordinateList(4UL, [ (1UL, 10); (3UL, 30) ])) - - let f idx x y = - match (x, y) with - | Some(a), Some(b) -> Some(a + b) - | Some(a), None -> Some(int idx + a * 2) - | None, Some(b) -> Some(int idx * b * 3) - | _ -> None + let d1 = [ (0UL, 1); (2UL, 3) ] + let d2 = [ (1UL, 10); (3UL, 30) ] + let dataLength = 4UL + + match + Vector.fromCoordinateList (Vector.CoordinateList(dataLength, d1)), + Vector.fromCoordinateList (Vector.CoordinateList(dataLength, d2)) + with + | Result.Ok v1, Result.Ok v2 -> + let f idx x y = + match (x, y) with + | Some(a), Some(b) -> Some(a + b) + | Some(a), None -> Some(int idx + a * 2) + | None, Some(b) -> Some(int idx * b * 3) + | _ -> None - let actual = Vector.map2i v1 v2 f + let actualResult = Vector.map2i v1 v2 f - let expected = - Vector.CoordinateList( - 4UL, - [ (0UL, 2); (1UL, 30); (2UL, 8); (3UL, 270) ] - ) - |> Vector.fromCoordinateList - |> Ok + let expectedResult = + Vector.CoordinateList(dataLength, [ (0UL, 2); (1UL, 30); (2UL, 8); (3UL, 270) ]) + |> Vector.fromCoordinateList - Assert.Equal(expected, actual) + match actualResult, expectedResult with + | Result.Ok actual, Result.Ok expected -> Assert.Equal(expected, actual) + | _ -> Assert.Fail() + | _ -> Assert.Fail() [] let ``Simple Vector.map2Values.`` () = @@ -411,19 +402,19 @@ let ``Simple Vector.map2Values.`` () = SparseVector(4UL, 2UL, store) let f a b = Some(a + b) + let actualResult = Vector.map2Values v1 v2 f - let actual = Vector.map2Values v1 v2 f - - let expected = + let expectedResult = Vector.fromCoordinateList ( Vector.CoordinateList( 4UL, [ (0UL, 11); (1UL, 11); (2UL, 22); (3UL, 22) ] ) ) - |> Ok - Assert.Equal(expected, actual) + match actualResult, expectedResult with + | Result.Ok actual, Result.Ok expected -> Assert.Equal(expected, actual) + | _ -> Assert.Fail() [] let ``Simple Vector.map2AllCells.`` () = @@ -495,138 +486,138 @@ let ``Simple Vector.map2LeftValues.`` () = let actual = Vector.map2LeftValues v1 v2 f Assert.Equal(expected, actual) - [] let ``Vector.map2Values compressed`` () = let dataLength = 5UL - let v1 = - let data = - [ 0UL, 1; 1UL, 1; 2UL, 1; 3UL, 1; 4UL, 1 ] - - CoordinateList(dataLength, data) |> Vector.fromCoordinateList - - let v2 = - let data = - [ 0UL, 2; 1UL, 3; 2UL, 4; 3UL, 5; 4UL, 6 ] - - CoordinateList(dataLength, data) |> Vector.fromCoordinateList + let d1 = + [ 0UL, 1; 1UL, 1; 2UL, 1; 3UL, 1; 4UL, 1 ] - let f a b = Some(a + b) + let d2 = + [ 0UL, 2; 1UL, 3; 2UL, 4; 3UL, 5; 4UL, 6 ] - let expected = - let data = - [ 0UL, 3; 1UL, 4; 2UL, 5; 3UL, 6; 4UL, 7 ] + match + Vector.fromCoordinateList (CoordinateList(dataLength, d1)), + Vector.fromCoordinateList (CoordinateList(dataLength, d2)) + with + | Result.Ok v1, Result.Ok v2 -> + let f a b = Some(a + b) + let actual = Vector.map2Values v1 v2 f - CoordinateList(dataLength, data) |> Vector.fromCoordinateList |> Ok + let expectedResult = + let data = + [ 0UL, 3; 1UL, 4; 2UL, 5; 3UL, 6; 4UL, 7 ] - let actual = Vector.map2Values v1 v2 f - Assert.Equal(expected, actual) + Vector.fromCoordinateList (CoordinateList(dataLength, data)) + match actual, expectedResult with + | Result.Ok actual, Result.Ok expected -> Assert.Equal(expected, actual) + | _ -> Assert.Fail() + | _ -> Assert.Fail() [] let ``Vector.map2Values compressed to None`` () = let dataLength = 5UL - let v1 = - let data = - [ 0UL, 1; 1UL, 1; 2UL, 1; 3UL, 1; 4UL, 1 ] - - CoordinateList(dataLength, data) |> Vector.fromCoordinateList - - let v2 = - let data = - [ 0UL, 2; 1UL, 3; 2UL, 4; 3UL, 5; 4UL, 6 ] - - CoordinateList(dataLength, data) |> Vector.fromCoordinateList + let d1 = + [ 0UL, 1; 1UL, 1; 2UL, 1; 3UL, 1; 4UL, 1 ] - let f a b = None + let d2 = + [ 0UL, 2; 1UL, 3; 2UL, 4; 3UL, 5; 4UL, 6 ] - let expected = Vector.empty dataLength |> Ok + match + Vector.fromCoordinateList (CoordinateList(dataLength, d1)), + Vector.fromCoordinateList (CoordinateList(dataLength, d2)) + with + | Result.Ok v1, Result.Ok v2 -> + let f a b = None + let actual = Vector.map2Values v1 v2 f - let actual = Vector.map2Values v1 v2 f - Assert.Equal(expected, actual) + let expected = Ok(Vector.empty dataLength) + Assert.Equal(expected, actual) + | _ -> Assert.Fail() [] let ``Vector.map2Values compressed both`` () = let dataLength = 5UL - let v1 = - let data = - [ 0UL, 1; 1UL, 1; 2UL, 1; 3UL, 1; 4UL, 1 ] - - CoordinateList(dataLength, data) |> Vector.fromCoordinateList - - let v2 = - let data = - [ 0UL, 2; 1UL, 2; 2UL, 2; 3UL, 2; 4UL, 2 ] + let d1 = + [ 0UL, 1; 1UL, 1; 2UL, 1; 3UL, 1; 4UL, 1 ] - CoordinateList(dataLength, data) |> Vector.fromCoordinateList + let d2 = + [ 0UL, 2; 1UL, 2; 2UL, 2; 3UL, 2; 4UL, 2 ] - let f a b = Some(a + b) + match + Vector.fromCoordinateList (CoordinateList(dataLength, d1)), + Vector.fromCoordinateList (CoordinateList(dataLength, d2)) + with + | Result.Ok v1, Result.Ok v2 -> + let f a b = Some(a + b) + let actual = Vector.map2Values v1 v2 f - let expected = - let data = - [ 0UL, 3; 1UL, 3; 2UL, 3; 3UL, 3; 4UL, 3 ] + let expectedResult = + let data = + [ 0UL, 3; 1UL, 3; 2UL, 3; 3UL, 3; 4UL, 3 ] - CoordinateList(dataLength, data) |> Vector.fromCoordinateList |> Ok + Vector.fromCoordinateList (CoordinateList(dataLength, data)) - let actual = Vector.map2Values v1 v2 f - Assert.Equal(expected, actual) + match actual, expectedResult with + | Result.Ok actual, Result.Ok expected -> Assert.Equal(expected, actual) + | _ -> Assert.Fail() + | _ -> Assert.Fail() [] let ``Vector.map2Values compressed both indexed`` () = let dataLength = 5UL - let v1 = - let data = - [ 0UL, 1; 1UL, 1; 2UL, 1; 3UL, 1; 4UL, 1 ] + let d1 = + [ 0UL, 1; 1UL, 1; 2UL, 1; 3UL, 1; 4UL, 1 ] - CoordinateList(dataLength, data) |> Vector.fromCoordinateList + let d2 = + [ 0UL, 2; 1UL, 2; 2UL, 2; 3UL, 2; 4UL, 2 ] - let v2 = - let data = - [ 0UL, 2; 1UL, 2; 2UL, 2; 3UL, 2; 4UL, 2 ] + match + Vector.fromCoordinateList (CoordinateList(dataLength, d1)), + Vector.fromCoordinateList (CoordinateList(dataLength, d2)) + with + | Result.Ok v1, Result.Ok v2 -> + let f i a b = Some(int i + a + b) + let actual = Vector.map2iValues v1 v2 f - CoordinateList(dataLength, data) |> Vector.fromCoordinateList + let expectedResult = + let data = + [ 0UL, 3; 1UL, 4; 2UL, 5; 3UL, 6; 4UL, 7 ] - let f i a b = Some(int i + a + b) - - let expected = - let data = - [ 0UL, 3; 1UL, 4; 2UL, 5; 3UL, 6; 4UL, 7 ] - - CoordinateList(dataLength, data) |> Vector.fromCoordinateList |> Ok - - let actual = Vector.map2iValues v1 v2 f - - Assert.Equal(expected, actual) + Vector.fromCoordinateList (CoordinateList(dataLength, data)) + match actual, expectedResult with + | Result.Ok actual, Result.Ok expected -> Assert.Equal(expected, actual) + | _ -> Assert.Fail() + | _ -> Assert.Fail() [] let ``Vector.map2Values compressed both to None`` () = let dataLength = 5UL - let v1 = - let data = - [ 0UL, 1; 1UL, 1; 2UL, 1; 3UL, 1; 4UL, 1 ] - - CoordinateList(dataLength, data) |> Vector.fromCoordinateList + let d1 = + [ 0UL, 1; 1UL, 1; 2UL, 1; 3UL, 1; 4UL, 1 ] - let v2 = - let data = - [ 0UL, 2; 1UL, 2; 2UL, 2; 3UL, 2; 4UL, 2 ] - - CoordinateList(dataLength, data) |> Vector.fromCoordinateList + let d2 = + [ 0UL, 2; 1UL, 2; 2UL, 2; 3UL, 2; 4UL, 2 ] - let f a b = None + match + Vector.fromCoordinateList (CoordinateList(dataLength, d1)), + Vector.fromCoordinateList (CoordinateList(dataLength, d2)) + with + | Result.Ok v1, Result.Ok v2 -> + let f a b = None + let actual = Vector.map2Values v1 v2 f - let expected = Vector.empty dataLength |> Ok - - let actual = Vector.map2Values v1 v2 f - Assert.Equal(expected, actual) + let expected = Ok(Vector.empty dataLength) + Assert.Equal(expected, actual) + | _ -> Assert.Fail() [] let ``Vector.map2Values with None returns None`` () = @@ -704,8 +695,6 @@ let ``Vector.map2AtLeastOne Right case`` () = [] let ``Conversion identity`` () = - let id = toCoordinateList << fromCoordinateList - let dataLength = 10UL let data = @@ -713,10 +702,11 @@ let ``Conversion identity`` () = let coordinates = CoordinateList(dataLength, data) - let expected = coordinates - let actual = id coordinates - - Assert.Equal(expected, actual) + match fromCoordinateList coordinates with + | Result.Error msg -> Assert.Fail() + | Result.Ok vec -> + let actual = toCoordinateList vec + Assert.Equal(coordinates, actual) [] let ``Simple addition`` () = @@ -729,97 +719,106 @@ let ``Simple addition`` () = let expectedList = [ 0UL, 5; 8UL, 1; 9UL, 1 ] CoordinateList(dataLength, expectedList) - let actual = - let c1 = CoordinateList(dataLength, d1) - let c2 = CoordinateList(dataLength, d2) - let v1 = fromCoordinateList c1 - let v2 = fromCoordinateList c2 + match fromCoordinateList (CoordinateList(dataLength, d1)) with + | Result.Error msg -> Assert.Fail() + | Result.Ok v1 -> + match fromCoordinateList (CoordinateList(dataLength, d2)) with + | Result.Error msg -> Assert.Fail() + | Result.Ok v2 -> + let addition o1 o2 = + match o1, o2 with + | Some x, Some y -> Some(x + y) + | Some x, None + | None, Some x -> Some x + | None, None -> None - let addition o1 o2 = - match o1, o2 with - | Some x, Some y -> Some(x + y) - | Some x, None - | None, Some x -> Some x - | None, None -> None - - let result = match map2 v1 v2 addition with - | Ok x -> x - | _ -> failwith "Unreachable" - - toCoordinateList result - - Assert.Equal(expected, actual) + | Ok resultVector -> + let actual = toCoordinateList resultVector + Assert.Equal(expected, actual) + | Error msg -> Assert.Fail() [] let ``Condensation of empty`` () = let clist = CoordinateList(10UL, []) - let actual = fromCoordinateList clist - - // 16 elements total None and Dummy: NNNNNNNN | NN DD | DDDD - let tree = - - btree.Node( - btree.Leaf <| UserValue None, - btree.Node(btree.Node(btree.Leaf <| UserValue None, btree.Leaf Dummy), btree.Leaf Dummy) - ) + match fromCoordinateList clist with + | Result.Error msg -> Assert.Fail() + | Result.Ok actual -> + // 16 elements total None and Dummy: NNNNNNNN | NN DD | DDDD + let tree = + btree.Node( + btree.Leaf <| UserValue None, + btree.Node(btree.Node(btree.Leaf <| UserValue None, btree.Leaf Dummy), btree.Leaf Dummy) + ) - let expected = - SparseVector(clist.length, 0UL, Storage(16UL, tree)) - - Assert.Equal(expected, actual) + let expectedResult = + SparseVector(clist.length, 0UL, Storage(16UL, tree)) + Assert.Equal(Ok expectedResult, Ok actual) [] let ``Gather`` () = - let data = - Vector.CoordinateList(5UL, [ (0UL, 0.0); (1UL, 1.0); (4UL, 5.0) ]) - |> Vector.fromCoordinateList - - let indices = - Vector.CoordinateList( - 5UL, - [ (0UL, 1UL); (1UL, 4UL); (3UL, 1UL) ] + match + Vector.fromCoordinateList ( + Vector.CoordinateList(5UL, [ (0UL, 0.0); (1UL, 1.0); (4UL, 5.0) ]) ) - |> Vector.fromCoordinateList - - let actual = Vector.gather data indices - - let expected = - Vector.CoordinateList(5UL, [ (0UL, 1.0); (1UL, 5.0); (3UL, 1.0) ]) - |> Vector.fromCoordinateList - - Assert.Equal(expected, actual) + with + | Error _ -> Assert.Fail() + | Ok data -> + match + Vector.fromCoordinateList ( + Vector.CoordinateList( + 5UL, + [ (0UL, 1UL); (1UL, 4UL); (3UL, 1UL) ] + ) + ) + with + | Error _ -> Assert.Fail() + | Ok indices -> + let actual = Vector.gather data indices + + match + Vector.fromCoordinateList ( + Vector.CoordinateList(5UL, [ (0UL, 1.0); (1UL, 5.0); (3UL, 1.0) ]) + ) + with + | Error _ -> Assert.Fail() + | Ok expected -> Assert.Equal(expected, actual) [] let ``Scatter`` () = let data = - Vector.CoordinateList(5UL, [ (0UL, 4.0); (2UL, 5.0) ]) - |> Vector.fromCoordinateList + Vector.fromCoordinateList (Vector.CoordinateList(5UL, [ (0UL, 4.0); (2UL, 5.0) ])) let indices = - Vector.CoordinateList(5UL, [ (0UL, 3UL); (2UL, 3UL) ]) - |> Vector.fromCoordinateList + Vector.fromCoordinateList ( + Vector.CoordinateList(5UL, [ (0UL, 3UL); (2UL, 3UL) ]) + ) let result = - Vector.CoordinateList(5UL, [ (3UL, 1.0); (4UL, 3.0) ]) - |> Vector.fromCoordinateList - - let actual = - Vector.scatter result data indices (fun x y -> - match (x, y) with - | Some x, Some y -> Some(x + y) - | Some x, _ - | _, Some x -> Some x - | _ -> None) - - let expected = - Vector.CoordinateList(5UL, [ (3UL, 10.0); (4UL, 3.0) ]) - |> Vector.fromCoordinateList - |> Result.Ok - - Assert.Equal(expected, actual) + Vector.fromCoordinateList (Vector.CoordinateList(5UL, [ (3UL, 1.0); (4UL, 3.0) ])) + + match data, indices, result with + | Ok dataOk, Ok indicesOk, Ok resultOk -> + let actual = + Vector.scatter resultOk dataOk indicesOk (fun x y -> + match (x, y) with + | Some x, Some y -> Some(x + y) + | Some x, _ + | _, Some x -> Some x + | _ -> None) + + let expected = + Vector.CoordinateList(5UL, [ (3UL, 10.0); (4UL, 3.0) ]) + |> Vector.fromCoordinateList + + match expected with + | Ok expectedOk -> Assert.Equal(Ok expectedOk, actual) + | Error msg -> Assert.Fail() + | Error msg, _, _ -> Assert.Fail() + | _, Error msg, _ -> Assert.Fail() + | _, _, Error msg -> Assert.Fail() let compare x y = match (x, y) with @@ -837,8 +836,11 @@ let ``Sort one element vector`` () = Vector.CoordinateList(1UL, [ (0UL, 0.0) ]) |> Vector.fromCoordinateList - let actual = Vector.mergeSort data compare - Assert.Equal(data, actual) + match data with + | Result.Error msg -> Assert.Fail() + | Result.Ok vec -> + let actual = Vector.mergeSort vec compare + Assert.Equal(data, Ok actual) [] let ``Sort vector of two equal elements`` () = @@ -846,8 +848,11 @@ let ``Sort vector of two equal elements`` () = Vector.CoordinateList(2UL, [ (0UL, 0.0); (1UL, 0.0) ]) |> Vector.fromCoordinateList - let actual = Vector.mergeSort data compare - Assert.Equal(data, actual) + match data with + | Result.Error msg -> Assert.Fail() + | Result.Ok vec -> + let actual = Vector.mergeSort vec compare + Assert.Equal(data, Ok actual) [] let ``Sort vector of three equal elements`` () = @@ -855,9 +860,11 @@ let ``Sort vector of three equal elements`` () = Vector.CoordinateList(3UL, [ (0UL, 2.0); (1UL, 2.0); (2UL, 2.0) ]) |> Vector.fromCoordinateList - let actual = Vector.mergeSort data compare - Assert.Equal(data, actual) - + match data with + | Result.Error msg -> Assert.Fail() + | Result.Ok vec -> + let actual = Vector.mergeSort vec compare + Assert.Equal(data, Ok actual) [] let ``Sort vector of three different unordered elements`` () = @@ -869,10 +876,11 @@ let ``Sort vector of three different unordered elements`` () = Vector.CoordinateList(3UL, [ (0UL, 1.0); (1UL, 2.0); (2UL, 4.0) ]) |> Vector.fromCoordinateList - let actual = Vector.mergeSort data compare - Assert.Equal(expected, actual) - - + match data with + | Result.Error msg -> Assert.Fail() + | Result.Ok vec -> + let actual = Vector.mergeSort vec compare + Assert.Equal(expected, Ok actual) [] let ``Sort long vector with one element`` () = @@ -880,10 +888,11 @@ let ``Sort long vector with one element`` () = Vector.CoordinateList(5UL, [ (0UL, 0.0) ]) |> Vector.fromCoordinateList - let actual = Vector.mergeSort data compare - Assert.Equal(data, actual) - - + match data with + | Result.Error msg -> Assert.Fail() + | Result.Ok vec -> + let actual = Vector.mergeSort vec compare + Assert.Equal(data, Ok actual) [] let ``Sort sorted vector`` () = @@ -891,9 +900,11 @@ let ``Sort sorted vector`` () = Vector.CoordinateList(5UL, [ (0UL, 0.0); (1UL, 0.0) ]) |> Vector.fromCoordinateList - let actual = Vector.mergeSort data compare - Assert.Equal(data, actual) - + match data with + | Result.Error msg -> Assert.Fail() + | Result.Ok vec -> + let actual = Vector.mergeSort vec compare + Assert.Equal(data, Ok actual) [] let ``Init vector`` () = @@ -902,4 +913,276 @@ let ``Init vector`` () = |> Vector.fromCoordinateList let actual = Vector.init 3UL (fun i -> Some(int i)) - Assert.Equal(expected, actual) + Assert.Equal(expected, Ok actual) + +[] +let ``map on empty vector returns empty vector`` () = + match fromCoordinateList (CoordinateList(0UL, [])) with + | Result.Error msg -> Assert.Fail() + | Result.Ok vec -> + let result = + map vec (fun (x: int option) -> + match x with + | Some v -> Some(v * 3) + | None -> None) + + let coo = toCoordinateList result + Assert.Equal(CoordinateList(0UL, []), coo) + +[] +let ``map with function that turns all the elements into zeros`` () = + match + fromCoordinateList (CoordinateList(7UL, [ (1UL, 3); (2UL, 3); (5UL, 100) ])) + with + | Result.Error msg -> Assert.Fail() + | Result.Ok vec -> + let result = map vec (fun _ -> Some 0) + let coo = toCoordinateList result + + Assert.Equal( + CoordinateList( + 7UL, + [ (0UL, 0) + (1UL, 0) + (2UL, 0) + (3UL, 0) + (4UL, 0) + (5UL, 0) + (6UL, 0) ] + ), + coo + ) + +[] +let ``map with function that resets all the elements`` () = + match + fromCoordinateList (CoordinateList(7UL, [ (1UL, 3); (2UL, 3); (5UL, 100) ])) + with + | Result.Error msg -> Assert.Fail() + | Result.Ok vec -> + let result = map vec (fun _ -> None) + let coo = toCoordinateList result + Assert.Equal(CoordinateList(7UL, []), coo) + +[] +let ``map can change type from int to string`` () = + match fromCoordinateList (CoordinateList(3UL, [ (1UL, 11); (2UL, 33) ])) with + | Result.Error msg -> Assert.Fail() + | Result.Ok vec -> + let result = + map vec (fun (x: int option) -> + match x with + | Some v -> Some(sprintf "str %d" v) + | None -> None) + + let coo = toCoordinateList result + Assert.Equal(CoordinateList(3UL, [ (1UL, "str 11"); (2UL, "str 33") ]), coo) + +[] +let ``fromCoordinateList with out-of-range index returns error`` () = + let coo = CoordinateList(6UL, [ (9UL, 8) ]) + let expected = Error "Index out of range" + Assert.Equal(expected, fromCoordinateList coo) + +[] +let ``fromCoordinateList with zero size returns error`` () = + let coo = CoordinateList(0UL, [ (33UL, 33); (39UL, 1) ]) + let expected = Error "Index out of range" + Assert.Equal(expected, fromCoordinateList coo) + +[] +let ``fromCoordinateList with unsorted coordinates works correctly`` () = + let coo = + CoordinateList(7UL, [ (5UL, 3); (3UL, 2); (1UL, 100) ]) + + match fromCoordinateList coo with + | Result.Error msg -> Assert.Fail() + | Result.Ok vec -> + let result = toCoordinateList vec + Assert.Equal(CoordinateList(7UL, [ (1UL, 100); (3UL, 2); (5UL, 3) ]), result) + +[] +let ``fromCoordinateList with duplicate indicies returns the last of them`` () = + let coo = CoordinateList(3UL, [ (1UL, 33); (1UL, 100) ]) + + match Vector.fromCoordinateList coo with + | Result.Error msg -> Assert.Fail() + | Result.Ok vec -> + let result = Vector.toCoordinateList vec + Assert.Equal(CoordinateList(3UL, [ (1UL, 100) ]), result) + +[] +let ``mapi works with index`` () = + match fromCoordinateList (CoordinateList(5UL, [ (2UL, 10); (3UL, 23) ])) with + | Result.Error msg -> Assert.Fail() + | Result.Ok vec -> + let result = + mapi vec (fun (i: uint64) (x: int option) -> + match x with + | Some v -> Some(int i + v) + | None -> None) + + let coo = toCoordinateList result + Assert.Equal(CoordinateList(5UL, [ (2UL, 12); (3UL, 26) ]), coo) + +[] +let ``mapi on empty vector returns empty vector`` () = + match fromCoordinateList (CoordinateList(0UL, [])) with + | Result.Error msg -> Assert.Fail() + | Result.Ok vec -> + let result = + mapi vec (fun (i: uint64) (x: int option) -> + match x with + | Some v -> Some(int i + v) + | None -> None) + + let coo = toCoordinateList result + Assert.Equal(CoordinateList(0UL, []), coo) + +[] +let ``mapi with special function returns empty vector`` () = + match fromCoordinateList (CoordinateList(5UL, [ (1UL, 33); (3UL, 88) ])) with + | Result.Error msg -> Assert.Fail() + | Result.Ok vec -> + let result = + mapi vec (fun (i: uint64) (x: int option) -> + match x with + | Some v -> + match int i % 2 with + | 0 -> Some(v * 2) + | _ -> None + | None -> None) + + let coo = toCoordinateList result + Assert.Equal(CoordinateList(5UL, []), coo) + +[] +let ``mapi works with function does not depend on the index`` () = + match fromCoordinateList (CoordinateList(5UL, [ (2UL, 10); (3UL, 23) ])) with + | Result.Error msg -> Assert.Fail() + | Result.Ok vec -> + let result = + mapi vec (fun (i: uint64) (x: int option) -> + match x with + | Some v -> Some(v + 4) + | None -> None) + + let coo = toCoordinateList result + Assert.Equal(CoordinateList(5UL, [ (2UL, 14); (3UL, 27) ]), coo) + +[] +let ``slice returns error when start is negative`` () = + match fromCoordinateList (CoordinateList(5UL, [ (2UL, 10) ])) with + | Result.Error msg -> Assert.Fail() + | Result.Ok vec -> + match slice -1 3 vec with + | Result.Ok _ -> Assert.Fail("Expected Error") + | Result.Error msg -> Assert.Equal("Start should be >= 0", msg) + +[] +let ``slice returns error when end is negative`` () = + match fromCoordinateList (CoordinateList(5UL, [ (2UL, 10) ])) with + | Result.Error msg -> Assert.Fail() + | Result.Ok vec -> + match slice 1 -3 vec with + | Result.Ok _ -> Assert.Fail("Expected Error") + | Result.Error msg -> Assert.Equal("End should be >= 0", msg) + +[] +let ``slice returns error when start is out of range`` () = + match fromCoordinateList (CoordinateList(5UL, [ (2UL, 10) ])) with + | Result.Error msg -> Assert.Fail() + | Result.Ok vec -> + match slice 7 3 vec with + | Result.Ok _ -> Assert.Fail("Expected Error") + | Result.Error msg -> Assert.Equal("Start is out of Vector length", msg) + +[] +let ``slice returns error when end is out of range`` () = + match fromCoordinateList (CoordinateList(5UL, [ (2UL, 10) ])) with + | Result.Error msg -> Assert.Fail() + | Result.Ok vec -> + match slice 3 7 vec with + | Result.Ok _ -> Assert.Fail("Expected Error") + | Result.Error msg -> Assert.Equal("End is out of Vector length", msg) + +[] +let ``slice returns error when end is less than start`` () = + match fromCoordinateList (CoordinateList(5UL, [ (2UL, 10) ])) with + | Result.Error msg -> Assert.Fail() + | Result.Ok vec -> + match slice 4 3 vec with + | Result.Ok _ -> Assert.Fail("Expected Error") + | Result.Error msg -> Assert.Equal("End should be >= Start", msg) + +[] +let ``slice returns correct subvector`` () = + match fromCoordinateList (CoordinateList(7UL, [ (2UL, 10); (6UL, 20) ])) with + | Result.Error msg -> Assert.Fail() + | Result.Ok vec -> + match slice 1 3 vec with + | Result.Ok result -> + let coo = Vector.toCoordinateList result + Assert.Equal(CoordinateList(3UL, [ (1UL, 10) ]), coo) + | Result.Error msg -> Assert.Fail(msg) + +[] +let ``slice returns subvector without elements`` () = + match fromCoordinateList (CoordinateList(7UL, [ (5UL, 10); (6UL, 20) ])) with + | Result.Error msg -> Assert.Fail() + | Result.Ok vec -> + match slice 0 2 vec with + | Result.Ok result -> + let coo = toCoordinateList result + Assert.Equal(CoordinateList(3UL, []), coo) + | Result.Error msg -> Assert.Fail(msg) + +[] +let ``slice returns single subvector`` () = + match fromCoordinateList (CoordinateList(7UL, [ (2UL, 10); (6UL, 20) ])) with + | Result.Error msg -> Assert.Fail() + | Result.Ok vec -> + match slice 2 2 vec with + | Result.Ok result -> + let coo = toCoordinateList result + Assert.Equal(CoordinateList(1UL, [ (0UL, 10) ]), coo) + | Result.Error msg -> Assert.Fail(msg) + +[] +let ``slice returns correct subvector equals to vector`` () = + match + fromCoordinateList (CoordinateList(7UL, [ (2UL, 10); (3UL, 33); (6UL, 20) ])) + with + | Result.Error msg -> Assert.Fail() + | Result.Ok vec -> + match slice 0 6 vec with + | Result.Ok result -> + let coo = toCoordinateList result + Assert.Equal(CoordinateList(7UL, [ (2UL, 10); (3UL, 33); (6UL, 20) ]), coo) + | Result.Error msg -> Assert.Fail(msg) + +[] +let ``slice returns correct subvector when start of subvector equals to start of vector`` () = + match + fromCoordinateList (CoordinateList(7UL, [ (0UL, 10); (3UL, 33); (6UL, 20) ])) + with + | Result.Error msg -> Assert.Fail() + | Result.Ok vec -> + match slice 0 4 vec with + | Result.Ok result -> + let coo = Vector.toCoordinateList result + Assert.Equal(CoordinateList(5UL, [ (0UL, 10); (3UL, 33) ]), coo) + | Result.Error msg -> Assert.Fail(msg) + +[] +let ``slice returns correct subvector when end of subvector equals to end of vector`` () = + match + fromCoordinateList (CoordinateList(7UL, [ (0UL, 10); (3UL, 33); (6UL, 20) ])) + with + | Result.Error msg -> Assert.Fail() + | Result.Ok vec -> + match slice 2 6 vec with + | Result.Ok result -> + let coo = Vector.toCoordinateList result + Assert.Equal(CoordinateList(5UL, [ (1UL, 33); (4UL, 20) ]), coo) + | Result.Error msg -> Assert.Fail(msg) diff --git a/QuadTree/Boruvka.fs b/QuadTree/Boruvka.fs index c0ed119..79f1dee 100644 --- a/QuadTree/Boruvka.fs +++ b/QuadTree/Boruvka.fs @@ -16,7 +16,13 @@ type Error = let mst (graph: Matrix.SparseMatrix<_>) = - let op_mult (i, x) (row, col, w) = Some(w, row) + // Canonical representation of an undirected edge: + // (weight, min endpoint, max endpoint). + // This is important for deterministic tie-breaking. + let op_mult (_i, _x) (row, col, w) = + let r = uint64 row + let c = uint64 col + Some(w, min r c, max r c) let op_min x y = match x, y with @@ -35,17 +41,20 @@ let mst (graph: Matrix.SparseMatrix<_>) = let treeFilter edges index = fun i j g -> - let i = uint64 i * 1UL - let j = uint64 j * 1UL + let row = uint64 i + let col = uint64 j + + let i = row * 1UL + let edge = Vector.unsafeGet edges i let idx = Vector.unsafeGet index i - let result = - match edge, idx with - | Some(w, dst), Some idxVal -> g = w && idxVal = i && uint64 dst = uint64 j - | _ -> false + let u = min row col + let v = max row col - result + match edge, idx with + | Some(w, edgeU, edgeV), Some idxVal -> g = w && idxVal = i && edgeU = u && edgeV = v + | _ -> false let graphFilter parent = fun i j -> @@ -66,44 +75,39 @@ let mst (graph: Matrix.SparseMatrix<_>) = if graph.nvals > 0UL then - // Cheapest outgoing edge for each vertex - // For each vertex j, find the smallest weight edge (i, j, w) - // such that i and j are in different components. - // Because graph contains only cross‑component edges, - // we simply take the min over all neighbors. + // Cheapest outgoing edge for each vertex. + // Edge is stored canonically as (weight, minEndpoint, maxEndpoint). resultM { let! edges = LinearAlgebra.vxmi_values op_min op_mult parent graph |> Result.mapError EdgesCalculationProblem - // Per‑component cheapest edge - // For each component, keep the smallest edges among its vertices. + // Per-component cheapest edge. let! cedges = Vector.scatter (Vector.empty length) edges parent op_min |> Result.mapError CEdgesCalculationProblem - // Propagate component's cheapest edge to all its vertices - // Each vertex gets its component's edge + // Propagate component's cheapest edge to all vertices. let t = Vector.gather cedges parent - // Identify a representative vertex for each component - // For each vertex, if its own edge is the component's cheapest, mark it. + // Identify representative vertices whose own edge equals + // the cheapest edge of their component. let! indexInner = Vector.map2i t edges (fun i t e -> match (t, e) with | Some v1, Some v2 when v1 = v2 -> Some i | _ -> None) |> Result.mapError IndexInnerCalculationProblem - // Among the marked vertices in a component, keep the smallest index. + + // Among marked vertices in a component, keep the smallest index. let! index = Vector.scatter (Vector.empty length) indexInner parent op_min |> Result.mapError IndexCalculationProblem - // now each vertex knows its component's representative + + // Now each vertex knows its component's representative. let index = Vector.gather index parent - // Add selected edges to the MST tree - // An edge (i, j, w) is added if vertex i is the representative for its component - // and (i, j, w) is the cheapest edge of that component. + // Add selected edges to the MST tree. let treeFilter = treeFilter edges index let! tree = @@ -114,24 +118,36 @@ let mst (graph: Matrix.SparseMatrix<_>) = | _ -> None) |> Result.mapError TreeSelectionProblem - // Compute new parent assignments (merge components) - // For each component representative i with cheapest edge (w, j), we want to merge - // the component of i with the component of j. Choose the smaller root. + // Compute new parent assignments. + // For selected canonical edge (u, v), if current selected + // representative is one endpoint, merge it with the other endpoint. let! data_for_update_parent = Vector.map2i edges index (fun i e idx -> match e, idx with - | Some(v, j), Some(_i) when _i = i -> - let j = uint64 j * 1UL - let parent_i = Vector.unsafeGet parent i - let parent_j = Vector.unsafeGet parent j - - match parent_i, parent_j with - | Some p_i, Some p_j -> if p_i < p_j then Some(j, p_i) else Some(i, p_j) - | x -> failwithf "Unreachable: %A" x + | Some(_w, u, v), Some selected when selected = i -> + let current = uint64 i + + let other = + if current = u then Some v + elif current = v then Some u + else None + + match other with + | Some otherVertex -> + let j = otherVertex * 1UL + let parent_i = Vector.unsafeGet parent i + let parent_j = Vector.unsafeGet parent j + + match parent_i, parent_j with + | Some p_i, Some p_j -> if p_i < p_j then Some(j, p_i) else Some(i, p_j) + | _ -> None + + | None -> None + | _ -> None) |> Result.mapError DataForUpgradeParentCalculationProblem - // Apply the updates + // Apply direct parent updates. let! initial_parent_update = Vector.foldValues data_for_update_parent @@ -151,11 +167,10 @@ let mst (graph: Matrix.SparseMatrix<_>) = Vector.scatter parent initial_parent_update parent op_min |> Result.mapError ScatterProblem - // Then ensure that all vertices in a merged component point to the same root. - // This is done by a fixpoint (path compression) that repeatedly gathers parents. + // Path compression. let parent = fixPoint parent - // Filter the graph to keep only edges between different components + // Keep only edges between different components. let graphFilter = graphFilter parent let graph = Matrix.mapi graph (fun i j v -> if graphFilter i j then v else None) diff --git a/QuadTree/Matrix.fs b/QuadTree/Matrix.fs index 68ea7d7..a9a5bf3 100644 --- a/QuadTree/Matrix.fs +++ b/QuadTree/Matrix.fs @@ -74,43 +74,56 @@ let private getQuadrantCoords (pr, pc) halfSize = (pr + halfSize * 1UL, pc + halfSize * 1UL) // SOUTH EAST let fromCoordinateList (coo: CoordinateList<'a>) = - let nvals = (uint64 <| List.length coo.list) * 1UL - let nrows = coo.nrows - let ncols = coo.ncols - - // the resulting matrix is always square - let storageSize = getNearestUpperPowerOfTwo (max (uint64 nrows) (uint64 ncols)) - - let isEntryInQuadrant (pr, pc) size (entry: COOEntry<'a>) = - let (i, j, _) = entry - - i >= pr - && j >= pc - && i < pr + size * 1UL - && j < pc + size * 1UL - - let rec traverse coordinates (pr, pc) size = - match coordinates with - | [] when (uint64 pr) + size < uint64 nrows && (uint64 pc) + size < uint64 ncols -> Leaf <| UserValue None - | [] when uint64 pr >= uint64 nrows || uint64 pc >= uint64 ncols -> Leaf Dummy - | (i, j, value) :: _ when pr = i && pc = j && size = 1UL -> Leaf << UserValue <| Some value - | _ -> - let halfSize = size / 2UL - let nwp, nep, swp, sep = getQuadrantCoords (pr, pc) halfSize - let nwCoo = coordinates |> List.filter (isEntryInQuadrant nwp halfSize) - let neCoo = coordinates |> List.filter (isEntryInQuadrant nep halfSize) - let swCoo = coordinates |> List.filter (isEntryInQuadrant swp halfSize) - let seCoo = coordinates |> List.filter (isEntryInQuadrant sep halfSize) + let unique = + coo.list + |> List.groupBy (fun (i, j, _) -> (i, j)) + |> List.map (fun ((i, j), entries) -> + let value = entries |> List.map (fun (_, _, v) -> v) |> List.last + (i, j, value)) + + if + unique + |> List.exists (fun (i, j, _) -> uint64 i >= uint64 coo.nrows || uint64 j >= uint64 coo.ncols) + then + Error "Coordinates out of range" + else + let nvals = (uint64 <| List.length unique) * 1UL + let nrows = coo.nrows + let ncols = coo.ncols + + // the resulting matrix is always square + let storageSize = getNearestUpperPowerOfTwo (max (uint64 nrows) (uint64 ncols)) + + let isEntryInQuadrant (pr, pc) size (entry: COOEntry<'a>) = + let (i, j, _) = entry + + i >= pr + && j >= pc + && i < pr + size * 1UL + && j < pc + size * 1UL + + let rec traverse coordinates (pr, pc) size = + match coordinates with + | [] when (uint64 pr) + size < uint64 nrows && (uint64 pc) + size < uint64 ncols -> Leaf <| UserValue None + | [] when uint64 pr >= uint64 nrows || uint64 pc >= uint64 ncols -> Leaf Dummy + | (i, j, value) :: _ when pr = i && pc = j && size = 1UL -> Leaf << UserValue <| Some value + | _ -> + let halfSize = size / 2UL + let nwp, nep, swp, sep = getQuadrantCoords (pr, pc) halfSize + let nwCoo = coordinates |> List.filter (isEntryInQuadrant nwp halfSize) + let neCoo = coordinates |> List.filter (isEntryInQuadrant nep halfSize) + let swCoo = coordinates |> List.filter (isEntryInQuadrant swp halfSize) + let seCoo = coordinates |> List.filter (isEntryInQuadrant sep halfSize) - mkNode - (traverse nwCoo nwp halfSize) - (traverse neCoo nep halfSize) - (traverse swCoo swp halfSize) - (traverse seCoo sep halfSize) + mkNode + (traverse nwCoo nwp halfSize) + (traverse neCoo nep halfSize) + (traverse swCoo swp halfSize) + (traverse seCoo sep halfSize) - let tree = traverse coo.list (0UL, 0UL) storageSize + let tree = traverse unique (0UL, 0UL) storageSize - SparseMatrix(nrows, ncols, nvals, Storage(storageSize * 1UL, tree)) + Ok(SparseMatrix(nrows, ncols, nvals, Storage(storageSize * 1UL, tree))) let toCoordinateList (matrix: SparseMatrix<'a>) = let nrows = matrix.nrows @@ -135,10 +148,40 @@ let toCoordinateList (matrix: SparseMatrix<'a>) = let coo = traverse matrix.storage.data (0UL, 0UL) (uint64 matrix.storage.size) - CoordinateList(nrows, ncols, coo) + let sorted = List.sort coo + CoordinateList(nrows, ncols, sorted) let empty nrows ncols = - fromCoordinateList (CoordinateList(nrows, ncols, [])) + match fromCoordinateList (CoordinateList(nrows, ncols, [])) with + | Ok m -> m + | Error _ -> + let storageSize = + getNearestUpperPowerOfTwo (max (uint64 nrows) (uint64 ncols)) * 1UL + + SparseMatrix(nrows, ncols, 0UL, Storage(storageSize, Leaf Dummy)) + +let map (matrix: SparseMatrix<'a>) f = + let rec inner (size: uint64) (tree: qtree>) = + match tree with + | Node(nw, ne, sw, se) -> + let nwTree, nwNvals = inner (size / 2UL) nw + let neTree, neNvals = inner (size / 2UL) ne + let swTree, swNvals = inner (size / 2UL) sw + let seTree, seNvals = inner (size / 2UL) se + (mkNode nwTree neTree swTree seTree), nwNvals + neNvals + swNvals + seNvals + | Leaf(Dummy) -> Leaf(Dummy), 0UL + | Leaf(UserValue(v)) -> + let res = f v + + let nnz = + match res with + | None -> 0UL + | _ -> (uint64 size) * (uint64 size) * 1UL + + Leaf(UserValue(res)), nnz + + let newTree, newNvals = inner matrix.storage.size matrix.storage.data + SparseMatrix(matrix.nrows, matrix.ncols, newNvals, Storage(matrix.storage.size, newTree)) let map2 (matrix1: SparseMatrix<_>) (matrix2: SparseMatrix<_>) f = let rec inner (size: uint64) matrix1 matrix2 = @@ -394,3 +437,324 @@ let transpose (matrix: SparseMatrix<_>) = let mask (m1: SparseMatrix<'a>) (m2: SparseMatrix<'b>) f = map2 m1 m2 (fun m1 m2 -> if f m2 then m1 else None) + +let slice + (matrix: SparseMatrix<'a>) + (rowStart: int) + (rowEnd: int) + (colStart: int) + (colEnd: int) + : Result, string> = + if rowStart < 0 then + Error "Start row should be >= 0" + elif rowEnd < 0 then + Error "End row should be >= 0" + elif colStart < 0 then + Error "Start column should be >= 0" + elif colEnd < 0 then + Error "End column should be >= 0" + elif rowStart > int matrix.nrows - 1 then + Error "Start row is out of matrix length" + elif rowEnd > int matrix.nrows - 1 then + Error "End row is out of matrix length" + elif colStart > int matrix.ncols - 1 then + Error "Start column is out of matrix length" + elif colEnd > int matrix.ncols - 1 then + Error "End column is out of matrix length" + elif rowStart > rowEnd then + Error "Start row should be <= end row" + elif colStart > colEnd then + Error "Start column should be <= end column" + else + let rowStartIdx = uint64 rowStart * 1UL + let rowEndIdx = uint64 rowEnd * 1UL + let colStartIdx = uint64 colStart * 1UL + let colEndIdx = uint64 colEnd * 1UL + let newRows = uint64 (rowEnd - rowStart + 1) * 1UL + let newCols = uint64 (colEnd - colStart + 1) * 1UL + + let newSize = + getNearestUpperPowerOfTwo (max (uint64 newRows) (uint64 newCols)) + * 1UL + + let rec cut (size: uint64) (row: uint64) (col: uint64) tree = + let sizeRow = (uint64 size) * 1UL + let sizeCol = (uint64 size) * 1UL + + match tree with + | Node(nw, ne, sw, se) -> + let half = size / 2UL + let halfRow = (uint64 half) * 1UL + let halfCol = (uint64 half) * 1UL + + mkNode + (cut half row col nw) + (cut half row (col + halfCol) ne) + (cut half (row + halfRow) col sw) + (cut half (row + halfRow) (col + halfCol) se) + | Leaf(Dummy) -> Leaf Dummy + | Leaf(UserValue(v)) when + row >= rowStartIdx + && row + sizeRow - 1UL <= rowEndIdx + && col >= colStartIdx + && col + sizeCol - 1UL <= colEndIdx + -> + Leaf(UserValue(v)) + | _ -> Leaf Dummy + + let cutTree = + cut matrix.storage.size 0UL 0UL matrix.storage.data + + let rec empty (size: uint64) = + match size with + | 1UL -> Leaf Dummy + | _ -> + let half = size / 2UL + let subtree = empty half + Node(subtree, subtree, subtree, subtree) + + let rec insert (size: uint64) (row: uint64) (col: uint64) value tree = + match size with + | 1UL -> Leaf(UserValue(value)) + | _ -> + let half = size / 2UL + let halfRow = (uint64 half) * 1UL + let halfCol = (uint64 half) * 1UL + + match tree with + | Node(nw, ne, sw, se) -> + if row < halfRow then + if col < halfCol then + Node(insert half row col value nw, ne, sw, se) + else + Node(nw, insert half row (col - halfCol) value ne, sw, se) + else if col < halfCol then + Node(nw, ne, insert half (row - halfRow) col value sw, se) + else + Node(nw, ne, sw, insert half (row - halfRow) (col - halfCol) value se) + | _ -> + let emptySub = empty half + + if row < halfRow then + if col < halfCol then + Node(insert half row col value emptySub, emptySub, emptySub, emptySub) + else + Node(emptySub, insert half row (col - halfCol) value emptySub, emptySub, emptySub) + else if col < halfCol then + Node(emptySub, emptySub, insert half (row - halfRow) col value emptySub, emptySub) + else + Node(emptySub, emptySub, emptySub, insert half (row - halfRow) (col - halfCol) value emptySub) + + let rec rebuild (size: uint64) (row: uint64) (col: uint64) tree acc = + match tree with + | Leaf(Dummy) -> acc + | Leaf(UserValue(v)) -> + let newRow = row - uint64 rowStart * 1UL + let newCol = col - uint64 colStart * 1UL + insert newSize newRow newCol v acc + | Node(nw, ne, sw, se) -> + let half = size / 2UL + let halfRow = (uint64 half) * 1UL + let halfCol = (uint64 half) * 1UL + let acc = rebuild half row col nw acc + let acc = rebuild half row (col + halfCol) ne acc + let acc = rebuild half (row + halfRow) col sw acc + rebuild half (row + halfRow) (col + halfCol) se acc + + let emptyTree = empty newSize + + let shiftedTree = + rebuild matrix.storage.size 0UL 0UL cutTree emptyTree + + let rec count (size: uint64) tree = + match tree with + | Node(nw, ne, sw, se) -> + let half = size / 2UL + count half nw + count half ne + count half sw + count half se + | Leaf(UserValue(_)) -> 1UL + | _ -> 0UL + + let nvals = count newSize shiftedTree + + Ok(SparseMatrix(newRows, newCols, nvals, Storage(newSize, shiftedTree))) + +let foldQuadtree folder state size tree = + let rec inner rowOffset colOffset currentSize subTree acc = + match subTree with + | Leaf Dummy -> acc + | Leaf(UserValue None) -> acc + | Leaf(UserValue(Some value)) -> + let rec loop currRow currCol currentAcc = + if currRow = rowOffset + currentSize then + currentAcc + elif currCol = colOffset + currentSize then + loop (currRow + 1UL) colOffset currentAcc + else + loop currRow (currCol + 1UL) (folder currentAcc currRow currCol value) + + loop rowOffset colOffset acc + | Node(nw, ne, sw, se) -> + let half = currentSize / 2UL + let acc1 = inner rowOffset colOffset half nw acc + let acc2 = inner rowOffset (colOffset + half) half ne acc1 + let acc3 = inner (rowOffset + half) colOffset half sw acc2 + inner (rowOffset + half) (colOffset + half) half se acc3 + + inner 0UL 0UL (uint64 size) tree state + +let reduceRows (op: 'a option -> 'a option -> 'a option) (matrix: SparseMatrix<'a>) : Vector.SparseVector<'a> = + let rows = matrix.nrows + + let pairs = + foldQuadtree (fun acc row col v -> (row, v) :: acc) [] matrix.storage.size matrix.storage.data + + let grouped = + List.sortBy fst pairs + |> List.groupBy fst + |> List.map (fun (row, rowSet) -> row, rowSet |> List.map snd |> List.map Some) + + let reduced = + grouped + |> List.map (fun (row, values) -> + match values with + | [] -> row, None + | head :: tail -> row, List.fold op head tail) + + let data = + reduced + |> List.choose (fun (row, v) -> + match v with + | None -> None + | Some x -> Some(row, 0UL, x)) + |> List.sort + + let vectorData = + data |> List.map (fun (row, _, v) -> (uint64 row * 1UL, v)) + + match Vector.fromCoordinateList (Vector.CoordinateList(uint64 rows * 1UL, vectorData)) with + | Ok v -> v + | Error _ -> + Vector.SparseVector( + uint64 rows * 1UL, + 0UL, + Vector.Storage(1UL, Vector.Leaf Dummy) + ) + +let reduceCols (op: 'a option -> 'a option -> 'a option) (matrix: SparseMatrix<'a>) : Vector.SparseVector<'a> = + let cols = matrix.ncols + + let pairs = + foldQuadtree (fun acc row col v -> (col, v) :: acc) [] matrix.storage.size matrix.storage.data + + let grouped = + List.sortBy fst pairs + |> List.groupBy fst + |> List.map (fun (col, colSet) -> col, colSet |> List.map snd |> List.map Some) + + let reduced = + grouped + |> List.map (fun (col, values) -> + match values with + | [] -> col, None + | head :: tail -> col, List.fold op head tail) + + let data = + reduced + |> List.choose (fun (col, v) -> + match v with + | None -> None + | Some x -> Some(0UL, col, x)) + |> List.sort + + let vectorData = + data |> List.map (fun (_, col, v) -> (uint64 col * 1UL, v)) + + match Vector.fromCoordinateList (Vector.CoordinateList(uint64 cols * 1UL, vectorData)) with + | Ok v -> v + | Error _ -> + Vector.SparseVector( + uint64 cols * 1UL, + 0UL, + Vector.Storage(1UL, Vector.Leaf Dummy) + ) + +let kroneckerProduct + (matrix1: SparseMatrix<'a>) + (matrix2: SparseMatrix<'b>) + (f: 'a -> 'b -> 'c option) + : Result, string> = + let newRows = uint64 matrix1.nrows * uint64 matrix2.nrows * 1UL + let newCols = uint64 matrix1.ncols * uint64 matrix2.ncols * 1UL + + let newSize = + getNearestUpperPowerOfTwo (max (uint64 newRows) (uint64 newCols)) + * 1UL + + let rec empty (size: uint64) = + match size with + | 1UL -> Leaf Dummy + | _ -> + let half = size / 2UL + let subtree = empty half + Node(subtree, subtree, subtree, subtree) + + let rec insert (size: uint64) (row: uint64) (col: uint64) value tree = + match size with + | 1UL -> Leaf(UserValue(value)) + | _ -> + let half = size / 2UL + let halfRow = (uint64 half) * 1UL + let halfCol = (uint64 half) * 1UL + + match tree with + | Node(nw, ne, sw, se) -> + match (row < halfRow, col < halfCol) with + | true, true -> Node(insert half row col value nw, ne, sw, se) + | true, false -> Node(nw, insert half row (col - halfCol) value ne, sw, se) + | false, true -> Node(nw, ne, insert half (row - halfRow) col value sw, se) + | false, false -> Node(nw, ne, sw, insert half (row - halfRow) (col - halfCol) value se) + | _ -> + let emptySub = empty half + + match (row < halfRow, col < halfCol) with + | true, true -> Node(insert half row col value emptySub, emptySub, emptySub, emptySub) + | true, false -> Node(emptySub, insert half row (col - halfCol) value emptySub, emptySub, emptySub) + | false, true -> Node(emptySub, emptySub, insert half (row - halfRow) col value emptySub, emptySub) + | false, false -> + Node(emptySub, emptySub, emptySub, insert half (row - halfRow) (col - halfCol) value emptySub) + + let mat2Rows = uint64 matrix2.nrows + let mat2Cols = uint64 matrix2.ncols + let initialTree = empty newSize + + let foldMatrixB acc rowMat1 colMat1 valMat1 = + foldQuadtree + (fun currentAcc rowMat2 colMat2 valMat2 -> + match f valMat1 valMat2 with + | Some computedVal -> + let destRow = rowMat1 * mat2Rows + rowMat2 + let destCol = colMat1 * mat2Cols + colMat2 + insert newSize (destRow * 1UL) (destCol * 1UL) (Some computedVal) currentAcc + | None -> currentAcc) + acc + matrix2.storage.size + matrix2.storage.data + + let finalTree = + foldQuadtree + (fun acc rowMat1 colMat1 valMat1 -> foldMatrixB acc rowMat1 colMat1 valMat1) + initialTree + matrix1.storage.size + matrix1.storage.data + + let rec count (size: uint64) tree = + match tree with + | Node(nw, ne, sw, se) -> + let half = size / 2UL + count half nw + count half ne + count half sw + count half se + | Leaf(UserValue(Some _)) -> 1UL + | _ -> 0UL + + let nvals = count newSize finalTree + + Ok(SparseMatrix(newRows, newCols, nvals, Storage(newSize, finalTree))) diff --git a/QuadTree/SSSP.fs b/QuadTree/SSSP.fs index 9a40fde..7aab468 100644 --- a/QuadTree/SSSP.fs +++ b/QuadTree/SSSP.fs @@ -45,8 +45,13 @@ let sssp graph (startVertex: uint64) = else Ok visited - let frontier = + let frontierResult = Vector.CoordinateList((uint64 graph.ncols) * 1UL, [ startVertex * 1UL, 0.0 ]) |> Vector.fromCoordinateList + let frontier = + match frontierResult with + | Ok v -> v + | Error _ -> Vector.empty ((uint64 graph.ncols) * 1UL) + inner frontier frontier 0 diff --git a/QuadTree/Vector.fs b/QuadTree/Vector.fs index b7bcb44..cecb7cb 100644 --- a/QuadTree/Vector.fs +++ b/QuadTree/Vector.fs @@ -306,31 +306,41 @@ let update (vector: SparseVector<_>) i v op = else Error Error.InconsistentSizeOfArguments -let fromCoordinateList (lst: CoordinateList<'a>) : SparseVector<'a> = - let length = lst.length - let nvals = (uint64 <| List.length lst.data) * 1UL - let storageSize = (getNearestUpperPowerOfTwo <| uint64 length) * 1UL - - let rec traverse coordinates pointer size = - match coordinates with - | [] when uint64 (pointer + size) < uint64 (length) -> Leaf <| UserValue None, [] - | [] when uint64 pointer >= uint64 length -> Leaf Dummy, [] - | (idx, _) :: _ when idx > pointer + size -> Leaf <| UserValue None, coordinates - | (idx, value) :: xs when idx = pointer && size = 1UL -> Leaf << UserValue <| Some value, xs - | _ -> - let halfSize = size / 2UL +let fromCoordinateList (lst: CoordinateList<'a>) : Result, string> = + let unique = + lst.data + |> List.groupBy fst + |> List.map (fun (idx, entries) -> + let value = entries |> List.map snd |> List.last + (idx, value)) + + if unique |> List.exists (fun (i, _) -> uint64 i >= uint64 lst.length) then + Error "Index out of range" + else + let length = lst.length + let nvals = (uint64 <| List.length lst.data) * 1UL + let storageSize = (getNearestUpperPowerOfTwo <| uint64 length) * 1UL + + let rec traverse coordinates pointer size = + match coordinates with + | [] when uint64 (pointer + size) < uint64 (length) -> Leaf <| UserValue None, [] + | [] when uint64 pointer >= uint64 length -> Leaf Dummy, [] + | (idx, _) :: _ when idx > pointer + size -> Leaf <| UserValue None, coordinates + | (idx, value) :: xs when idx = pointer && size = 1UL -> Leaf << UserValue <| Some value, xs + | _ -> + let halfSize = size / 2UL - let left, lCoordinates = traverse coordinates pointer halfSize - let right, rCoordinates = traverse lCoordinates (pointer + halfSize) halfSize + let left, lCoordinates = traverse coordinates pointer halfSize + let right, rCoordinates = traverse lCoordinates (pointer + halfSize) halfSize - mkNode left right, rCoordinates + mkNode left right, rCoordinates - let sortedCoordinates = List.sort lst.data + let sortedCoordinates = List.sort unique - let tree, _ = - traverse sortedCoordinates 0UL ((uint64 storageSize) * 1UL) + let tree, _ = + traverse sortedCoordinates 0UL ((uint64 storageSize) * 1UL) - SparseVector(length, nvals, Storage(storageSize, tree)) + Ok(SparseVector(length, nvals, Storage(storageSize, tree))) let toCoordinateList (vector: SparseVector<'a>) = let length = vector.length @@ -354,7 +364,11 @@ let toCoordinateList (vector: SparseVector<'a>) = CoordinateList(length, lst) let empty length = - fromCoordinateList (CoordinateList(length, [])) + match fromCoordinateList (CoordinateList(length, [])) with + | Ok v -> v + | Error _ -> + let storageSize = (getNearestUpperPowerOfTwo <| uint64 length) * 1UL + SparseVector(length, 0UL, Storage(storageSize, Leaf Dummy)) let foldValues (vector: SparseVector<'a>) (f: 'b -> 'a -> 'b) (state: 'b) = let rec inner state (size: uint64) vector = @@ -561,3 +575,91 @@ let scatter | Error x -> Error x) (Ok w) | Error x -> Error Error.InconsistentStructureOfStorages + + +let slice (_start: int) (_end: int) (vector: SparseVector<'a>) : Result, string> = + if _start < 0 then + Error "Start should be >= 0" + elif _end < 0 then + Error "End should be >= 0" + elif _start > int vector.length - 1 then + Error "Start is out of Vector length" + elif _end > int vector.length - 1 then + Error "End is out of Vector length" + elif _start > _end then + Error "End should be >= Start" + else + let startIdx = uint64 _start * 1UL + let endIdx = uint64 _end * 1UL + let newLength = uint64 (_end - _start + 1) * 1UL + let newSize = getNearestUpperPowerOfTwo (uint64 newLength) * 1UL + + let rec cut (size: uint64) (pos: uint64) tree = + let sizeIdx = (uint64 size) * 1UL + + match tree with + | Node(l, r) -> + let half = size / 2UL + let halfIdx = (uint64 half) * 1UL + Node(cut half pos l, cut half (pos + halfIdx) r) + | Leaf(Dummy) -> Leaf Dummy + | Leaf(UserValue(v)) when pos >= startIdx && pos + sizeIdx - 1UL <= endIdx -> Leaf(UserValue(v)) + | _ -> Leaf Dummy + + let cutTree = cut vector.storage.size 0UL vector.storage.data + + let rec empty (size: uint64) = + match size with + | 1UL -> Leaf Dummy + | _ -> + let half = size / 2UL + let subtree = empty half + Node(subtree, subtree) + + let rec insert (size: uint64) (idx: uint64) value tree = + match size with + | 1UL -> Leaf(UserValue(value)) + | _ -> + let half = size / 2UL + let border = (uint64 half) * 1UL + + match tree with + | Node(left, right) -> + if idx < border then + Node(insert half idx value left, right) + else + Node(left, insert half (idx - border) value right) + | _ -> + let emptySub = empty half + + if idx < border then + Node(insert half idx value emptySub, emptySub) + else + Node(emptySub, insert half (idx - border) value emptySub) + + let rec rebuild (size: uint64) (pos: uint64) tree acc = + match tree with + | Leaf(Dummy) -> acc + | Leaf(UserValue(v)) -> + let newIdx = pos - startIdx + insert newSize newIdx v acc + | Node(left, right) -> + let half = size / 2UL + let border = (uint64 half) * 1UL + let acc = rebuild half pos left acc + rebuild half (pos + border) right acc + + let emptyTree = empty newSize + let shiftedTree = rebuild vector.storage.size 0UL cutTree emptyTree + + let rec count (size: uint64) tree = + match tree with + | Node(l, r) -> + let half = size / 2UL + count half l + count half r + | Leaf(UserValue(_)) -> (uint64 size) * 1UL + | _ -> 0UL + + let nvals = count newSize shiftedTree + + Ok(SparseVector(newLength, nvals, Storage(newSize, shiftedTree)))