namespace MathNet.Numerics.Tests open NUnit.Framework open FsUnit open MathNet.Numerics.LinearAlgebra.Generic open MathNet.Numerics.LinearAlgebra.Double /// Unit tests for the vector type. module VectorTests = /// A small uniform vector. let smallv = new DenseVector([|0.3;0.3;0.3;0.3;0.3|]) :> Vector /// A small uniform vector. let sparsev = SparseVector.OfIndexedEnumerable(5, [(1,0.3)]) :> Vector /// A large vector with increasingly large entries let largev = new DenseVector(Array.init 100 (fun i -> float i / 100.0)) :> Vector [] let ``Vector.GetSlice`` () = largev.[*] |> should equal largev largev.[0..99] |> should equal largev largev.[1..3] |> should equal (DenseVector([|0.01;0.02;0.03|])) largev.[97..] |> should equal (DenseVector([|0.97;0.98;0.99|])) largev.[..4] |> should equal (DenseVector([|0.00;0.01;0.02;0.03;0.04|])) [] let ``Vector.SetSlice`` () = let v = smallv.Clone() in v.[*] <- DenseVector([|0.1;0.2;0.3;0.4;0.5|]); v |> should equal (DenseVector([|0.1;0.2;0.3;0.4;0.5|])) let v = smallv.Clone() in v.[0..4] <- DenseVector([|0.1;0.2;0.3;0.4;0.5|]); v |> should equal (DenseVector([|0.1;0.2;0.3;0.4;0.5|])) let v = smallv.Clone() in v.[1..3] <- DenseVector([|7.0;8.0;9.0|]); v |> should equal (DenseVector([|0.3;7.0;8.0;9.0;0.3|])) let v = smallv.Clone() in v.[2..] <- DenseVector([|7.0;8.0;9.0|]); v |> should equal (DenseVector([|0.3;0.3;7.0;8.0;9.0|])) let v = smallv.Clone() in v.[..2] <- DenseVector([|7.0;8.0;9.0|]); v |> should equal (DenseVector([|7.0;8.0;9.0;0.3;0.3|])) [] let ``Vector.toArray`` () = Vector.toArray smallv |> should array_equal [|0.3;0.3;0.3;0.3;0.3|] [] let ``Vector.toList`` () = Vector.toList smallv |> should equal [0.3;0.3;0.3;0.3;0.3] [] let ``Vector.mapInPlace.Dense`` () = let w = smallv.Clone() Vector.mapInPlace (fun x -> 2.0 * x) w w |> should equal (2.0 * smallv) [] let ``Vector.mapInPlace.Sparse`` () = let w = sparsev.Clone() Vector.mapInPlace (fun x -> 2.0 * x) w w |> should equal (2.0 * sparsev) [] let ``Vector.mapnzInPlace.Sparse`` () = let w = sparsev.Clone() Vector.mapnzInPlace (fun x -> 2.0 * x) w w |> should equal (2.0 * sparsev) [] let ``Vector.mapiInPlace.Dense`` () = let w = largev.Clone() Vector.mapiInPlace (fun i x -> float i / 100.0) w w |> should equal (largev) [] let ``Vector.mapiInPlace.Sparse`` () = let w = sparsev.Clone() Vector.mapiInPlace (fun i x -> 2.0 * float i * x) w w |> should equal (2.0 * sparsev) [] let ``Vector.mapinzInPlace.Sparse`` () = let w = sparsev.Clone() Vector.mapinzInPlace (fun i x -> 2.0 * float i * x) w w |> should equal (2.0 * sparsev) [] let ``Vector.addInPlace`` () = let w = largev.Clone() Vector.addInPlace w largev w |> should equal (2.0 * largev) [] let ``Vector.subInPlace`` () = let w = largev.Clone() Vector.subInPlace w largev w |> should equal (0.0 * largev) [] let ``Vector.map`` () = Vector.map (fun x -> 2.0 * x) largev |> should equal (2.0 * largev) [] let ``Vector.mapnz`` () = Vector.mapnz (fun x -> 2.0 * x) largev |> should equal (2.0 * largev) [] let ``Vector.mapi`` () = Vector.mapi (fun i x -> float i / 100.0) largev |> should equal largev [] let ``Vector.mapinz`` () = Vector.mapinz (fun i x -> float i / 100.0) largev |> should equal largev [] let ``Vector.fold`` () = Vector.fold (fun a b -> a - b) 0.0 smallv |> should equal -1.5 [] let ``Vector.foldBack`` () = Vector.foldBack (fun a b -> a - b) 0.0 smallv |> should equal 0.0 [] let ``Vector.foldi`` () = Vector.foldi (fun i a b -> a + b) 0.0 smallv |> should equal 1.5 [] let ``Vector.forall`` () = Vector.forall (fun x -> x = 0.3) smallv |> should equal true [] let ``Vector.exists`` () = Vector.exists (fun x -> x = 0.3) smallv |> should equal true [] let ``Vector.foralli`` () = Vector.foralli (fun i x -> x = 0.3 && i < 5) smallv |> should equal true [] let ``Vector.existsi`` () = Vector.existsi (fun i x -> x = 0.3 && i = 2) smallv |> should equal true [] let ``Vector.scan`` () = Vector.scan (fun acc x -> acc + x) smallv |> should (approximately_vector_equal 14) (new DenseVector( [|0.3;0.6;0.9;1.2;1.5|] ) :> Vector) [] let ``Vector.scanBack`` () = Vector.scanBack (fun x acc -> acc + x) smallv |> should (approximately_vector_equal 14) (new DenseVector( [|1.5;1.2;0.9;0.6;0.3|] ) :> Vector) [] let ``Vector.reduce`` () = Vector.reduce (fun acc x -> acc ** x) smallv |> should (approximately_equal 14) 0.990295218585507 [] let ``Vector.reduceBack`` () = Vector.reduceBack (fun x acc -> x ** acc) smallv |> should (approximately_equal 14) 0.488911287726319 [] let ``Vector.insert`` () = Vector.insert 2 0.5 smallv |> should (approximately_vector_equal 14) (new DenseVector ( [|0.3;0.3;0.5;0.3;0.3;0.3|] ) :> Vector)