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: 2 additions & 0 deletions QuadTree.Tests/QuadTree.Tests.fsproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
<ItemGroup>
<Compile Include="Tests.fs" />
<Compile Include="Tests.Vector.fs" />
<Compile Include="Tests.Matrix.fs" />
<Compile Include="Tests.LinearAlgebra.fs" />
</ItemGroup>

<ItemGroup>
Expand Down
237 changes: 237 additions & 0 deletions QuadTree.Tests/Tests.LinearAlgebra.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,237 @@
module LinearAlgebra.Tests

open System
open Xunit

open Matrix
open Vector
open Common

(*
2,2,2,2
*
N,1,1,N
3,2,2,3
N,N,1,2
N,N,3,N
=
6,6,14,10
*)
[<Fact>]
let ``Simple vxm. All sizes are power of two.`` () =
let m =
let tree =
Matrix.qtree.Node(
Matrix.qtree.Node(
Matrix.qtree.Leaf(UserValue(None)),
Matrix.qtree.Leaf(UserValue(Some(1))),
Matrix.qtree.Leaf(UserValue(Some(3))),
Matrix.qtree.Leaf(UserValue(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.Leaf(UserValue(None)),
Matrix.qtree.Node(
Matrix.qtree.Leaf(UserValue(Some(1))),
Matrix.qtree.Leaf(UserValue(Some(2))),
Matrix.qtree.Leaf(UserValue(Some(3))),
Matrix.qtree.Leaf(UserValue(None))
)
)

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

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

let store = Vector.Storage(4UL<storageSize>, tree)
SparseVector(4UL<dataLength>, 4UL<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.Leaf(UserValue(Some(6))),
Vector.btree.Node(Vector.btree.Leaf(UserValue(Some(14))), Vector.btree.Leaf(UserValue(Some(10))))
)

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

let actual = LinearAlgebra.vxm op_add op_mult v m

let eq = actual = expected

Assert.True(eq)

(*
2,2,2,D
*
N,1,1,N
3,2,2,3
N,N,1,2
D,D,D,D
=
6,6,8,10
*)
[<Fact>]
let ``Simple vxm. 3 * (3x4)`` () =
let m =
let tree =
Matrix.qtree.Node(
Matrix.qtree.Node(
Matrix.qtree.Leaf(UserValue(None)),
Matrix.qtree.Leaf(UserValue(Some(1))),
Matrix.qtree.Leaf(UserValue(Some(3))),
Matrix.qtree.Leaf(UserValue(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)
)
)

let store = Matrix.Storage(4UL<storageVSize>, 4UL<storageHSize>, tree)
SparseMatrix(3UL<nrows>, 4UL<ncols>, 8UL<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.Leaf(UserValue(Some(6))),
Vector.btree.Node(Vector.btree.Leaf(UserValue(Some(8))), Vector.btree.Leaf(UserValue(Some(10))))
)

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

let actual = LinearAlgebra.vxm op_add op_mult v m

let eq = actual = expected

Assert.True(eq)


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

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

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

let store = Vector.Storage(4UL<storageSize>, tree)
SparseVector(4UL<dataLength>, 4UL<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.Leaf(UserValue(Some(6))),
Vector.btree.Node(Vector.btree.Leaf(UserValue(Some(14))), Vector.btree.Leaf(Dummy))
)

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

let actual = LinearAlgebra.vxm op_add op_mult v m

let eq = actual = expected

Assert.True(eq)
Loading
Loading