Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion QuadTree.Tests/Tests.BFS.fs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ let ``Simple level bfs.`` () =
)
)

let store = Matrix.Storage(4UL<storageVSize>, 4UL<storageHSize>, tree)
let store = Matrix.Storage(4UL<storageSize>, tree)
SparseMatrix(4UL<nrows>, 4UL<ncols>, 9UL<nvals>, store)

let startVertices =
Expand Down
117 changes: 114 additions & 3 deletions QuadTree.Tests/Tests.LinearAlgebra.fs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ let ``Simple vxm. All sizes are power of two.`` () =
)
)

let store = Matrix.Storage(4UL<storageVSize>, 4UL<storageHSize>, tree)
let store = Matrix.Storage(4UL<storageSize>, tree)
SparseMatrix(4UL<nrows>, 4UL<ncols>, 9UL<nvals>, store)

let v =
Expand Down Expand Up @@ -119,7 +119,7 @@ let ``Simple vxm. 3 * (3x4)`` () =
)
)

let store = Matrix.Storage(4UL<storageVSize>, 4UL<storageHSize>, tree)
let store = Matrix.Storage(4UL<storageSize>, tree)
SparseMatrix(3UL<nrows>, 4UL<ncols>, 8UL<nvals>, store)

let v =
Expand Down Expand Up @@ -195,7 +195,7 @@ let ``Simple vxm. 4 * (4x3).`` () =
)
)

let store = Matrix.Storage(4UL<storageVSize>, 4UL<storageHSize>, tree)
let store = Matrix.Storage(4UL<storageSize>, tree)
SparseMatrix(4UL<nrows>, 3UL<ncols>, 7UL<nvals>, store)

let v =
Expand Down Expand Up @@ -229,3 +229,114 @@ let ``Simple vxm. 4 * (4x3).`` () =
let actual = LinearAlgebra.vxm op_add op_mult v m

Assert.Equal(expected, actual)


(*
2,2,2,D
*
N,1,1,N,N,D,D,D
3,2,2,3,1,D,D,D
N,N,1,2,N,D,D,D
D,D,D,D,D,D,D,D
D,D,D,D,D,D,D,D
D,D,D,D,D,D,D,D
D,D,D,D,D,D,D,D
D,D,D,D,D,D,D,D
=
6,6,8,10,2,D,D,D
*)
[<Fact>]
let ``Simple vxm. 3 * (3x5)`` () =
let m =
let tree =
Matrix.qtree.Node(
Matrix.qtree.Node(
Matrix.qtree.Node(
Matrix.qtree.Leaf(UserValue(None)),
Matrix.qtree.Leaf(UserValue(Some(1))),
Matrix.qtree.Leaf(UserValue(Some(3))),
Matrix.qtree.Leaf(UserValue(Some(2)))
),
Matrix.qtree.Node(
Matrix.qtree.Leaf(UserValue(Some(1))),
Matrix.qtree.Leaf(UserValue(None)),
Matrix.qtree.Leaf(UserValue(Some(2))),
Matrix.qtree.Leaf(UserValue(Some(3)))
),
Matrix.qtree.Node(
Matrix.qtree.Leaf(UserValue(None)),
Matrix.qtree.Leaf(UserValue(None)),
Matrix.qtree.Leaf(Dummy),
Matrix.qtree.Leaf(Dummy)
),
Matrix.qtree.Node(
Matrix.qtree.Leaf(UserValue(Some(1))),
Matrix.qtree.Leaf(UserValue(Some(2))),
Matrix.qtree.Leaf(Dummy),
Matrix.qtree.Leaf(Dummy)
)
),
Matrix.qtree.Node(
Matrix.qtree.Node(
Matrix.qtree.Leaf(UserValue(None)),
Matrix.qtree.Leaf(Dummy),
Matrix.qtree.Leaf(UserValue(Some(1))),
Matrix.qtree.Leaf(Dummy)
),
Matrix.qtree.Leaf(Dummy),
Matrix.qtree.Node(
Matrix.qtree.Leaf(UserValue(None)),
Matrix.qtree.Leaf(Dummy),
Matrix.qtree.Leaf(Dummy),
Matrix.qtree.Leaf(Dummy)
),
Matrix.qtree.Leaf(Dummy)
),
Matrix.qtree.Leaf(Dummy),
Matrix.qtree.Leaf(Dummy)
)

