Browse Source

Add Vector.insert (similar to Matrix.InsertColumn and Matrix.InsertRow)

la-knuth
Gustavo Guerra 15 years ago
committed by Christoph Ruegg
parent
commit
4dfa1c66a5
  1. 13
      src/FSharp/Vector.fs
  2. 2
      src/FSharpUnitTests/Program.fs

13
src/FSharp/Vector.fs

@ -32,6 +32,7 @@ namespace MathNet.Numerics.LinearAlgebra.Double
open MathNet.Numerics.LinearAlgebra open MathNet.Numerics.LinearAlgebra
open MathNet.Numerics.LinearAlgebra.Generic open MathNet.Numerics.LinearAlgebra.Generic
/// A module which implements functional vector operations. /// A module which implements functional vector operations.
module Vector = module Vector =
@ -173,4 +174,14 @@ module Vector =
let mutable p = v.Item(v.Count-1) let mutable p = v.Item(v.Count-1)
for i=2 to v.Count do for i=2 to v.Count do
p <- f (v.Item(v.Count - i)) p p <- f (v.Item(v.Count - i)) p
p p
/// Creates a new vector and inserts the given value at the given index.
let inline insert index value (v: #Vector<float>) =
let newV = new DenseVector(v.Count + 1)
for i = 0 to index - 1 do
newV.Item(i) <- v.Item(i)
newV.Item(index) <- value
for i = index + 1 to v.Count do
newV.Item(i) <- v.Item(i - 1)
newV

2
src/FSharpUnitTests/Program.fs

@ -96,6 +96,8 @@ let VectorTests =
(Vector.reduce (fun acc x -> acc ** x) smallv |> should approximately_equal 14 0.990295218585507) (Vector.reduce (fun acc x -> acc ** x) smallv |> should approximately_equal 14 0.990295218585507)
spec "Vector.reduceBack" spec "Vector.reduceBack"
(Vector.reduceBack (fun x acc -> x ** acc) smallv |> should approximately_equal 14 0.488911287726319) (Vector.reduceBack (fun x acc -> x ** acc) smallv |> should approximately_equal 14 0.488911287726319)
spec "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<float>) )
] ]

Loading…
Cancel
Save