From f1e8b9c6ec4c02a826d9f4308f96a36ff10d9e4e Mon Sep 17 00:00:00 2001 From: Brulevich-Nikita Date: Fri, 8 May 2026 21:54:24 +0300 Subject: [PATCH 1/3] add slice in Vector.fs and Matrix.fs, add reduceRows and reduceCols in Matrix.fs, add tests --- QuadTree.Tests/Tests.Matrix.fs | 1062 ++++++++++++++++++++++++++++++++ QuadTree.Tests/Tests.Vector.fs | 253 ++++++++ QuadTree/Matrix.fs | 187 +++++- QuadTree/Vector.fs | 43 ++ 4 files changed, 1544 insertions(+), 1 deletion(-) diff --git a/QuadTree.Tests/Tests.Matrix.fs b/QuadTree.Tests/Tests.Matrix.fs index 816b4a2..42934a5 100644 --- a/QuadTree.Tests/Tests.Matrix.fs +++ b/QuadTree.Tests/Tests.Matrix.fs @@ -642,3 +642,1065 @@ let ``Fold sum`` () = let actual = foldAssociative op_add None m1 |> Option.get Assert.Equal(expected, actual) + +[] +let ``fromCoordinateList with index out of range`` () = + let coo = + CoordinateList(6UL, 6UL, [ (9UL, 9UL, 13) ]) + + let matrix = fromCoordinateList coo + let result = toCoordinateList matrix + Assert.Equal(CoordinateList(6UL, 6UL, []), result) + +[] +let ``fromCoordinateList with unsorted coordinates works correctly`` () = + let coo = + CoordinateList( + 7UL, + 7UL, + [ (6UL, 6UL, 10) + (1UL, 2UL, 8) + (1UL, 1UL, 100) ] + ) + + let matrix = fromCoordinateList coo + let result = toCoordinateList matrix + + Assert.Equal( + CoordinateList( + 7UL, + 7UL, + [ (1UL, 1UL, 100) + (1UL, 2UL, 8) + (6UL, 6UL, 10) ] + ), + result + ) + +[] +let ``fromCoordinateList with zero length and some values returns empty matrix`` () = + let coo = + CoordinateList( + 0UL, + 0UL, + [ (33UL, 33UL, 33); (39UL, 39UL, 1) ] + ) + + let matrix = fromCoordinateList coo + let result = toCoordinateList matrix + Assert.Equal(CoordinateList(0UL, 0UL, []), result) + +[] +let ``map works on square matrix`` () = + let matrix = + fromCoordinateList ( + CoordinateList( + 4UL, + 4UL, + [ (1UL, 1UL, 22); (2UL, 3UL, 37) ] + ) + ) + + let result = + map matrix (fun (x: int option) -> + match x with + | Some(v) -> Some(v + 9) + | None -> None) + + let coo = toCoordinateList result + + Assert.Equal( + CoordinateList( + 4UL, + 4UL, + [ (1UL, 1UL, 31); (2UL, 3UL, 46) ] + ), + coo + ) + +[] +let ``map works on rectangular matrix`` () = + let matrix = + fromCoordinateList ( + CoordinateList( + 5UL, + 6UL, + [ (1UL, 1UL, 21); (4UL, 3UL, 36) ] + ) + ) + + let result = + map matrix (fun (x: int option) -> + match x with + | Some(v) -> Some(v / 3) + | None -> None) + + let coo = toCoordinateList result + + Assert.Equal( + CoordinateList( + 5UL, + 6UL, + [ (1UL, 1UL, 7); (4UL, 3UL, 12) ] + ), + coo + ) + +[] +let ``map on empty matrix returns empty matrix`` () = + let matrix = fromCoordinateList (CoordinateList(0UL, 0UL, [])) + + let result = + map matrix (fun (x: int option) -> + match x with + | Some(v) -> Some(v * 5) + | None -> None) + + let coo = toCoordinateList result + Assert.Equal(CoordinateList(0UL, 0UL, []), coo) + +[] +let ``map with function that turns all the elements into zeros`` () = + let matrix = + fromCoordinateList ( + CoordinateList( + 5UL, + 7UL, + [ (1UL, 1UL, 5) + (4UL, 1UL, 17) + (4UL, 6UL, 33) ] + ) + ) + + let result = map matrix (fun _ -> Some(0)) + let coo = 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) + +[] +let ``map with function that turns all the elements to None`` () = + let matrix = + fromCoordinateList ( + CoordinateList( + 5UL, + 7UL, + [ (1UL, 1UL, 5) + (4UL, 1UL, 17) + (4UL, 6UL, 33) ] + ) + ) + + let result = map matrix (fun _ -> None) + let coo = toCoordinateList result + Assert.Equal(CoordinateList(5UL, 7UL, []), coo) + +[] +let ``map can change type from int to string`` () = + let matrix = + fromCoordinateList ( + CoordinateList( + 3UL, + 5UL, + [ (4UL, 1UL, 17); (4UL, 6UL, 33) ] + ) + ) + + let result = + map matrix (fun (x: int option) -> + match x with + | Some v -> Some(sprintf "str %d" v) + | None -> None) + + let coo = toCoordinateList result + + Assert.Equal( + CoordinateList( + 3UL, + 5UL, + [ (4UL, 1UL, "str 17") + (4UL, 6UL, "str 33") ] + ), + coo + ) + +[] +let ``mapi works with row index and col index on square matrix`` () = + let coo = + CoordinateList( + 4UL, + 4UL, + [ (1UL, 1UL, 12); (2UL, 3UL, 13) ] + ) + + let matrix = fromCoordinateList coo + + let result = + mapi matrix (fun (i: uint64) (j: uint64) (x: int option) -> + match x with + | Some v -> Some(v * int (i) + int (j)) + | None -> None) + + let actual = toCoordinateList result + + Assert.Equal( + CoordinateList( + 4UL, + 4UL, + [ (1UL, 1UL, 13); (2UL, 3UL, 29) ] + ), + actual + ) + +[] +let ``mapi works with row index and col index on rectangular matrix`` () = + let coo = + CoordinateList( + 3UL, + 5UL, + [ (1UL, 2UL, 15); (2UL, 2UL, 13) ] + ) + + let matrix = fromCoordinateList coo + + let result = + mapi matrix (fun (i: uint64) (j: uint64) (x: int option) -> + match x with + | Some v -> Some(v * int (j) + int (i)) + | None -> None) + + let actual = toCoordinateList result + + Assert.Equal( + CoordinateList( + 3UL, + 5UL, + [ (1UL, 2UL, 31); (2UL, 2UL, 28) ] + ), + actual + ) + +[] +let ``mapi on empty matrix returns empty matrix`` () = + let matrix = fromCoordinateList (CoordinateList(0UL, 0UL, [])) + + let result = + mapi matrix (fun (i: uint64) (j: uint64) (x: int option) -> + match x with + | Some v -> Some((v + int (1)) * 3 + 2 * int (j)) + | None -> None) + + let actual = toCoordinateList result + Assert.Equal(CoordinateList(0UL, 0UL, []), actual) + +[] +let ``mapi with special function returns empty matrix`` () = + let coo = + CoordinateList( + 5UL, + 7UL, + [ (1UL, 2UL, 15); (4UL, 5UL, 13) ] + ) + + let matrix = fromCoordinateList coo + + let result = + mapi matrix (fun (i: uint64) (j: uint64) (x: int option) -> + match x with + | Some v -> + match (int (i) + int (j)) % 2 with + | 0 -> Some(v * 2) + | _ -> None + | None -> None) + + let actual = toCoordinateList result + Assert.Equal(CoordinateList(5UL, 7UL, []), actual) + +[] +let ``mapi works with function does not depend on the indexes`` () = + let coo = + CoordinateList( + 5UL, + 7UL, + [ (1UL, 2UL, 15); (4UL, 5UL, 13) ] + ) + + let matrix = fromCoordinateList coo + + let result = + mapi matrix (fun (i: uint64) (j: uint64) (x: int option) -> + match x with + | Some v -> Some(v * 2) + | None -> None) + + let actual = toCoordinateList result + + Assert.Equal( + CoordinateList( + 5UL, + 7UL, + [ (1UL, 2UL, 30); (4UL, 5UL, 26) ] + ), + actual + ) + +[] +let ``slice returns error when row start is negative`` () = + let matrix = + fromCoordinateList ( + CoordinateList( + 5UL, + 6UL, + [ (2UL, 2UL, 3); (3UL, 4UL, 17) ] + ) + ) + + match slice matrix -1 4 2 3 with + | Result.Ok _ -> failwith "Expected Error" + | Result.Error msg -> Assert.Equal("Start row should be >= 0", msg) + +[] +let ``slice returns error when row end is negative`` () = + let matrix = + fromCoordinateList ( + CoordinateList( + 5UL, + 6UL, + [ (2UL, 2UL, 3); (3UL, 4UL, 17) ] + ) + ) + + match slice matrix 1 -4 2 3 with + | Result.Ok _ -> failwith "Expected Error" + | Result.Error msg -> Assert.Equal("End row should be >= 0", msg) + +[] +let ``slice returns error when col start is negative`` () = + let matrix = + fromCoordinateList ( + CoordinateList( + 5UL, + 6UL, + [ (2UL, 2UL, 3); (3UL, 4UL, 17) ] + ) + ) + + match slice matrix 1 4 -2 3 with + | Result.Ok _ -> failwith "Expected Error" + | Result.Error msg -> Assert.Equal("Start column should be >= 0", msg) + +[] +let ``slice returns error when col end is negative`` () = + let matrix = + fromCoordinateList ( + CoordinateList( + 5UL, + 6UL, + [ (2UL, 2UL, 3); (3UL, 4UL, 17) ] + ) + ) + + match slice matrix 1 4 2 -3 with + | Result.Ok _ -> failwith "Expected Error" + | Result.Error msg -> Assert.Equal("End column should be >= 0", msg) + +[] +let ``slice returns error when row start is out of range`` () = + let matrix = + fromCoordinateList ( + CoordinateList( + 5UL, + 6UL, + [ (2UL, 2UL, 3); (3UL, 4UL, 17) ] + ) + ) + + match slice matrix 6 4 2 3 with + | Result.Ok _ -> failwith "Expected Error" + | Result.Error msg -> Assert.Equal("Start row is out of matrix length", msg) + +[] +let ``slice returns error when row end is out of range`` () = + let matrix = + fromCoordinateList ( + CoordinateList( + 5UL, + 6UL, + [ (2UL, 2UL, 3); (3UL, 4UL, 17) ] + ) + ) + + match slice matrix 1 10 2 3 with + | Result.Ok _ -> failwith "Expected Error" + | Result.Error msg -> Assert.Equal("End row is out of matrix length", msg) + +[] +let ``slice returns error when col start is out of range`` () = + let matrix = + fromCoordinateList ( + CoordinateList( + 5UL, + 6UL, + [ (2UL, 2UL, 3); (3UL, 4UL, 17) ] + ) + ) + + match slice matrix 1 4 10 3 with + | Result.Ok _ -> failwith "Expected Error" + | Result.Error msg -> Assert.Equal("Start column is out of matrix length", msg) + +[] +let ``slice returns error when col end is out of range`` () = + let matrix = + fromCoordinateList ( + CoordinateList( + 5UL, + 6UL, + [ (2UL, 2UL, 3); (3UL, 4UL, 17) ] + ) + ) + + match slice matrix 1 2 2 10 with + | Result.Ok _ -> failwith "Expected Error" + | Result.Error msg -> Assert.Equal("End column is out of matrix length", msg) + +[] +let ``slice returns error when row end is less than row start`` () = + let matrix = + fromCoordinateList ( + CoordinateList( + 5UL, + 6UL, + [ (2UL, 2UL, 3); (3UL, 4UL, 17) ] + ) + ) + + match slice matrix 2 1 2 3 with + | Result.Ok _ -> failwith "Expected Error" + | Result.Error msg -> Assert.Equal("Start row should be <= end row", msg) + +[] +let ``slice returns error when col end is less than col start`` () = + let matrix = + fromCoordinateList ( + CoordinateList( + 5UL, + 6UL, + [ (2UL, 2UL, 3); (3UL, 4UL, 17) ] + ) + ) + + match slice matrix 1 2 3 2 with + | Result.Ok _ -> failwith "Expected Error" + | Result.Error msg -> Assert.Equal("Start column should be <= end column", msg) + +[] +let ``slice returns correct square submatrix`` () = + let matrix = + fromCoordinateList ( + CoordinateList( + 7UL, + 7UL, + [ (2UL, 2UL, 33); (5UL, 5UL, 28) ] + ) + ) + + match slice matrix 1 3 1 3 with + | Result.Ok result -> + let coo = toCoordinateList result + Assert.Equal(CoordinateList(3UL, 3UL, [ (1UL, 1UL, 33) ]), coo) + | Result.Error msg -> failwith msg + +[] +let ``slice returns correct rectangular submatrix`` () = + let matrix = + fromCoordinateList ( + CoordinateList( + 5UL, + 9UL, + [ (3UL, 7UL, 33); (1UL, 2UL, 28) ] + ) + ) + + match slice matrix 2 4 5 8 with + | Result.Ok result -> + let coo = toCoordinateList result + Assert.Equal(CoordinateList(3UL, 4UL, [ (1UL, 2UL, 33) ]), coo) + | Result.Error msg -> failwith msg + +[] +let ``slice returns empty matrix`` () = + let matrix = + fromCoordinateList ( + CoordinateList( + 7UL, + 9UL, + [ (3UL, 3UL, 33); (1UL, 2UL, 28) ] + ) + ) + + match slice matrix 4 6 5 8 with + | Result.Ok result -> + let coo = toCoordinateList result + Assert.Equal(CoordinateList(3UL, 4UL, []), coo) + | Result.Error msg -> failwith msg + +[] +let ``slice returns single submatrix`` () = + let matrix = + fromCoordinateList ( + CoordinateList( + 7UL, + 9UL, + [ (3UL, 3UL, 33); (1UL, 2UL, 28) ] + ) + ) + + match slice matrix 1 1 2 2 with + | Result.Ok result -> + let coo = toCoordinateList result + Assert.Equal(CoordinateList(1UL, 1UL, [ (0UL, 0UL, 28) ]), coo) + | Result.Error msg -> failwith msg + +[] +let ``slice returns correct submatrix equals to matrix`` () = + let matrix = + fromCoordinateList ( + CoordinateList( + 7UL, + 9UL, + [ (3UL, 3UL, 33); (1UL, 2UL, 28) ] + ) + ) + + match slice matrix 0 6 0 8 with + | Result.Ok result -> + let coo = toCoordinateList result + + Assert.Equal( + CoordinateList( + 7UL, + 9UL, + [ (1UL, 2UL, 28); (3UL, 3UL, 33) ] + ), + coo + ) + | Result.Error msg -> failwith msg + +[] +let ``slice returns correct submatrix when row start of submatrix equals to row start of matrix`` () = + let matrix = + fromCoordinateList ( + CoordinateList( + 7UL, + 7UL, + [ (0UL, 0UL, 10) + (3UL, 3UL, 33) + (6UL, 6UL, 6) ] + ) + ) + + match slice matrix 0 4 0 4 with + | Result.Ok result -> + let coo = toCoordinateList result + + Assert.Equal( + CoordinateList( + 5UL, + 5UL, + [ (0UL, 0UL, 10); (3UL, 3UL, 33) ] + ), + coo + ) + | Result.Error msg -> failwith msg + +[] +let ``slice returns correct submatrix when row end of submatrix equals to row end of matrix`` () = + let matrix = + fromCoordinateList ( + CoordinateList( + 7UL, + 7UL, + [ (0UL, 0UL, 10) + (3UL, 3UL, 33) + (6UL, 6UL, 6) ] + ) + ) + + match slice matrix 2 6 2 4 with + | Result.Ok result -> + let coo = toCoordinateList result + Assert.Equal(CoordinateList(5UL, 3UL, [ (1UL, 1UL, 33) ]), coo) + | Result.Error msg -> failwith msg + +[] +let ``slice returns correct submatrix when col start of submatrix equals to col start of matrix`` () = + let matrix = + fromCoordinateList ( + CoordinateList( + 7UL, + 7UL, + [ (0UL, 0UL, 10) + (3UL, 3UL, 33) + (6UL, 6UL, 6) ] + ) + ) + + match slice matrix 2 5 0 6 with + | Result.Ok result -> + let coo = toCoordinateList result + Assert.Equal(CoordinateList(4UL, 7UL, [ (1UL, 3UL, 33) ]), coo) + | Result.Error msg -> failwith msg + +[] +let ``slice returns correct submatrix when col end of submatrix equals to col end of matrix`` () = + let matrix = + fromCoordinateList ( + CoordinateList( + 7UL, + 7UL, + [ (0UL, 0UL, 10) + (3UL, 3UL, 33) + (6UL, 6UL, 6) ] + ) + ) + + match slice matrix 2 4 2 6 with + | Result.Ok result -> + let coo = toCoordinateList result + Assert.Equal(CoordinateList(3UL, 5UL, [ (1UL, 1UL, 33) ]), coo) + | Result.Error msg -> failwith msg + +[] +let ``slice returns single column`` () = + let matrix = + fromCoordinateList ( + CoordinateList( + 10UL, + 10UL, + [ (1UL, 3UL, 1) + (2UL, 2UL, 2) + (5UL, 7UL, 3) ] + ) + ) + + match slice matrix 1 6 2 2 with + | Result.Ok result -> + let coo = toCoordinateList result + Assert.Equal(CoordinateList(6UL, 1UL, [ (1UL, 0UL, 2) ]), coo) + | Result.Error msg -> failwith msg + +[] +let ``slice returns single row`` () = + let matrix = + fromCoordinateList ( + CoordinateList( + 10UL, + 10UL, + [ (1UL, 3UL, 1) + (2UL, 2UL, 2) + (5UL, 7UL, 3) ] + ) + ) + + match slice matrix 5 5 3 9 with + | Result.Ok result -> + let coo = toCoordinateList result + Assert.Equal(CoordinateList(1UL, 7UL, [ (0UL, 4UL, 3) ]), coo) + | Result.Error msg -> failwith 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) ] + ) + + let matrix = fromCoordinateList coo + + let add x y = + match x, y with + | None, None -> None + | None, Some(b) -> Some(b) + | Some(a), None -> Some(a) + | Some(a), Some(b) -> Some(a + b) + + let result = 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) ] + ) + + let matrix = fromCoordinateList coo + + let add x y = + match x, y with + | None, None -> None + | None, Some(b) -> Some(b) + | Some(a), None -> Some(a) + | Some(a), Some(b) -> Some(a + b) + + let result = reduceRows add matrix + let vectorCoordinates = Vector.toCoordinateList result + + let expected = + Vector.CoordinateList(2UL, [ (1UL, 20) ]) + + Assert.Equal(expected, vectorCoordinates) + +[] +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) ] + ) + + let matrix = fromCoordinateList coo + + let add x y = + match x, y with + | None, None -> None + | None, Some(b) -> Some(b) + | Some(a), None -> Some(a) + | Some(a), Some(b) -> Some(a + b) + + let result = reduceRows add matrix + let vectorCoordinates = Vector.toCoordinateList result + + let expected = + Vector.CoordinateList( + 3UL, + [ (0UL, 21); (1UL, 24); (2UL, 32) ] + ) + + Assert.Equal(expected, vectorCoordinates) + +[] +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) ] + ) + + let matrix = fromCoordinateList coo + + let mul x y = + match x, y with + | None, None -> None + | None, Some(b) -> Some(b) + | Some(a), None -> Some(a) + | Some(a), Some(b) -> Some(a * b) + + let result = reduceRows mul matrix + let vectorCoordinates = Vector.toCoordinateList result + + let expected = + Vector.CoordinateList( + 3UL, + [ (0UL, 315); (1UL, 143); (2UL, 255) ] + ) + + Assert.Equal(expected, vectorCoordinates) + +[] +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) ] + ) + + let matrix = fromCoordinateList coo + + let sum x y = + match x, y with + | None, None -> None + | None, Some(b) -> Some(b) + | Some(a), None -> Some(a) + | Some(a), Some(b) -> Some(a + b) + + let result = reduceRows sum matrix + let vectorCoordinates = Vector.toCoordinateList result + + let expected = + Vector.CoordinateList(2UL, [ (0UL, 21); (1UL, 24) ]) + + Assert.Equal(expected, vectorCoordinates) + +[] +let ``let reduceRows sum on empty matrix`` () = + let coo = CoordinateList(2UL, 3UL, []) + let matrix = fromCoordinateList coo + + let sum x y = + match x, y with + | None, None -> None + | None, Some(b) -> Some(b) + | Some(a), None -> Some(a) + | Some(a), Some(b) -> Some(a + b) + + let result = reduceRows sum matrix + let vectorCoordinates = Vector.toCoordinateList result + let expected = Vector.CoordinateList(2UL, []) + Assert.Equal(expected, vectorCoordinates) + +[] +let ``let reduceRows mul on single matrix`` () = + let coo = + CoordinateList(1UL, 1UL, [ (0UL, 0UL, 33) ]) + + let matrix = fromCoordinateList coo + + let mul x y = + match x, y with + | None, None -> None + | None, Some(b) -> Some(b) + | Some(a), None -> Some(a) + | Some(a), Some(b) -> Some(a * b) + + let result = reduceRows mul matrix + let vectorCoordinates = Vector.toCoordinateList result + + let expected = + Vector.CoordinateList(1UL, [ (0UL, 33) ]) + + Assert.Equal(expected, vectorCoordinates) + + +[] +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) ] + ) + + let matrix = fromCoordinateList coo + + let add x y = + match x, y with + | None, None -> None + | None, Some(b) -> Some(b) + | Some(a), None -> Some(a) + | Some(a), Some(b) -> Some(a + b) + + let result = reduceCols add matrix + let vectorCoordinates = Vector.toCoordinateList result + + let expected = + Vector.CoordinateList(2UL, [ (0UL, 29); (1UL, 18) ]) + + Assert.Equal(expected, vectorCoordinates) + +[] +let ``let reduceCols sum on square power of two matrix with empty col`` () = + let coo = + CoordinateList( + 2UL, + 2UL, + [ (0UL, 0UL, 17); (1UL, 0UL, 3) ] + ) + + let matrix = fromCoordinateList coo + + let add x y = + match x, y with + | None, None -> None + | None, Some(b) -> Some(b) + | Some(a), None -> Some(a) + | Some(a), Some(b) -> Some(a + b) + + let result = reduceCols add matrix + let vectorCoordinates = Vector.toCoordinateList result + + let expected = + Vector.CoordinateList(2UL, [ (0UL, 20) ]) + + Assert.Equal(expected, vectorCoordinates) + +[] +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) ] + ) + + let matrix = fromCoordinateList coo + + let add x y = + match x, y with + | None, None -> None + | None, Some(b) -> Some(b) + | Some(a), None -> Some(a) + | Some(a), Some(b) -> Some(a + b) + + let result = reduceCols add matrix + let vectorCoordinates = Vector.toCoordinateList result + + let expected = + Vector.CoordinateList( + 3UL, + [ (0UL, 20); (1UL, 35); (2UL, 22) ] + ) + + Assert.Equal(expected, vectorCoordinates) + +[] +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) ] + ) + + let matrix = fromCoordinateList coo + + let mul x y = + match x, y with + | None, None -> None + | None, Some(b) -> Some(b) + | Some(a), None -> Some(a) + | Some(a), Some(b) -> Some(a * b) + + let result = reduceCols mul matrix + let vectorCoordinates = Vector.toCoordinateList result + + let expected = + Vector.CoordinateList( + 3UL, + [ (0UL, 75); (1UL, 1309); (2UL, 117) ] + ) + + Assert.Equal(expected, vectorCoordinates) + +[] +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) ] + ) + + let matrix = fromCoordinateList coo + + let sum x y = + match x, y with + | None, None -> None + | None, Some(b) -> Some(b) + | Some(a), None -> Some(a) + | Some(a), Some(b) -> Some(a + b) + + let result = reduceCols sum matrix + let vectorCoordinates = Vector.toCoordinateList result + + let expected = + Vector.CoordinateList( + 3UL, + [ (0UL, 5); (1UL, 18); (2UL, 22) ] + ) + + Assert.Equal(expected, vectorCoordinates) + +[] +let ``let reduceCols sum on empty matrix`` () = + let coo = CoordinateList(2UL, 3UL, []) + let matrix = fromCoordinateList coo + + let sum x y = + match x, y with + | None, None -> None + | None, Some(b) -> Some(b) + | Some(a), None -> Some(a) + | Some(a), Some(b) -> Some(a + b) + + let result = reduceCols sum matrix + let vectorCoordinates = Vector.toCoordinateList result + let expected = Vector.CoordinateList(3UL, []) + Assert.Equal(expected, vectorCoordinates) + +[] +let ``let reduceCols mul on single matrix`` () = + let coo = + CoordinateList(1UL, 1UL, [ (0UL, 0UL, 33) ]) + + let matrix = fromCoordinateList coo + + let mul x y = + match x, y with + | None, None -> None + | None, Some(b) -> Some(b) + | Some(a), None -> Some(a) + | Some(a), Some(b) -> Some(a * b) + + let result = reduceCols mul matrix + let vectorCoordinates = Vector.toCoordinateList result + + let expected = + Vector.CoordinateList(1UL, [ (0UL, 33) ]) + + Assert.Equal(expected, vectorCoordinates) diff --git a/QuadTree.Tests/Tests.Vector.fs b/QuadTree.Tests/Tests.Vector.fs index 4eb1af9..e10ae47 100644 --- a/QuadTree.Tests/Tests.Vector.fs +++ b/QuadTree.Tests/Tests.Vector.fs @@ -903,3 +903,256 @@ let ``Init vector`` () = let actual = Vector.init 3UL (fun i -> Some(int i)) Assert.Equal(expected, actual) + + +[] +let ``map on empty vector returns empty vector`` () = + let vec = fromCoordinateList (CoordinateList(0UL, [])) + + 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`` () = + let vec = + fromCoordinateList (CoordinateList(7UL, [ (1UL, 3); (2UL, 3); (5UL, 100) ])) + + 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`` () = + let vec = + fromCoordinateList (CoordinateList(7UL, [ (1UL, 3); (2UL, 3); (5UL, 100) ])) + + let result = map vec (fun _ -> None) + let coo = toCoordinateList result + Assert.Equal(CoordinateList(7UL, []), coo) + +[] +let ``map can change type from int to string`` () = + let vec = + fromCoordinateList (CoordinateList(3UL, [ (1UL, 11); (2UL, 33) ])) + + 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 index out of range`` () = + let coo = CoordinateList(6UL, [ (9UL, 8) ]) + let vec = fromCoordinateList coo + let result = toCoordinateList vec + Assert.Equal(CoordinateList(6UL, []), result) + +[] +let ``fromCoordinateList with unsorted coordinates works correctly`` () = + let coo = + CoordinateList(7UL, [ (5UL, 3); (3UL, 2); (1UL, 100) ]) + + let vec = fromCoordinateList coo + 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)]) + let vec = Vector.fromCoordinateList coo + let result = Vector.toCoordinateList vec + Assert.Equal(CoordinateList(3UL, [(1UL, 100)]), result) +*) +[] +let ``fromCoordinateList with zero length and some values returns empty vector`` () = + let coo = CoordinateList(0UL, [ (33UL, 33); (39UL, 1) ]) + let vec = fromCoordinateList coo + let result = toCoordinateList vec + Assert.Equal(CoordinateList(0UL, []), result) + +[] +let ``mapi works with index`` () = + let vec = + fromCoordinateList (CoordinateList(5UL, [ (2UL, 10); (3UL, 23) ])) + + 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`` () = + let vec = fromCoordinateList (CoordinateList(0UL, [])) + + 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`` () = + let vec = + fromCoordinateList (CoordinateList(5UL, [ (1UL, 33); (3UL, 88) ])) + + 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`` () = + let vec = + fromCoordinateList (CoordinateList(5UL, [ (2UL, 10); (3UL, 23) ])) + + 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`` () = + let vec = fromCoordinateList (CoordinateList(5UL, [ (2UL, 10) ])) + + match slice -1 3 vec with + | Result.Ok _ -> failwith "Expected Error" + | Result.Error msg -> Assert.Equal("Start should be >= 0", msg) + +[] +let ``slice returns error when end is negative`` () = + let vec = fromCoordinateList (CoordinateList(5UL, [ (2UL, 10) ])) + + match slice 1 -3 vec with + | Result.Ok _ -> failwith "Expected Error" + | Result.Error msg -> Assert.Equal("End should be >= 0", msg) + +[] +let ``slice returns error when start is out of range`` () = + let vec = fromCoordinateList (CoordinateList(5UL, [ (2UL, 10) ])) + + match slice 7 3 vec with + | Result.Ok _ -> failwith "Expected Error" + | Result.Error msg -> Assert.Equal("Start is out of Vector length", msg) + +[] +let ``slice returns error when end is out of range`` () = + let vec = fromCoordinateList (CoordinateList(5UL, [ (2UL, 10) ])) + + match slice 3 7 vec with + | Result.Ok _ -> failwith "Expected Error" + | Result.Error msg -> Assert.Equal("End is out of Vector length", msg) + +[] +let ``slice returns error when end is less than start`` () = + let vec = fromCoordinateList (CoordinateList(5UL, [ (2UL, 10) ])) + + match slice 4 3 vec with + | Result.Ok _ -> failwith "Expected Error" + | Result.Error msg -> Assert.Equal("End should be >= Start", msg) + +[] +let ``slice returns correct subvector`` () = + let vec = + fromCoordinateList (CoordinateList(7UL, [ (2UL, 10); (6UL, 20) ])) + + match slice 1 3 vec with + | Result.Ok result -> + let coo = Vector.toCoordinateList result + Assert.Equal(CoordinateList(3UL, [ (1UL, 10) ]), coo) + | Result.Error msg -> failwith msg + +[] +let ``slice returns subvector without elements`` () = + let vec = + fromCoordinateList (CoordinateList(7UL, [ (5UL, 10); (6UL, 20) ])) + + match slice 0 2 vec with + | Result.Ok result -> + let coo = toCoordinateList result + Assert.Equal(CoordinateList(3UL, []), coo) + | Result.Error msg -> failwith msg + +[] +let ``slice returns single subvector`` () = + let vec = + fromCoordinateList (CoordinateList(7UL, [ (2UL, 10); (6UL, 20) ])) + + match slice 2 2 vec with + | Result.Ok result -> + let coo = toCoordinateList result + Assert.Equal(CoordinateList(1UL, [ (0UL, 10) ]), coo) + | Result.Error msg -> failwith msg + +[] +let ``slice returns correct subvector equals to vector`` () = + let vec = + fromCoordinateList (CoordinateList(7UL, [ (2UL, 10); (3UL, 33); (6UL, 20) ])) + + 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 -> failwith msg + +[] +let ``slice returns correct subvector when start of subvector equals to start of vector`` () = + let vec = + fromCoordinateList (CoordinateList(7UL, [ (0UL, 10); (3UL, 33); (6UL, 20) ])) + + 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 -> failwith msg + +[] +let ``slice returns correct subvector when end of subvector equals to end of vector`` () = + let vec = + fromCoordinateList (CoordinateList(7UL, [ (0UL, 10); (3UL, 33); (6UL, 20) ])) + + 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 -> failwith msg diff --git a/QuadTree/Matrix.fs b/QuadTree/Matrix.fs index 68ea7d7..f7fb260 100644 --- a/QuadTree/Matrix.fs +++ b/QuadTree/Matrix.fs @@ -135,11 +135,35 @@ 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, [])) +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 = let _do x1 x2 x3 x4 y1 y2 y3 y4 = @@ -394,3 +418,164 @@ 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> = + match () with + | _ when rowStart < 0 -> Error "Start row should be >= 0" + | _ when rowEnd < 0 -> Error "End row should be >= 0" + | _ when colStart < 0 -> Error "Start column should be >= 0" + | _ when colEnd < 0 -> Error "End column should be >= 0" + | _ when rowStart > int matrix.nrows - 1 -> Error "Start row is out of matrix length" + | _ when rowEnd > int matrix.nrows - 1 -> Error "End row is out of matrix length" + | _ when colStart > int matrix.ncols - 1 -> Error "Start column is out of matrix length" + | _ when colEnd > int matrix.ncols - 1 -> Error "End column is out of matrix length" + | _ when rowStart > rowEnd -> Error "Start row should be <= end row" + | _ when colStart > colEnd -> Error "Start column should be <= end column" + | _ -> + let rowStartIdx = uint64 rowStart * 1UL + let rowEndIdx = uint64 rowEnd * 1UL + let colStartIdx = uint64 colStart * 1UL + let colEndIdx = uint64 colEnd * 1UL + + let rec inner (size: uint64) (row: uint64) (col: uint64) matrix = + let sizeRow = (uint64 size) * 1UL + let sizeCol = (uint64 size) * 1UL + + match matrix with + | Node(nw, ne, sw, se) -> + let halfSize = size / 2UL + let halfRow = (uint64 halfSize) * 1UL + let halfCol = (uint64 halfSize) * 1UL + let nwTree, nwNvals = inner halfSize row col nw + let neTree, neNvals = inner halfSize row (col + halfCol) ne + let swTree, swNvals = inner halfSize (row + halfRow) col sw + let seTree, seNvals = inner halfSize (row + halfRow) (col + halfCol) se + (mkNode nwTree neTree swTree seTree), nwNvals + neNvals + swNvals + seNvals + | Leaf(Dummy) -> Leaf(Dummy), 0UL + | Leaf(UserValue(v)) when + row >= rowStartIdx + && row + sizeRow - 1UL <= rowEndIdx + && col >= colStartIdx + && col + sizeCol - 1UL <= colEndIdx + -> + Leaf(UserValue(v)), (uint64 size) * (uint64 size) * 1UL + | Leaf(UserValue(v)) when row + sizeRow - 1UL < rowStartIdx -> Leaf(Dummy), 0UL + | Leaf(UserValue(v)) when col + sizeCol - 1UL < colStartIdx -> Leaf(Dummy), 0UL + | Leaf(UserValue(v)) when row > rowEndIdx -> Leaf(Dummy), 0UL + | Leaf(UserValue(v)) when col > colEndIdx -> Leaf(Dummy), 0UL + | _ -> Leaf(Dummy), 0UL + + let storage, nvals = + inner matrix.storage.size 0UL 0UL matrix.storage.data + + let newRows = uint64 (rowEnd - rowStart + 1) * 1UL + let newCols = uint64 (colEnd - colStart + 1) * 1UL + + let coo = + toCoordinateList (SparseMatrix(newRows, newCols, nvals, Storage(matrix.storage.size, storage))) + + let shiftedData = + coo.list + |> List.map (fun (i, j, v) -> (i - uint64 rowStart * 1UL, j - uint64 colStart * 1UL, v)) + + let shiftedCoo = CoordinateList(newRows, newCols, shiftedData) + let newMatrix = fromCoordinateList shiftedCoo + Ok newMatrix + +let reduceRows (op: 'a option -> 'a option -> 'a option) (matrix: SparseMatrix<'a>) : Vector.SparseVector<'a> = + let rows = matrix.nrows + + let rec inner (size: uint64) (row: uint64) (col: uint64) tree acc = + match tree with + | Node(nw, ne, sw, se) -> + let half = size / 2UL + let halfRow = (uint64 half) * 1UL + let halfCol = (uint64 half) * 1UL + let acc = inner half row col nw acc + let acc = inner half row (col + halfCol) ne acc + let acc = inner half (row + halfRow) col sw acc + let acc = inner half (row + halfRow) (col + halfCol) se acc + acc + | Leaf(Dummy) -> acc + | Leaf(UserValue(None)) -> acc + | Leaf(UserValue(Some v)) -> (row, v) :: acc + + let pairs = + inner matrix.storage.size 0UL 0UL 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)) + + Vector.fromCoordinateList (Vector.CoordinateList(uint64 rows * 1UL, vectorData)) + +let reduceCols (op: 'a option -> 'a option -> 'a option) (matrix: SparseMatrix<'a>) : Vector.SparseVector<'a> = + let cols = matrix.ncols + + let rec inner (size: uint64) (row: uint64) (col: uint64) tree acc = + match tree with + | Node(nw, ne, sw, se) -> + let half = size / 2UL + let halfRow = (uint64 half) * 1UL + let halfCol = (uint64 half) * 1UL + let acc = inner half row col nw acc + let acc = inner half row (col + halfCol) ne acc + let acc = inner half (row + halfRow) col sw acc + let acc = inner half (row + halfRow) (col + halfCol) se acc + acc + | Leaf(Dummy) -> acc + | Leaf(UserValue(None)) -> acc + | Leaf(UserValue(Some v)) -> (col, v) :: acc + + let pairs = + inner matrix.storage.size 0UL 0UL 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)) + + Vector.fromCoordinateList (Vector.CoordinateList(uint64 cols * 1UL, vectorData)) diff --git a/QuadTree/Vector.fs b/QuadTree/Vector.fs index b7bcb44..a620616 100644 --- a/QuadTree/Vector.fs +++ b/QuadTree/Vector.fs @@ -561,3 +561,46 @@ let scatter | Error x -> Error x) (Ok w) | Error x -> Error Error.InconsistentStructureOfStorages + + +let slice (_start: int) (_end: int) (vector: SparseVector<'a>) : Result, string> = + match () with + | _ when _start < 0 -> Error "Start should be >= 0" + | _ when _end < 0 -> Error "End should be >= 0" + | _ when _start > int vector.length - 1 -> Error "Start is out of Vector length" + | _ when _end > int vector.length - 1 -> Error "End is out of Vector length" + | _ when _start > _end -> Error "End should be >= Start" + | _ -> + let startIdx = uint64 _start * 1UL + let endIdx = uint64 _end * 1UL + + let rec inner (size: uint64) (pos: uint64) vector = + let sizeIdx = (uint64 size) * 1UL + + match vector with + | Node(x1, x2) -> + let half = size / 2UL + let halfIdx = (uint64 half) * 1UL + let t1, n1 = inner half pos x1 + let t2, n2 = inner half (pos + halfIdx) x2 + (mkNode t1 t2), n1 + n2 + | Leaf(Dummy) -> Leaf(Dummy), 0UL + | Leaf(UserValue(v)) when pos >= startIdx && pos + sizeIdx - 1UL <= endIdx -> + Leaf(UserValue(v)), (uint64 size) * 1UL + | Leaf(UserValue(v)) when pos + sizeIdx - 1UL < startIdx -> Leaf(Dummy), 0UL + | Leaf(UserValue(v)) when pos > endIdx -> Leaf(Dummy), 0UL + | Leaf(UserValue(v)) -> Leaf(Dummy), 0UL + + let storage, nvals = inner vector.storage.size 0UL vector.storage.data + let newLength = uint64 (_end - _start + 1) * 1UL + + let coo = + toCoordinateList (SparseVector(newLength, nvals, Storage(vector.storage.size, storage))) + + let shiftedData = + coo.data + |> List.map (fun (idx, value) -> (idx - uint64 _start * 1UL, value)) + + let shiftedCoo = CoordinateList(newLength, shiftedData) + let newVec = fromCoordinateList shiftedCoo + Ok newVec From c0ba718258c7b7c8b3b7e5e0ae5c81c547c2d553 Mon Sep 17 00:00:00 2001 From: Brulevich-Nikita Date: Mon, 18 May 2026 10:51:56 +0300 Subject: [PATCH 2/3] add Kronecker product to Matrix.fs with tests, fix tests, duplicate handling, slice and more --- QuadTree.Tests/Tests.Matrix.fs | 685 +++++++++++++++++++++++++++++++-- QuadTree.Tests/Tests.Vector.fs | 43 +-- QuadTree/Matrix.fs | 275 ++++++++++--- QuadTree/Vector.fs | 117 ++++-- 4 files changed, 991 insertions(+), 129 deletions(-) diff --git a/QuadTree.Tests/Tests.Matrix.fs b/QuadTree.Tests/Tests.Matrix.fs index 42934a5..74e4c42 100644 --- a/QuadTree.Tests/Tests.Matrix.fs +++ b/QuadTree.Tests/Tests.Matrix.fs @@ -644,13 +644,11 @@ let ``Fold sum`` () = Assert.Equal(expected, actual) [] -let ``fromCoordinateList with index out of range`` () = +let ``fromCoordinateList with out-of-range coordinates throws exception`` () = let coo = CoordinateList(6UL, 6UL, [ (9UL, 9UL, 13) ]) - let matrix = fromCoordinateList coo - let result = toCoordinateList matrix - Assert.Equal(CoordinateList(6UL, 6UL, []), result) + Assert.Throws(fun () -> fromCoordinateList coo |> ignore) [] let ``fromCoordinateList with unsorted coordinates works correctly`` () = @@ -678,7 +676,20 @@ let ``fromCoordinateList with unsorted coordinates works correctly`` () = ) [] -let ``fromCoordinateList with zero length and some values returns empty matrix`` () = +let ``fromCoordinateList with duplicate indices returns the last of them`` () = + let coo = + CoordinateList( + 3UL, + 3UL, + [ (1UL, 1UL, 33); (1UL, 1UL, 100) ] + ) + + let matrix = fromCoordinateList coo + let result = toCoordinateList matrix + Assert.Equal(CoordinateList(3UL, 3UL, [ (1UL, 1UL, 100) ]), result) + +[] +let ``fromCoordinateList with zero size throws Exception`` () = let coo = CoordinateList( 0UL, @@ -686,9 +697,7 @@ let ``fromCoordinateList with zero length and some values returns empty matrix`` [ (33UL, 33UL, 33); (39UL, 39UL, 1) ] ) - let matrix = fromCoordinateList coo - let result = toCoordinateList matrix - Assert.Equal(CoordinateList(0UL, 0UL, []), result) + Assert.Throws(fun () -> fromCoordinateList coo |> ignore) [] let ``map works on square matrix`` () = @@ -806,7 +815,7 @@ let ``map can change type from int to string`` () = CoordinateList( 3UL, 5UL, - [ (4UL, 1UL, 17); (4UL, 6UL, 33) ] + [ (2UL, 1UL, 17); (2UL, 4UL, 33) ] ) ) @@ -822,8 +831,8 @@ let ``map can change type from int to string`` () = CoordinateList( 3UL, 5UL, - [ (4UL, 1UL, "str 17") - (4UL, 6UL, "str 33") ] + [ (2UL, 1UL, "str 17") + (2UL, 4UL, "str 33") ] ), coo ) @@ -960,7 +969,7 @@ let ``slice returns error when row start is negative`` () = ) match slice matrix -1 4 2 3 with - | Result.Ok _ -> failwith "Expected Error" + | Result.Ok _ -> Assert.Fail("Expected Error") | Result.Error msg -> Assert.Equal("Start row should be >= 0", msg) [] @@ -975,7 +984,7 @@ let ``slice returns error when row end is negative`` () = ) match slice matrix 1 -4 2 3 with - | Result.Ok _ -> failwith "Expected Error" + | Result.Ok _ -> Assert.Fail("Expected Error") | Result.Error msg -> Assert.Equal("End row should be >= 0", msg) [] @@ -990,7 +999,7 @@ let ``slice returns error when col start is negative`` () = ) match slice matrix 1 4 -2 3 with - | Result.Ok _ -> failwith "Expected Error" + | Result.Ok _ -> Assert.Fail("Expected Error") | Result.Error msg -> Assert.Equal("Start column should be >= 0", msg) [] @@ -1005,7 +1014,7 @@ let ``slice returns error when col end is negative`` () = ) match slice matrix 1 4 2 -3 with - | Result.Ok _ -> failwith "Expected Error" + | Result.Ok _ -> Assert.Fail("Expected Error") | Result.Error msg -> Assert.Equal("End column should be >= 0", msg) [] @@ -1020,7 +1029,7 @@ let ``slice returns error when row start is out of range`` () = ) match slice matrix 6 4 2 3 with - | Result.Ok _ -> failwith "Expected Error" + | Result.Ok _ -> Assert.Fail("Expected Error") | Result.Error msg -> Assert.Equal("Start row is out of matrix length", msg) [] @@ -1035,7 +1044,7 @@ let ``slice returns error when row end is out of range`` () = ) match slice matrix 1 10 2 3 with - | Result.Ok _ -> failwith "Expected Error" + | Result.Ok _ -> Assert.Fail("Expected Error") | Result.Error msg -> Assert.Equal("End row is out of matrix length", msg) [] @@ -1050,7 +1059,7 @@ let ``slice returns error when col start is out of range`` () = ) match slice matrix 1 4 10 3 with - | Result.Ok _ -> failwith "Expected Error" + | Result.Ok _ -> Assert.Fail("Expected Error") | Result.Error msg -> Assert.Equal("Start column is out of matrix length", msg) [] @@ -1065,7 +1074,7 @@ let ``slice returns error when col end is out of range`` () = ) match slice matrix 1 2 2 10 with - | Result.Ok _ -> failwith "Expected Error" + | Result.Ok _ -> Assert.Fail("Expected Error") | Result.Error msg -> Assert.Equal("End column is out of matrix length", msg) [] @@ -1080,7 +1089,7 @@ let ``slice returns error when row end is less than row start`` () = ) match slice matrix 2 1 2 3 with - | Result.Ok _ -> failwith "Expected Error" + | Result.Ok _ -> Assert.Fail("Expected Error") | Result.Error msg -> Assert.Equal("Start row should be <= end row", msg) [] @@ -1095,7 +1104,7 @@ let ``slice returns error when col end is less than col start`` () = ) match slice matrix 1 2 3 2 with - | Result.Ok _ -> failwith "Expected Error" + | Result.Ok _ -> Assert.Fail("Expected Error") | Result.Error msg -> Assert.Equal("Start column should be <= end column", msg) [] @@ -1113,7 +1122,7 @@ let ``slice returns correct square submatrix`` () = | Result.Ok result -> let coo = toCoordinateList result Assert.Equal(CoordinateList(3UL, 3UL, [ (1UL, 1UL, 33) ]), coo) - | Result.Error msg -> failwith msg + | Result.Error msg -> Assert.Fail(msg) [] let ``slice returns correct rectangular submatrix`` () = @@ -1130,7 +1139,7 @@ let ``slice returns correct rectangular submatrix`` () = | Result.Ok result -> let coo = toCoordinateList result Assert.Equal(CoordinateList(3UL, 4UL, [ (1UL, 2UL, 33) ]), coo) - | Result.Error msg -> failwith msg + | Result.Error msg -> Assert.Fail(msg) [] let ``slice returns empty matrix`` () = @@ -1147,7 +1156,7 @@ let ``slice returns empty matrix`` () = | Result.Ok result -> let coo = toCoordinateList result Assert.Equal(CoordinateList(3UL, 4UL, []), coo) - | Result.Error msg -> failwith msg + | Result.Error msg -> Assert.Fail(msg) [] let ``slice returns single submatrix`` () = @@ -1164,7 +1173,7 @@ let ``slice returns single submatrix`` () = | Result.Ok result -> let coo = toCoordinateList result Assert.Equal(CoordinateList(1UL, 1UL, [ (0UL, 0UL, 28) ]), coo) - | Result.Error msg -> failwith msg + | Result.Error msg -> Assert.Fail(msg) [] let ``slice returns correct submatrix equals to matrix`` () = @@ -1189,7 +1198,7 @@ let ``slice returns correct submatrix equals to matrix`` () = ), coo ) - | Result.Error msg -> failwith msg + | Result.Error msg -> Assert.Fail(msg) [] let ``slice returns correct submatrix when row start of submatrix equals to row start of matrix`` () = @@ -1216,7 +1225,7 @@ let ``slice returns correct submatrix when row start of submatrix equals to row ), coo ) - | Result.Error msg -> failwith msg + | Result.Error msg -> Assert.Fail(msg) [] let ``slice returns correct submatrix when row end of submatrix equals to row end of matrix`` () = @@ -1235,7 +1244,7 @@ let ``slice returns correct submatrix when row end of submatrix equals to row en | Result.Ok result -> let coo = toCoordinateList result Assert.Equal(CoordinateList(5UL, 3UL, [ (1UL, 1UL, 33) ]), coo) - | Result.Error msg -> failwith msg + | Result.Error msg -> Assert.Fail(msg) [] let ``slice returns correct submatrix when col start of submatrix equals to col start of matrix`` () = @@ -1254,7 +1263,7 @@ let ``slice returns correct submatrix when col start of submatrix equals to col | Result.Ok result -> let coo = toCoordinateList result Assert.Equal(CoordinateList(4UL, 7UL, [ (1UL, 3UL, 33) ]), coo) - | Result.Error msg -> failwith msg + | Result.Error msg -> Assert.Fail(msg) [] let ``slice returns correct submatrix when col end of submatrix equals to col end of matrix`` () = @@ -1273,7 +1282,7 @@ let ``slice returns correct submatrix when col end of submatrix equals to col en | Result.Ok result -> let coo = toCoordinateList result Assert.Equal(CoordinateList(3UL, 5UL, [ (1UL, 1UL, 33) ]), coo) - | Result.Error msg -> failwith msg + | Result.Error msg -> Assert.Fail(msg) [] let ``slice returns single column`` () = @@ -1292,7 +1301,7 @@ let ``slice returns single column`` () = | Result.Ok result -> let coo = toCoordinateList result Assert.Equal(CoordinateList(6UL, 1UL, [ (1UL, 0UL, 2) ]), coo) - | Result.Error msg -> failwith msg + | Result.Error msg -> Assert.Fail(msg) [] let ``slice returns single row`` () = @@ -1311,7 +1320,7 @@ let ``slice returns single row`` () = | Result.Ok result -> let coo = toCoordinateList result Assert.Equal(CoordinateList(1UL, 7UL, [ (0UL, 4UL, 3) ]), coo) - | Result.Error msg -> failwith msg + | Result.Error msg -> Assert.Fail(msg) [] let ``let reduceRows sum on square power of two matrix`` () = @@ -1704,3 +1713,615 @@ let ``let reduceCols mul on single matrix`` () = Vector.CoordinateList(1UL, [ (0UL, 33) ]) Assert.Equal(expected, vectorCoordinates) + +[] +let ``kronecker product with square power of 2 x square power of two matrixes`` () = + let cooA = + CoordinateList( + 2UL, + 2UL, + [ (0UL, 0UL, 1) + (0UL, 1UL, 2) + (1UL, 0UL, 3) + (1UL, 1UL, 4) ] + ) + + let A = fromCoordinateList cooA + + let cooB = + CoordinateList( + 2UL, + 2UL, + [ (0UL, 0UL, 5) + (0UL, 1UL, 6) + (1UL, 0UL, 7) + (1UL, 1UL, 8) ] + ) + + let B = fromCoordinateList cooB + + let multiplyOp a b = Some(a * b) + + match kroneckerProduct A B multiplyOp with + | Error msg -> Assert.True(false, msg) + | Ok result -> + let coo = toCoordinateList result + + 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) ] + ) + + Assert.Equal(expected, coo) + +[] +let ``kronecker product with square not power of 2 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 A = fromCoordinateList cooA + + 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) ] + ) + + let B = fromCoordinateList cooB + + let multiplyOp a b = Some(a * b) + + match kroneckerProduct A B multiplyOp with + | Error msg -> Assert.True(false, msg) + | Ok result -> + let coo = toCoordinateList result + + let expected = + CoordinateList( + 9UL, + 9UL, + [ (0UL, 0UL, 10) + (0UL, 1UL, 11) + (0UL, 2UL, 12) + (0UL, 3UL, 20) + (0UL, 4UL, 22) + (0UL, 5UL, 24) + (0UL, 6UL, 30) + (0UL, 7UL, 33) + (0UL, 8UL, 36) + (1UL, 0UL, 13) + (1UL, 1UL, 14) + (1UL, 2UL, 15) + (1UL, 3UL, 26) + (1UL, 4UL, 28) + (1UL, 5UL, 30) + (1UL, 6UL, 39) + (1UL, 7UL, 42) + (1UL, 8UL, 45) + (2UL, 0UL, 16) + (2UL, 1UL, 17) + (2UL, 2UL, 18) + (2UL, 3UL, 32) + (2UL, 4UL, 34) + (2UL, 5UL, 36) + (2UL, 6UL, 48) + (2UL, 7UL, 51) + (2UL, 8UL, 54) + (3UL, 0UL, 40) + (3UL, 1UL, 44) + (3UL, 2UL, 48) + (3UL, 3UL, 50) + (3UL, 4UL, 55) + (3UL, 5UL, 60) + (3UL, 6UL, 60) + (3UL, 7UL, 66) + (3UL, 8UL, 72) + (4UL, 0UL, 52) + (4UL, 1UL, 56) + (4UL, 2UL, 60) + (4UL, 3UL, 65) + (4UL, 4UL, 70) + (4UL, 5UL, 75) + (4UL, 6UL, 78) + (4UL, 7UL, 84) + (4UL, 8UL, 90) + (5UL, 0UL, 64) + (5UL, 1UL, 68) + (5UL, 2UL, 72) + (5UL, 3UL, 80) + (5UL, 4UL, 85) + (5UL, 5UL, 90) + (5UL, 6UL, 96) + (5UL, 7UL, 102) + (5UL, 8UL, 108) + (6UL, 0UL, 70) + (6UL, 1UL, 77) + (6UL, 2UL, 84) + (6UL, 3UL, 80) + (6UL, 4UL, 88) + (6UL, 5UL, 96) + (6UL, 6UL, 90) + (6UL, 7UL, 99) + (6UL, 8UL, 108) + (7UL, 0UL, 91) + (7UL, 1UL, 98) + (7UL, 2UL, 105) + (7UL, 3UL, 104) + (7UL, 4UL, 112) + (7UL, 5UL, 120) + (7UL, 6UL, 117) + (7UL, 7UL, 126) + (7UL, 8UL, 135) + (8UL, 0UL, 112) + (8UL, 1UL, 119) + (8UL, 2UL, 126) + (8UL, 3UL, 128) + (8UL, 4UL, 136) + (8UL, 5UL, 144) + (8UL, 6UL, 144) + (8UL, 7UL, 153) + (8UL, 8UL, 162) ] + ) + + Assert.Equal(expected, coo) + +[] +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 A = fromCoordinateList cooA + + let cooB = + CoordinateList( + 2UL, + 2UL, + [ (0UL, 0UL, 5) + (0UL, 1UL, 6) + (1UL, 0UL, 7) + (1UL, 1UL, 8) ] + ) + + let B = fromCoordinateList cooB + + let result = kroneckerProduct A B (fun a b -> Some(a * b)) + + match result with + | Error msg -> Assert.True(false, msg) + | Ok res -> + let coo = toCoordinateList res + + let expectedElements = + [ (0UL, 0UL, 5) + (0UL, 1UL, 6) + (1UL, 0UL, 7) + (1UL, 1UL, 8) + + (0UL, 2UL, 10) + (0UL, 3UL, 12) + (1UL, 2UL, 14) + (1UL, 3UL, 16) + + (2UL, 0UL, 15) + (2UL, 1UL, 18) + (3UL, 0UL, 21) + (3UL, 1UL, 24) + + (2UL, 4UL, 20) + (2UL, 5UL, 24) + (3UL, 4UL, 28) + (3UL, 5UL, 32) ] + |> List.sortBy (fun (r, c, _) -> (r, c)) + + let actualElements = coo.list |> List.sortBy (fun (r, c, _) -> (r, c)) + + Assert.Equal(4UL, coo.nrows) + Assert.Equal(6UL, coo.ncols) + Assert.Equal * uint64 * int>>(expectedElements, actualElements) + +[] +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 A = fromCoordinateList cooA + + let cooB = + CoordinateList( + 2UL, + 3UL, + [ (0UL, 0UL, 5) + (0UL, 1UL, 6) + (0UL, 2UL, 7) + (1UL, 0UL, 8) + (1UL, 1UL, 9) + (1UL, 2UL, 10) ] + ) + + let B = fromCoordinateList cooB + + let result = kroneckerProduct A B (fun a b -> Some(a * b)) + + match result with + | Error msg -> Assert.True(false, msg) + | Ok res -> + let coo = toCoordinateList res + + 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)) + + let actualElements = coo.list |> List.sortBy (fun (r, c, _) -> (r, c)) + + Assert.Equal(4UL, coo.nrows) + Assert.Equal(6UL, coo.ncols) + Assert.Equal * uint64 * int>>(expectedElements, actualElements) + +[] +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 A = fromCoordinateList cooA + + let emptyMatrix = fromCoordinateList (CoordinateList(2UL, 2UL, [])) + + let result = kroneckerProduct A emptyMatrix (fun a b -> Some(a * b)) + + match result with + | Error msg -> Assert.True(false, msg) + | Ok res -> + let coo = toCoordinateList res + let expected = CoordinateList(4UL, 4UL, []) + Assert.Equal(expected, coo) + +[] +let ``kronecker product of empty matrix with matrix`` () = + let emptyMatrix = fromCoordinateList (CoordinateList(2UL, 2UL, [])) + + let cooB = + CoordinateList( + 2UL, + 2UL, + [ (0UL, 0UL, 1) + (0UL, 1UL, 2) + (1UL, 0UL, 3) + (1UL, 1UL, 4) ] + ) + + let B = fromCoordinateList cooB + + let result = kroneckerProduct emptyMatrix B (fun a b -> Some(a * b)) + + match result with + | Error msg -> Assert.True(false, msg) + | Ok res -> + let coo = toCoordinateList res + let expected = CoordinateList(4UL, 4UL, []) + Assert.Equal(expected, coo) + +[] +let ``kronecker product of matrix with zeros`` () = + let cooA = + CoordinateList(1UL, 1UL, [ (0UL, 0UL, 2) ]) + + let A = fromCoordinateList cooA + + let cooB = + CoordinateList(2UL, 2UL, [ (0UL, 0UL, 0); (1UL, 1UL, 3) ]) + + let B = fromCoordinateList cooB + + let result = kroneckerProduct A B (fun a b -> Some(a * b)) + + match result with + | Error msg -> Assert.True(false, msg) + | Ok res -> + let coo = toCoordinateList res + + let expectedElements = + [ (0UL, 0UL, 0); (1UL, 1UL, 6) ] + |> List.sortBy (fun (r, c, _) -> (r, c)) + + let actualElements = coo.list |> List.sortBy (fun (r, c, _) -> (r, c)) + + Assert.Equal(2UL, coo.nrows) + Assert.Equal(2UL, coo.ncols) + Assert.Equal * uint64 * int>>(expectedElements, actualElements) + +[] +let ``kronecker product resulting entirely in explicit zeros`` () = + let cooA = + CoordinateList(2UL, 2UL, [ (0UL, 0UL, 0); (1UL, 1UL, 0) ]) + + let A = fromCoordinateList cooA + + let cooB = + CoordinateList(1UL, 1UL, [ (0UL, 0UL, 5) ]) + + let B = fromCoordinateList cooB + + let result = kroneckerProduct A B (fun a b -> Some(a * b)) + + match result with + | Error msg -> Assert.True(false, msg) + | Ok res -> + let coo = toCoordinateList res + + let expectedElements = + [ (0UL, 0UL, 0); (1UL, 1UL, 0) ] + |> List.sortBy (fun (r, c, _) -> (r, c)) + + let actualElements = coo.list |> List.sortBy (fun (r, c, _) -> (r, c)) + + Assert.Equal(2UL, coo.nrows) + Assert.Equal(2UL, coo.ncols) + Assert.Equal * uint64 * int>>(expectedElements, actualElements) + +[] +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 A = fromCoordinateList cooA + + let cooB = + CoordinateList(1UL, 1UL, [ (0UL, 0UL, 3) ]) + + let B = fromCoordinateList cooB + + let result = kroneckerProduct A B (fun a b -> Some(a * b)) + + match result with + | Error msg -> Assert.True(false, msg) + | Ok res -> + let coo = toCoordinateList res + + let expected = + CoordinateList( + 2UL, + 2UL, + [ (0UL, 0UL, 3) + (0UL, 1UL, 6) + (1UL, 0UL, 9) + (1UL, 1UL, 12) ] + ) + + Assert.Equal(expected, coo) + +[] +let ``kronecker product of matrix 1x1 with square matrix`` () = + let cooA = + CoordinateList(1UL, 1UL, [ (0UL, 0UL, 3) ]) + + let A = fromCoordinateList cooA + + let cooB = + CoordinateList( + 2UL, + 2UL, + [ (0UL, 0UL, 1) + (0UL, 1UL, 2) + (1UL, 0UL, 3) + (1UL, 1UL, 4) ] + ) + + let B = fromCoordinateList cooB + + let result = kroneckerProduct A B (fun a b -> Some(a * b)) + + match result with + | Error msg -> Assert.True(false, msg) + | Ok res -> + let coo = toCoordinateList res + + let expected = + CoordinateList( + 2UL, + 2UL, + [ (0UL, 0UL, 3) + (0UL, 1UL, 6) + (1UL, 0UL, 9) + (1UL, 1UL, 12) ] + ) + + Assert.Equal(expected, coo) + +[] +let ``kronecker dimension check`` () = + let cooA = + CoordinateList(3UL, 4UL, [ (0UL, 0UL, 1) ]) + + let A = fromCoordinateList cooA + + let cooB = + CoordinateList(2UL, 5UL, [ (0UL, 0UL, 1) ]) + + let B = fromCoordinateList cooB + + let result = kroneckerProduct A B (fun a b -> Some(a * b)) + + match result with + | Error msg -> Assert.True(false, msg) + | Ok res -> + Assert.Equal(6UL, res.nrows) + Assert.Equal(20UL, res.ncols) + +[] +let ``kronecker product with sparse matrix on dense matrix`` () = + let cooA = + CoordinateList(10UL, 10UL, [ (5UL, 5UL, 2) ]) + + let A = fromCoordinateList cooA + + 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) ] + ) + + let B = fromCoordinateList cooB + + let result = kroneckerProduct A B (fun a b -> Some(a * b)) + + match result with + | Error msg -> Assert.True(false, msg) + | Ok res -> + let coo = toCoordinateList 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, coo) + +[] +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 A = fromCoordinateList cooA + + let cooB = + CoordinateList( + 2UL, + 2UL, + [ (0UL, 0UL, 1) + (0UL, 1UL, 2) + (1UL, 0UL, 3) + (1UL, 1UL, 4) ] + ) + + let B = fromCoordinateList cooB + + let evenOnly a b = + let prod = a * b + if prod % 2 = 0 then Some prod else None + + let result = kroneckerProduct A B evenOnly + + match result with + | Error msg -> Assert.True(false, msg) + | Ok res -> + let coo = toCoordinateList res + Assert.True(coo.list |> List.forall (fun (_, _, v) -> v % 2 = 0)) + Assert.True(List.length coo.list < 16) diff --git a/QuadTree.Tests/Tests.Vector.fs b/QuadTree.Tests/Tests.Vector.fs index e10ae47..61ee41f 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 @@ -964,11 +965,9 @@ let ``map can change type from int to string`` () = Assert.Equal(CoordinateList(3UL, [ (1UL, "str 11"); (2UL, "str 33") ]), coo) [] -let ``fromCoordinateList with index out of range`` () = +let ``fromCoordinateList with out-of-range index throws exception`` () = let coo = CoordinateList(6UL, [ (9UL, 8) ]) - let vec = fromCoordinateList coo - let result = toCoordinateList vec - Assert.Equal(CoordinateList(6UL, []), result) + Assert.Throws(fun () -> fromCoordinateList coo |> ignore) [] let ``fromCoordinateList with unsorted coordinates works correctly`` () = @@ -978,20 +977,18 @@ let ``fromCoordinateList with unsorted coordinates works correctly`` () = let vec = fromCoordinateList coo 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)]) + let coo = CoordinateList(3UL, [ (1UL, 33); (1UL, 100) ]) let vec = Vector.fromCoordinateList coo let result = Vector.toCoordinateList vec - Assert.Equal(CoordinateList(3UL, [(1UL, 100)]), result) -*) + Assert.Equal(CoordinateList(3UL, [ (1UL, 100) ]), result) + [] -let ``fromCoordinateList with zero length and some values returns empty vector`` () = +let ``fromCoordinateList with zero size throws Exception`` () = let coo = CoordinateList(0UL, [ (33UL, 33); (39UL, 1) ]) - let vec = fromCoordinateList coo - let result = toCoordinateList vec - Assert.Equal(CoordinateList(0UL, []), result) + Assert.Throws(fun () -> fromCoordinateList coo |> ignore) [] let ``mapi works with index`` () = @@ -1056,7 +1053,7 @@ let ``slice returns error when start is negative`` () = let vec = fromCoordinateList (CoordinateList(5UL, [ (2UL, 10) ])) match slice -1 3 vec with - | Result.Ok _ -> failwith "Expected Error" + | Result.Ok _ -> Assert.Fail("Expected Error") | Result.Error msg -> Assert.Equal("Start should be >= 0", msg) [] @@ -1064,7 +1061,7 @@ let ``slice returns error when end is negative`` () = let vec = fromCoordinateList (CoordinateList(5UL, [ (2UL, 10) ])) match slice 1 -3 vec with - | Result.Ok _ -> failwith "Expected Error" + | Result.Ok _ -> Assert.Fail("Expected Error") | Result.Error msg -> Assert.Equal("End should be >= 0", msg) [] @@ -1072,7 +1069,7 @@ let ``slice returns error when start is out of range`` () = let vec = fromCoordinateList (CoordinateList(5UL, [ (2UL, 10) ])) match slice 7 3 vec with - | Result.Ok _ -> failwith "Expected Error" + | Result.Ok _ -> Assert.Fail("Expected Error") | Result.Error msg -> Assert.Equal("Start is out of Vector length", msg) [] @@ -1080,7 +1077,7 @@ let ``slice returns error when end is out of range`` () = let vec = fromCoordinateList (CoordinateList(5UL, [ (2UL, 10) ])) match slice 3 7 vec with - | Result.Ok _ -> failwith "Expected Error" + | Result.Ok _ -> Assert.Fail("Expected Error") | Result.Error msg -> Assert.Equal("End is out of Vector length", msg) [] @@ -1088,7 +1085,7 @@ let ``slice returns error when end is less than start`` () = let vec = fromCoordinateList (CoordinateList(5UL, [ (2UL, 10) ])) match slice 4 3 vec with - | Result.Ok _ -> failwith "Expected Error" + | Result.Ok _ -> Assert.Fail("Expected Error") | Result.Error msg -> Assert.Equal("End should be >= Start", msg) [] @@ -1100,7 +1097,7 @@ let ``slice returns correct subvector`` () = | Result.Ok result -> let coo = Vector.toCoordinateList result Assert.Equal(CoordinateList(3UL, [ (1UL, 10) ]), coo) - | Result.Error msg -> failwith msg + | Result.Error msg -> Assert.Fail(msg) [] let ``slice returns subvector without elements`` () = @@ -1111,7 +1108,7 @@ let ``slice returns subvector without elements`` () = | Result.Ok result -> let coo = toCoordinateList result Assert.Equal(CoordinateList(3UL, []), coo) - | Result.Error msg -> failwith msg + | Result.Error msg -> Assert.Fail(msg) [] let ``slice returns single subvector`` () = @@ -1122,7 +1119,7 @@ let ``slice returns single subvector`` () = | Result.Ok result -> let coo = toCoordinateList result Assert.Equal(CoordinateList(1UL, [ (0UL, 10) ]), coo) - | Result.Error msg -> failwith msg + | Result.Error msg -> Assert.Fail(msg) [] let ``slice returns correct subvector equals to vector`` () = @@ -1133,7 +1130,7 @@ let ``slice returns correct subvector equals to vector`` () = | Result.Ok result -> let coo = toCoordinateList result Assert.Equal(CoordinateList(7UL, [ (2UL, 10); (3UL, 33); (6UL, 20) ]), coo) - | Result.Error msg -> failwith msg + | Result.Error msg -> Assert.Fail(msg) [] let ``slice returns correct subvector when start of subvector equals to start of vector`` () = @@ -1144,7 +1141,7 @@ let ``slice returns correct subvector when start of subvector equals to start of | Result.Ok result -> let coo = Vector.toCoordinateList result Assert.Equal(CoordinateList(5UL, [ (0UL, 10); (3UL, 33) ]), coo) - | Result.Error msg -> failwith msg + | Result.Error msg -> Assert.Fail(msg) [] let ``slice returns correct subvector when end of subvector equals to end of vector`` () = @@ -1155,4 +1152,4 @@ let ``slice returns correct subvector when end of subvector equals to end of vec | Result.Ok result -> let coo = Vector.toCoordinateList result Assert.Equal(CoordinateList(5UL, [ (1UL, 33); (4UL, 20) ]), coo) - | Result.Error msg -> failwith msg + | Result.Error msg -> Assert.Fail(msg) diff --git a/QuadTree/Matrix.fs b/QuadTree/Matrix.fs index f7fb260..e42917d 100644 --- a/QuadTree/Matrix.fs +++ b/QuadTree/Matrix.fs @@ -74,7 +74,20 @@ 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 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 + failwith "Coordinates out of range" + + let nvals = (uint64 <| List.length unique) * 1UL let nrows = coo.nrows let ncols = coo.ncols @@ -108,7 +121,7 @@ let fromCoordinateList (coo: CoordinateList<'a>) = (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)) @@ -426,67 +439,138 @@ let slice (colStart: int) (colEnd: int) : Result, string> = - match () with - | _ when rowStart < 0 -> Error "Start row should be >= 0" - | _ when rowEnd < 0 -> Error "End row should be >= 0" - | _ when colStart < 0 -> Error "Start column should be >= 0" - | _ when colEnd < 0 -> Error "End column should be >= 0" - | _ when rowStart > int matrix.nrows - 1 -> Error "Start row is out of matrix length" - | _ when rowEnd > int matrix.nrows - 1 -> Error "End row is out of matrix length" - | _ when colStart > int matrix.ncols - 1 -> Error "Start column is out of matrix length" - | _ when colEnd > int matrix.ncols - 1 -> Error "End column is out of matrix length" - | _ when rowStart > rowEnd -> Error "Start row should be <= end row" - | _ when colStart > colEnd -> Error "Start column should be <= end column" - | _ -> + 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 inner (size: uint64) (row: uint64) (col: uint64) matrix = + let rec cut (size: uint64) (row: uint64) (col: uint64) tree = let sizeRow = (uint64 size) * 1UL let sizeCol = (uint64 size) * 1UL - match matrix with + match tree with | Node(nw, ne, sw, se) -> - let halfSize = size / 2UL - let halfRow = (uint64 halfSize) * 1UL - let halfCol = (uint64 halfSize) * 1UL - let nwTree, nwNvals = inner halfSize row col nw - let neTree, neNvals = inner halfSize row (col + halfCol) ne - let swTree, swNvals = inner halfSize (row + halfRow) col sw - let seTree, seNvals = inner halfSize (row + halfRow) (col + halfCol) se - (mkNode nwTree neTree swTree seTree), nwNvals + neNvals + swNvals + seNvals - | Leaf(Dummy) -> Leaf(Dummy), 0UL + 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)), (uint64 size) * (uint64 size) * 1UL - | Leaf(UserValue(v)) when row + sizeRow - 1UL < rowStartIdx -> Leaf(Dummy), 0UL - | Leaf(UserValue(v)) when col + sizeCol - 1UL < colStartIdx -> Leaf(Dummy), 0UL - | Leaf(UserValue(v)) when row > rowEndIdx -> Leaf(Dummy), 0UL - | Leaf(UserValue(v)) when col > colEndIdx -> Leaf(Dummy), 0UL - | _ -> Leaf(Dummy), 0UL + 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 storage, nvals = - inner matrix.storage.size 0UL 0UL matrix.storage.data + let emptyTree = empty newSize - let newRows = uint64 (rowEnd - rowStart + 1) * 1UL - let newCols = uint64 (colEnd - colStart + 1) * 1UL + let shiftedTree = + rebuild matrix.storage.size 0UL 0UL cutTree emptyTree - let coo = - toCoordinateList (SparseMatrix(newRows, newCols, nvals, Storage(matrix.storage.size, storage))) + 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 shiftedData = - coo.list - |> List.map (fun (i, j, v) -> (i - uint64 rowStart * 1UL, j - uint64 colStart * 1UL, v)) + let nvals = count newSize shiftedTree - let shiftedCoo = CoordinateList(newRows, newCols, shiftedData) - let newMatrix = fromCoordinateList shiftedCoo - Ok newMatrix + Ok(SparseMatrix(newRows, newCols, nvals, Storage(newSize, shiftedTree))) let reduceRows (op: 'a option -> 'a option -> 'a option) (matrix: SparseMatrix<'a>) : Vector.SparseVector<'a> = let rows = matrix.nrows @@ -579,3 +663,108 @@ let reduceCols (op: 'a option -> 'a option -> 'a option) (matrix: SparseMatrix<' data |> List.map (fun (_, col, v) -> (uint64 col * 1UL, v)) Vector.fromCoordinateList (Vector.CoordinateList(uint64 cols * 1UL, vectorData)) + +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 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 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/Vector.fs b/QuadTree/Vector.fs index a620616..42961f2 100644 --- a/QuadTree/Vector.fs +++ b/QuadTree/Vector.fs @@ -307,6 +307,16 @@ let update (vector: SparseVector<_>) i v op = Error Error.InconsistentSizeOfArguments let fromCoordinateList (lst: CoordinateList<'a>) : SparseVector<'a> = + 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 (idx, _) -> uint64 idx >= uint64 lst.length) then + failwith "Index out of range" + let length = lst.length let nvals = (uint64 <| List.length lst.data) * 1UL let storageSize = (getNearestUpperPowerOfTwo <| uint64 length) * 1UL @@ -325,7 +335,7 @@ let fromCoordinateList (lst: CoordinateList<'a>) : SparseVector<'a> = mkNode left right, rCoordinates - let sortedCoordinates = List.sort lst.data + let sortedCoordinates = List.sort unique let tree, _ = traverse sortedCoordinates 0UL ((uint64 storageSize) * 1UL) @@ -564,43 +574,88 @@ let scatter let slice (_start: int) (_end: int) (vector: SparseVector<'a>) : Result, string> = - match () with - | _ when _start < 0 -> Error "Start should be >= 0" - | _ when _end < 0 -> Error "End should be >= 0" - | _ when _start > int vector.length - 1 -> Error "Start is out of Vector length" - | _ when _end > int vector.length - 1 -> Error "End is out of Vector length" - | _ when _start > _end -> Error "End should be >= Start" - | _ -> + 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 inner (size: uint64) (pos: uint64) vector = + let rec cut (size: uint64) (pos: uint64) tree = let sizeIdx = (uint64 size) * 1UL - match vector with - | Node(x1, x2) -> + match tree with + | Node(l, r) -> let half = size / 2UL let halfIdx = (uint64 half) * 1UL - let t1, n1 = inner half pos x1 - let t2, n2 = inner half (pos + halfIdx) x2 - (mkNode t1 t2), n1 + n2 - | Leaf(Dummy) -> Leaf(Dummy), 0UL - | Leaf(UserValue(v)) when pos >= startIdx && pos + sizeIdx - 1UL <= endIdx -> - Leaf(UserValue(v)), (uint64 size) * 1UL - | Leaf(UserValue(v)) when pos + sizeIdx - 1UL < startIdx -> Leaf(Dummy), 0UL - | Leaf(UserValue(v)) when pos > endIdx -> Leaf(Dummy), 0UL - | Leaf(UserValue(v)) -> Leaf(Dummy), 0UL - - let storage, nvals = inner vector.storage.size 0UL vector.storage.data - let newLength = uint64 (_end - _start + 1) * 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 coo = - toCoordinateList (SparseVector(newLength, nvals, Storage(vector.storage.size, storage))) + 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 shiftedData = - coo.data - |> List.map (fun (idx, value) -> (idx - uint64 _start * 1UL, value)) + let nvals = count newSize shiftedTree - let shiftedCoo = CoordinateList(newLength, shiftedData) - let newVec = fromCoordinateList shiftedCoo - Ok newVec + Ok(SparseVector(newLength, nvals, Storage(newSize, shiftedTree))) From ad3ccbf82aa17643845beaf90f5895e7eb70e8a3 Mon Sep 17 00:00:00 2001 From: Brulevich-Nikita Date: Thu, 28 May 2026 23:05:19 +0300 Subject: [PATCH 3/3] change fromCoordinateList signature, fix failwith, fix boruvka and tests, fix reduces and kronecker --- QuadTree.Benchmark/BFS.fs | 13 +- QuadTree.Benchmark/SSSP.fs | 6 +- QuadTree.Benchmark/Triangles.fs | 5 +- QuadTree.Tests/Tests.Boruvka.fs | 375 ++-- QuadTree.Tests/Tests.LinearAlgebra.fs | 75 +- QuadTree.Tests/Tests.Matrix.fs | 2401 ++++++++++++------------- QuadTree.Tests/Tests.SSSP.fs | 56 +- QuadTree.Tests/Tests.TriangleCount.fs | 24 +- QuadTree.Tests/Tests.Vector.fs | 1061 +++++------ QuadTree/Boruvka.fs | 93 +- QuadTree/Matrix.fs | 180 +- QuadTree/SSSP.fs | 7 +- QuadTree/Vector.fs | 52 +- 13 files changed, 2130 insertions(+), 2218 deletions(-) diff --git a/QuadTree.Benchmark/BFS.fs b/QuadTree.Benchmark/BFS.fs index b68adcd..d3fbc23 100644 --- a/QuadTree.Benchmark/BFS.fs +++ b/QuadTree.Benchmark/BFS.fs @@ -8,18 +8,25 @@ type Benchmark() = let mutable matrix = Unchecked.defaultof> - [] member val MatrixName = "" with get, set [] member this.LoadMatrix() = - matrix <- readMtx (System.IO.Path.Combine(DIR_WITH_MATRICES, this.MatrixName)) false + matrix <- + match readMtx (System.IO.Path.Combine(DIR_WITH_MATRICES, this.MatrixName)) false with + | Ok m -> m + | Error _ -> Matrix.empty 0UL 0UL [] member this.BFS() = - let startVertices = + let startVerticesResult = Vector.CoordinateList((uint64 matrix.ncols) * 1UL, [ 0UL, 1UL ]) |> Vector.fromCoordinateList + let startVertices = + match startVerticesResult with + | Ok v -> v + | Error _ -> Vector.empty ((uint64 matrix.ncols) * 1UL) + Graph.BFS.bfs_level matrix startVertices diff --git a/QuadTree.Benchmark/SSSP.fs b/QuadTree.Benchmark/SSSP.fs index 002fa3a..1857a28 100644 --- a/QuadTree.Benchmark/SSSP.fs +++ b/QuadTree.Benchmark/SSSP.fs @@ -7,13 +7,15 @@ open QuadTree.Benchmarks.Utils type Benchmark() = let mutable matrix = Unchecked.defaultof> - [] member val MatrixName = "" with get, set [] member this.LoadMatrix() = - matrix <- readMtx (System.IO.Path.Combine(DIR_WITH_MATRICES, this.MatrixName)) false + matrix <- + match readMtx (System.IO.Path.Combine(DIR_WITH_MATRICES, this.MatrixName)) false with + | Ok m -> m + | Error _ -> Matrix.empty 0UL 0UL [] member this.SSSP() = Graph.SSSP.sssp matrix 0UL diff --git a/QuadTree.Benchmark/Triangles.fs b/QuadTree.Benchmark/Triangles.fs index 7fa31e4..5b0532e 100644 --- a/QuadTree.Benchmark/Triangles.fs +++ b/QuadTree.Benchmark/Triangles.fs @@ -13,7 +13,10 @@ type Benchmark() = [] member this.LoadMatrix() = - matrix <- readMtx (System.IO.Path.Combine(DIR_WITH_MATRICES, this.MatrixName)) false + matrix <- + match readMtx (System.IO.Path.Combine(DIR_WITH_MATRICES, this.MatrixName)) false with + | Ok m -> m + | Error _ -> Matrix.empty 0UL 0UL [] member this.TriangleCount() = diff --git a/QuadTree.Tests/Tests.Boruvka.fs b/QuadTree.Tests/Tests.Boruvka.fs index 6f2e2a0..1a22f37 100644 --- a/QuadTree.Tests/Tests.Boruvka.fs +++ b/QuadTree.Tests/Tests.Boruvka.fs @@ -7,46 +7,41 @@ open Matrix open Vector open Common -let checkResult name actual expected = +let assertBoruvkaEqualsExpected actual expected = match actual with | Ok tree -> - let tree_transposed = Matrix.transpose tree - - let actual = - Matrix.map2 tree tree_transposed (fun x y -> - match (x, y) with - | (Some(x), _) - | (_, Some x) -> Some x + let actualResult = + Matrix.map2 tree (Matrix.transpose tree) (fun x y -> + match x, y with + | Some x, _ + | _, Some x -> Some x | _ -> None) - Assert.Equal(expected, actual) - | x -> Assert.Fail(sprintf "Boruvka failed: %A" x) + match actualResult with + | Ok actual -> Assert.Equal(expected, actual) + | Error err -> Assert.Fail(sprintf "Symmetrization failed: %A" err) + + | Error err -> Assert.Fail(sprintf "Boruvka failed: %A" err) [] let ``Boruvka MST 2 nodes.`` () = - let graph = - let clist = - Matrix.CoordinateList( - 2UL, - 2UL, - [ 0UL, 1UL, 5UL; 1UL, 0UL, 5UL ] - ) - - Matrix.fromCoordinateList clist - - let expected = - let clist = - Matrix.CoordinateList( - 2UL, - 2UL, - [ 0UL, 1UL, 5UL; 1UL, 0UL, 5UL ] - ) - - Matrix.fromCoordinateList clist |> Ok - - checkResult (Graph.Boruvka.mst graph) expected - + let clist = + Matrix.CoordinateList( + 2UL, + 2UL, + [ 0UL, 1UL, 5UL; 1UL, 0UL, 5UL ] + ) + + match Matrix.fromCoordinateList clist with + | Result.Error _ -> Assert.Fail() + | Result.Ok graph -> + match Matrix.fromCoordinateList clist with + | Result.Error _ -> Assert.Fail() + | Result.Ok expected -> + match Graph.Boruvka.mst graph with + | Result.Error _ -> Assert.Fail() + | Result.Ok actual -> Assert.Equal(expected, actual) [] let ``Boruvka MST 3 nodes line.`` () = @@ -75,11 +70,13 @@ let ``Boruvka MST 3 nodes line.`` () = 2UL, 1UL, 2UL ] ) - Matrix.fromCoordinateList clist |> Ok - - checkResult (Graph.Boruvka.mst graph) expected - + Matrix.fromCoordinateList clist + match graph, expected with + | Ok g, Ok e -> + let actual = Graph.Boruvka.mst g + assertBoruvkaEqualsExpected actual e + | _ -> Assert.Fail("Setup failed") [] let ``Boruvka MST 4 nodes line.`` () = @@ -112,10 +109,13 @@ let ``Boruvka MST 4 nodes line.`` () = 3UL, 2UL, 3UL ] ) - Matrix.fromCoordinateList clist |> Ok - - checkResult (Graph.Boruvka.mst graph) expected + Matrix.fromCoordinateList clist + match graph, expected with + | Ok g, Ok e -> + let actual = Graph.Boruvka.mst g + assertBoruvkaEqualsExpected actual e + | _ -> Assert.Fail("Setup failed") [] let ``Boruvka MST 5 nodes line.`` () = @@ -152,10 +152,13 @@ let ``Boruvka MST 5 nodes line.`` () = 4UL, 3UL, 4UL ] ) - Matrix.fromCoordinateList clist |> Ok - - checkResult (Graph.Boruvka.mst graph) expected + Matrix.fromCoordinateList clist + match graph, expected with + | Ok g, Ok e -> + let actual = Graph.Boruvka.mst g + assertBoruvkaEqualsExpected actual e + | _ -> Assert.Fail("Setup failed") [] let ``Boruvka MST 5 nodes star.`` () = @@ -192,9 +195,13 @@ let ``Boruvka MST 5 nodes star.`` () = 4UL, 0UL, 2UL ] ) - Matrix.fromCoordinateList clist |> Ok + Matrix.fromCoordinateList clist - checkResult (Graph.Boruvka.mst graph) expected + match graph, expected with + | Ok g, Ok e -> + let actual = Graph.Boruvka.mst g + assertBoruvkaEqualsExpected actual e + | _ -> Assert.Fail("Setup failed") [] @@ -244,10 +251,13 @@ let ``Boruvka MST 5 nodes complete.`` () = 4UL, 0UL, 4UL ] ) - Matrix.fromCoordinateList clist |> Ok - - checkResult (Graph.Boruvka.mst graph) expected + Matrix.fromCoordinateList clist + match graph, expected with + | Ok g, Ok e -> + let actual = Graph.Boruvka.mst g + assertBoruvkaEqualsExpected actual e + | _ -> Assert.Fail("Setup failed") [] let ``Boruvka MST two components.`` () = @@ -291,10 +301,13 @@ let ``Boruvka MST two components.`` () = 5UL, 4UL, 2UL ] ) - Matrix.fromCoordinateList clist |> Ok - - checkResult (Graph.Boruvka.mst graph) expected + Matrix.fromCoordinateList clist + match graph, expected with + | Ok g, Ok e -> + let actual = Graph.Boruvka.mst g + assertBoruvkaEqualsExpected actual e + | _ -> Assert.Fail("Setup failed") [] let ``Boruvka MST cycle graph 6 nodes.`` () = @@ -337,9 +350,13 @@ let ``Boruvka MST cycle graph 6 nodes.`` () = 5UL, 4UL, 5UL ] ) - Matrix.fromCoordinateList clist |> Ok + Matrix.fromCoordinateList clist - checkResult (Graph.Boruvka.mst graph) expected + match graph, expected with + | Ok g, Ok e -> + let actual = Graph.Boruvka.mst g + assertBoruvkaEqualsExpected actual e + | _ -> Assert.Fail("Setup failed") [] let ``Boruvka MST complete bipartite K3,3.`` () = @@ -349,8 +366,7 @@ let ``Boruvka MST complete bipartite K3,3.`` () = Matrix.CoordinateList( 6UL, 6UL, - [ - // K3,3: vertices 0,1,2 connected to 3,4,5 + [ // K3,3: vertices 0,1,2 connected to 3,4,5 0UL, 3UL, 1UL 3UL, 0UL, 1UL @@ -398,9 +414,13 @@ let ``Boruvka MST complete bipartite K3,3.`` () = 5UL, 0UL, 3UL ] ) - Matrix.fromCoordinateList clist |> Ok + Matrix.fromCoordinateList clist - checkResult (Graph.Boruvka.mst graph) expected + match graph, expected with + | Ok g, Ok e -> + let actual = Graph.Boruvka.mst g + assertBoruvkaEqualsExpected actual e + | _ -> Assert.Fail("Setup failed") [] let ``Boruvka MST random weights.`` () = @@ -452,8 +472,6 @@ let ``Boruvka MST random weights.`` () = 2UL, 1UL, 3UL 2UL, 3UL, 2UL 3UL, 2UL, 2UL - 1UL, 3UL, 4UL - 3UL, 1UL, 4UL 4UL, 5UL, 1UL 5UL, 4UL, 1UL 5UL, 6UL, 3UL @@ -464,9 +482,13 @@ let ``Boruvka MST random weights.`` () = 4UL, 3UL, 10UL ] ) - Matrix.fromCoordinateList clist |> Ok + Matrix.fromCoordinateList clist - checkResult (Graph.Boruvka.mst graph) expected + match graph, expected with + | Ok g, Ok e -> + let actual = Graph.Boruvka.mst g + assertBoruvkaEqualsExpected actual e + | _ -> Assert.Fail("Graph creation failed") [] let ``Boruvka MST 8 nodes grid.`` () = @@ -476,8 +498,7 @@ let ``Boruvka MST 8 nodes grid.`` () = Matrix.CoordinateList( 8UL, 8UL, - [ - // Row 0-1 connections + [ // Row 0-1 connections 0UL, 1UL, 1UL 1UL, 0UL, 1UL 1UL, 2UL, 2UL @@ -525,9 +546,13 @@ let ``Boruvka MST 8 nodes grid.`` () = 7UL, 6UL, 3UL ] ) - Matrix.fromCoordinateList clist |> Ok + Matrix.fromCoordinateList clist - checkResult (Graph.Boruvka.mst graph) expected + match graph, expected with + | Ok g, Ok e -> + let actual = Graph.Boruvka.mst g + assertBoruvkaEqualsExpected actual e + | _ -> Assert.Fail("Setup failed") [] let ``Boruvka MST 10 nodes random.`` () = @@ -539,28 +564,40 @@ let ``Boruvka MST 10 nodes random.`` () = 10UL, [ 0UL, 1UL, 4UL 1UL, 0UL, 4UL + 0UL, 5UL, 2UL 5UL, 0UL, 2UL + 1UL, 2UL, 3UL 2UL, 1UL, 3UL + 1UL, 6UL, 5UL 6UL, 1UL, 5UL + 2UL, 3UL, 1UL 3UL, 2UL, 1UL + 2UL, 7UL, 4UL 7UL, 2UL, 4UL + 3UL, 4UL, 2UL 4UL, 3UL, 2UL + 3UL, 8UL, 6UL 8UL, 3UL, 6UL + 4UL, 9UL, 3UL 9UL, 4UL, 3UL + 5UL, 6UL, 1UL 6UL, 5UL, 1UL + 6UL, 7UL, 2UL 7UL, 6UL, 2UL + 7UL, 8UL, 1UL 8UL, 7UL, 1UL + 8UL, 9UL, 4UL 9UL, 8UL, 4UL ] ) @@ -574,36 +611,39 @@ let ``Boruvka MST 10 nodes random.`` () = 10UL, [ 0UL, 1UL, 4UL 1UL, 0UL, 4UL + 0UL, 5UL, 2UL 5UL, 0UL, 2UL + 1UL, 2UL, 3UL 2UL, 1UL, 3UL - 1UL, 6UL, 5UL - 6UL, 1UL, 5UL + 2UL, 3UL, 1UL 3UL, 2UL, 1UL - 2UL, 7UL, 4UL - 7UL, 2UL, 4UL + 3UL, 4UL, 2UL 4UL, 3UL, 2UL - 3UL, 8UL, 6UL - 8UL, 3UL, 6UL + 4UL, 9UL, 3UL 9UL, 4UL, 3UL + 5UL, 6UL, 1UL 6UL, 5UL, 1UL + 6UL, 7UL, 2UL 7UL, 6UL, 2UL + 7UL, 8UL, 1UL - 8UL, 7UL, 1UL - 8UL, 9UL, 4UL - 9UL, 8UL, 4UL ] + 8UL, 7UL, 1UL ] ) - Matrix.fromCoordinateList clist |> Ok - - checkResult (Graph.Boruvka.mst graph) expected + Matrix.fromCoordinateList clist + match graph, expected with + | Ok g, Ok e -> + let actual = Graph.Boruvka.mst g + assertBoruvkaEqualsExpected actual e + | _ -> Assert.Fail("Failed to create matrices!") [] let ``Boruvka MST simple triangle.`` () = @@ -615,19 +655,14 @@ let ``Boruvka MST simple triangle.`` () = 3UL, [ 0UL, 1UL, 1UL 1UL, 0UL, 1UL - 0UL, 2UL, 1UL 2UL, 0UL, 1UL - 1UL, 2UL, 1UL - 2UL, 1UL, 1UL - - ] + 2UL, 1UL, 1UL ] ) Matrix.fromCoordinateList clist - let expected = let clist = Matrix.CoordinateList( @@ -635,15 +670,17 @@ let ``Boruvka MST simple triangle.`` () = 3UL, [ 0UL, 1UL, 1UL 1UL, 0UL, 1UL - 0UL, 2UL, 1UL 2UL, 0UL, 1UL ] ) - Matrix.fromCoordinateList clist |> Ok - - checkResult (Graph.Boruvka.mst graph) expected + Matrix.fromCoordinateList clist + match graph, expected with + | Ok g, Ok e -> + let actual = Graph.Boruvka.mst g + assertBoruvkaEqualsExpected actual e + | _ -> Assert.Fail("Setup failed") [] let ``Boruvka MST simple square.`` () = @@ -655,22 +692,16 @@ let ``Boruvka MST simple square.`` () = 4UL, [ 0UL, 1UL, 1UL 1UL, 0UL, 1UL - 1UL, 2UL, 1UL 2UL, 1UL, 1UL - 2UL, 3UL, 1UL 3UL, 2UL, 1UL - 0UL, 3UL, 1UL - 3UL, 0UL, 1UL - - ] + 3UL, 0UL, 1UL ] ) Matrix.fromCoordinateList clist - let expected = let clist = Matrix.CoordinateList( @@ -678,20 +709,19 @@ let ``Boruvka MST simple square.`` () = 4UL, [ 0UL, 1UL, 1UL 1UL, 0UL, 1UL - 0UL, 3UL, 1UL 3UL, 0UL, 1UL - 1UL, 2UL, 1UL 2UL, 1UL, 1UL ] ) - Matrix.fromCoordinateList clist |> Ok - - checkResult (Graph.Boruvka.mst graph) expected - - + Matrix.fromCoordinateList clist + match graph, expected with + | Ok g, Ok e -> + let actual = Graph.Boruvka.mst g + assertBoruvkaEqualsExpected actual e + | _ -> Assert.Fail("Setup failed") [] let ``Boruvka MST simple square in two steps.`` () = @@ -703,22 +733,16 @@ let ``Boruvka MST simple square in two steps.`` () = 4UL, [ 0UL, 1UL, 2UL 1UL, 0UL, 2UL - 1UL, 2UL, 1UL 2UL, 1UL, 1UL - 2UL, 3UL, 2UL 3UL, 2UL, 2UL - 0UL, 3UL, 1UL - 3UL, 0UL, 1UL - - ] + 3UL, 0UL, 1UL ] ) Matrix.fromCoordinateList clist - let expected = let clist = Matrix.CoordinateList( @@ -726,17 +750,19 @@ let ``Boruvka MST simple square in two steps.`` () = 4UL, [ 0UL, 1UL, 2UL 1UL, 0UL, 2UL - 0UL, 3UL, 1UL 3UL, 0UL, 1UL - 1UL, 2UL, 1UL 2UL, 1UL, 1UL ] ) - Matrix.fromCoordinateList clist |> Ok + Matrix.fromCoordinateList clist - checkResult (Graph.Boruvka.mst graph) expected + match graph, expected with + | Ok g, Ok e -> + let actual = Graph.Boruvka.mst g + assertBoruvkaEqualsExpected actual e + | _ -> Assert.Fail("Setup failed") @@ -751,41 +777,30 @@ let ``Boruvka MST.`` () = 7UL, [ 0UL, 1UL, 7UL 1UL, 0UL, 7UL - 0UL, 4UL, 4UL 4UL, 0UL, 4UL - 1UL, 2UL, 11UL 2UL, 1UL, 11UL - 1UL, 3UL, 10UL 3UL, 1UL, 10UL - 1UL, 4UL, 9UL 4UL, 1UL, 9UL - 2UL, 3UL, 5UL 3UL, 2UL, 5UL - 4UL, 3UL, 15UL 3UL, 4UL, 15UL - 4UL, 5UL, 6UL 5UL, 4UL, 6UL - 5UL, 3UL, 12UL 3UL, 5UL, 12UL - 6UL, 3UL, 8UL 3UL, 6UL, 8UL - 5UL, 6UL, 13UL 6UL, 5UL, 13UL ] ) Matrix.fromCoordinateList clist - let expected = let clist = Matrix.CoordinateList( @@ -793,29 +808,25 @@ let ``Boruvka MST.`` () = 7UL, [ 0UL, 1UL, 7UL 1UL, 0UL, 7UL - 0UL, 4UL, 4UL 4UL, 0UL, 4UL - 1UL, 3UL, 10UL 3UL, 1UL, 10UL - 2UL, 3UL, 5UL 3UL, 2UL, 5UL - 4UL, 5UL, 6UL 5UL, 4UL, 6UL - 6UL, 3UL, 8UL - 3UL, 6UL, 8UL - - ] + 3UL, 6UL, 8UL ] ) - Matrix.fromCoordinateList clist |> Ok - - checkResult (Graph.Boruvka.mst graph) expected + Matrix.fromCoordinateList clist + match graph, expected with + | Ok g, Ok e -> + let actual = Graph.Boruvka.mst g + assertBoruvkaEqualsExpected actual e + | _ -> Assert.Fail("Setup failed") [] let ``Boruvka MST big.`` () = @@ -833,7 +844,7 @@ let ``Boruvka MST big.`` () = 0UL, 11UL, 1UL 11UL, 0UL, 1UL - //================================================= + 2UL, 3UL, 1UL 3UL, 2UL, 1UL @@ -842,7 +853,7 @@ let ``Boruvka MST big.`` () = 2UL, 4UL, 1UL 4UL, 2UL, 1UL - //================================================= + 5UL, 6UL, 1UL 6UL, 5UL, 1UL @@ -851,7 +862,7 @@ let ``Boruvka MST big.`` () = 5UL, 7UL, 1UL 7UL, 5UL, 1UL - //================================================= + 8UL, 9UL, 1UL 9UL, 8UL, 1UL @@ -860,7 +871,7 @@ let ``Boruvka MST big.`` () = 8UL, 10UL, 1UL 10UL, 8UL, 1UL - //================================================ + 1UL, 2UL, 2UL 2UL, 1UL, 2UL @@ -872,19 +883,16 @@ let ``Boruvka MST big.`` () = 8UL, 7UL, 2UL 7UL, 8UL, 2UL - //================================================ + 10UL, 11UL, 3UL 11UL, 10UL, 3UL 5UL, 4UL, 3UL - 4UL, 5UL, 3UL - - ] + 4UL, 5UL, 3UL ] ) Matrix.fromCoordinateList clist - let expected = let clist = Matrix.CoordinateList( @@ -921,15 +929,16 @@ let ``Boruvka MST big.`` () = 10UL, 5UL, 2UL 4UL, 5UL, 3UL - 5UL, 4UL, 3UL - - ] + 5UL, 4UL, 3UL ] ) - Matrix.fromCoordinateList clist |> Ok - - checkResult (Graph.Boruvka.mst graph) expected + Matrix.fromCoordinateList clist + match graph, expected with + | Ok g, Ok e -> + let actual = Graph.Boruvka.mst g + assertBoruvkaEqualsExpected actual e + | _ -> Assert.Fail("Setup failed") [] let ``Boruvka MST complex line.`` () = @@ -941,38 +950,26 @@ let ``Boruvka MST complex line.`` () = 10UL, [ 0UL, 1UL, 1UL 1UL, 0UL, 1UL - 1UL, 2UL, 2UL 2UL, 1UL, 2UL - 2UL, 3UL, 1UL 3UL, 2UL, 1UL - 3UL, 4UL, 3UL 4UL, 3UL, 3UL - 4UL, 5UL, 1UL 5UL, 4UL, 1UL - 5UL, 6UL, 1UL 6UL, 5UL, 1UL - 6UL, 7UL, 2UL 7UL, 6UL, 2UL - 7UL, 8UL, 1UL 8UL, 7UL, 1UL - 8UL, 9UL, 1UL - 9UL, 8UL, 1UL - - - ] + 9UL, 8UL, 1UL ] ) Matrix.fromCoordinateList clist - let expected = let clist = Matrix.CoordinateList( @@ -980,39 +977,31 @@ let ``Boruvka MST complex line.`` () = 10UL, [ 0UL, 1UL, 1UL 1UL, 0UL, 1UL - 1UL, 2UL, 2UL 2UL, 1UL, 2UL - 2UL, 3UL, 1UL 3UL, 2UL, 1UL - 3UL, 4UL, 3UL 4UL, 3UL, 3UL - 4UL, 5UL, 1UL 5UL, 4UL, 1UL - 5UL, 6UL, 1UL 6UL, 5UL, 1UL - 6UL, 7UL, 2UL 7UL, 6UL, 2UL - 7UL, 8UL, 1UL 8UL, 7UL, 1UL - 8UL, 9UL, 1UL - 9UL, 8UL, 1UL - - - ] + 9UL, 8UL, 1UL ] ) - Matrix.fromCoordinateList clist |> Ok - - checkResult (Graph.Boruvka.mst graph) expected + Matrix.fromCoordinateList clist + match graph, expected with + | Ok g, Ok e -> + let actual = Graph.Boruvka.mst g + assertBoruvkaEqualsExpected actual e + | _ -> Assert.Fail("Setup failed") [] let ``Boruvka MST complex line 2.`` () = @@ -1024,38 +1013,26 @@ let ``Boruvka MST complex line 2.`` () = 10UL, [ 0UL, 1UL, 1UL 1UL, 0UL, 1UL - 1UL, 2UL, 2UL 2UL, 1UL, 2UL - 2UL, 3UL, 1UL 3UL, 2UL, 1UL - 3UL, 9UL, 3UL 9UL, 3UL, 3UL - 9UL, 8UL, 1UL 8UL, 9UL, 1UL - 8UL, 7UL, 1UL 7UL, 8UL, 1UL - 6UL, 7UL, 2UL 7UL, 6UL, 2UL - 5UL, 6UL, 1UL 6UL, 5UL, 1UL - 5UL, 4UL, 1UL - 4UL, 5UL, 1UL - - - ] + 4UL, 5UL, 1UL ] ) Matrix.fromCoordinateList clist - let expected = let clist = Matrix.CoordinateList( @@ -1063,34 +1040,28 @@ let ``Boruvka MST complex line 2.`` () = 10UL, [ 0UL, 1UL, 1UL 1UL, 0UL, 1UL - 1UL, 2UL, 2UL 2UL, 1UL, 2UL - 2UL, 3UL, 1UL 3UL, 2UL, 1UL - 3UL, 9UL, 3UL 9UL, 3UL, 3UL - 9UL, 8UL, 1UL 8UL, 9UL, 1UL - 8UL, 7UL, 1UL 7UL, 8UL, 1UL - 6UL, 7UL, 2UL 7UL, 6UL, 2UL - 5UL, 6UL, 1UL 6UL, 5UL, 1UL - 5UL, 4UL, 1UL - 4UL, 5UL, 1UL - - ] + 4UL, 5UL, 1UL ] ) - Matrix.fromCoordinateList clist |> Ok + Matrix.fromCoordinateList clist - checkResult (Graph.Boruvka.mst graph) expected + match graph, expected with + | Ok g, Ok e -> + let actual = Graph.Boruvka.mst g + assertBoruvkaEqualsExpected actual e + | _ -> Assert.Fail("Setup failed") diff --git a/QuadTree.Tests/Tests.LinearAlgebra.fs b/QuadTree.Tests/Tests.LinearAlgebra.fs index 3bde7b3..f22206b 100644 --- a/QuadTree.Tests/Tests.LinearAlgebra.fs +++ b/QuadTree.Tests/Tests.LinearAlgebra.fs @@ -345,7 +345,6 @@ let ``Simple vxmi_values. 4 * (4x3).`` () = Assert.Equal(expected, actual) - [] let ``Simple mxm`` () = // 222D @@ -376,16 +375,13 @@ let ``Simple mxm`` () = let expected = SparseMatrix(3UL, 3UL, 9UL, Matrix.Storage(4UL, tree_expected)) - let actual = - match LinearAlgebra.mxm op_add op_mult m1 m2 with - | Ok m -> m - | _ -> failwith "Unreachable" - - Assert.Equal(expected.storage.data, actual.storage.data) + match LinearAlgebra.mxm op_add op_mult m1 m2 with + | Ok actual -> Assert.Equal(expected.storage.data, actual.storage.data) + | Error _ -> Assert.Fail() [] let ``Sparse mxm`` () = - let m1 = + let m1Result = let d = [ 0UL, 0UL, 1 1UL, 1UL, 2 @@ -394,7 +390,7 @@ let ``Sparse mxm`` () = let clist = Matrix.CoordinateList(3UL, 3UL, d) Matrix.fromCoordinateList clist - let m2 = + let m2Result = let d = [ 0UL, 0UL, 3 1UL, 1UL, 2 @@ -403,7 +399,7 @@ let ``Sparse mxm`` () = let clist = Matrix.CoordinateList(3UL, 3UL, d) Matrix.fromCoordinateList clist - let expected = + let expectedResult = let d = [ 0UL, 0UL, 3 1UL, 1UL, 4 @@ -412,19 +408,21 @@ let ``Sparse mxm`` () = let clist = Matrix.CoordinateList(3UL, 3UL, d) Matrix.fromCoordinateList clist - let actual = + match m1Result, m2Result, expectedResult with + | Ok m1, Ok m2, Ok expected -> match LinearAlgebra.mxm op_add op_mult m1 m2 with - | Ok m -> m - | Error e -> failwith (e.ToString()) - - Assert.Equal(expected, actual) + | Ok actual -> Assert.Equal(Matrix.toCoordinateList expected, Matrix.toCoordinateList actual) + | Error e -> Assert.Fail() + | Error e, _, _ + | _, Error e, _ + | _, _, Error e -> Assert.Fail() [] let ``Shrinking mxm`` () = // 2 x 3 // 1 0 2 // 0 3 0 - let m1 = + let m1Result = let d = [ 0UL, 0UL, 1 0UL, 2UL, 2 @@ -437,7 +435,7 @@ let ``Shrinking mxm`` () = // 0 4 // 5 0 // 6 0 - let m2 = + let m2Result = let d = [ 0UL, 1UL, 4 1UL, 0UL, 5 @@ -449,7 +447,7 @@ let ``Shrinking mxm`` () = // 2 x 2 // 12 4 // 15 0 - let expected = + let expectedResult = let d = [ 0UL, 0UL, 12 0UL, 1UL, 4 @@ -458,12 +456,21 @@ let ``Shrinking mxm`` () = let clist = Matrix.CoordinateList(2UL, 2UL, d) Matrix.fromCoordinateList clist - let actual = + match m1Result, m2Result, expectedResult with + | Ok m1, Ok m2, Ok expected -> match LinearAlgebra.mxm op_add op_mult m1 m2 with - | Ok m -> m - | Error e -> failwith (e.ToString()) + | Ok actual -> + let expectedList = Matrix.toCoordinateList expected + let actualList = Matrix.toCoordinateList actual - Assert.Equal(expected, actual) + Assert.Equal<(uint64 * uint64 * int) list>( + expectedList.list |> List.sortBy (fun (r, c, _) -> (r, c)), + actualList.list |> List.sortBy (fun (r, c, _) -> (r, c)) + ) + | Error e -> Assert.Fail() + | Error e, _, _ -> Assert.Fail() + | _, Error e, _ -> Assert.Fail() + | _, _, Error e -> Assert.Fail() [] let ``Expanding mxm`` () = @@ -471,7 +478,7 @@ let ``Expanding mxm`` () = // 1 0 // 0 2 // 3 0 - let m1 = + let m1Result = let d = [ 0UL, 0UL, 1 1UL, 1UL, 2 @@ -479,10 +486,11 @@ let ``Expanding mxm`` () = let clist = Matrix.CoordinateList(3UL, 2UL, d) Matrix.fromCoordinateList clist + // 2 x 3 // 4 5 6 // 0 0 0 - let m2 = + let m2Result = let d = [ 0UL, 0UL, 4 0UL, 1UL, 5 @@ -495,7 +503,7 @@ let ``Expanding mxm`` () = // 4 5 6 // 0 0 0 // 12 15 18 - let expected = + let expectedResult = let d = [ 0UL, 0UL, 4 0UL, 1UL, 5 @@ -507,9 +515,18 @@ let ``Expanding mxm`` () = let clist = Matrix.CoordinateList(3UL, 3UL, d) Matrix.fromCoordinateList clist - let actual = + match m1Result, m2Result, expectedResult with + | Ok m1, Ok m2, Ok expected -> match LinearAlgebra.mxm op_add op_mult m1 m2 with - | Ok m -> m - | Error e -> failwith (e.ToString()) + | Ok actual -> + let sortList (coo: Matrix.CoordinateList) = + coo.list |> List.sortBy (fun (r, c, _) -> (r, c)) - Assert.Equal(expected, actual) + Assert.Equal<(uint64 * uint64 * int) list>( + sortList (Matrix.toCoordinateList expected), + sortList (Matrix.toCoordinateList actual) + ) + | Error e -> Assert.Fail() + | Error e, _, _ -> Assert.Fail() + | _, Error e, _ -> Assert.Fail() + | _, _, Error e -> Assert.Fail() diff --git a/QuadTree.Tests/Tests.Matrix.fs b/QuadTree.Tests/Tests.Matrix.fs index 74e4c42..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) ] ) - ) + |> Matrix.fromCoordinateList - 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 - - 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 nrows = 3UL + let ncols = 3UL - 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 - - 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 ( + 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, - [ (0UL, 0UL, 1); (2UL, 2UL, 3) ] + nrows, + ncols, + [ (0UL, 0UL, 2) + (1UL, 1UL, 31) + (2UL, 2UL, 8) + (3UL, 3UL, 93) ] ) - ) + |> Matrix.fromCoordinateList - let m2 = - Matrix.fromCoordinateList ( - Matrix.CoordinateList( - 4UL, - 4UL, - [ (1UL, 1UL, 10); (3UL, 3UL, 30) ] - ) - ) - - 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 - - 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 + 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 - Assert.Equal(4UL, actual.nvals) + 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 + 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 - Assert.Equal(6UL, actual.nvals) + 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 + 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 - Assert.Equal(4UL, actual.nvals) + 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 coordinates = Matrix.CoordinateList(nrows, ncols, data) - let expected = coordinates - let actual = id coordinates - - 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 ()) + + let expected = + SparseMatrix(2UL, 3UL, 0UL, Storage(4UL, tree)) - Assert.Equal(expected.storage.data, actual.storage.data) + 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 @@ -644,11 +616,30 @@ let ``Fold sum`` () = Assert.Equal(expected, actual) [] -let ``fromCoordinateList with out-of-range coordinates throws exception`` () = +let ``fromCoordinateList with out-of-range coordinates returns Error`` () = let coo = CoordinateList(6UL, 6UL, [ (9UL, 9UL, 13) ]) - Assert.Throws(fun () -> fromCoordinateList coo |> ignore) + 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`` () = @@ -661,19 +652,21 @@ let ``fromCoordinateList with unsorted coordinates works correctly`` () = (1UL, 1UL, 100) ] ) - let matrix = fromCoordinateList coo - let result = toCoordinateList matrix + 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.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`` () = @@ -684,642 +677,663 @@ let ``fromCoordinateList with duplicate indices returns the last of them`` () = [ (1UL, 1UL, 33); (1UL, 1UL, 100) ] ) - let matrix = fromCoordinateList coo - let result = toCoordinateList matrix - Assert.Equal(CoordinateList(3UL, 3UL, [ (1UL, 1UL, 100) ]), result) + 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 ``fromCoordinateList with zero size throws Exception`` () = - let coo = +let ``map works on square matrix`` () = + let clist = CoordinateList( - 0UL, - 0UL, - [ (33UL, 33UL, 33); (39UL, 39UL, 1) ] + 4UL, + 4UL, + [ (1UL, 1UL, 22); (2UL, 3UL, 37) ] ) - Assert.Throws(fun () -> fromCoordinateList coo |> ignore) + 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 ``map works on square matrix`` () = - let matrix = - fromCoordinateList ( + let coo = Matrix.toCoordinateList result + + Assert.Equal( CoordinateList( 4UL, 4UL, - [ (1UL, 1UL, 22); (2UL, 3UL, 37) ] - ) + [ (1UL, 1UL, 31); (2UL, 3UL, 46) ] + ), + coo ) + | _ -> Assert.Fail() - let result = - map matrix (fun (x: int option) -> - match x with - | Some(v) -> Some(v + 9) - | None -> None) +[] +let ``map works on rectangular matrix`` () = + let clist = + CoordinateList( + 5UL, + 6UL, + [ (1UL, 1UL, 21); (4UL, 3UL, 36) ] + ) - let coo = toCoordinateList result + 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) - Assert.Equal( - CoordinateList( - 4UL, - 4UL, - [ (1UL, 1UL, 31); (2UL, 3UL, 46) ] - ), - coo - ) + let coo = Matrix.toCoordinateList result -[] -let ``map works on rectangular matrix`` () = - let matrix = - fromCoordinateList ( + Assert.Equal( CoordinateList( 5UL, 6UL, - [ (1UL, 1UL, 21); (4UL, 3UL, 36) ] - ) + [ (1UL, 1UL, 7); (4UL, 3UL, 12) ] + ), + coo ) - - let result = - map matrix (fun (x: int option) -> - match x with - | Some(v) -> Some(v / 3) - | None -> None) - - let coo = toCoordinateList result - - Assert.Equal( - CoordinateList( - 5UL, - 6UL, - [ (1UL, 1UL, 7); (4UL, 3UL, 12) ] - ), - coo - ) + | _ -> Assert.Fail() [] let ``map on empty matrix returns empty matrix`` () = - let matrix = fromCoordinateList (CoordinateList(0UL, 0UL, [])) - - let result = - map matrix (fun (x: int option) -> - match x with - | Some(v) -> Some(v * 5) - | None -> None) + 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 = toCoordinateList result - Assert.Equal(CoordinateList(0UL, 0UL, []), coo) + 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 matrix = - fromCoordinateList ( - CoordinateList( - 5UL, - 7UL, - [ (1UL, 1UL, 5) - (4UL, 1UL, 17) - (4UL, 6UL, 33) ] - ) + let clist = + CoordinateList( + 5UL, + 7UL, + [ (1UL, 1UL, 5) + (4UL, 1UL, 17) + (4UL, 6UL, 33) ] ) - let result = map matrix (fun _ -> Some(0)) - let coo = toCoordinateList result + 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 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) + let expected = CoordinateList(5UL, 7UL, expectedData) + Assert.Equal(expected, coo) + | _ -> Assert.Fail() [] let ``map with function that turns all the elements to None`` () = - let matrix = - fromCoordinateList ( - CoordinateList( - 5UL, - 7UL, - [ (1UL, 1UL, 5) - (4UL, 1UL, 17) - (4UL, 6UL, 33) ] - ) + let clist = + CoordinateList( + 5UL, + 7UL, + [ (1UL, 1UL, 5) + (4UL, 1UL, 17) + (4UL, 6UL, 33) ] ) - let result = map matrix (fun _ -> None) - let coo = toCoordinateList result - Assert.Equal(CoordinateList(5UL, 7UL, []), coo) + 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 matrix = - fromCoordinateList ( - CoordinateList( - 3UL, - 5UL, - [ (2UL, 1UL, 17); (2UL, 4UL, 33) ] - ) + let clist = + CoordinateList( + 3UL, + 5UL, + [ (2UL, 1UL, 17); (2UL, 4UL, 33) ] ) - let result = - map matrix (fun (x: int option) -> - match x with - | Some v -> Some(sprintf "str %d" v) - | None -> None) + 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 coo = toCoordinateList result + let actual = Matrix.toCoordinateList result - Assert.Equal( - CoordinateList( - 3UL, - 5UL, - [ (2UL, 1UL, "str 17") - (2UL, 4UL, "str 33") ] - ), - coo - ) + 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 coo = + let clist = CoordinateList( 4UL, 4UL, [ (1UL, 1UL, 12); (2UL, 3UL, 13) ] ) - let matrix = fromCoordinateList coo - - let result = - mapi matrix (fun (i: uint64) (j: uint64) (x: int option) -> - match x with - | Some v -> Some(v * int (i) + int (j)) - | None -> None) - - let actual = toCoordinateList result + 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) ] - ), - actual - ) + 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 coo = + let clist = CoordinateList( 3UL, 5UL, [ (1UL, 2UL, 15); (2UL, 2UL, 13) ] ) - let matrix = fromCoordinateList coo - - let result = - mapi matrix (fun (i: uint64) (j: uint64) (x: int option) -> - match x with - | Some v -> Some(v * int (j) + int (i)) - | None -> None) - - let actual = toCoordinateList result + 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) ] - ), - actual - ) + Assert.Equal( + CoordinateList( + 3UL, + 5UL, + [ (1UL, 2UL, 31); (2UL, 2UL, 28) ] + ), + Matrix.toCoordinateList result + ) + | _ -> Assert.Fail() [] let ``mapi on empty matrix returns empty matrix`` () = - let matrix = fromCoordinateList (CoordinateList(0UL, 0UL, [])) - - let result = - mapi matrix (fun (i: uint64) (j: uint64) (x: int option) -> - match x with - | Some v -> Some((v + int (1)) * 3 + 2 * int (j)) - | None -> None) + 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) - let actual = toCoordinateList result - Assert.Equal(CoordinateList(0UL, 0UL, []), actual) + Assert.Equal(CoordinateList(0UL, 0UL, []), Matrix.toCoordinateList result) + | _ -> Assert.Fail() [] let ``mapi with special function returns empty matrix`` () = - let coo = + let clist = CoordinateList( 5UL, 7UL, [ (1UL, 2UL, 15); (4UL, 5UL, 13) ] ) - let matrix = fromCoordinateList coo - - let result = - mapi matrix (fun (i: uint64) (j: uint64) (x: int option) -> - match x with - | Some v -> - match (int (i) + int (j)) % 2 with - | 0 -> Some(v * 2) - | _ -> None - | None -> None) + 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) - let actual = toCoordinateList result - Assert.Equal(CoordinateList(5UL, 7UL, []), actual) + Assert.Equal(CoordinateList(5UL, 7UL, []), Matrix.toCoordinateList result) + | _ -> Assert.Fail() [] let ``mapi works with function does not depend on the indexes`` () = - let coo = + let clist = CoordinateList( 5UL, 7UL, [ (1UL, 2UL, 15); (4UL, 5UL, 13) ] ) - let matrix = fromCoordinateList coo - - let result = - mapi matrix (fun (i: uint64) (j: uint64) (x: int option) -> - match x with - | Some v -> Some(v * 2) - | None -> None) - - let actual = toCoordinateList result + 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) ] - ), - actual - ) + 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 matrix = - fromCoordinateList ( - CoordinateList( - 5UL, - 6UL, - [ (2UL, 2UL, 3); (3UL, 4UL, 17) ] - ) + let clist = + CoordinateList( + 5UL, + 6UL, + [ (2UL, 2UL, 3); (3UL, 4UL, 17) ] ) - match slice matrix -1 4 2 3 with - | Result.Ok _ -> Assert.Fail("Expected Error") - | Result.Error msg -> Assert.Equal("Start row should be >= 0", msg) + 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 matrix = - fromCoordinateList ( - CoordinateList( - 5UL, - 6UL, - [ (2UL, 2UL, 3); (3UL, 4UL, 17) ] - ) + let clist = + CoordinateList( + 5UL, + 6UL, + [ (2UL, 2UL, 3); (3UL, 4UL, 17) ] ) - match slice matrix 1 -4 2 3 with - | Result.Ok _ -> Assert.Fail("Expected Error") - | Result.Error msg -> Assert.Equal("End row should be >= 0", msg) + 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 matrix = - fromCoordinateList ( - CoordinateList( - 5UL, - 6UL, - [ (2UL, 2UL, 3); (3UL, 4UL, 17) ] - ) + let clist = + CoordinateList( + 5UL, + 6UL, + [ (2UL, 2UL, 3); (3UL, 4UL, 17) ] ) - match slice matrix 1 4 -2 3 with - | Result.Ok _ -> Assert.Fail("Expected Error") - | Result.Error msg -> Assert.Equal("Start column should be >= 0", msg) + 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 matrix = - fromCoordinateList ( - CoordinateList( - 5UL, - 6UL, - [ (2UL, 2UL, 3); (3UL, 4UL, 17) ] - ) + let clist = + CoordinateList( + 5UL, + 6UL, + [ (2UL, 2UL, 3); (3UL, 4UL, 17) ] ) - match slice matrix 1 4 2 -3 with - | Result.Ok _ -> Assert.Fail("Expected Error") - | Result.Error msg -> Assert.Equal("End column should be >= 0", msg) + 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 matrix = - fromCoordinateList ( - CoordinateList( - 5UL, - 6UL, - [ (2UL, 2UL, 3); (3UL, 4UL, 17) ] - ) + let clist = + CoordinateList( + 5UL, + 6UL, + [ (2UL, 2UL, 3); (3UL, 4UL, 17) ] ) - match slice matrix 6 4 2 3 with - | Result.Ok _ -> Assert.Fail("Expected Error") - | Result.Error msg -> Assert.Equal("Start row is out of matrix length", msg) + 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 matrix = - fromCoordinateList ( - CoordinateList( - 5UL, - 6UL, - [ (2UL, 2UL, 3); (3UL, 4UL, 17) ] - ) + let clist = + CoordinateList( + 5UL, + 6UL, + [ (2UL, 2UL, 3); (3UL, 4UL, 17) ] ) - match slice matrix 1 10 2 3 with - | Result.Ok _ -> Assert.Fail("Expected Error") - | Result.Error msg -> Assert.Equal("End row is out of matrix length", msg) + 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 matrix = - fromCoordinateList ( - CoordinateList( - 5UL, - 6UL, - [ (2UL, 2UL, 3); (3UL, 4UL, 17) ] - ) + let clist = + CoordinateList( + 5UL, + 6UL, + [ (2UL, 2UL, 3); (3UL, 4UL, 17) ] ) - match slice matrix 1 4 10 3 with - | Result.Ok _ -> Assert.Fail("Expected Error") - | Result.Error msg -> Assert.Equal("Start column is out of matrix length", msg) + 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 matrix = - fromCoordinateList ( - CoordinateList( - 5UL, - 6UL, - [ (2UL, 2UL, 3); (3UL, 4UL, 17) ] - ) + let clist = + CoordinateList( + 5UL, + 6UL, + [ (2UL, 2UL, 3); (3UL, 4UL, 17) ] ) - match slice matrix 1 2 2 10 with - | Result.Ok _ -> Assert.Fail("Expected Error") - | Result.Error msg -> Assert.Equal("End column is out of matrix length", msg) + 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 matrix = - fromCoordinateList ( - CoordinateList( - 5UL, - 6UL, - [ (2UL, 2UL, 3); (3UL, 4UL, 17) ] - ) + let clist = + CoordinateList( + 5UL, + 6UL, + [ (2UL, 2UL, 3); (3UL, 4UL, 17) ] ) - match slice matrix 2 1 2 3 with - | Result.Ok _ -> Assert.Fail("Expected Error") - | Result.Error msg -> Assert.Equal("Start row should be <= end row", msg) + 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 matrix = - fromCoordinateList ( - CoordinateList( - 5UL, - 6UL, - [ (2UL, 2UL, 3); (3UL, 4UL, 17) ] - ) + let clist = + CoordinateList( + 5UL, + 6UL, + [ (2UL, 2UL, 3); (3UL, 4UL, 17) ] ) - match slice matrix 1 2 3 2 with - | Result.Ok _ -> Assert.Fail("Expected Error") - | Result.Error msg -> Assert.Equal("Start column should be <= end column", msg) + 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 matrix = - fromCoordinateList ( - CoordinateList( - 7UL, - 7UL, - [ (2UL, 2UL, 33); (5UL, 5UL, 28) ] - ) + let clist = + CoordinateList( + 7UL, + 7UL, + [ (2UL, 2UL, 33); (5UL, 5UL, 28) ] ) - match slice matrix 1 3 1 3 with - | Result.Ok result -> - let coo = toCoordinateList result - Assert.Equal(CoordinateList(3UL, 3UL, [ (1UL, 1UL, 33) ]), coo) - | Result.Error msg -> Assert.Fail(msg) + 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 matrix = - fromCoordinateList ( - CoordinateList( - 5UL, - 9UL, - [ (3UL, 7UL, 33); (1UL, 2UL, 28) ] - ) + let clist = + CoordinateList( + 5UL, + 9UL, + [ (3UL, 7UL, 33); (1UL, 2UL, 28) ] ) - match slice matrix 2 4 5 8 with - | Result.Ok result -> - let coo = toCoordinateList result - Assert.Equal(CoordinateList(3UL, 4UL, [ (1UL, 2UL, 33) ]), coo) - | Result.Error msg -> Assert.Fail(msg) + 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 matrix = - fromCoordinateList ( - CoordinateList( - 7UL, - 9UL, - [ (3UL, 3UL, 33); (1UL, 2UL, 28) ] - ) + let clist = + CoordinateList( + 7UL, + 9UL, + [ (3UL, 3UL, 33); (1UL, 2UL, 28) ] ) - match slice matrix 4 6 5 8 with - | Result.Ok result -> - let coo = toCoordinateList result - Assert.Equal(CoordinateList(3UL, 4UL, []), coo) - | Result.Error msg -> Assert.Fail(msg) + 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 matrix = - fromCoordinateList ( - CoordinateList( - 7UL, - 9UL, - [ (3UL, 3UL, 33); (1UL, 2UL, 28) ] - ) + let clist = + CoordinateList( + 7UL, + 9UL, + [ (3UL, 3UL, 33); (1UL, 2UL, 28) ] ) - match slice matrix 1 1 2 2 with - | Result.Ok result -> - let coo = toCoordinateList result - Assert.Equal(CoordinateList(1UL, 1UL, [ (0UL, 0UL, 28) ]), coo) - | Result.Error msg -> Assert.Fail(msg) + 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 matrix = - fromCoordinateList ( - CoordinateList( - 7UL, - 9UL, - [ (3UL, 3UL, 33); (1UL, 2UL, 28) ] + 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 ) - ) - - match slice matrix 0 6 0 8 with - | Result.Ok result -> - let coo = toCoordinateList result - - Assert.Equal( - CoordinateList( - 7UL, - 9UL, - [ (1UL, 2UL, 28); (3UL, 3UL, 33) ] - ), - coo - ) - | Result.Error msg -> Assert.Fail(msg) + | Result.Error msg -> Assert.Fail() + | _ -> Assert.Fail() [] let ``slice returns correct submatrix when row start of submatrix equals to row start of matrix`` () = - let matrix = - fromCoordinateList ( - CoordinateList( - 7UL, - 7UL, - [ (0UL, 0UL, 10) - (3UL, 3UL, 33) - (6UL, 6UL, 6) ] + 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 ) - ) - - match slice matrix 0 4 0 4 with - | Result.Ok result -> - let coo = toCoordinateList result - - Assert.Equal( - CoordinateList( - 5UL, - 5UL, - [ (0UL, 0UL, 10); (3UL, 3UL, 33) ] - ), - coo - ) - | Result.Error msg -> Assert.Fail(msg) + | Result.Error msg -> Assert.Fail() + | _ -> Assert.Fail() [] let ``slice returns correct submatrix when row end of submatrix equals to row end of matrix`` () = - let matrix = - fromCoordinateList ( - CoordinateList( - 7UL, - 7UL, - [ (0UL, 0UL, 10) - (3UL, 3UL, 33) - (6UL, 6UL, 6) ] - ) + let clist = + CoordinateList( + 7UL, + 7UL, + [ (0UL, 0UL, 10) + (3UL, 3UL, 33) + (6UL, 6UL, 6) ] ) - match slice matrix 2 6 2 4 with - | Result.Ok result -> - let coo = toCoordinateList result - Assert.Equal(CoordinateList(5UL, 3UL, [ (1UL, 1UL, 33) ]), coo) - | Result.Error msg -> Assert.Fail(msg) - -[] -let ``slice returns correct submatrix when col start of submatrix equals to col start of matrix`` () = - let matrix = - fromCoordinateList ( - 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 slice matrix 2 5 0 6 with - | Result.Ok result -> - let coo = toCoordinateList result - Assert.Equal(CoordinateList(4UL, 7UL, [ (1UL, 3UL, 33) ]), coo) - | Result.Error msg -> Assert.Fail(msg) + 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 matrix = - fromCoordinateList ( - CoordinateList( - 7UL, - 7UL, - [ (0UL, 0UL, 10) - (3UL, 3UL, 33) - (6UL, 6UL, 6) ] - ) + let clist = + CoordinateList( + 7UL, + 7UL, + [ (0UL, 0UL, 10) + (3UL, 3UL, 33) + (6UL, 6UL, 6) ] ) - match slice matrix 2 4 2 6 with - | Result.Ok result -> - let coo = toCoordinateList result - Assert.Equal(CoordinateList(3UL, 5UL, [ (1UL, 1UL, 33) ]), coo) - | Result.Error msg -> Assert.Fail(msg) + 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 matrix = - fromCoordinateList ( - CoordinateList( - 10UL, - 10UL, - [ (1UL, 3UL, 1) - (2UL, 2UL, 2) - (5UL, 7UL, 3) ] + 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 ) - ) - - match slice matrix 1 6 2 2 with - | Result.Ok result -> - let coo = toCoordinateList result - Assert.Equal(CoordinateList(6UL, 1UL, [ (1UL, 0UL, 2) ]), coo) + | Result.Error msg -> Assert.Fail(msg) | Result.Error msg -> Assert.Fail(msg) [] let ``slice returns single row`` () = - let matrix = - fromCoordinateList ( - CoordinateList( - 10UL, - 10UL, - [ (1UL, 3UL, 1) - (2UL, 2UL, 2) - (5UL, 7UL, 3) ] + 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 ) - ) - - match slice matrix 5 5 3 9 with - | Result.Ok result -> - let coo = toCoordinateList result - Assert.Equal(CoordinateList(1UL, 7UL, [ (0UL, 4UL, 3) ]), coo) + | Result.Error msg -> Assert.Fail(msg) | Result.Error msg -> Assert.Fail(msg) [] @@ -1334,22 +1348,24 @@ let ``let reduceRows sum on square power of two matrix`` () = (1UL, 1UL, 3) ] ) - let matrix = fromCoordinateList coo + 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 add x y = - match x, y with - | None, None -> None - | None, Some(b) -> Some(b) - | Some(a), None -> Some(a) - | Some(a), Some(b) -> Some(a + b) + let result = Matrix.reduceRows add matrix - let result = reduceRows add matrix - let vectorCoordinates = Vector.toCoordinateList result + let vectorCoordinates = Vector.toCoordinateList result - let expected = - Vector.CoordinateList(2UL, [ (0UL, 27); (1UL, 20) ]) + let expected = + Vector.CoordinateList(2UL, [ (0UL, 27); (1UL, 20) ]) - Assert.Equal(expected, vectorCoordinates) + Assert.Equal(expected, vectorCoordinates) [] let ``let reduceRows sum on square power of two matrix with empty row`` () = @@ -1360,22 +1376,22 @@ let ``let reduceRows sum on square power of two matrix with empty row`` () = [ (1UL, 0UL, 17); (1UL, 1UL, 3) ] ) - let matrix = fromCoordinateList coo - - let add x y = - match x, y with - | None, None -> None - | None, Some(b) -> Some(b) - | Some(a), None -> Some(a) - | Some(a), Some(b) -> Some(a + b) - - let result = reduceRows add matrix - let vectorCoordinates = Vector.toCoordinateList result + 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 expected = - Vector.CoordinateList(2UL, [ (1UL, 20) ]) + let res = Matrix.reduceRows add m - Assert.Equal(expected, vectorCoordinates) + 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`` () = @@ -1392,25 +1408,25 @@ let ``let reduceRows sum on square not power of two matrix`` () = (2UL, 1UL, 17) ] ) - let matrix = fromCoordinateList coo - - let add x y = - match x, y with - | None, None -> None - | None, Some(b) -> Some(b) - | Some(a), None -> Some(a) - | Some(a), Some(b) -> Some(a + b) + 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 result = reduceRows add matrix - let vectorCoordinates = Vector.toCoordinateList result + let res = Matrix.reduceRows add m - let expected = - Vector.CoordinateList( - 3UL, - [ (0UL, 21); (1UL, 24); (2UL, 32) ] + Assert.Equal( + Vector.CoordinateList( + 3UL, + [ (0UL, 21); (1UL, 24); (2UL, 32) ] + ), + Vector.toCoordinateList res ) - - Assert.Equal(expected, vectorCoordinates) + | Result.Error msg -> Assert.Fail(msg) [] let ``let reduceRows mul on square not power of two matrix`` () = @@ -1427,25 +1443,25 @@ let ``let reduceRows mul on square not power of two matrix`` () = (2UL, 1UL, 17) ] ) - let matrix = fromCoordinateList coo + 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 mul x y = - match x, y with - | None, None -> None - | None, Some(b) -> Some(b) - | Some(a), None -> Some(a) - | Some(a), Some(b) -> Some(a * b) + let res = Matrix.reduceRows mul m - let result = reduceRows mul matrix - let vectorCoordinates = Vector.toCoordinateList result - - let expected = - Vector.CoordinateList( - 3UL, - [ (0UL, 315); (1UL, 143); (2UL, 255) ] + Assert.Equal( + Vector.CoordinateList( + 3UL, + [ (0UL, 315); (1UL, 143); (2UL, 255) ] + ), + Vector.toCoordinateList res ) - - Assert.Equal(expected, vectorCoordinates) + | Result.Error msg -> Assert.Fail(msg) [] let ``let reduceRows sum on rectangular matrix`` () = @@ -1460,62 +1476,58 @@ let ``let reduceRows sum on rectangular matrix`` () = (1UL, 2UL, 13) ] ) - let matrix = fromCoordinateList coo - - let sum x y = - match x, y with - | None, None -> None - | None, Some(b) -> Some(b) - | Some(a), None -> Some(a) - | Some(a), Some(b) -> Some(a + b) - - let result = reduceRows sum matrix - let vectorCoordinates = Vector.toCoordinateList result + 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 expected = - Vector.CoordinateList(2UL, [ (0UL, 21); (1UL, 24) ]) + let res = Matrix.reduceRows sum m - Assert.Equal(expected, vectorCoordinates) + 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`` () = - let coo = CoordinateList(2UL, 3UL, []) - let matrix = fromCoordinateList coo - - let sum x y = - match x, y with - | None, None -> None - | None, Some(b) -> Some(b) - | Some(a), None -> Some(a) - | Some(a), Some(b) -> Some(a + b) - - let result = reduceRows sum matrix - let vectorCoordinates = Vector.toCoordinateList result - let expected = Vector.CoordinateList(2UL, []) - Assert.Equal(expected, vectorCoordinates) + 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`` () = - let coo = - CoordinateList(1UL, 1UL, [ (0UL, 0UL, 33) ]) - - let matrix = fromCoordinateList coo - - let mul x y = - match x, y with - | None, None -> None - | None, Some(b) -> Some(b) - | Some(a), None -> Some(a) - | Some(a), Some(b) -> Some(a * b) - - let result = reduceRows mul matrix - let vectorCoordinates = Vector.toCoordinateList result - - let expected = - Vector.CoordinateList(1UL, [ (0UL, 33) ]) - - Assert.Equal(expected, vectorCoordinates) + 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`` () = @@ -1529,22 +1541,22 @@ let ``let reduceCols sum on square power of two matrix`` () = (1UL, 1UL, 3) ] ) - let matrix = fromCoordinateList coo - - let add x y = - match x, y with - | None, None -> None - | None, Some(b) -> Some(b) - | Some(a), None -> Some(a) - | Some(a), Some(b) -> Some(a + b) - - let result = reduceCols add matrix - let vectorCoordinates = Vector.toCoordinateList result + 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 expected = - Vector.CoordinateList(2UL, [ (0UL, 29); (1UL, 18) ]) + let res = Matrix.reduceCols add m - Assert.Equal(expected, vectorCoordinates) + 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`` () = @@ -1555,22 +1567,22 @@ let ``let reduceCols sum on square power of two matrix with empty col`` () = [ (0UL, 0UL, 17); (1UL, 0UL, 3) ] ) - let matrix = fromCoordinateList coo - - let add x y = - match x, y with - | None, None -> None - | None, Some(b) -> Some(b) - | Some(a), None -> Some(a) - | Some(a), Some(b) -> Some(a + b) - - let result = reduceCols add matrix - let vectorCoordinates = Vector.toCoordinateList result + 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 expected = - Vector.CoordinateList(2UL, [ (0UL, 20) ]) + let res = Matrix.reduceCols add m - Assert.Equal(expected, vectorCoordinates) + 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`` () = @@ -1587,25 +1599,25 @@ let ``let reduceCols sum on square not power of two matrix`` () = (2UL, 1UL, 17) ] ) - let matrix = fromCoordinateList coo + 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 add x y = - match x, y with - | None, None -> None - | None, Some(b) -> Some(b) - | Some(a), None -> Some(a) - | Some(a), Some(b) -> Some(a + b) - - let result = reduceCols add matrix - let vectorCoordinates = Vector.toCoordinateList result + let res = Matrix.reduceCols add m - let expected = - Vector.CoordinateList( - 3UL, - [ (0UL, 20); (1UL, 35); (2UL, 22) ] + Assert.Equal( + Vector.CoordinateList( + 3UL, + [ (0UL, 20); (1UL, 35); (2UL, 22) ] + ), + Vector.toCoordinateList res ) - - Assert.Equal(expected, vectorCoordinates) + | Result.Error msg -> Assert.Fail(msg) [] let ``let reduceCols mul on square not power of two matrix`` () = @@ -1622,25 +1634,25 @@ let ``let reduceCols mul on square not power of two matrix`` () = (2UL, 1UL, 17) ] ) - let matrix = fromCoordinateList coo - - let mul x y = - match x, y with - | None, None -> None - | None, Some(b) -> Some(b) - | Some(a), None -> Some(a) - | Some(a), Some(b) -> Some(a * b) + 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 result = reduceCols mul matrix - let vectorCoordinates = Vector.toCoordinateList result + let res = Matrix.reduceCols mul m - let expected = - Vector.CoordinateList( - 3UL, - [ (0UL, 75); (1UL, 1309); (2UL, 117) ] + Assert.Equal( + Vector.CoordinateList( + 3UL, + [ (0UL, 75); (1UL, 1309); (2UL, 117) ] + ), + Vector.toCoordinateList res ) - - Assert.Equal(expected, vectorCoordinates) + | Result.Error msg -> Assert.Fail(msg) [] let ``let reduceCols sum on rectangular matrix`` () = @@ -1655,67 +1667,64 @@ let ``let reduceCols sum on rectangular matrix`` () = (1UL, 2UL, 13) ] ) - let matrix = fromCoordinateList coo - - let sum x y = - match x, y with - | None, None -> None - | None, Some(b) -> Some(b) - | Some(a), None -> Some(a) - | Some(a), Some(b) -> Some(a + b) + 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 result = reduceCols sum matrix - let vectorCoordinates = Vector.toCoordinateList result + let res = Matrix.reduceCols sum m - let expected = - Vector.CoordinateList( - 3UL, - [ (0UL, 5); (1UL, 18); (2UL, 22) ] + Assert.Equal( + Vector.CoordinateList( + 3UL, + [ (0UL, 5); (1UL, 18); (2UL, 22) ] + ), + Vector.toCoordinateList res ) - - Assert.Equal(expected, vectorCoordinates) + | Result.Error msg -> Assert.Fail(msg) [] let ``let reduceCols sum on empty matrix`` () = - let coo = CoordinateList(2UL, 3UL, []) - let matrix = fromCoordinateList coo - - let sum x y = - match x, y with - | None, None -> None - | None, Some(b) -> Some(b) - | Some(a), None -> Some(a) - | Some(a), Some(b) -> Some(a + b) - - let result = reduceCols sum matrix - let vectorCoordinates = Vector.toCoordinateList result - let expected = Vector.CoordinateList(3UL, []) - Assert.Equal(expected, vectorCoordinates) + 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`` () = - let coo = - CoordinateList(1UL, 1UL, [ (0UL, 0UL, 33) ]) + 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 - let matrix = fromCoordinateList coo - - let mul x y = - match x, y with - | None, None -> None - | None, Some(b) -> Some(b) - | Some(a), None -> Some(a) - | Some(a), Some(b) -> Some(a * b) - - let result = reduceCols mul matrix - let vectorCoordinates = Vector.toCoordinateList result - - let expected = - Vector.CoordinateList(1UL, [ (0UL, 33) ]) - - Assert.Equal(expected, vectorCoordinates) + Assert.Equal( + Vector.CoordinateList(1UL, [ (0UL, 33) ]), + Vector.toCoordinateList res + ) + | Result.Error msg -> Assert.Fail(msg) [] -let ``kronecker product with square power of 2 x square power of two matrixes`` () = +let ``kronecker product with square power of two x square power of two matrixes`` () = let cooA = CoordinateList( 2UL, @@ -1726,8 +1735,6 @@ let ``kronecker product with square power of 2 x square power of two matrixes`` (1UL, 1UL, 4) ] ) - let A = fromCoordinateList cooA - let cooB = CoordinateList( 2UL, @@ -1738,41 +1745,39 @@ let ``kronecker product with square power of 2 x square power of two matrixes`` (1UL, 1UL, 8) ] ) - let B = fromCoordinateList cooB - - let multiplyOp a b = Some(a * b) - - match kroneckerProduct A B multiplyOp with - | Error msg -> Assert.True(false, msg) - | Ok result -> - let coo = toCoordinateList result - - 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) ] - ) + 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) ] + ) - Assert.Equal(expected, coo) + 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 2 x square not power of two matrixes`` () = +let ``kronecker product with square not power of two x square not power of two matrixes`` () = let cooA = CoordinateList( 3UL, @@ -1788,8 +1793,6 @@ let ``kronecker product with square not power of 2 x square not power of two mat (2UL, 2UL, 9) ] ) - let A = fromCoordinateList cooA - let cooB = CoordinateList( 3UL, @@ -1805,103 +1808,12 @@ let ``kronecker product with square not power of 2 x square not power of two mat (2UL, 2UL, 18) ] ) - let B = fromCoordinateList cooB - - let multiplyOp a b = Some(a * b) - - match kroneckerProduct A B multiplyOp with - | Error msg -> Assert.True(false, msg) - | Ok result -> - let coo = toCoordinateList result - - let expected = - CoordinateList( - 9UL, - 9UL, - [ (0UL, 0UL, 10) - (0UL, 1UL, 11) - (0UL, 2UL, 12) - (0UL, 3UL, 20) - (0UL, 4UL, 22) - (0UL, 5UL, 24) - (0UL, 6UL, 30) - (0UL, 7UL, 33) - (0UL, 8UL, 36) - (1UL, 0UL, 13) - (1UL, 1UL, 14) - (1UL, 2UL, 15) - (1UL, 3UL, 26) - (1UL, 4UL, 28) - (1UL, 5UL, 30) - (1UL, 6UL, 39) - (1UL, 7UL, 42) - (1UL, 8UL, 45) - (2UL, 0UL, 16) - (2UL, 1UL, 17) - (2UL, 2UL, 18) - (2UL, 3UL, 32) - (2UL, 4UL, 34) - (2UL, 5UL, 36) - (2UL, 6UL, 48) - (2UL, 7UL, 51) - (2UL, 8UL, 54) - (3UL, 0UL, 40) - (3UL, 1UL, 44) - (3UL, 2UL, 48) - (3UL, 3UL, 50) - (3UL, 4UL, 55) - (3UL, 5UL, 60) - (3UL, 6UL, 60) - (3UL, 7UL, 66) - (3UL, 8UL, 72) - (4UL, 0UL, 52) - (4UL, 1UL, 56) - (4UL, 2UL, 60) - (4UL, 3UL, 65) - (4UL, 4UL, 70) - (4UL, 5UL, 75) - (4UL, 6UL, 78) - (4UL, 7UL, 84) - (4UL, 8UL, 90) - (5UL, 0UL, 64) - (5UL, 1UL, 68) - (5UL, 2UL, 72) - (5UL, 3UL, 80) - (5UL, 4UL, 85) - (5UL, 5UL, 90) - (5UL, 6UL, 96) - (5UL, 7UL, 102) - (5UL, 8UL, 108) - (6UL, 0UL, 70) - (6UL, 1UL, 77) - (6UL, 2UL, 84) - (6UL, 3UL, 80) - (6UL, 4UL, 88) - (6UL, 5UL, 96) - (6UL, 6UL, 90) - (6UL, 7UL, 99) - (6UL, 8UL, 108) - (7UL, 0UL, 91) - (7UL, 1UL, 98) - (7UL, 2UL, 105) - (7UL, 3UL, 104) - (7UL, 4UL, 112) - (7UL, 5UL, 120) - (7UL, 6UL, 117) - (7UL, 7UL, 126) - (7UL, 8UL, 135) - (8UL, 0UL, 112) - (8UL, 1UL, 119) - (8UL, 2UL, 126) - (8UL, 3UL, 128) - (8UL, 4UL, 136) - (8UL, 5UL, 144) - (8UL, 6UL, 144) - (8UL, 7UL, 153) - (8UL, 8UL, 162) ] - ) - - Assert.Equal(expected, coo) + 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`` () = @@ -1915,8 +1827,6 @@ let ``kronecker product with rectangular and square matrixes`` () = (1UL, 2UL, 4) ] ) - let A = fromCoordinateList cooA - let cooB = CoordinateList( 2UL, @@ -1927,42 +1837,36 @@ let ``kronecker product with rectangular and square matrixes`` () = (1UL, 1UL, 8) ] ) - let B = fromCoordinateList cooB - - let result = kroneckerProduct A B (fun a b -> Some(a * b)) - - match result with - | Error msg -> Assert.True(false, msg) - | Ok res -> - let coo = toCoordinateList res - - let expectedElements = + let expected = + CoordinateList( + 4UL, + 6UL, [ (0UL, 0UL, 5) (0UL, 1UL, 6) - (1UL, 0UL, 7) - (1UL, 1UL, 8) - (0UL, 2UL, 10) (0UL, 3UL, 12) + (1UL, 0UL, 7) + (1UL, 1UL, 8) (1UL, 2UL, 14) (1UL, 3UL, 16) - (2UL, 0UL, 15) (2UL, 1UL, 18) - (3UL, 0UL, 21) - (3UL, 1UL, 24) - (2UL, 4UL, 20) (2UL, 5UL, 24) + (3UL, 0UL, 21) + (3UL, 1UL, 24) (3UL, 4UL, 28) (3UL, 5UL, 32) ] - |> List.sortBy (fun (r, c, _) -> (r, c)) - - let actualElements = coo.list |> List.sortBy (fun (r, c, _) -> (r, c)) + ) - Assert.Equal(4UL, coo.nrows) - Assert.Equal(6UL, coo.ncols) - Assert.Equal * uint64 * int>>(expectedElements, actualElements) + 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`` () = @@ -1976,8 +1880,6 @@ let ``kronecker product with square and rectangular matrixes`` () = (1UL, 1UL, 4) ] ) - let A = fromCoordinateList cooA - let cooB = CoordinateList( 2UL, @@ -1990,50 +1892,57 @@ let ``kronecker product with square and rectangular matrixes`` () = (1UL, 2UL, 10) ] ) - let B = fromCoordinateList cooB + let expectedElements = + [ (0UL, 0UL, 5) + (0UL, 1UL, 6) + (0UL, 2UL, 7) - let result = kroneckerProduct A B (fun a b -> Some(a * b)) + (1UL, 0UL, 8) + (1UL, 1UL, 9) + (1UL, 2UL, 10) - match result with - | Error msg -> Assert.True(false, msg) - | Ok res -> - let coo = toCoordinateList res + (0UL, 3UL, 10) + (0UL, 4UL, 12) + (0UL, 5UL, 14) - let expectedElements = - [ (0UL, 0UL, 5) - (0UL, 1UL, 6) - (0UL, 2UL, 7) - (1UL, 0UL, 8) - (1UL, 1UL, 9) - (1UL, 2UL, 10) + (1UL, 3UL, 16) + (1UL, 4UL, 18) + (1UL, 5UL, 20) - (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) - (2UL, 0UL, 15) - (2UL, 1UL, 18) - (2UL, 2UL, 21) - (3UL, 0UL, 24) - (3UL, 1UL, 27) - (3UL, 2UL, 30) + (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) - (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)) + Assert.Equal * uint64 * int>>( + expectedElements, + coo.list |> List.sortBy (fun (r, c, _) -> (r, c)) + ) - let actualElements = coo.list |> List.sortBy (fun (r, c, _) -> (r, c)) + | Result.Error msg -> Assert.Fail(msg) - Assert.Equal(4UL, coo.nrows) - Assert.Equal(6UL, coo.ncols) - Assert.Equal * uint64 * int>>(expectedElements, actualElements) + | _ -> Assert.Fail() [] let ``kronecker product of matrix with empty matrix`` () = @@ -2047,101 +1956,81 @@ let ``kronecker product of matrix with empty matrix`` () = (1UL, 1UL, 4) ] ) - let A = fromCoordinateList cooA - - let emptyMatrix = fromCoordinateList (CoordinateList(2UL, 2UL, [])) + let cooEmpty = CoordinateList(2UL, 2UL, []) - let result = kroneckerProduct A emptyMatrix (fun a b -> Some(a * b)) - - match result with - | Error msg -> Assert.True(false, msg) - | Ok res -> - let coo = toCoordinateList res - let expected = CoordinateList(4UL, 4UL, []) - Assert.Equal(expected, coo) + 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 emptyMatrix = fromCoordinateList (CoordinateList(2UL, 2UL, [])) + let cooEmpty = CoordinateList(2UL, 2UL, []) let cooB = - CoordinateList( - 2UL, - 2UL, - [ (0UL, 0UL, 1) - (0UL, 1UL, 2) - (1UL, 0UL, 3) - (1UL, 1UL, 4) ] - ) - - let B = fromCoordinateList cooB + CoordinateList(2UL, 2UL, [ (0UL, 0UL, 1); (1UL, 1UL, 4) ]) - let result = kroneckerProduct emptyMatrix B (fun a b -> Some(a * b)) - - match result with - | Error msg -> Assert.True(false, msg) - | Ok res -> - let coo = toCoordinateList res - let expected = CoordinateList(4UL, 4UL, []) - Assert.Equal(expected, coo) + 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 A = fromCoordinateList cooA - let cooB = CoordinateList(2UL, 2UL, [ (0UL, 0UL, 0); (1UL, 1UL, 3) ]) - let B = fromCoordinateList cooB - - let result = kroneckerProduct A B (fun a b -> Some(a * b)) - - match result with - | Error msg -> Assert.True(false, msg) - | Ok res -> - let coo = toCoordinateList res - - let expectedElements = - [ (0UL, 0UL, 0); (1UL, 1UL, 6) ] - |> 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 expected = + [ (0UL, 0UL, 0); (1UL, 1UL, 6) ] - let actualElements = coo.list |> List.sortBy (fun (r, c, _) -> (r, c)) + let actual = + (Matrix.toCoordinateList res).list |> List.sortBy (fun (r, c, _) -> (r, c)) - Assert.Equal(2UL, coo.nrows) - Assert.Equal(2UL, coo.ncols) - Assert.Equal * uint64 * int>>(expectedElements, actualElements) + 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 A = fromCoordinateList cooA - let cooB = CoordinateList(1UL, 1UL, [ (0UL, 0UL, 5) ]) - let B = fromCoordinateList cooB - - let result = kroneckerProduct A B (fun a b -> Some(a * b)) - - match result with - | Error msg -> Assert.True(false, msg) - | Ok res -> - let coo = toCoordinateList res + 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)) + let expectedElements = + [ (0UL, 0UL, 0); (1UL, 1UL, 0) ] + |> List.sortBy (fun (r, c, _) -> (r, c)) - let actualElements = coo.list |> List.sortBy (fun (r, c, _) -> (r, c)) + Assert.Equal(2UL, coo.nrows) + Assert.Equal(2UL, coo.ncols) - Assert.Equal(2UL, coo.nrows) - Assert.Equal(2UL, coo.ncols) - Assert.Equal * uint64 * int>>(expectedElements, actualElements) + 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`` () = @@ -2155,39 +2044,32 @@ let ``kronecker product of square matrix with matrix 1x1`` () = (1UL, 1UL, 4) ] ) - let A = fromCoordinateList cooA - let cooB = CoordinateList(1UL, 1UL, [ (0UL, 0UL, 3) ]) - let B = fromCoordinateList cooB - - let result = kroneckerProduct A B (fun a b -> Some(a * b)) - - match result with - | Error msg -> Assert.True(false, msg) - | Ok res -> - let coo = toCoordinateList res - - let expected = - CoordinateList( - 2UL, - 2UL, - [ (0UL, 0UL, 3) - (0UL, 1UL, 6) - (1UL, 0UL, 9) - (1UL, 1UL, 12) ] - ) - - Assert.Equal(expected, coo) + 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 A = fromCoordinateList cooA - let cooB = CoordinateList( 2UL, @@ -2198,54 +2080,46 @@ let ``kronecker product of matrix 1x1 with square matrix`` () = (1UL, 1UL, 4) ] ) - let B = fromCoordinateList cooB - - let result = kroneckerProduct A B (fun a b -> Some(a * b)) - - match result with - | Error msg -> Assert.True(false, msg) - | Ok res -> - let coo = toCoordinateList res - - let expected = - CoordinateList( - 2UL, - 2UL, - [ (0UL, 0UL, 3) - (0UL, 1UL, 6) - (1UL, 0UL, 9) - (1UL, 1UL, 12) ] - ) + 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, coo) + 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 A = fromCoordinateList cooA - let cooB = CoordinateList(2UL, 5UL, [ (0UL, 0UL, 1) ]) - let B = fromCoordinateList cooB - - let result = kroneckerProduct A B (fun a b -> Some(a * b)) - - match result with - | Error msg -> Assert.True(false, msg) - | Ok res -> - Assert.Equal(6UL, res.nrows) - Assert.Equal(20UL, res.ncols) + 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 A = fromCoordinateList cooA - let cooB = CoordinateList( 3UL, @@ -2261,31 +2135,28 @@ let ``kronecker product with sparse matrix on dense matrix`` () = (2UL, 2UL, 9) ] ) - let B = fromCoordinateList cooB - - let result = kroneckerProduct A B (fun a b -> Some(a * b)) - - match result with - | Error msg -> Assert.True(false, msg) - | Ok res -> - let coo = toCoordinateList 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, coo) + 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)`` () = @@ -2299,8 +2170,6 @@ let ``kronecker product with filtering (only even results)`` () = (1UL, 1UL, 4) ] ) - let A = fromCoordinateList cooA - let cooB = CoordinateList( 2UL, @@ -2311,17 +2180,15 @@ let ``kronecker product with filtering (only even results)`` () = (1UL, 1UL, 4) ] ) - let B = fromCoordinateList cooB - - let evenOnly a b = - let prod = a * b - if prod % 2 = 0 then Some prod else None + 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 - let result = kroneckerProduct A B evenOnly - - match result with - | Error msg -> Assert.True(false, msg) - | Ok res -> - let coo = toCoordinateList res - Assert.True(coo.list |> List.forall (fun (_, _, v) -> v % 2 = 0)) - Assert.True(List.length coo.list < 16) + match Matrix.kroneckerProduct a b evenOnly with + | Result.Ok res -> + let coo = Matrix.toCoordinateList res + Assert.True(coo.list |> List.forall (fun (_, _, v) -> v % 2 = 0)) + Assert.True(List.length coo.list < 16) + | Result.Error msg -> Assert.Fail(msg) + | _ -> Assert.Fail() diff --git a/QuadTree.Tests/Tests.SSSP.fs b/QuadTree.Tests/Tests.SSSP.fs index ff28383..1dd69cf 100644 --- a/QuadTree.Tests/Tests.SSSP.fs +++ b/QuadTree.Tests/Tests.SSSP.fs @@ -71,34 +71,32 @@ let ``Simple SSSP.`` () = *) [] let ``SSSP with recalculation`` () = - let graph = - let clist = - Matrix.CoordinateList( - 5UL, - 5UL, - [ 0UL, 1UL, 1.0 - 1UL, 2UL, 1.0 - 2UL, 3UL, 1.0 - 2UL, 4UL, 4.0 - 3UL, 4UL, 2.0 - 0UL, 3UL, 6.0 ] - ) - - Matrix.fromCoordinateList clist + let clist = + Matrix.CoordinateList( + 5UL, + 5UL, + [ 0UL, 1UL, 1.0 + 1UL, 2UL, 1.0 + 2UL, 3UL, 1.0 + 2UL, 4UL, 4.0 + 3UL, 4UL, 2.0 + 0UL, 3UL, 6.0 ] + ) - let expected = - let clist = - Vector.CoordinateList( - 5UL, - [ (0UL, 0.0) - (1UL, 1.0) - (2UL, 2.0) - (3UL, 3.0) - (4UL, 5.0) ] - ) + let expectedClist = + Vector.CoordinateList( + 5UL, + [ (0UL, 0.0) + (1UL, 1.0) + (2UL, 2.0) + (3UL, 3.0) + (4UL, 5.0) ] + ) - Ok(Vector.fromCoordinateList clist) - - let actual = Graph.SSSP.sssp graph 0UL - - Assert.Equal(expected, actual) + match Matrix.fromCoordinateList clist, Vector.fromCoordinateList expectedClist with + | Ok graph, Ok expected -> + match Graph.SSSP.sssp graph 0UL with + | Ok actual -> Assert.Equal(expected, actual) + | Error msg -> Assert.Fail() + | Error msg, _ -> Assert.Fail() + | _, Error msg -> Assert.Fail() diff --git a/QuadTree.Tests/Tests.TriangleCount.fs b/QuadTree.Tests/Tests.TriangleCount.fs index 1b95cae..76e4969 100644 --- a/QuadTree.Tests/Tests.TriangleCount.fs +++ b/QuadTree.Tests/Tests.TriangleCount.fs @@ -52,12 +52,12 @@ let ``7V Triangle count`` () = let expected = 5UL - let actual = - match triangle_count g with - | Ok(Some x) -> x - | _ -> failwith "Unreachable" - - Assert.Equal(expected, actual) + match g with + | Ok graph -> + match triangle_count graph with + | Ok(Some actual) -> Assert.Equal(expected, actual) + | _ -> Assert.Fail() + | Error _ -> Assert.Fail() [] let ``5V Triangle count`` () = @@ -86,9 +86,9 @@ let ``5V Triangle count`` () = let expected = 2UL - let actual = - match triangle_count g with - | Ok(Some x) -> x - | _ -> failwith "Unreachable" - - Assert.Equal(expected, actual) + match g with + | Ok graph -> + match triangle_count graph with + | Ok(Some actual) -> Assert.Equal(expected, actual) + | _ -> Assert.Fail() + | Error _ -> Assert.Fail() diff --git a/QuadTree.Tests/Tests.Vector.fs b/QuadTree.Tests/Tests.Vector.fs index 61ee41f..ad6ad17 100644 --- a/QuadTree.Tests/Tests.Vector.fs +++ b/QuadTree.Tests/Tests.Vector.fs @@ -14,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 = @@ -79,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 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 expected = - Vector.fromCoordinateList ( - Vector.CoordinateList( - 8UL, + let expectedResult = + let expectedData = [ (0UL, 0) (1UL, 1) (2UL, 2) @@ -111,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 + 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 expected = - Vector.fromCoordinateList ( - Vector.CoordinateList( - 6UL, + // 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.`` () = @@ -183,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.`` () = @@ -283,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.`` () = @@ -412,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.`` () = @@ -496,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 ] + let d1 = + [ 0UL, 1; 1UL, 1; 2UL, 1; 3UL, 1; 4UL, 1 ] - CoordinateList(dataLength, data) |> Vector.fromCoordinateList + let d2 = + [ 0UL, 2; 1UL, 3; 2UL, 4; 3UL, 5; 4UL, 6 ] - let v2 = - let data = - [ 0UL, 2; 1UL, 3; 2UL, 4; 3UL, 5; 4UL, 6 ] + 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 + let expectedResult = + let data = + [ 0UL, 3; 1UL, 4; 2UL, 5; 3UL, 6; 4UL, 7 ] - let f a b = Some(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.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 ] + let d1 = + [ 0UL, 1; 1UL, 1; 2UL, 1; 3UL, 1; 4UL, 1 ] - CoordinateList(dataLength, data) |> Vector.fromCoordinateList + let d2 = + [ 0UL, 2; 1UL, 3; 2UL, 4; 3UL, 5; 4UL, 6 ] - let v2 = - let data = - [ 0UL, 2; 1UL, 3; 2UL, 4; 3UL, 5; 4UL, 6 ] - - CoordinateList(dataLength, data) |> Vector.fromCoordinateList + 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 f a b = None - - 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 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 ] - - CoordinateList(dataLength, data) |> Vector.fromCoordinateList - - let v2 = - let data = - [ 0UL, 2; 1UL, 2; 2UL, 2; 3UL, 2; 4UL, 2 ] - - CoordinateList(dataLength, data) |> Vector.fromCoordinateList + let d1 = + [ 0UL, 1; 1UL, 1; 2UL, 1; 3UL, 1; 4UL, 1 ] - let f i a b = Some(int i + a + b) + let d2 = + [ 0UL, 2; 1UL, 2; 2UL, 2; 3UL, 2; 4UL, 2 ] - let expected = - let data = - [ 0UL, 3; 1UL, 4; 2UL, 5; 3UL, 6; 4UL, 7 ] - - CoordinateList(dataLength, data) |> Vector.fromCoordinateList |> Ok + 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 - let actual = Vector.map2iValues v1 v2 f + let expectedResult = + let data = + [ 0UL, 3; 1UL, 4; 2UL, 5; 3UL, 6; 4UL, 7 ] - 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 v2 = - let data = - [ 0UL, 2; 1UL, 2; 2UL, 2; 3UL, 2; 4UL, 2 ] - - 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, 2; 2UL, 2; 3UL, 2; 4UL, 2 ] - 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 with None returns None`` () = @@ -705,8 +695,6 @@ let ``Vector.map2AtLeastOne Right case`` () = [] let ``Conversion identity`` () = - let id = toCoordinateList << fromCoordinateList - let dataLength = 10UL let data = @@ -714,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`` () = @@ -730,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 - - 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 + 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 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) - ) - - let expected = - SparseVector(clist.length, 0UL, Storage(16UL, tree)) + 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) + ) - 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 @@ -838,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`` () = @@ -847,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`` () = @@ -856,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`` () = @@ -870,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`` () = @@ -881,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`` () = @@ -892,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`` () = @@ -903,253 +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`` () = - let vec = fromCoordinateList (CoordinateList(0UL, [])) - - let result = - map vec (fun (x: int option) -> - match x with - | Some v -> Some(v * 3) - | None -> None) + 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 coo = toCoordinateList result + Assert.Equal(CoordinateList(0UL, []), coo) [] let ``map with function that turns all the elements into zeros`` () = - let vec = + 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 - 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 - ) + 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`` () = - let vec = + match fromCoordinateList (CoordinateList(7UL, [ (1UL, 3); (2UL, 3); (5UL, 100) ])) - - let result = map vec (fun _ -> None) - let coo = toCoordinateList result - Assert.Equal(CoordinateList(7UL, []), coo) + 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`` () = - let vec = - fromCoordinateList (CoordinateList(3UL, [ (1UL, 11); (2UL, 33) ])) - - let result = - map vec (fun (x: int option) -> - match x with - | Some v -> Some(sprintf "str %d" v) - | None -> None) + 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 coo = toCoordinateList result + Assert.Equal(CoordinateList(3UL, [ (1UL, "str 11"); (2UL, "str 33") ]), coo) [] -let ``fromCoordinateList with out-of-range index throws exception`` () = +let ``fromCoordinateList with out-of-range index returns error`` () = let coo = CoordinateList(6UL, [ (9UL, 8) ]) - Assert.Throws(fun () -> fromCoordinateList coo |> ignore) + 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) ]) - let vec = fromCoordinateList coo - let result = toCoordinateList vec - Assert.Equal(CoordinateList(7UL, [ (1UL, 100); (3UL, 2); (5UL, 3) ]), result) + 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) ]) - let vec = Vector.fromCoordinateList coo - let result = Vector.toCoordinateList vec - Assert.Equal(CoordinateList(3UL, [ (1UL, 100) ]), result) -[] -let ``fromCoordinateList with zero size throws Exception`` () = - let coo = CoordinateList(0UL, [ (33UL, 33); (39UL, 1) ]) - Assert.Throws(fun () -> fromCoordinateList coo |> ignore) + 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`` () = - let vec = - fromCoordinateList (CoordinateList(5UL, [ (2UL, 10); (3UL, 23) ])) - - let result = - mapi vec (fun (i: uint64) (x: int option) -> - match x with - | Some v -> Some(int i + v) - | None -> None) + 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 coo = toCoordinateList result + Assert.Equal(CoordinateList(5UL, [ (2UL, 12); (3UL, 26) ]), coo) [] let ``mapi on empty vector returns empty vector`` () = - let vec = fromCoordinateList (CoordinateList(0UL, [])) - - let result = - mapi vec (fun (i: uint64) (x: int option) -> - match x with - | Some v -> Some(int i + v) - | None -> None) + 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 coo = toCoordinateList result + Assert.Equal(CoordinateList(0UL, []), coo) [] let ``mapi with special function returns empty vector`` () = - let vec = - fromCoordinateList (CoordinateList(5UL, [ (1UL, 33); (3UL, 88) ])) - - 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) + 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 coo = toCoordinateList result + Assert.Equal(CoordinateList(5UL, []), coo) [] let ``mapi works with function does not depend on the index`` () = - let vec = - fromCoordinateList (CoordinateList(5UL, [ (2UL, 10); (3UL, 23) ])) - - let result = - mapi vec (fun (i: uint64) (x: int option) -> - match x with - | Some v -> Some(v + 4) - | None -> None) + 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 coo = toCoordinateList result + Assert.Equal(CoordinateList(5UL, [ (2UL, 14); (3UL, 27) ]), coo) [] let ``slice returns error when start is negative`` () = - let vec = fromCoordinateList (CoordinateList(5UL, [ (2UL, 10) ])) - - match slice -1 3 vec with - | Result.Ok _ -> Assert.Fail("Expected Error") - | Result.Error msg -> Assert.Equal("Start should be >= 0", msg) + 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`` () = - let vec = fromCoordinateList (CoordinateList(5UL, [ (2UL, 10) ])) - - match slice 1 -3 vec with - | Result.Ok _ -> Assert.Fail("Expected Error") - | Result.Error msg -> Assert.Equal("End should be >= 0", msg) + 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`` () = - let vec = fromCoordinateList (CoordinateList(5UL, [ (2UL, 10) ])) - - match slice 7 3 vec with - | Result.Ok _ -> Assert.Fail("Expected Error") - | Result.Error msg -> Assert.Equal("Start is out of Vector length", msg) + 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`` () = - let vec = fromCoordinateList (CoordinateList(5UL, [ (2UL, 10) ])) - - match slice 3 7 vec with - | Result.Ok _ -> Assert.Fail("Expected Error") - | Result.Error msg -> Assert.Equal("End is out of Vector length", msg) + 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`` () = - let vec = fromCoordinateList (CoordinateList(5UL, [ (2UL, 10) ])) - - match slice 4 3 vec with - | Result.Ok _ -> Assert.Fail("Expected Error") - | Result.Error msg -> Assert.Equal("End should be >= Start", msg) + 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`` () = - let vec = - fromCoordinateList (CoordinateList(7UL, [ (2UL, 10); (6UL, 20) ])) - - 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) + 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`` () = - let vec = - fromCoordinateList (CoordinateList(7UL, [ (5UL, 10); (6UL, 20) ])) - - match slice 0 2 vec with - | Result.Ok result -> - let coo = toCoordinateList result - Assert.Equal(CoordinateList(3UL, []), coo) - | Result.Error msg -> Assert.Fail(msg) + 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`` () = - let vec = - fromCoordinateList (CoordinateList(7UL, [ (2UL, 10); (6UL, 20) ])) - - 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) + 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`` () = - let vec = + match fromCoordinateList (CoordinateList(7UL, [ (2UL, 10); (3UL, 33); (6UL, 20) ])) - - 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) + 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`` () = - let vec = + match fromCoordinateList (CoordinateList(7UL, [ (0UL, 10); (3UL, 33); (6UL, 20) ])) - - 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) + 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`` () = - let vec = + match fromCoordinateList (CoordinateList(7UL, [ (0UL, 10); (3UL, 33); (6UL, 20) ])) - - 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) + with + | Result.Error msg -> Assert.Fail() + | Result.Ok vec -> + match slice 2 6 vec with + | Result.Ok result -> + let coo = Vector.toCoordinateList result + Assert.Equal(CoordinateList(5UL, [ (1UL, 33); (4UL, 20) ]), coo) + | Result.Error msg -> Assert.Fail(msg) diff --git a/QuadTree/Boruvka.fs b/QuadTree/Boruvka.fs index c0ed119..79f1dee 100644 --- a/QuadTree/Boruvka.fs +++ b/QuadTree/Boruvka.fs @@ -16,7 +16,13 @@ type Error = let mst (graph: Matrix.SparseMatrix<_>) = - let op_mult (i, x) (row, col, w) = Some(w, row) + // Canonical representation of an undirected edge: + // (weight, min endpoint, max endpoint). + // This is important for deterministic tie-breaking. + let op_mult (_i, _x) (row, col, w) = + let r = uint64 row + let c = uint64 col + Some(w, min r c, max r c) let op_min x y = match x, y with @@ -35,17 +41,20 @@ let mst (graph: Matrix.SparseMatrix<_>) = let treeFilter edges index = fun i j g -> - let i = uint64 i * 1UL - let j = uint64 j * 1UL + let row = uint64 i + let col = uint64 j + + let i = row * 1UL + let edge = Vector.unsafeGet edges i let idx = Vector.unsafeGet index i - let result = - match edge, idx with - | Some(w, dst), Some idxVal -> g = w && idxVal = i && uint64 dst = uint64 j - | _ -> false + let u = min row col + let v = max row col - result + match edge, idx with + | Some(w, edgeU, edgeV), Some idxVal -> g = w && idxVal = i && edgeU = u && edgeV = v + | _ -> false let graphFilter parent = fun i j -> @@ -66,44 +75,39 @@ let mst (graph: Matrix.SparseMatrix<_>) = if graph.nvals > 0UL then - // Cheapest outgoing edge for each vertex - // For each vertex j, find the smallest weight edge (i, j, w) - // such that i and j are in different components. - // Because graph contains only cross‑component edges, - // we simply take the min over all neighbors. + // Cheapest outgoing edge for each vertex. + // Edge is stored canonically as (weight, minEndpoint, maxEndpoint). resultM { let! edges = LinearAlgebra.vxmi_values op_min op_mult parent graph |> Result.mapError EdgesCalculationProblem - // Per‑component cheapest edge - // For each component, keep the smallest edges among its vertices. + // Per-component cheapest edge. let! cedges = Vector.scatter (Vector.empty length) edges parent op_min |> Result.mapError CEdgesCalculationProblem - // Propagate component's cheapest edge to all its vertices - // Each vertex gets its component's edge + // Propagate component's cheapest edge to all vertices. let t = Vector.gather cedges parent - // Identify a representative vertex for each component - // For each vertex, if its own edge is the component's cheapest, mark it. + // Identify representative vertices whose own edge equals + // the cheapest edge of their component. let! indexInner = Vector.map2i t edges (fun i t e -> match (t, e) with | Some v1, Some v2 when v1 = v2 -> Some i | _ -> None) |> Result.mapError IndexInnerCalculationProblem - // Among the marked vertices in a component, keep the smallest index. + + // Among marked vertices in a component, keep the smallest index. let! index = Vector.scatter (Vector.empty length) indexInner parent op_min |> Result.mapError IndexCalculationProblem - // now each vertex knows its component's representative + + // Now each vertex knows its component's representative. let index = Vector.gather index parent - // Add selected edges to the MST tree - // An edge (i, j, w) is added if vertex i is the representative for its component - // and (i, j, w) is the cheapest edge of that component. + // Add selected edges to the MST tree. let treeFilter = treeFilter edges index let! tree = @@ -114,24 +118,36 @@ let mst (graph: Matrix.SparseMatrix<_>) = | _ -> None) |> Result.mapError TreeSelectionProblem - // Compute new parent assignments (merge components) - // For each component representative i with cheapest edge (w, j), we want to merge - // the component of i with the component of j. Choose the smaller root. + // Compute new parent assignments. + // For selected canonical edge (u, v), if current selected + // representative is one endpoint, merge it with the other endpoint. let! data_for_update_parent = Vector.map2i edges index (fun i e idx -> match e, idx with - | Some(v, j), Some(_i) when _i = i -> - let j = uint64 j * 1UL - let parent_i = Vector.unsafeGet parent i - let parent_j = Vector.unsafeGet parent j - - match parent_i, parent_j with - | Some p_i, Some p_j -> if p_i < p_j then Some(j, p_i) else Some(i, p_j) - | x -> failwithf "Unreachable: %A" x + | Some(_w, u, v), Some selected when selected = i -> + let current = uint64 i + + let other = + if current = u then Some v + elif current = v then Some u + else None + + match other with + | Some otherVertex -> + let j = otherVertex * 1UL + let parent_i = Vector.unsafeGet parent i + let parent_j = Vector.unsafeGet parent j + + match parent_i, parent_j with + | Some p_i, Some p_j -> if p_i < p_j then Some(j, p_i) else Some(i, p_j) + | _ -> None + + | None -> None + | _ -> None) |> Result.mapError DataForUpgradeParentCalculationProblem - // Apply the updates + // Apply direct parent updates. let! initial_parent_update = Vector.foldValues data_for_update_parent @@ -151,11 +167,10 @@ let mst (graph: Matrix.SparseMatrix<_>) = Vector.scatter parent initial_parent_update parent op_min |> Result.mapError ScatterProblem - // Then ensure that all vertices in a merged component point to the same root. - // This is done by a fixpoint (path compression) that repeatedly gathers parents. + // Path compression. let parent = fixPoint parent - // Filter the graph to keep only edges between different components + // Keep only edges between different components. let graphFilter = graphFilter parent let graph = Matrix.mapi graph (fun i j v -> if graphFilter i j then v else None) diff --git a/QuadTree/Matrix.fs b/QuadTree/Matrix.fs index e42917d..a9a5bf3 100644 --- a/QuadTree/Matrix.fs +++ b/QuadTree/Matrix.fs @@ -85,45 +85,45 @@ let fromCoordinateList (coo: CoordinateList<'a>) = unique |> List.exists (fun (i, j, _) -> uint64 i >= uint64 coo.nrows || uint64 j >= uint64 coo.ncols) then - failwith "Coordinates out of range" - - 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) + 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 unique (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 @@ -152,7 +152,13 @@ let toCoordinateList (matrix: SparseMatrix<'a>) = 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>) = @@ -572,26 +578,35 @@ let slice Ok(SparseMatrix(newRows, newCols, nvals, Storage(newSize, shiftedTree))) -let reduceRows (op: 'a option -> 'a option -> 'a option) (matrix: SparseMatrix<'a>) : Vector.SparseVector<'a> = - let rows = matrix.nrows +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) - let rec inner (size: uint64) (row: uint64) (col: uint64) tree acc = - match tree with + loop rowOffset colOffset acc | Node(nw, ne, sw, se) -> - let half = size / 2UL - let halfRow = (uint64 half) * 1UL - let halfCol = (uint64 half) * 1UL - let acc = inner half row col nw acc - let acc = inner half row (col + halfCol) ne acc - let acc = inner half (row + halfRow) col sw acc - let acc = inner half (row + halfRow) (col + halfCol) se acc - acc - | Leaf(Dummy) -> acc - | Leaf(UserValue(None)) -> acc - | Leaf(UserValue(Some v)) -> (row, v) :: acc + 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 = - inner matrix.storage.size 0UL 0UL matrix.storage.data [] + foldQuadtree (fun acc row col v -> (row, v) :: acc) [] matrix.storage.size matrix.storage.data let grouped = List.sortBy fst pairs @@ -616,28 +631,20 @@ let reduceRows (op: 'a option -> 'a option -> 'a option) (matrix: SparseMatrix<' let vectorData = data |> List.map (fun (row, _, v) -> (uint64 row * 1UL, v)) - Vector.fromCoordinateList (Vector.CoordinateList(uint64 rows * 1UL, vectorData)) + 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 rec inner (size: uint64) (row: uint64) (col: uint64) tree acc = - match tree with - | Node(nw, ne, sw, se) -> - let half = size / 2UL - let halfRow = (uint64 half) * 1UL - let halfCol = (uint64 half) * 1UL - let acc = inner half row col nw acc - let acc = inner half row (col + halfCol) ne acc - let acc = inner half (row + halfRow) col sw acc - let acc = inner half (row + halfRow) (col + halfCol) se acc - acc - | Leaf(Dummy) -> acc - | Leaf(UserValue(None)) -> acc - | Leaf(UserValue(Some v)) -> (col, v) :: acc - let pairs = - inner matrix.storage.size 0UL 0UL matrix.storage.data [] + foldQuadtree (fun acc row col v -> (col, v) :: acc) [] matrix.storage.size matrix.storage.data let grouped = List.sortBy fst pairs @@ -662,7 +669,14 @@ let reduceCols (op: 'a option -> 'a option -> 'a option) (matrix: SparseMatrix<' let vectorData = data |> List.map (fun (_, col, v) -> (uint64 col * 1UL, v)) - Vector.fromCoordinateList (Vector.CoordinateList(uint64 cols * 1UL, vectorData)) + 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>) @@ -709,30 +723,6 @@ let kroneckerProduct | false, false -> Node(emptySub, emptySub, emptySub, insert half (row - halfRow) (col - halfCol) value emptySub) - 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 mat2Rows = uint64 matrix2.nrows let mat2Cols = uint64 matrix2.ncols let initialTree = empty newSize 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 42961f2..cecb7cb 100644 --- a/QuadTree/Vector.fs +++ b/QuadTree/Vector.fs @@ -306,7 +306,7 @@ let update (vector: SparseVector<_>) i v op = else Error Error.InconsistentSizeOfArguments -let fromCoordinateList (lst: CoordinateList<'a>) : SparseVector<'a> = +let fromCoordinateList (lst: CoordinateList<'a>) : Result, string> = let unique = lst.data |> List.groupBy fst @@ -314,33 +314,33 @@ let fromCoordinateList (lst: CoordinateList<'a>) : SparseVector<'a> = let value = entries |> List.map snd |> List.last (idx, value)) - if unique |> List.exists (fun (idx, _) -> uint64 idx >= uint64 lst.length) then - failwith "Index out of range" - - 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 + 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 unique + 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 @@ -364,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 =