let store = Matrix.Storage(8UL<storageSize>, tree)
SparseMatrix(3UL<nrows>, 5UL<ncols>, 9UL<nvals>, store)

let v =
let tree =
Vector.btree.Node(
Vector.btree.Leaf(UserValue(Some(2))),
Vector.btree.Node(Vector.btree.Leaf(UserValue(Some(2))), Vector.btree.Leaf(Dummy))
)

let store = Vector.Storage(4UL<storageSize>, tree)
SparseVector(3UL<dataLength>, 3UL<nvals>, store)

let op_add x y =
match (x, y) with
| Some(a), Some(b) -> Some(a + b)
| Some(a), _
| _, Some(a) -> Some(a)
| _ -> None

let op_mult x y =
match (x, y) with
| Some(a), Some(b) -> Some(a * b)
| _ -> None

let expected =
let tree =
Vector.btree.Node(
Vector.btree.Node(
Vector.btree.Leaf(UserValue(Some(6))),
Vector.btree.Node(Vector.btree.Leaf(UserValue(Some(8))), Vector.btree.Leaf(UserValue(Some(10))))
),
Vector.btree.Node(
Vector.btree.Node(Vector.btree.Leaf(UserValue(Some(2))), Vector.btree.Leaf(Dummy)),
Vector.btree.Leaf(Dummy)
)
)

let store = Vector.Storage(8UL<storageSize>, tree)
Result.Success(SparseVector(5UL<dataLength>, 5UL<nvals>, store))

let actual = LinearAlgebra.vxm op_add op_mult v m

Assert.Equal(expected, actual)
19 changes: 9 additions & 10 deletions QuadTree.Tests/Tests.Matrix.fs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@ let printMatrix (matrix: SparseMatrix<_>) =
printfn " Columns: %A" matrix.ncols
printfn " Nvals: %A" matrix.nvals
printfn " Storage:"
printfn " vSize: %A" matrix.storage.vSize
printfn " hSize: %A" matrix.storage.hSize
printfn " size: %A" matrix.storage.size
printfn " Data: %A" matrix.storage.data


Expand Down Expand Up @@ -59,7 +58,7 @@ let ``Simple Matrix.map2. Square where number of cols and rows are power of two.
)
)

let store = Storage(4UL<storageVSize>, 4UL<storageHSize>, tree)
let store = Storage(4UL<storageSize>, tree)
SparseMatrix(4UL<nrows>, 4UL<ncols>, 9UL<nvals>, store)

let m2 =
Expand All @@ -71,7 +70,7 @@ let ``Simple Matrix.map2. Square where number of cols and rows are power of two.
Matrix.qtree.Leaf(UserValue(None))
)

let store = Storage(4UL<storageVSize>, 4UL<storageHSize>, tree)
let store = Storage(4UL<storageSize>, tree)
SparseMatrix(4UL<nrows>, 4UL<ncols>, 12UL<nvals>, store)

let f x y =
Expand All @@ -98,7 +97,7 @@ let ``Simple Matrix.map2. Square where number of cols and rows are power of two.
Matrix.qtree.Leaf(UserValue(None))
)

let store = Storage(4UL<storageVSize>, 4UL<storageHSize>, tree)
let store = Storage(4UL<storageSize>, tree)
Result.Success(SparseMatrix(4UL<nrows>, 4UL<ncols>, 6UL<nvals>, store))

let actual = Matrix.map2 m1 m2 f
Expand Down Expand Up @@ -152,7 +151,7 @@ let ``Simple Matrix.map2. Square where number of cols and rows are not power of
)
)

let store = Storage(4UL<storageVSize>, 4UL<storageHSize>, tree)
let store = Storage(4UL<storageSize>, tree)
SparseMatrix(3UL<nrows>, 3UL<ncols>, 6UL<nvals>, store)

