diff --git a/QuadTree.Benchmark/BFS.fs b/QuadTree.Benchmark/BFS.fs index b68adcd..d21def0 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 msg -> failwith $"Failed to load matrix {this.MatrixName}: {msg}" [] 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 msg -> failwith $"Failed to create start vertices: {msg}" + Graph.BFS.bfs_level matrix startVertices diff --git a/QuadTree.Benchmark/SSSP.fs b/QuadTree.Benchmark/SSSP.fs index 002fa3a..80f6e68 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 e -> failwith $"Failed to load matrix {this.MatrixName}: {e}" [] member this.SSSP() = Graph.SSSP.sssp matrix 0UL diff --git a/QuadTree.Benchmark/Triangles.fs b/QuadTree.Benchmark/Triangles.fs index 7fa31e4..4dce806 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 msg -> failwith $"Failed to load matrix {this.MatrixName}: {msg}" [] member this.TriangleCount() = diff --git a/QuadTree.Tests/QuadTree.Tests.fsproj b/QuadTree.Tests/QuadTree.Tests.fsproj index 4de3d64..bc76cf2 100644 --- a/QuadTree.Tests/QuadTree.Tests.fsproj +++ b/QuadTree.Tests/QuadTree.Tests.fsproj @@ -13,7 +13,7 @@ - + diff --git a/QuadTree.Tests/Tests.BFS.fs b/QuadTree.Tests/Tests.BFS.fs index df5d0f3..df9981c 100644 --- a/QuadTree.Tests/Tests.BFS.fs +++ b/QuadTree.Tests/Tests.BFS.fs @@ -7,16 +7,29 @@ open Matrix open Vector open Common -(* -1,N,N,N -, -N,1,1,N -3,N,2,3 -N,N,N,2 -N,N,3,N -=> -0,1,1,2 -*) +let singleStart (n: uint64) (s: uint64) = + Vector.CoordinateList(n * 1UL, [ s * 1UL, 1UL ]) + |> Vector.fromCoordinateList + +let unsafes (n: uint64) (v: Vector.SparseVector<_>) = + List.init (int n) (fun i -> Vector.unsafeGet v (uint64 i * 1UL)) + +let runTest + (graphResult: Result, string>) + (startResult: Result, string>) + bfsFunc + expected + = + match graphResult with + | Error msg -> Assert.Fail $"Graph init failed: {msg}" + | Ok graph -> + match startResult with + | Error msg -> Assert.Fail $"Start init failed: {msg}" + | Ok start -> + let result = bfsFunc graph start + let n = uint64 graph.ncols + Assert.Equal(Ok expected, Result.map (unsafes n) result) + [] let ``Simple level bfs.`` () = let graph = @@ -69,3 +82,1176 @@ let ``Simple level bfs.`` () = let actual = Graph.BFS.bfs_level graph startVertices Assert.Equal(expected, actual) + +[] +let ``Simple parent bfs.`` () = + let graph = + let tree = + Matrix.qtree.Node( + Matrix.qtree.Node( + Matrix.qtree.Leaf(UserValue(None)), + Matrix.qtree.Leaf(UserValue(Some(1))), + Matrix.qtree.Leaf(UserValue(Some(3))), + Matrix.qtree.Leaf(UserValue(None)) + ), + Matrix.qtree.Node( + Matrix.qtree.Leaf(UserValue(Some(1))), + Matrix.qtree.Leaf(UserValue(None)), + Matrix.qtree.Leaf(UserValue(Some(2))), + Matrix.qtree.Leaf(UserValue(Some(3))) + ), + Matrix.qtree.Leaf(UserValue(None)), + Matrix.qtree.Node( + Matrix.qtree.Leaf(UserValue(None)), + Matrix.qtree.Leaf(UserValue(Some(2))), + Matrix.qtree.Leaf(UserValue(Some(3))), + Matrix.qtree.Leaf(UserValue(None)) + ) + ) + + let store = Matrix.Storage(4UL, tree) + SparseMatrix(4UL, 4UL, 9UL, store) + + let startVertices = + let tree = + Vector.btree.Node( + Vector.btree.Node(Vector.btree.Leaf(UserValue(Some(1UL))), Vector.btree.Leaf(UserValue(None))), + Vector.btree.Leaf(UserValue(None)) + ) + + let store = Vector.Storage(4UL, tree) + SparseVector(4UL, 1UL, store) + + let expected = + let tree = + Vector.btree.Node( + Vector.btree.Leaf(UserValue(Some(0UL))), + Vector.btree.Node(Vector.btree.Leaf(UserValue(Some(0UL))), Vector.btree.Leaf(UserValue(Some(1UL)))) + ) + + let store = Vector.Storage(4UL, tree) + Ok(SparseVector(4UL, 4UL, store)) + + let actual = Graph.BFS.bfs_parent graph startVertices + + Assert.Equal(expected, actual) + +// ============== 3-node line ============== + +let private line3graph = + Matrix.fromCoordinateList ( + Matrix.CoordinateList( + 3UL, + 3UL, + [ 0UL, 1UL, 1UL + 1UL, 0UL, 1UL + 1UL, 2UL, 1UL + 2UL, 1UL, 1UL ] + ) + ) + +[] +let ``Level bfs 3 node line start 0`` () = + runTest line3graph (singleStart 3UL 0UL) Graph.BFS.bfs_level [ Some 0UL; Some 1UL; Some 2UL ] + +[] +let ``Parent bfs 3 node line start 0`` () = + runTest line3graph (singleStart 3UL 0UL) Graph.BFS.bfs_parent [ Some 0UL; Some 0UL; Some 1UL ] + +[] +let ``Level bfs 3 node line start 1`` () = + runTest line3graph (singleStart 3UL 1UL) Graph.BFS.bfs_level [ Some 1UL; Some 0UL; Some 1UL ] + +[] +let ``Parent bfs 3 node line start 1`` () = + runTest line3graph (singleStart 3UL 1UL) Graph.BFS.bfs_parent [ Some 1UL; Some 1UL; Some 1UL ] + +// ============== 5-node star (center 0) ============== + +let private star5graph = + Matrix.fromCoordinateList ( + Matrix.CoordinateList( + 5UL, + 5UL, + [ 0UL, 1UL, 1UL + 1UL, 0UL, 1UL + 0UL, 2UL, 1UL + 2UL, 0UL, 1UL + 0UL, 3UL, 1UL + 3UL, 0UL, 1UL + 0UL, 4UL, 1UL + 4UL, 0UL, 1UL ] + ) + ) + +[] +let ``Level bfs 5 node star start center`` () = + runTest star5graph (singleStart 5UL 0UL) Graph.BFS.bfs_level [ Some 0UL; Some 1UL; Some 1UL; Some 1UL; Some 1UL ] + +[] +let ``Parent bfs 5 node star start center`` () = + runTest star5graph (singleStart 5UL 0UL) Graph.BFS.bfs_parent [ Some 0UL; Some 0UL; Some 0UL; Some 0UL; Some 0UL ] + +[] +let ``Level bfs 5 node star start leaf`` () = + runTest star5graph (singleStart 5UL 1UL) Graph.BFS.bfs_level [ Some 1UL; Some 0UL; Some 2UL; Some 2UL; Some 2UL ] + +[] +let ``Parent bfs 5 node star start leaf`` () = + runTest star5graph (singleStart 5UL 1UL) Graph.BFS.bfs_parent [ Some 1UL; Some 1UL; Some 0UL; Some 0UL; Some 0UL ] + +// ============== Two components ============== + +let private twoCompGraph = + Matrix.fromCoordinateList ( + Matrix.CoordinateList( + 6UL, + 6UL, + [ 0UL, 1UL, 1UL + 1UL, 0UL, 1UL + 1UL, 2UL, 1UL + 2UL, 1UL, 1UL + 0UL, 2UL, 1UL + 2UL, 0UL, 1UL + + 3UL, 4UL, 1UL + 4UL, 3UL, 1UL + 4UL, 5UL, 1UL + 5UL, 4UL, 1UL + 3UL, 5UL, 1UL + 5UL, 3UL, 1UL ] + ) + ) + +[] +let ``Level bfs two components start 0`` () = + runTest twoCompGraph (singleStart 6UL 0UL) Graph.BFS.bfs_level [ Some 0UL; Some 1UL; Some 1UL; None; None; None ] + +[] +let ``Parent bfs two components start 0`` () = + runTest twoCompGraph (singleStart 6UL 0UL) Graph.BFS.bfs_parent [ Some 0UL; Some 0UL; Some 0UL; None; None; None ] + +// ============== Square (4-cycle) ============== + +let private squareGraph = + Matrix.fromCoordinateList ( + Matrix.CoordinateList( + 4UL, + 4UL, + [ 0UL, 1UL, 1UL + 1UL, 0UL, 1UL + 1UL, 2UL, 2UL + 2UL, 1UL, 2UL + 2UL, 3UL, 1UL + 3UL, 2UL, 1UL + 3UL, 0UL, 2UL + 0UL, 3UL, 2UL ] + ) + ) + +[] +let ``Level bfs square start 0`` () = + runTest squareGraph (singleStart 4UL 0UL) Graph.BFS.bfs_level [ Some 0UL; Some 1UL; Some 2UL; Some 1UL ] + +[] +let ``Parent bfs square start 0`` () = + runTest squareGraph (singleStart 4UL 0UL) Graph.BFS.bfs_parent [ Some 0UL; Some 0UL; Some 1UL; Some 0UL ] + +// ============== 6-cycle ============== + +let private cycle6graph = + Matrix.fromCoordinateList ( + Matrix.CoordinateList( + 6UL, + 6UL, + [ 0UL, 1UL, 1UL + 1UL, 0UL, 1UL + 1UL, 2UL, 2UL + 2UL, 1UL, 2UL + 2UL, 3UL, 3UL + 3UL, 2UL, 3UL + 3UL, 4UL, 4UL + 4UL, 3UL, 4UL + 4UL, 5UL, 5UL + 5UL, 4UL, 5UL + 5UL, 0UL, 6UL + 0UL, 5UL, 6UL ] + ) + ) + +[] +let ``Level bfs 6 cycle start 0`` () = + runTest + cycle6graph + (singleStart 6UL 0UL) + Graph.BFS.bfs_level + [ Some 0UL; Some 1UL; Some 2UL; Some 3UL; Some 2UL; Some 1UL ] + +[] +let ``Parent bfs 6 cycle start 0`` () = + runTest + cycle6graph + (singleStart 6UL 0UL) + Graph.BFS.bfs_parent + [ Some 0UL; Some 0UL; Some 1UL; Some 2UL; Some 5UL; Some 0UL ] + +// ============== 2 nodes ============== + +let private graph2 = + Matrix.fromCoordinateList ( + Matrix.CoordinateList( + 2UL, + 2UL, + [ 0UL, 1UL, 5UL; 1UL, 0UL, 5UL ] + ) + ) + +[] +let ``Level bfs 2 nodes start 0`` () = + runTest graph2 (singleStart 2UL 0UL) Graph.BFS.bfs_level [ Some 0UL; Some 1UL ] + +[] +let ``Parent bfs 2 nodes start 0`` () = + runTest graph2 (singleStart 2UL 0UL) Graph.BFS.bfs_parent [ Some 0UL; Some 0UL ] + +[] +let ``Level bfs 2 nodes start 1`` () = + runTest graph2 (singleStart 2UL 1UL) Graph.BFS.bfs_level [ Some 1UL; Some 0UL ] + +[] +let ``Parent bfs 2 nodes start 1`` () = + runTest graph2 (singleStart 2UL 1UL) Graph.BFS.bfs_parent [ Some 1UL; Some 1UL ] + +// ============== 4-node line ============== + +let private line4graph = + Matrix.fromCoordinateList ( + Matrix.CoordinateList( + 4UL, + 4UL, + [ 0UL, 1UL, 1UL + 1UL, 0UL, 1UL + 1UL, 2UL, 2UL + 2UL, 1UL, 2UL + 2UL, 3UL, 3UL + 3UL, 2UL, 3UL ] + ) + ) + +[] +let ``Level bfs 4 node line start 0`` () = + runTest line4graph (singleStart 4UL 0UL) Graph.BFS.bfs_level [ Some 0UL; Some 1UL; Some 2UL; Some 3UL ] + +[] +let ``Parent bfs 4 node line start 0`` () = + runTest line4graph (singleStart 4UL 0UL) Graph.BFS.bfs_parent [ Some 0UL; Some 0UL; Some 1UL; Some 2UL ] + +[] +let ``Level bfs 4 node line start 3`` () = + runTest line4graph (singleStart 4UL 3UL) Graph.BFS.bfs_level [ Some 3UL; Some 2UL; Some 1UL; Some 0UL ] + +[] +let ``Parent bfs 4 node line start 3`` () = + runTest line4graph (singleStart 4UL 3UL) Graph.BFS.bfs_parent [ Some 1UL; Some 2UL; Some 3UL; Some 3UL ] + +// ============== 5-node line ============== + +let private line5graph = + Matrix.fromCoordinateList ( + Matrix.CoordinateList( + 5UL, + 5UL, + [ 0UL, 1UL, 1UL + 1UL, 0UL, 1UL + 1UL, 2UL, 2UL + 2UL, 1UL, 2UL + 2UL, 3UL, 3UL + 3UL, 2UL, 3UL + 3UL, 4UL, 4UL + 4UL, 3UL, 4UL ] + ) + ) + +[] +let ``Level bfs 5 node line start 0`` () = + runTest line5graph (singleStart 5UL 0UL) Graph.BFS.bfs_level [ Some 0UL; Some 1UL; Some 2UL; Some 3UL; Some 4UL ] + +[] +let ``Parent bfs 5 node line start 0`` () = + runTest line5graph (singleStart 5UL 0UL) Graph.BFS.bfs_parent [ Some 0UL; Some 0UL; Some 1UL; Some 2UL; Some 3UL ] + +[] +let ``Level bfs 5 node line start 4`` () = + runTest line5graph (singleStart 5UL 4UL) Graph.BFS.bfs_level [ Some 4UL; Some 3UL; Some 2UL; Some 1UL; Some 0UL ] + +[] +let ``Parent bfs 5 node line start 4`` () = + runTest line5graph (singleStart 5UL 4UL) Graph.BFS.bfs_parent [ Some 1UL; Some 2UL; Some 3UL; Some 4UL; Some 4UL ] + +[] +let ``Level bfs 5 node line start 2`` () = + runTest line5graph (singleStart 5UL 2UL) Graph.BFS.bfs_level [ Some 2UL; Some 1UL; Some 0UL; Some 1UL; Some 2UL ] + +[] +let ``Parent bfs 5 node line start 2`` () = + runTest line5graph (singleStart 5UL 2UL) Graph.BFS.bfs_parent [ Some 1UL; Some 2UL; Some 2UL; Some 2UL; Some 3UL ] + +// ============== Simple triangle ============== + +let private triangleGraph = + Matrix.fromCoordinateList ( + Matrix.CoordinateList( + 3UL, + 3UL, + [ 0UL, 1UL, 1UL + 1UL, 0UL, 1UL + 0UL, 2UL, 1UL + 2UL, 0UL, 1UL + 1UL, 2UL, 1UL + 2UL, 1UL, 1UL ] + ) + ) + +[] +let ``Level bfs triangle start 0`` () = + runTest triangleGraph (singleStart 3UL 0UL) Graph.BFS.bfs_level [ Some 0UL; Some 1UL; Some 1UL ] + +[] +let ``Parent bfs triangle start 0`` () = + runTest triangleGraph (singleStart 3UL 0UL) Graph.BFS.bfs_parent [ Some 0UL; Some 0UL; Some 0UL ] + +[] +let ``Level bfs triangle start 2`` () = + runTest triangleGraph (singleStart 3UL 2UL) Graph.BFS.bfs_level [ Some 1UL; Some 1UL; Some 0UL ] + +[] +let ``Parent bfs triangle start 2`` () = + runTest triangleGraph (singleStart 3UL 2UL) Graph.BFS.bfs_parent [ Some 2UL; Some 2UL; Some 2UL ] + +// ============== 5-node complete graph ============== + +let private complete5graph = + Matrix.fromCoordinateList ( + Matrix.CoordinateList( + 5UL, + 5UL, + [ 0UL, 1UL, 1UL + 1UL, 0UL, 1UL + 0UL, 2UL, 2UL + 2UL, 0UL, 2UL + 0UL, 3UL, 3UL + 3UL, 0UL, 3UL + 0UL, 4UL, 4UL + 4UL, 0UL, 4UL + 1UL, 2UL, 5UL + 2UL, 1UL, 5UL + 1UL, 3UL, 6UL + 3UL, 1UL, 6UL + 1UL, 4UL, 7UL + 4UL, 1UL, 7UL + 2UL, 3UL, 8UL + 3UL, 2UL, 8UL + 2UL, 4UL, 9UL + 4UL, 2UL, 9UL + 3UL, 4UL, 10UL + 4UL, 3UL, 10UL ] + ) + ) + +[] +let ``Level bfs 5 node complete start 0`` () = + runTest + complete5graph + (singleStart 5UL 0UL) + Graph.BFS.bfs_level + [ Some 0UL; Some 1UL; Some 1UL; Some 1UL; Some 1UL ] + +[] +let ``Parent bfs 5 node complete start 0`` () = + runTest + complete5graph + (singleStart 5UL 0UL) + Graph.BFS.bfs_parent + [ Some 0UL; Some 0UL; Some 0UL; Some 0UL; Some 0UL ] + +[] +let ``Level bfs 5 node complete start 4`` () = + runTest + complete5graph + (singleStart 5UL 4UL) + Graph.BFS.bfs_level + [ Some 1UL; Some 1UL; Some 1UL; Some 1UL; Some 0UL ] + +[] +let ``Parent bfs 5 node complete start 4`` () = + runTest + complete5graph + (singleStart 5UL 4UL) + Graph.BFS.bfs_parent + [ Some 4UL; Some 4UL; Some 4UL; Some 4UL; Some 4UL ] + +[] +let ``Level bfs 5 node complete start 2`` () = + runTest + complete5graph + (singleStart 5UL 2UL) + Graph.BFS.bfs_level + [ Some 1UL; Some 1UL; Some 0UL; Some 1UL; Some 1UL ] + +[] +let ``Parent bfs 5 node complete start 2`` () = + runTest + complete5graph + (singleStart 5UL 2UL) + Graph.BFS.bfs_parent + [ Some 2UL; Some 2UL; Some 2UL; Some 2UL; Some 2UL ] + +// ============== K3,3 bipartite ============== + +let private k33graph = + Matrix.fromCoordinateList ( + Matrix.CoordinateList( + 6UL, + 6UL, + [ 0UL, 3UL, 1UL + 3UL, 0UL, 1UL + 0UL, 4UL, 2UL + 4UL, 0UL, 2UL + 0UL, 5UL, 3UL + 5UL, 0UL, 3UL + 1UL, 3UL, 4UL + 3UL, 1UL, 4UL + 1UL, 4UL, 5UL + 4UL, 1UL, 5UL + 1UL, 5UL, 6UL + 5UL, 1UL, 6UL + 2UL, 3UL, 7UL + 3UL, 2UL, 7UL + 2UL, 4UL, 8UL + 4UL, 2UL, 8UL + 2UL, 5UL, 9UL + 5UL, 2UL, 9UL ] + ) + ) + +[] +let ``Level bfs K3 3 start 0`` () = + runTest + k33graph + (singleStart 6UL 0UL) + Graph.BFS.bfs_level + [ Some 0UL; Some 2UL; Some 2UL; Some 1UL; Some 1UL; Some 1UL ] + +[] +let ``Parent bfs K3 3 start 0`` () = + runTest + k33graph + (singleStart 6UL 0UL) + Graph.BFS.bfs_parent + [ Some 0UL; Some 3UL; Some 3UL; Some 0UL; Some 0UL; Some 0UL ] + +[] +let ``Level bfs K3 3 start 5`` () = + runTest + k33graph + (singleStart 6UL 5UL) + Graph.BFS.bfs_level + [ Some 1UL; Some 1UL; Some 1UL; Some 2UL; Some 2UL; Some 0UL ] + +[] +let ``Parent bfs K3 3 start 5`` () = + runTest + k33graph + (singleStart 6UL 5UL) + Graph.BFS.bfs_parent + [ Some 5UL; Some 5UL; Some 5UL; Some 0UL; Some 0UL; Some 5UL ] + +[] +let ``Level bfs K3 3 start 3`` () = + runTest + k33graph + (singleStart 6UL 3UL) + Graph.BFS.bfs_level + [ Some 1UL; Some 1UL; Some 1UL; Some 0UL; Some 2UL; Some 2UL ] + +[] +let ``Parent bfs K3 3 start 3`` () = + runTest + k33graph + (singleStart 6UL 3UL) + Graph.BFS.bfs_parent + [ Some 3UL; Some 3UL; Some 3UL; Some 3UL; Some 0UL; Some 0UL ] + +// ============== 8 nodes random weights ============== + +let private random8graph = + Matrix.fromCoordinateList ( + Matrix.CoordinateList( + 8UL, + 8UL, + [ 0UL, 1UL, 7UL + 1UL, 0UL, 7UL + 0UL, 2UL, 5UL + 2UL, 0UL, 5UL + 0UL, 3UL, 9UL + 3UL, 0UL, 9UL + 1UL, 2UL, 3UL + 2UL, 1UL, 3UL + 1UL, 3UL, 4UL + 3UL, 1UL, 4UL + 2UL, 3UL, 2UL + 3UL, 2UL, 2UL + 4UL, 5UL, 1UL + 5UL, 4UL, 1UL + 4UL, 6UL, 6UL + 6UL, 4UL, 6UL + 4UL, 7UL, 8UL + 7UL, 4UL, 8UL + 5UL, 6UL, 3UL + 6UL, 5UL, 3UL + 5UL, 7UL, 5UL + 7UL, 5UL, 5UL + 6UL, 7UL, 2UL + 7UL, 6UL, 2UL + 3UL, 4UL, 10UL + 4UL, 3UL, 10UL ] + ) + ) + +[] +let ``Level bfs 8 node random start 0`` () = + runTest + random8graph + (singleStart 8UL 0UL) + Graph.BFS.bfs_level + [ Some 0UL + Some 1UL + Some 1UL + Some 1UL + Some 2UL + Some 3UL + Some 3UL + Some 3UL ] + +[] +let ``Parent bfs 8 node random start 0`` () = + runTest + random8graph + (singleStart 8UL 0UL) + Graph.BFS.bfs_parent + [ Some 0UL + Some 0UL + Some 0UL + Some 0UL + Some 3UL + Some 4UL + Some 4UL + Some 4UL ] + +[] +let ``Level bfs 8 node random start 7`` () = + runTest + random8graph + (singleStart 8UL 7UL) + Graph.BFS.bfs_level + [ Some 3UL + Some 3UL + Some 3UL + Some 2UL + Some 1UL + Some 1UL + Some 1UL + Some 0UL ] + +[] +let ``Parent bfs 8 node random start 7`` () = + runTest + random8graph + (singleStart 8UL 7UL) + Graph.BFS.bfs_parent + [ Some 3UL + Some 3UL + Some 3UL + Some 4UL + Some 7UL + Some 7UL + Some 7UL + Some 7UL ] + +[] +let ``Level bfs 8 node random start 4`` () = + runTest + random8graph + (singleStart 8UL 4UL) + Graph.BFS.bfs_level + [ Some 2UL + Some 2UL + Some 2UL + Some 1UL + Some 0UL + Some 1UL + Some 1UL + Some 1UL ] + +[] +let ``Parent bfs 8 node random start 4`` () = + runTest + random8graph + (singleStart 8UL 4UL) + Graph.BFS.bfs_parent + [ Some 3UL + Some 3UL + Some 3UL + Some 4UL + Some 4UL + Some 4UL + Some 4UL + Some 4UL ] + +// ============== 8 nodes grid ============== + +let private grid8graph = + Matrix.fromCoordinateList ( + Matrix.CoordinateList( + 8UL, + 8UL, + [ 0UL, 1UL, 1UL + 1UL, 0UL, 1UL + 1UL, 2UL, 2UL + 2UL, 1UL, 2UL + 2UL, 3UL, 1UL + 3UL, 2UL, 1UL + 0UL, 4UL, 3UL + 4UL, 0UL, 3UL + 1UL, 5UL, 4UL + 5UL, 1UL, 4UL + 2UL, 6UL, 5UL + 6UL, 2UL, 5UL + 3UL, 7UL, 6UL + 7UL, 3UL, 6UL + 4UL, 5UL, 2UL + 5UL, 4UL, 2UL + 5UL, 6UL, 1UL + 6UL, 5UL, 1UL + 6UL, 7UL, 3UL + 7UL, 6UL, 3UL ] + ) + ) + +[] +let ``Level bfs 8 node grid start 0`` () = + runTest + grid8graph + (singleStart 8UL 0UL) + Graph.BFS.bfs_level + [ Some 0UL + Some 1UL + Some 2UL + Some 3UL + Some 1UL + Some 2UL + Some 3UL + Some 4UL ] + +[] +let ``Parent bfs 8 node grid start 0`` () = + runTest + grid8graph + (singleStart 8UL 0UL) + Graph.BFS.bfs_parent + [ Some 0UL + Some 0UL + Some 1UL + Some 2UL + Some 0UL + Some 1UL + Some 2UL + Some 3UL ] + +[] +let ``Level bfs 8 node grid start 7`` () = + runTest + grid8graph + (singleStart 8UL 7UL) + Graph.BFS.bfs_level + [ Some 4UL + Some 3UL + Some 2UL + Some 1UL + Some 3UL + Some 2UL + Some 1UL + Some 0UL ] + +[] +let ``Parent bfs 8 node grid start 7`` () = + runTest + grid8graph + (singleStart 8UL 7UL) + Graph.BFS.bfs_parent + [ Some 1UL + Some 2UL + Some 3UL + Some 7UL + Some 5UL + Some 6UL + Some 7UL + Some 7UL ] + +[] +let ``Level bfs 8 node grid start 4`` () = + runTest + grid8graph + (singleStart 8UL 4UL) + Graph.BFS.bfs_level + [ Some 1UL + Some 2UL + Some 3UL + Some 4UL + Some 0UL + Some 1UL + Some 2UL + Some 3UL ] + +[] +let ``Parent bfs 8 node grid start 4`` () = + runTest + grid8graph + (singleStart 8UL 4UL) + Graph.BFS.bfs_parent + [ Some 4UL + Some 0UL + Some 1UL + Some 2UL + Some 4UL + Some 4UL + Some 5UL + Some 6UL ] + +// ============== 10 nodes random ============== + +let private random10graph = + Matrix.fromCoordinateList ( + Matrix.CoordinateList( + 10UL, + 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 ] + ) + ) + +[] +let ``Level bfs 10 node random start 0`` () = + runTest + random10graph + (singleStart 10UL 0UL) + Graph.BFS.bfs_level + [ Some 0UL + Some 1UL + Some 2UL + Some 3UL + Some 4UL + Some 1UL + Some 2UL + Some 3UL + Some 4UL + Some 5UL ] + +[] +let ``Parent bfs 10 node random start 0`` () = + runTest + random10graph + (singleStart 10UL 0UL) + Graph.BFS.bfs_parent + [ Some 0UL + Some 0UL + Some 1UL + Some 2UL + Some 3UL + Some 0UL + Some 1UL + Some 2UL + Some 3UL + Some 4UL ] + +[] +let ``Level bfs 10 node random start 9`` () = + runTest + random10graph + (singleStart 10UL 9UL) + Graph.BFS.bfs_level + [ Some 5UL + Some 4UL + Some 3UL + Some 2UL + Some 1UL + Some 4UL + Some 3UL + Some 2UL + Some 1UL + Some 0UL ] + +[] +let ``Parent bfs 10 node random start 9`` () = + runTest + random10graph + (singleStart 10UL 9UL) + Graph.BFS.bfs_parent + [ Some 1UL + Some 2UL + Some 3UL + Some 4UL + Some 9UL + Some 6UL + Some 7UL + Some 8UL + Some 9UL + Some 9UL ] + +[] +let ``Level bfs 10 node random start 5`` () = + runTest + random10graph + (singleStart 10UL 5UL) + Graph.BFS.bfs_level + [ Some 1UL + Some 2UL + Some 3UL + Some 4UL + Some 5UL + Some 0UL + Some 1UL + Some 2UL + Some 3UL + Some 4UL ] + +[] +let ``Parent bfs 10 node random start 5`` () = + runTest + random10graph + (singleStart 10UL 5UL) + Graph.BFS.bfs_parent + [ Some 5UL + Some 0UL + Some 1UL + Some 2UL + Some 3UL + Some 5UL + Some 5UL + Some 6UL + Some 7UL + Some 8UL ] + +// ============== 12 nodes big ============== + +let private big12graph = + Matrix.fromCoordinateList ( + Matrix.CoordinateList( + 12UL, + 12UL, + [ 0UL, 1UL, 1UL + 1UL, 0UL, 1UL + 1UL, 11UL, 1UL + 11UL, 1UL, 1UL + 0UL, 11UL, 1UL + 11UL, 0UL, 1UL + 2UL, 3UL, 1UL + 3UL, 2UL, 1UL + 3UL, 4UL, 1UL + 4UL, 3UL, 1UL + 2UL, 4UL, 1UL + 4UL, 2UL, 1UL + 5UL, 6UL, 1UL + 6UL, 5UL, 1UL + 6UL, 7UL, 1UL + 7UL, 6UL, 1UL + 5UL, 7UL, 1UL + 7UL, 5UL, 1UL + 8UL, 9UL, 1UL + 9UL, 8UL, 1UL + 9UL, 10UL, 1UL + 10UL, 9UL, 1UL + 8UL, 10UL, 1UL + 10UL, 8UL, 1UL + 1UL, 2UL, 2UL + 2UL, 1UL, 2UL + 11UL, 4UL, 2UL + 4UL, 11UL, 2UL + 10UL, 5UL, 2UL + 5UL, 10UL, 2UL + 8UL, 7UL, 2UL + 7UL, 8UL, 2UL + 10UL, 11UL, 3UL + 11UL, 10UL, 3UL + 5UL, 4UL, 3UL + 4UL, 5UL, 3UL ] + ) + ) + +[] +let ``Level bfs 12 node big start 0`` () = + runTest + big12graph + (singleStart 12UL 0UL) + Graph.BFS.bfs_level + [ Some 0UL + Some 1UL + Some 2UL + Some 3UL + Some 2UL + Some 3UL + Some 4UL + Some 4UL + Some 3UL + Some 3UL + Some 2UL + Some 1UL ] + +[] +let ``Parent bfs 12 node big start 0`` () = + runTest + big12graph + (singleStart 12UL 0UL) + Graph.BFS.bfs_parent + [ Some 0UL + Some 0UL + Some 1UL + Some 2UL + Some 11UL + Some 4UL + Some 5UL + Some 5UL + Some 10UL + Some 10UL + Some 11UL + Some 0UL ] + +[] +let ``Level bfs 12 node big start 11`` () = + runTest + big12graph + (singleStart 12UL 11UL) + Graph.BFS.bfs_level + [ Some 1UL + Some 1UL + Some 2UL + Some 2UL + Some 1UL + Some 2UL + Some 3UL + Some 3UL + Some 2UL + Some 2UL + Some 1UL + Some 0UL ] + +[] +let ``Parent bfs 12 node big start 11`` () = + runTest + big12graph + (singleStart 12UL 11UL) + Graph.BFS.bfs_parent + [ Some 11UL + Some 11UL + Some 1UL + Some 4UL + Some 11UL + Some 4UL + Some 5UL + Some 5UL + Some 10UL + Some 10UL + Some 11UL + Some 11UL ] + +[] +let ``Level bfs 12 node big start 6`` () = + runTest + big12graph + (singleStart 12UL 6UL) + Graph.BFS.bfs_level + [ Some 4UL + Some 4UL + Some 3UL + Some 3UL + Some 2UL + Some 1UL + Some 0UL + Some 1UL + Some 2UL + Some 3UL + Some 2UL + Some 3UL ] + +[] +let ``Parent bfs 12 node big start 6`` () = + runTest + big12graph + (singleStart 12UL 6UL) + Graph.BFS.bfs_parent + [ Some 11UL + Some 2UL + Some 4UL + Some 4UL + Some 5UL + Some 6UL + Some 6UL + Some 6UL + Some 7UL + Some 8UL + Some 5UL + Some 4UL ] + +// ============== 10 nodes complex line ============== + +let private complexLine10graph = + Matrix.fromCoordinateList ( + Matrix.CoordinateList( + 10UL, + 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 ] + ) + ) + +[] +let ``Level bfs 10 node complex line start 0`` () = + runTest + complexLine10graph + (singleStart 10UL 0UL) + Graph.BFS.bfs_level + [ Some 0UL + Some 1UL + Some 2UL + Some 3UL + Some 4UL + Some 5UL + Some 6UL + Some 7UL + Some 8UL + Some 9UL ] + +[] +let ``Parent bfs 10 node complex line start 0`` () = + runTest + complexLine10graph + (singleStart 10UL 0UL) + Graph.BFS.bfs_parent + [ Some 0UL + Some 0UL + Some 1UL + Some 2UL + Some 3UL + Some 4UL + Some 5UL + Some 6UL + Some 7UL + Some 8UL ] + +[] +let ``Level bfs 10 node complex line start 9`` () = + runTest + complexLine10graph + (singleStart 10UL 9UL) + Graph.BFS.bfs_level + [ Some 9UL + Some 8UL + Some 7UL + Some 6UL + Some 5UL + Some 4UL + Some 3UL + Some 2UL + Some 1UL + Some 0UL ] + +[] +let ``Parent bfs 10 node complex line start 9`` () = + runTest + complexLine10graph + (singleStart 10UL 9UL) + Graph.BFS.bfs_parent + [ Some 1UL + Some 2UL + Some 3UL + Some 4UL + Some 5UL + Some 6UL + Some 7UL + Some 8UL + Some 9UL + Some 9UL ] + +[] +let ``Level bfs 10 node complex line start 5`` () = + runTest + complexLine10graph + (singleStart 10UL 5UL) + Graph.BFS.bfs_level + [ Some 5UL + Some 4UL + Some 3UL + Some 2UL + Some 1UL + Some 0UL + Some 1UL + Some 2UL + Some 3UL + Some 4UL ] + +[] +let ``Parent bfs 10 node complex line start 5`` () = + runTest + complexLine10graph + (singleStart 10UL 5UL) + Graph.BFS.bfs_parent + [ Some 1UL + Some 2UL + Some 3UL + Some 4UL + Some 5UL + Some 5UL + Some 5UL + Some 6UL + Some 7UL + Some 8UL ] diff --git a/QuadTree.Tests/Tests.LinearAlgebra.fs b/QuadTree.Tests/Tests.LinearAlgebra.fs index 3bde7b3..f71278c 100644 --- a/QuadTree.Tests/Tests.LinearAlgebra.fs +++ b/QuadTree.Tests/Tests.LinearAlgebra.fs @@ -6,6 +6,7 @@ open Xunit open Matrix open Vector open Common +open Result (* 2,2,2,2 @@ -345,6 +346,54 @@ let ``Simple vxmi_values. 4 * (4x3).`` () = Assert.Equal(expected, actual) +[] +let ``vxmi_values 3x3 line graph start 0. BFS semantics`` () = + // 3-node line graph (3x3 stored as 4x4): + // N 1 N D + // 1 N 1 D + // N 1 N D + // D D D D + let m = + let tree = + Matrix.qtree.Node( + Matrix.qtree.Node(leaf_n (), leaf_v 1UL, leaf_v 1UL, leaf_n ()), + Matrix.qtree.Node(leaf_n (), leaf_d (), leaf_v 1UL, leaf_d ()), + Matrix.qtree.Node(leaf_n (), leaf_v 1UL, leaf_d (), leaf_d ()), + Matrix.qtree.Node(leaf_n (), leaf_d (), leaf_d (), leaf_d ()) + ) + + let store = Matrix.Storage(4UL, tree) + SparseMatrix(3UL, 3UL, 4UL, store) + + // frontier = start 0 mapped to level 0: + // [0, N, N, D] = Node(Node(0, N), Node(N, D)) + let f = + let tree = + Vector.btree.Node(Vector.btree.Node(vleaf_v 0UL, vleaf_n ()), Vector.btree.Node(vleaf_n (), vleaf_d ())) + + let store = Vector.Storage(4UL, tree) + SparseVector(3UL, 1UL, store) + + let op_add x y = + match (x, y) with + | Some(v), _ + | _, Some(v) -> Some(v) + | _ -> None + + let op_mult (_, vp) (_, _, _) = Some(vp + 1UL) + + let expected = + // result = [N, 1, N, D] = Node(Node(N, 1), Node(N, D)) + let tree = + Vector.btree.Node(Vector.btree.Node(vleaf_n (), vleaf_v 1UL), Vector.btree.Node(vleaf_n (), vleaf_d ())) + + let store = Vector.Storage(4UL, tree) + Ok(SparseVector(3UL, 1UL, store)) + + let actual = LinearAlgebra.vxmi_values op_add op_mult f m + + Assert.Equal(expected, actual) + [] let ``Simple mxm`` () = @@ -376,94 +425,98 @@ 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 msg -> Assert.Fail "mxm failed" [] let ``Sparse mxm`` () = - let m1 = - let d = - [ 0UL, 0UL, 1 - 1UL, 1UL, 2 - 2UL, 2UL, 3 ] - - let clist = Matrix.CoordinateList(3UL, 3UL, d) - Matrix.fromCoordinateList clist - - let m2 = - let d = - [ 0UL, 0UL, 3 - 1UL, 1UL, 2 - 2UL, 2UL, 1 ] - - let clist = Matrix.CoordinateList(3UL, 3UL, d) - Matrix.fromCoordinateList clist - - let expected = - let d = - [ 0UL, 0UL, 3 - 1UL, 1UL, 4 - 2UL, 2UL, 3 ] - - let clist = Matrix.CoordinateList(3UL, 3UL, d) - Matrix.fromCoordinateList clist - - let actual = - match LinearAlgebra.mxm op_add op_mult m1 m2 with - | Ok m -> m - | Error e -> failwith (e.ToString()) - - Assert.Equal(expected, actual) + let mkMatrix rows cols data : Result, string> = + Matrix.fromCoordinateList (Matrix.CoordinateList(rows, cols, data)) + + let d1 = + [ (0UL, 0UL, 1) + (1UL, 1UL, 2) + (2UL, 2UL, 3) ] + + let d2 = + [ (0UL, 0UL, 3) + (1UL, 1UL, 2) + (2UL, 2UL, 1) ] + + let dExpected = + [ (0UL, 0UL, 3) + (1UL, 1UL, 4) + (2UL, 2UL, 3) ] + + resultM { + let! m1 = mkMatrix 3UL 3UL d1 + let! m2 = mkMatrix 3UL 3UL d2 + let! expected = mkMatrix 3UL 3UL dExpected + + let! actual = + match LinearAlgebra.mxm op_add op_mult m1 m2 with + | Ok x -> Ok x + | Error _ -> Error "mxm failed" + + Assert.Equal(Matrix.toCoordinateList expected, Matrix.toCoordinateList actual) + return () + } + |> ignore [] let ``Shrinking mxm`` () = // 2 x 3 // 1 0 2 // 0 3 0 - let m1 = - let d = - [ 0UL, 0UL, 1 - 0UL, 2UL, 2 - 1UL, 1UL, 3 ] + let mkMatrix rows cols data : Result, string> = + Matrix.fromCoordinateList (Matrix.CoordinateList(rows, cols, data)) - let clist = Matrix.CoordinateList(2UL, 3UL, d) - Matrix.fromCoordinateList clist + let d1 = + [ (0UL, 0UL, 1) + (0UL, 2UL, 2) + (1UL, 1UL, 3) ] // 3 x 2 // 0 4 // 5 0 // 6 0 - let m2 = - let d = - [ 0UL, 1UL, 4 - 1UL, 0UL, 5 - 2UL, 0UL, 6 ] - let clist = Matrix.CoordinateList(3UL, 2UL, d) - Matrix.fromCoordinateList clist + let d2 = + [ (0UL, 1UL, 4) + (1UL, 0UL, 5) + (2UL, 0UL, 6) ] // 2 x 2 // 12 4 // 15 0 - let expected = - let d = - [ 0UL, 0UL, 12 - 0UL, 1UL, 4 - 1UL, 0UL, 15 ] - let clist = Matrix.CoordinateList(2UL, 2UL, d) - Matrix.fromCoordinateList clist + let dExpected = + [ (0UL, 0UL, 12) + (0UL, 1UL, 4) + (1UL, 0UL, 15) ] - let actual = - match LinearAlgebra.mxm op_add op_mult m1 m2 with - | Ok m -> m - | Error e -> failwith (e.ToString()) + resultM { + let! m1 = mkMatrix 2UL 3UL d1 + let! m2 = mkMatrix 3UL 2UL d2 + let! expected = mkMatrix 2UL 2UL dExpected - Assert.Equal(expected, actual) + let! actual = + match LinearAlgebra.mxm op_add op_mult m1 m2 with + | Ok x -> Ok x + | Error msg -> Error "mxm failed" + + let expectedList = Matrix.toCoordinateList expected + let actualList = Matrix.toCoordinateList 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)) + ) + + return () + } + |> ignore [] let ``Expanding mxm`` () = @@ -471,45 +524,54 @@ let ``Expanding mxm`` () = // 1 0 // 0 2 // 3 0 - let m1 = - let d = - [ 0UL, 0UL, 1 - 1UL, 1UL, 2 - 2UL, 0UL, 3 ] + let mkMatrix rows cols data : Result, string> = + Matrix.fromCoordinateList (Matrix.CoordinateList(rows, cols, data)) + + let d1 = + [ (0UL, 0UL, 1) + (1UL, 1UL, 2) + (2UL, 0UL, 3) ] - let clist = Matrix.CoordinateList(3UL, 2UL, d) - Matrix.fromCoordinateList clist // 2 x 3 // 4 5 6 // 0 0 0 - let m2 = - let d = - [ 0UL, 0UL, 4 - 0UL, 1UL, 5 - 0UL, 2UL, 6 ] - let clist = Matrix.CoordinateList(2UL, 3UL, d) - Matrix.fromCoordinateList clist + let d2 = + [ (0UL, 0UL, 4) + (0UL, 1UL, 5) + (0UL, 2UL, 6) ] // 3 x 3 // 4 5 6 // 0 0 0 // 12 15 18 - let expected = - let d = - [ 0UL, 0UL, 4 - 0UL, 1UL, 5 - 0UL, 2UL, 6 - 2UL, 0UL, 12 - 2UL, 1UL, 15 - 2UL, 2UL, 18 ] - - let clist = Matrix.CoordinateList(3UL, 3UL, d) - Matrix.fromCoordinateList clist - - let actual = - match LinearAlgebra.mxm op_add op_mult m1 m2 with - | Ok m -> m - | Error e -> failwith (e.ToString()) - Assert.Equal(expected, actual) + let dExpected = + [ (0UL, 0UL, 4) + (0UL, 1UL, 5) + (0UL, 2UL, 6) + (2UL, 0UL, 12) + (2UL, 1UL, 15) + (2UL, 2UL, 18) ] + + resultM { + let! m1 = mkMatrix 3UL 2UL d1 + let! m2 = mkMatrix 2UL 3UL d2 + let! expected = mkMatrix 3UL 3UL dExpected + + let! actual = + match LinearAlgebra.mxm op_add op_mult m1 m2 with + | Ok x -> Ok x + | Error _ -> Error "mxm failed" + + let sortList (coo: Matrix.CoordinateList) = + coo.list |> List.sortBy (fun (r, c, _) -> (r, c)) + + Assert.Equal<(uint64 * uint64 * int) list>( + sortList (Matrix.toCoordinateList expected), + sortList (Matrix.toCoordinateList actual) + ) + + return () + } + |> ignore diff --git a/QuadTree.Tests/Tests.Boruvka.fs b/QuadTree.Tests/Tests.MST.fs similarity index 77% rename from QuadTree.Tests/Tests.Boruvka.fs rename to QuadTree.Tests/Tests.MST.fs index 6f2e2a0..fbc448a 100644 --- a/QuadTree.Tests/Tests.Boruvka.fs +++ b/QuadTree.Tests/Tests.MST.fs @@ -1,11 +1,7 @@ module Graph.Boruvka.Tests -open System open Xunit - open Matrix -open Vector -open Common let checkResult name actual expected = match actual with @@ -20,11 +16,23 @@ let checkResult name actual expected = | _ -> None) Assert.Equal(expected, actual) - | x -> Assert.Fail(sprintf "Boruvka failed: %A" x) - -[] -let ``Boruvka MST 2 nodes.`` () = - + | x -> Assert.Fail(sprintf $"MST {name} failed: {x}") + +let runMstTest + (graphResult: Result, string>) + (expectedResult: Result, string>) + mstFunc + = + match graphResult, expectedResult with + | Ok graph, Ok expected -> + let actual = mstFunc graph + checkResult "MST" actual (Ok expected) + | Error msg, _ -> Assert.Fail $"Graph init failed: {msg}" + | _, Error msg -> Assert.Fail $"Expected init failed: {msg}" + +// ============== Shared test data ============== + +let private ``test 2 nodes`` () = let graph = let clist = Matrix.CoordinateList( @@ -35,22 +43,9 @@ let ``Boruvka MST 2 nodes.`` () = 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 ``Boruvka MST 3 nodes line.`` () = + graph, graph +let private ``test 3 nodes line`` () = let graph = let clist = Matrix.CoordinateList( @@ -64,26 +59,9 @@ let ``Boruvka MST 3 nodes line.`` () = Matrix.fromCoordinateList clist - let expected = - let clist = - Matrix.CoordinateList( - 3UL, - 3UL, - [ 0UL, 1UL, 1UL - 1UL, 0UL, 1UL - 1UL, 2UL, 2UL - 2UL, 1UL, 2UL ] - ) - - Matrix.fromCoordinateList clist |> Ok - - checkResult (Graph.Boruvka.mst graph) expected - - - -[] -let ``Boruvka MST 4 nodes line.`` () = + graph, graph +let private ``test 4 nodes line`` () = let graph = let clist = Matrix.CoordinateList( @@ -99,27 +77,9 @@ let ``Boruvka MST 4 nodes line.`` () = Matrix.fromCoordinateList clist - let expected = - let clist = - Matrix.CoordinateList( - 4UL, - 4UL, - [ 0UL, 1UL, 1UL - 1UL, 0UL, 1UL - 1UL, 2UL, 2UL - 2UL, 1UL, 2UL - 2UL, 3UL, 3UL - 3UL, 2UL, 3UL ] - ) - - Matrix.fromCoordinateList clist |> Ok - - checkResult (Graph.Boruvka.mst graph) expected - - -[] -let ``Boruvka MST 5 nodes line.`` () = + graph, graph +let private ``test 5 nodes line`` () = let graph = let clist = Matrix.CoordinateList( @@ -137,29 +97,9 @@ let ``Boruvka MST 5 nodes line.`` () = Matrix.fromCoordinateList clist - let expected = - let clist = - Matrix.CoordinateList( - 5UL, - 5UL, - [ 0UL, 1UL, 1UL - 1UL, 0UL, 1UL - 1UL, 2UL, 2UL - 2UL, 1UL, 2UL - 2UL, 3UL, 3UL - 3UL, 2UL, 3UL - 3UL, 4UL, 4UL - 4UL, 3UL, 4UL ] - ) - - Matrix.fromCoordinateList clist |> Ok - - checkResult (Graph.Boruvka.mst graph) expected - - -[] -let ``Boruvka MST 5 nodes star.`` () = + graph, graph +let private ``test 5 nodes star`` () = let graph = let clist = Matrix.CoordinateList( @@ -177,29 +117,51 @@ let ``Boruvka MST 5 nodes star.`` () = Matrix.fromCoordinateList clist + graph, graph + +let private ``test square`` () = + let graph = + let clist = + Matrix.CoordinateList( + 4UL, + 4UL, + [ 0UL, 1UL, 1UL + 1UL, 0UL, 1UL + + 1UL, 2UL, 2UL + 2UL, 1UL, 2UL + + 2UL, 3UL, 1UL + 3UL, 2UL, 1UL + + 3UL, 0UL, 2UL + 0UL, 3UL, 2UL ] + ) + + Matrix.fromCoordinateList clist + let expected = let clist = Matrix.CoordinateList( - 5UL, - 5UL, - [ 0UL, 1UL, 5UL - 1UL, 0UL, 5UL - 0UL, 2UL, 4UL - 2UL, 0UL, 4UL - 0UL, 3UL, 3UL - 3UL, 0UL, 3UL - 0UL, 4UL, 2UL - 4UL, 0UL, 2UL ] + 4UL, + 4UL, + [ 0UL, 1UL, 1UL + 1UL, 0UL, 1UL + + 2UL, 3UL, 1UL + 3UL, 2UL, 1UL + + 3UL, 0UL, 2UL + 0UL, 3UL, 2UL ] ) - Matrix.fromCoordinateList clist |> Ok + Matrix.fromCoordinateList clist - checkResult (Graph.Boruvka.mst graph) expected + graph, expected -[] -let ``Boruvka MST 5 nodes complete.`` () = +let private ``test 5 nodes complete`` () = let graph = let clist = Matrix.CoordinateList( @@ -244,14 +206,11 @@ let ``Boruvka MST 5 nodes complete.`` () = 4UL, 0UL, 4UL ] ) - Matrix.fromCoordinateList clist |> Ok - - checkResult (Graph.Boruvka.mst graph) expected + Matrix.fromCoordinateList clist + graph, expected -[] -let ``Boruvka MST two components.`` () = - +let private ``test two components`` () = let graph = let clist = Matrix.CoordinateList( @@ -291,14 +250,11 @@ let ``Boruvka MST two components.`` () = 5UL, 4UL, 2UL ] ) - Matrix.fromCoordinateList clist |> Ok - - checkResult (Graph.Boruvka.mst graph) expected - + Matrix.fromCoordinateList clist -[] -let ``Boruvka MST cycle graph 6 nodes.`` () = + graph, expected +let private ``test cycle graph 6 nodes`` () = let graph = let clist = Matrix.CoordinateList( @@ -337,13 +293,11 @@ let ``Boruvka MST cycle graph 6 nodes.`` () = 5UL, 4UL, 5UL ] ) - Matrix.fromCoordinateList clist |> Ok - - checkResult (Graph.Boruvka.mst graph) expected + Matrix.fromCoordinateList clist -[] -let ``Boruvka MST complete bipartite K3,3.`` () = + graph, expected +let private ``test complete bipartite K3,3`` () = let graph = let clist = Matrix.CoordinateList( @@ -398,13 +352,11 @@ let ``Boruvka MST complete bipartite K3,3.`` () = 5UL, 0UL, 3UL ] ) - Matrix.fromCoordinateList clist |> Ok - - checkResult (Graph.Boruvka.mst graph) expected + Matrix.fromCoordinateList clist -[] -let ``Boruvka MST random weights.`` () = + graph, expected +let private ``test random weights`` () = let graph = let clist = Matrix.CoordinateList( @@ -412,28 +364,40 @@ let ``Boruvka MST random weights.`` () = 8UL, [ 0UL, 1UL, 7UL 1UL, 0UL, 7UL + 0UL, 2UL, 5UL 2UL, 0UL, 5UL + 0UL, 3UL, 9UL 3UL, 0UL, 9UL + 1UL, 2UL, 3UL 2UL, 1UL, 3UL + 1UL, 3UL, 4UL 3UL, 1UL, 4UL + 2UL, 3UL, 2UL 3UL, 2UL, 2UL + 4UL, 5UL, 1UL 5UL, 4UL, 1UL + 4UL, 6UL, 6UL 6UL, 4UL, 6UL + 4UL, 7UL, 8UL 7UL, 4UL, 8UL + 5UL, 6UL, 3UL 6UL, 5UL, 3UL + 5UL, 7UL, 5UL 7UL, 5UL, 5UL + 6UL, 7UL, 2UL 7UL, 6UL, 2UL + // Connect two components 3UL, 4UL, 10UL 4UL, 3UL, 10UL ] @@ -448,29 +412,31 @@ let ``Boruvka MST random weights.`` () = 8UL, [ 0UL, 2UL, 5UL 2UL, 0UL, 5UL + 1UL, 2UL, 3UL 2UL, 1UL, 3UL + 2UL, 3UL, 2UL 3UL, 2UL, 2UL - 1UL, 3UL, 4UL - 3UL, 1UL, 4UL + + 4UL, 3UL, 10UL + 3UL, 4UL, 10UL + 4UL, 5UL, 1UL 5UL, 4UL, 1UL + 5UL, 6UL, 3UL 6UL, 5UL, 3UL + 6UL, 7UL, 2UL - 7UL, 6UL, 2UL - 3UL, 4UL, 10UL - 4UL, 3UL, 10UL ] + 7UL, 6UL, 2UL ] ) - Matrix.fromCoordinateList clist |> Ok - - checkResult (Graph.Boruvka.mst graph) expected + Matrix.fromCoordinateList clist -[] -let ``Boruvka MST 8 nodes grid.`` () = + graph, expected +let private ``test 8 nodes grid`` () = let graph = let clist = Matrix.CoordinateList( @@ -525,13 +491,11 @@ let ``Boruvka MST 8 nodes grid.`` () = 7UL, 6UL, 3UL ] ) - Matrix.fromCoordinateList clist |> Ok - - checkResult (Graph.Boruvka.mst graph) expected + Matrix.fromCoordinateList clist -[] -let ``Boruvka MST 10 nodes random.`` () = + graph, expected +let private ``test 10 nodes random`` () = let graph = let clist = Matrix.CoordinateList( @@ -539,28 +503,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,40 +550,37 @@ 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 + 2UL, 3UL, 1UL + 3UL, 4UL, 2UL 4UL, 3UL, 2UL - 3UL, 8UL, 6UL - 8UL, 3UL, 6UL - 4UL, 9UL, 3UL + 9UL, 4UL, 3UL - 5UL, 6UL, 1UL + 4UL, 9UL, 3UL + + 0UL, 5UL, 2UL + 5UL, 0UL, 2UL + 6UL, 5UL, 1UL - 6UL, 7UL, 2UL + 5UL, 6UL, 1UL + 7UL, 6UL, 2UL + 6UL, 7UL, 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 -[] -let ``Boruvka MST simple triangle.`` () = + graph, expected +let private ``test simple triangle`` () = let graph = let clist = Matrix.CoordinateList( @@ -620,14 +593,11 @@ let ``Boruvka MST simple triangle.`` () = 2UL, 0UL, 1UL 1UL, 2UL, 1UL - 2UL, 1UL, 1UL - - ] + 2UL, 1UL, 1UL ] ) Matrix.fromCoordinateList clist - let expected = let clist = Matrix.CoordinateList( @@ -640,14 +610,11 @@ let ``Boruvka MST simple triangle.`` () = 2UL, 0UL, 1UL ] ) - Matrix.fromCoordinateList clist |> Ok - - checkResult (Graph.Boruvka.mst graph) expected + Matrix.fromCoordinateList clist + graph, expected -[] -let ``Boruvka MST simple square.`` () = - +let private ``test simple square`` () = let graph = let clist = Matrix.CoordinateList( @@ -663,14 +630,11 @@ let ``Boruvka MST simple square.`` () = 3UL, 2UL, 1UL 0UL, 3UL, 1UL - 3UL, 0UL, 1UL - - ] + 3UL, 0UL, 1UL ] ) Matrix.fromCoordinateList clist - let expected = let clist = Matrix.CoordinateList( @@ -686,16 +650,11 @@ let ``Boruvka MST simple square.`` () = 2UL, 1UL, 1UL ] ) - Matrix.fromCoordinateList clist |> Ok - - checkResult (Graph.Boruvka.mst graph) expected - - + Matrix.fromCoordinateList clist + graph, expected -[] -let ``Boruvka MST simple square in two steps.`` () = - +let private ``test simple square in two steps`` () = let graph = let clist = Matrix.CoordinateList( @@ -711,14 +670,11 @@ let ``Boruvka MST simple square in two steps.`` () = 3UL, 2UL, 2UL 0UL, 3UL, 1UL - 3UL, 0UL, 1UL - - ] + 3UL, 0UL, 1UL ] ) Matrix.fromCoordinateList clist - let expected = let clist = Matrix.CoordinateList( @@ -734,16 +690,11 @@ let ``Boruvka MST simple square in two steps.`` () = 2UL, 1UL, 1UL ] ) - Matrix.fromCoordinateList clist |> Ok - - checkResult (Graph.Boruvka.mst graph) expected - - - + Matrix.fromCoordinateList clist -[] -let ``Boruvka MST.`` () = + graph, expected +let private ``test 7 nodes`` () = let graph = let clist = Matrix.CoordinateList( @@ -785,7 +736,6 @@ let ``Boruvka MST.`` () = Matrix.fromCoordinateList clist - let expected = let clist = Matrix.CoordinateList( @@ -807,19 +757,14 @@ let ``Boruvka MST.`` () = 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 -[] -let ``Boruvka MST big.`` () = + graph, expected +let private ``test big`` () = let graph = let clist = Matrix.CoordinateList( @@ -877,14 +822,11 @@ let ``Boruvka MST big.`` () = 11UL, 10UL, 3UL 5UL, 4UL, 3UL - 4UL, 5UL, 3UL - - ] + 4UL, 5UL, 3UL ] ) Matrix.fromCoordinateList clist - let expected = let clist = Matrix.CoordinateList( @@ -921,19 +863,14 @@ 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 -[] -let ``Boruvka MST complex line.`` () = + graph, expected +let private ``test complex line`` () = let graph = let clist = Matrix.CoordinateList( @@ -964,16 +901,15 @@ let ``Boruvka MST complex line.`` () = 8UL, 7UL, 1UL 8UL, 9UL, 1UL - 9UL, 8UL, 1UL - - - ] + 9UL, 8UL, 1UL ] ) Matrix.fromCoordinateList clist + graph, graph - let expected = +let private ``test complex line 2`` () = + let graph = let clist = Matrix.CoordinateList( 10UL, @@ -987,110 +923,247 @@ let ``Boruvka MST complex line.`` () = 2UL, 3UL, 1UL 3UL, 2UL, 1UL - 3UL, 4UL, 3UL - 4UL, 3UL, 3UL + 3UL, 9UL, 3UL + 9UL, 3UL, 3UL - 4UL, 5UL, 1UL - 5UL, 4UL, 1UL + 9UL, 8UL, 1UL + 8UL, 9UL, 1UL - 5UL, 6UL, 1UL - 6UL, 5UL, 1UL + 8UL, 7UL, 1UL + 7UL, 8UL, 1UL 6UL, 7UL, 2UL 7UL, 6UL, 2UL - 7UL, 8UL, 1UL - 8UL, 7UL, 1UL + 5UL, 6UL, 1UL + 6UL, 5UL, 1UL - 8UL, 9UL, 1UL - 9UL, 8UL, 1UL + 5UL, 4UL, 1UL + 4UL, 5UL, 1UL ] + ) + Matrix.fromCoordinateList clist - ] - ) + graph, graph - Matrix.fromCoordinateList clist |> Ok - checkResult (Graph.Boruvka.mst graph) expected +// ============== Tests ============== +[] +let ``Boruvka MST 2 nodes.`` () = + let graph, expected = ``test 2 nodes`` () + runMstTest graph expected Graph.Boruvka.mst [] -let ``Boruvka MST complex line 2.`` () = +let ``Maggs-Plotkin MST 2 nodes.`` () = + let graph, expected = ``test 2 nodes`` () + runMstTest graph expected Graph.Maggs_Plotkin_MST.mst - let graph = - let clist = - Matrix.CoordinateList( - 10UL, - 10UL, - [ 0UL, 1UL, 1UL - 1UL, 0UL, 1UL - 1UL, 2UL, 2UL - 2UL, 1UL, 2UL +[] +let ``Boruvka MST 3 nodes line.`` () = + let graph, expected = ``test 3 nodes line`` () + runMstTest graph expected Graph.Boruvka.mst - 2UL, 3UL, 1UL - 3UL, 2UL, 1UL +[] +let ``Maggs-Plotkin MST 3 nodes line.`` () = + let graph, expected = ``test 3 nodes line`` () + runMstTest graph expected Graph.Maggs_Plotkin_MST.mst - 3UL, 9UL, 3UL - 9UL, 3UL, 3UL - 9UL, 8UL, 1UL - 8UL, 9UL, 1UL +[] +let ``Boruvka MST 4 nodes line.`` () = + let graph, expected = ``test 4 nodes line`` () + runMstTest graph expected Graph.Boruvka.mst - 8UL, 7UL, 1UL - 7UL, 8UL, 1UL +[] +let ``Maggs-Plotkin MST 4 nodes line.`` () = + let graph, expected = ``test 4 nodes line`` () + runMstTest graph expected Graph.Maggs_Plotkin_MST.mst - 6UL, 7UL, 2UL - 7UL, 6UL, 2UL - 5UL, 6UL, 1UL - 6UL, 5UL, 1UL +[] +let ``Boruvka MST square.`` () = + let graph, expected = ``test square`` () + runMstTest graph expected Graph.Boruvka.mst - 5UL, 4UL, 1UL - 4UL, 5UL, 1UL +[] +let ``Maggs-Plotkin MST square.`` () = + let graph, expected = ``test square`` () + runMstTest graph expected Graph.Maggs_Plotkin_MST.mst - ] - ) +[] +let ``Boruvka MST 5 nodes line.`` () = + let graph, expected = ``test 5 nodes line`` () + runMstTest graph expected Graph.Boruvka.mst - Matrix.fromCoordinateList clist +[] +let ``Maggs-Plotkin MST 5 nodes line.`` () = + let graph, expected = ``test 5 nodes line`` () + runMstTest graph expected Graph.Maggs_Plotkin_MST.mst - let expected = - let clist = - Matrix.CoordinateList( - 10UL, - 10UL, - [ 0UL, 1UL, 1UL - 1UL, 0UL, 1UL +[] +let ``Boruvka MST 5 nodes star.`` () = + let graph, expected = ``test 5 nodes star`` () + runMstTest graph expected Graph.Boruvka.mst - 1UL, 2UL, 2UL - 2UL, 1UL, 2UL +[] +let ``Maggs-Plotkin MST 5 nodes star.`` () = + let graph, expected = ``test 5 nodes star`` () + runMstTest graph expected Graph.Maggs_Plotkin_MST.mst - 2UL, 3UL, 1UL - 3UL, 2UL, 1UL - 3UL, 9UL, 3UL - 9UL, 3UL, 3UL +[] +let ``Boruvka MST 5 nodes complete.`` () = + let graph, expected = ``test 5 nodes complete`` () + runMstTest graph expected Graph.Boruvka.mst - 9UL, 8UL, 1UL - 8UL, 9UL, 1UL +[] +let ``Maggs-Plotkin MST 5 nodes complete.`` () = + let graph, expected = ``test 5 nodes complete`` () + runMstTest graph expected Graph.Maggs_Plotkin_MST.mst - 8UL, 7UL, 1UL - 7UL, 8UL, 1UL - 6UL, 7UL, 2UL - 7UL, 6UL, 2UL +[] +let ``Boruvka MST two components.`` () = + let graph, expected = ``test two components`` () + runMstTest graph expected Graph.Boruvka.mst - 5UL, 6UL, 1UL - 6UL, 5UL, 1UL +[] +let ``Maggs-Plotkin MST two components.`` () = + let graph, expected = ``test two components`` () + runMstTest graph expected Graph.Maggs_Plotkin_MST.mst - 5UL, 4UL, 1UL - 4UL, 5UL, 1UL - ] - ) +[] +let ``Boruvka MST cycle graph 6 nodes.`` () = + let graph, expected = ``test cycle graph 6 nodes`` () + runMstTest graph expected Graph.Boruvka.mst + +[] +let ``Maggs-Plotkin MST cycle graph 6 nodes.`` () = + let graph, expected = ``test cycle graph 6 nodes`` () + runMstTest graph expected Graph.Maggs_Plotkin_MST.mst + + +[] +let ``Boruvka MST complete bipartite K3,3.`` () = + let graph, expected = ``test complete bipartite K3,3`` () + runMstTest graph expected Graph.Boruvka.mst + +[] +let ``Maggs-Plotkin MST complete bipartite K3,3.`` () = + let graph, expected = ``test complete bipartite K3,3`` () + runMstTest graph expected Graph.Maggs_Plotkin_MST.mst + + +[] +let ``Boruvka MST random weights.`` () = + let graph, expected = ``test random weights`` () + runMstTest graph expected Graph.Boruvka.mst + +[] +let ``Maggs-Plotkin MST random weights.`` () = + let graph, expected = ``test random weights`` () + runMstTest graph expected Graph.Maggs_Plotkin_MST.mst - Matrix.fromCoordinateList clist |> Ok - checkResult (Graph.Boruvka.mst graph) expected +[] +let ``Boruvka MST 8 nodes grid.`` () = + let graph, expected = ``test 8 nodes grid`` () + runMstTest graph expected Graph.Boruvka.mst + +[] +let ``Maggs-Plotkin MST 8 nodes grid.`` () = + let graph, expected = ``test 8 nodes grid`` () + runMstTest graph expected Graph.Maggs_Plotkin_MST.mst + + +[] +let ``Boruvka MST 10 nodes random.`` () = + let graph, expected = ``test 10 nodes random`` () + runMstTest graph expected Graph.Boruvka.mst + +[] +let ``Maggs-Plotkin MST 10 nodes random.`` () = + let graph, expected = ``test 10 nodes random`` () + runMstTest graph expected Graph.Maggs_Plotkin_MST.mst + + +[] +let ``Boruvka MST simple triangle.`` () = + let graph, expected = ``test simple triangle`` () + runMstTest graph expected Graph.Boruvka.mst + +[] +let ``Maggs-Plotkin MST simple triangle.`` () = + let graph, expected = ``test simple triangle`` () + runMstTest graph expected Graph.Maggs_Plotkin_MST.mst + + +[] +let ``Boruvka MST simple square.`` () = + let graph, expected = ``test simple square`` () + runMstTest graph expected Graph.Boruvka.mst + +[] +let ``Maggs-Plotkin MST simple square.`` () = + let graph, expected = ``test simple square`` () + runMstTest graph expected Graph.Maggs_Plotkin_MST.mst + + +[] +let ``Boruvka MST simple square in two steps.`` () = + let graph, expected = ``test simple square in two steps`` () + runMstTest graph expected Graph.Boruvka.mst + +[] +let ``Maggs-Plotkin MST simple square in two steps.`` () = + let graph, expected = ``test simple square in two steps`` () + runMstTest graph expected Graph.Maggs_Plotkin_MST.mst + + +[] +let ``Boruvka MST.`` () = + let graph, expected = ``test 7 nodes`` () + runMstTest graph expected Graph.Boruvka.mst + +[] +let ``Maggs-Plotkin MST.`` () = + let graph, expected = ``test 7 nodes`` () + runMstTest graph expected Graph.Maggs_Plotkin_MST.mst + + +[] +let ``Boruvka MST big.`` () = + let graph, expected = ``test big`` () + runMstTest graph expected Graph.Boruvka.mst + +[] +let ``Maggs-Plotkin MST big.`` () = + let graph, expected = ``test big`` () + runMstTest graph expected Graph.Maggs_Plotkin_MST.mst + + +[] +let ``Boruvka MST complex line.`` () = + let graph, expected = ``test complex line`` () + runMstTest graph expected Graph.Boruvka.mst + +[] +let ``Maggs-Plotkin MST complex line.`` () = + let graph, expected = ``test complex line`` () + runMstTest graph expected Graph.Maggs_Plotkin_MST.mst + + +[] +let ``Boruvka MST complex line 2.`` () = + let graph, expected = ``test complex line 2`` () + runMstTest graph expected Graph.Boruvka.mst + +[] +let ``Maggs-Plotkin MST complex line 2.`` () = + let graph, expected = ``test complex line 2`` () + runMstTest graph expected Graph.Maggs_Plotkin_MST.mst 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..6036668 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 $"SSSP failed: {msg}" + | Error msg, _ -> Assert.Fail $"Matrix creation failed: {msg}" + | _, Error msg -> Assert.Fail $"Vector creation failed: {msg}" diff --git a/QuadTree.Tests/Tests.TriangleCount.fs b/QuadTree.Tests/Tests.TriangleCount.fs index 1b95cae..f184d12 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 "Triangle count failed" + | Error _ -> Assert.Fail "fromCoordinateList failed" [] 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 "Triangle count failed" + | Error _ -> Assert.Fail "fromCoordinateList failed" 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/BFS.fs b/QuadTree/BFS.fs index 0ba2fc5..4cda183 100644 --- a/QuadTree/BFS.fs +++ b/QuadTree/BFS.fs @@ -8,23 +8,20 @@ type Error = | FrontierCalculationProblem of Vector.Error | VisitedCalculationProblem of Vector.Error -let bfs_level graph startVertices = - let rec inner level (frontier: Vector.SparseVector<_>) (visited: Vector.SparseVector<_>) = +let bfs + (op_add: 'c option -> 'c option -> 'c option) + (op_mult: uint64 * 'c -> uint64 * uint64 * 'b -> Option<'c>) + (initVisited: uint64 -> Option<'c> -> Option<'c>) + (graph: Matrix.SparseMatrix<'b>) + (startVertices: Vector.SparseVector<'c>) + = + let initialVisited = Vector.mapi startVertices initVisited + + let rec inner (frontier: Vector.SparseVector<'c>) (visited: Vector.SparseVector<'c>) = if frontier.nvals > 0UL then resultM { let! new_frontier = - LinearAlgebra.vxm - (fun x y -> - match (x, y) with - | Some(v), _ - | _, Some(v) -> Some(v) - | _ -> None) - (fun x y -> - match (x, y) with - | Some(v), Some(_) -> Some(v) - | _ -> None) - frontier - graph + LinearAlgebra.vxmi_values op_add op_mult frontier graph |> Result.mapError NewFrontierCalculationProblem let! frontier = @@ -32,17 +29,44 @@ let bfs_level graph startVertices = |> Result.mapError FrontierCalculationProblem let! visited = - Vector.map2 visited new_frontier (fun x y -> - match (x, y) with - | Some(_), _ -> x - | None, Some(_) -> Some(level) - | _ -> None) + Vector.map2 visited frontier (fun oldVal newVal -> + match oldVal with + | Some _ -> oldVal + | None -> newVal) |> Result.mapError VisitedCalculationProblem - return! inner (level + 1UL) frontier visited + return! inner frontier visited } else Ok visited - let initialVisited = Vector.map startVertices (Option.map (fun x -> 0UL)) - inner 1UL startVertices initialVisited + inner startVertices initialVisited + +let bfs_level graph startVertices = + let op_add x y = + match (x, y) with + | Some(v), _ + | _, Some(v) -> Some(v) + | _ -> None + + let op_mult (_, vp) (_, _, _) = Some(vp + 1UL) + + let initVisited _ v = v |> Option.map (fun _ -> 0UL) + + let frontier0 = + Vector.mapi startVertices (fun _ v -> v |> Option.map (fun _ -> 0UL)) + + bfs op_add op_mult initVisited graph frontier0 + +let bfs_parent graph startVertices = + let op_add x y = + match (x, y) with + | Some(v), _ + | _, Some(v) -> Some(v) + | _ -> None + + let op_mult (vi, _) (_, _, _) = Some(uint64 vi) + + let initVisited i v = v |> Option.map (fun _ -> uint64 i) + + bfs op_add op_mult initVisited graph startVertices 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/LinearAlgebra.fs b/QuadTree/LinearAlgebra.fs index 7405e02..a57daca 100644 --- a/QuadTree/LinearAlgebra.fs +++ b/QuadTree/LinearAlgebra.fs @@ -36,9 +36,9 @@ let vxm op_add op_mult (vector: Vector.SparseVector<'a>) (matrix: Matrix.SparseM let v3 = Vector.SparseVector(data_length, nvals3, (Vector.Storage(new_size, t3))) let v4 = Vector.SparseVector(data_length, nvals4, (Vector.Storage(new_size, t4))) - let vAdd v1 (v2: Vector.SparseVector<_>) = - match v2.storage.data with - | Vector.Leaf(Dummy) -> Ok(v1) + let vAdd (v1: Vector.SparseVector<_>) (v2: Vector.SparseVector<_>) = + match v1.storage.data, v2.storage.data with + | _, Vector.Leaf(Dummy) -> Ok(v1) | _ -> Vector.map2 v1 v2 op_add let z1 = vAdd v1 v3 @@ -144,9 +144,9 @@ let vxmi_values let v3 = Vector.SparseVector(data_length, nvals3, (Vector.Storage(new_size, t3))) let v4 = Vector.SparseVector(data_length, nvals4, (Vector.Storage(new_size, t4))) - let vAdd v1 (v2: Vector.SparseVector<_>) = - match v2.storage.data with - | Vector.Leaf(Dummy) -> Ok(v1) + let vAdd (v1: Vector.SparseVector<_>) (v2: Vector.SparseVector<_>) = + match v1.storage.data, v2.storage.data with + | _, Vector.Leaf(Dummy) -> Ok(v1) | _ -> Vector.map2 v1 v2 op_add let z1 = vAdd v1 v3 @@ -183,14 +183,16 @@ let vxmi_values colIdx | Vector.btree.Leaf(UserValue(Some(_))), Matrix.qtree.Node(y1, y2, y3, y4) -> _do vector vector y1 y2 y3 y4 + | Vector.btree.Leaf(UserValue(None)), Matrix.qtree.Node(y1, y2, y3, y4) -> _do vector vector y1 y2 y3 y4 | Vector.btree.Node(x1, x2), Matrix.qtree.Leaf(UserValue(Some(_))) -> _do x1 x2 matrix matrix matrix matrix + | Vector.btree.Node(x1, x2), Matrix.qtree.Leaf(UserValue(None)) -> _do x1 x2 matrix matrix matrix matrix | Vector.btree.Node(x1, x2), Matrix.qtree.Node(y1, y2, y3, y4) -> _do x1 x2 y1 y2 y3 y4 - | Vector.btree.Leaf(UserValue(None)), _ - | _, Matrix.qtree.Leaf(UserValue(None)) -> Ok(Vector.btree.Leaf(UserValue(None)), 0UL) - | Vector.btree.Leaf(Dummy), _ | _, Matrix.qtree.Leaf(Dummy) -> Ok(Vector.btree.Leaf(Dummy), 0UL) + | Vector.btree.Leaf(UserValue(None)), _ + | _, Matrix.qtree.Leaf(UserValue(None)) -> Ok(Vector.btree.Leaf(UserValue(None)), 0UL) + if uint64 vector.length = uint64 matrix.nrows then let vector_storage = if uint64 vector.storage.size < uint64 matrix.storage.size then diff --git a/QuadTree/Maggs_Plotkin_MST.fs b/QuadTree/Maggs_Plotkin_MST.fs new file mode 100644 index 0000000..7027d7a --- /dev/null +++ b/QuadTree/Maggs_Plotkin_MST.fs @@ -0,0 +1,87 @@ +module Graph.Maggs_Plotkin_MST + +open Common +open Result +open Matrix + + +type Error = + | DiagAdditionProblem of Matrix.Error + | MSTComputationProblem of Matrix.Error + | ClosureComputationProblem of LinearAlgebra.Error + +let mst (graph: Matrix.SparseMatrix<'a>) = + + let _max x y = + match (x, y) with + | Some x, Some y -> max x y |> Some + | _ -> None + + let _min x y = + match (x, y) with + | Some x, Some y -> min x y |> Some + | Some x, None + | None, Some x -> Some x + | _ -> None + + resultM { + let! diag = + let zero = Unchecked.defaultof<'a> + + Matrix.fromCoordinateList ( + Matrix.CoordinateList( + graph.nrows, + graph.ncols, + [ for i in 0UL .. uint64 graph.nrows - 1UL -> (i * 1UL, i * 1UL, zero) ] + ) + ) + + let! graph = + match + Matrix.map2 graph diag (fun x y -> + match y with + | None -> x + | _ -> y) + with + | Ok g -> Ok g + | Error e -> Error(sprintf "DiagAdditionProblem: %A" e) + + let graph = + Matrix.mapi graph (fun i j v -> + Option.map (fun x -> x, min (uint64 i) (uint64 j), max (uint64 i) (uint64 j)) v) + + let! closure = + let rec compute (matrix: SparseMatrix<_>) = + let nnz = matrix.nvals + + resultM { + + let! step = LinearAlgebra.mxm _min _max matrix matrix + + if nnz = step.nvals && matrix.storage = step.storage then + return step + else + return! compute step + } + + match compute graph with + | Ok c -> Ok c + | Error e -> Error(sprintf "ClosureComputationProblem: %A" e) + + let! mst = + match + Matrix.map2i graph closure (fun i j x y -> + if uint64 i = uint64 j then + None + elif x = y then + match x with + | Some(w, _, _) -> Some(w) + | _ -> None + else + None) + with + | Ok m -> Ok m + | Error e -> Error(sprintf "MSTComputationProblem: %A" e) + + return mst + } 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/QuadTree.fsproj b/QuadTree/QuadTree.fsproj index 438678c..abfc6ec 100644 --- a/QuadTree/QuadTree.fsproj +++ b/QuadTree/QuadTree.fsproj @@ -14,6 +14,7 @@ + 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))) diff --git a/README.md b/README.md index 8084adb..bd6ec23 100644 --- a/README.md +++ b/README.md @@ -9,12 +9,13 @@ Infrastructure for benchmarking the implemented algorithms is available in the [ ## Implemented Algorithms * Single-source level BFS +* Single-source parent BFS * Single-source shortest path (SSSP) * Triangles counting * Boruvka MSF +* Maggs-Plotkin MSF ## TODO -* [ ] Single-source parent BFS * [ ] Multiple-source level BFS * [ ] Multiple-source parent BFS * [ ] PageRank