let m2 =
Expand All @@ -179,7 +178,7 @@ let ``Simple Matrix.map2. Square where number of cols and rows are not power of
)
)

let store = Storage(4UL<storageVSize>, 4UL<storageHSize>, tree)
let store = Storage(4UL<storageSize>, tree)
SparseMatrix(3UL<nrows>, 3UL<ncols>, 8UL<nvals>, store)

let f x y =
Expand Down Expand Up @@ -216,7 +215,7 @@ let ``Simple Matrix.map2. Square where number of cols and rows are not power of
)
)

let store = Storage(4UL<storageVSize>, 4UL<storageHSize>, tree)
let store = Storage(4UL<storageSize>, tree)
Result.Success(SparseMatrix(3UL<nrows>, 3UL<ncols>, 5UL<nvals>, store))

let actual = Matrix.map2 m1 m2 f
Expand Down Expand Up @@ -312,7 +311,7 @@ let ``Condensation of empty`` () =
)

let expected =
SparseMatrix(2UL<nrows>, 3UL<ncols>, 0UL<nvals>, Storage(4UL<storageVSize>, 4UL<storageHSize>, tree))
SparseMatrix(2UL<nrows>, 3UL<ncols>, 0UL<nvals>, Storage(4UL<storageSize>, tree))

Assert.Equal(expected.storage.data, actual.storage.data)

Expand Down Expand Up @@ -347,6 +346,6 @@ let ``Condensation of sparse`` () =
)

let expected =
SparseMatrix(4UL<nrows>, 3UL<ncols>, 0UL<nvals>, Storage(4UL<storageVSize>, 4UL<storageHSize>, tree))
SparseMatrix(4UL<nrows>, 3UL<ncols>, 0UL<nvals>, Storage(4UL<storageSize>, tree))

Assert.Equal(expected.storage.data, actual.storage.data)
3 changes: 3 additions & 0 deletions QuadTree/Common.fs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ module Common
[<Measure>]
type nvals

[<Measure>]
type storageSize

type 'value treeValue =
| Dummy
| UserValue of 'value
Expand Down
22 changes: 19 additions & 3 deletions QuadTree/LinearAlgebra.fs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ let rec multScalar op_add (x: uint64) y =

let vxm op_add op_mult (vector: Vector.SparseVector<'a>) (matrix: Matrix.SparseMatrix<'b>) =

let rec inner (size: uint64<Vector.storageSize>) vector matrix =
let rec inner (size: uint64<storageSize>) vector matrix =
let _do x1 x2 y1 y2 y3 y4 =
let new_size = size / 2UL

Expand Down Expand Up @@ -72,13 +72,29 @@ let vxm op_add op_mult (vector: Vector.SparseVector<'a>) (matrix: Matrix.SparseM
| (x, y) -> Result.Failure <| Error.InconsistentStructureOfStorages(x, y)

if uint64 vector.length = uint64 matrix.nrows then
match inner vector.storage.size vector.storage.data matrix.storage.data with
let vector_storage =
if uint64 vector.storage.size < uint64 matrix.storage.size then
let rec increaseStorage storage_data (current_size: uint64<storageSize>) bound =
if current_size = bound then
storage_data
else
increaseStorage
(Vector.btree.Node(storage_data, Vector.btree.Leaf(Dummy)))
(current_size * 2UL)
bound

let target_size = matrix.storage.size
Vector.Storage(target_size, increaseStorage vector.storage.data vector.storage.size target_size)
else
vector.storage

match inner vector_storage.size vector_storage.data matrix.storage.data with
| Result.Failure x -> Result.Failure x
| Result.Success(storage, nvals) ->
(Vector.SparseVector(
(uint64 matrix.ncols) * 1UL<Vector.dataLength>,
nvals,
(Vector.Storage((uint64 matrix.storage.hSize) * 1UL<Vector.storageSize>, storage))
(Vector.Storage(matrix.storage.size, storage))
))
|> Result.Success
else
Expand Down
Loading
Loading