FsAlg


Vector Operations

This page lists only a selection of commonly used vector operations provided by FsAlg.

Please refer to API Reference for a full list of supported operations.

The FsAlg.Generic.Vector module provides functionality similar to the F# Collecitons.Array module, for creating and manipulating vectors.

Creating Vectors

1: 
2: 
3: 
4: 
5: 
open FsAlg.Generic

let v1 = vector [1.; 2.; 3.]
let v2 = Vector.create 3 1.
let v3 = Vector.init 3 (fun i -> exp (float i))
val v1 : Vector<float> = Vector [|1.0; 2.0; 3.0|]
val v2 : Vector<float> = Vector [|1.0; 1.0; 1.0|]
val v3 : Vector<float> = Vector [|1.0; 2.718281828; 7.389056099|]

Basic Operations

 1: 
 2: 
 3: 
 4: 
 5: 
 6: 
 7: 
 8: 
 9: 
10: 
11: 
12: 
13: 
let v4  = v1 + v2  // Vector addition
let v5  = v1 - v2  // Vector substraction
let v6  = v1 * v2  // Vector inner product (dot / scalar product)
let v7  = v1 %* v2 // Vector cross product
let v8  = v1 .* v2 // Vector element-wise (Hadamard) product
let v9  = v1 ./ v2 // Vector element-wise (Hadamard) division
let v10 = v1 + 2.  // Add scalar to vector
let v11 = v1 - 2.  // Subtract scalar from vector
let v12 = 2. - v1  // Subtract each element of vector from scalar
let v13 = v1 * 2.  // Vector-scalar multiplication
let v14 = v1 / 2.  // Vector-scalar division
let v15 = 2. / v1  // Divides each element of vector by scalar
let v16 = -v1      // Unary negation
val v4 : Vector<float> = Vector [|2.0; 3.0; 4.0|]
val v5 : Vector<float> = Vector [|0.0; 1.0; 2.0|]
val v6 : float = 6.0
val v7 : Vector<float> = Vector [|-1.0; 2.0; -1.0|]
val v8 : Vector<float> = Vector [|1.0; 2.0; 3.0|]
val v9 : Vector<float> = Vector [|1.0; 2.0; 3.0|]
val v10 : Vector<float> = Vector [|3.0; 4.0; 5.0|]
val v11 : Vector<float> = Vector [|-1.0; 0.0; 1.0|]
val v12 : Vector<float> = Vector [|1.0; 0.0; -1.0|]
val v13 : Vector<float> = Vector [|2.0; 4.0; 6.0|]
val v14 : Vector<float> = Vector [|0.5; 1.0; 1.5|]
val v15 : Vector<float> = Vector [|2.0; 1.0; 0.6666666667|]
val v16 : Vector<float> = Vector [|-1.0; -2.0; -3.0|]

Vector Norms

1: 
2: 
3: 
4: 
let n1 = Vector.l1norm v1    // L1 norm
let n2 = Vector.l2norm v1    // L2 norm
let n3 = Vector.l2normSq v1  // Squared L2 norm
let n4 = Vector.lpnorm 3. v1 // Lp norm
val n1 : float = 6.0
val n2 : float = 3.741657387
val n3 : float = 14.0
val n4 : float = 3.301927249

Accessing Elements & Conversions

 1: 
 2: 
 3: 
 4: 
 5: 
 6: 
 7: 
 8: 
 9: 
10: 
let e1 = v1.[0]          // 1st element of v1
let e2 = Vector.get v1 1 // 2nd element of v1
let e3 = v1.[..1]        // Slice, until 2nd element
let e4 = v1.[1..2]       // Slice, between 2nd and 3rd elements
let v20 = Vector.ofSeq [1.; 2.; 3.] // Convert sequence to vector
let v21 = vector [1.; 2.; 3.]       // Same with above, supports lists,
let v22 = vector [|1.; 2.; 3.|]     // arrays,
let v23 = vector (seq {yield 1.})   // and sequences.
let aa1 = Vector.toArray v1 // Convert vector to array
let sq1 = Vector.toSeq v1   // Convert vector to sequence
val e1 : float = 1.0
val e2 : float = 2.0
val e3 : Vector<float> = Vector [|1.0; 2.0|]
val e4 : Vector<float> = Vector [|2.0; 3.0|]
val v20 : Vector<float> = Vector [|1.0; 2.0; 3.0|]
val v21 : Vector<float> = Vector [|1.0; 2.0; 3.0|]
val v22 : Vector<float> = Vector [|1.0; 2.0; 3.0|]
val v23 : Vector<float> = Vector [|1.0|]
val aa1 : float [] = [|1.0; 2.0; 3.0|]
val sq1 : seq<float>

Mutating/Replacing Elements

1: 
2: 
3: 
4: 
5: 
6: 
v1.[0] <- 4. // Mutate an element
Vector.replace (fun x -> x + 2.) v1 // Replace by mapping a function, mutating in place
Vector.replacei (fun i x -> x + float i) v1 // Replace in place, with index
Vector.replace2 (fun x y -> x - y) v1 v2 // Replace v1 in place, using a function of v1 and v2
Vector.replacei2 (fun i x y -> x - y + float i) v1 v2 // Replace v1 in place, with index
Vector.replaceWith v1 v2 // Replace elements of v1 with v2, mutating in place

Splitting and Concatenating

1: 
2: 
3: 
let ss1 = Vector.splitEqual 3 (vector [1.; 2.; 3.; 4.; 5.; 6.]) // Split into 3 vectors of equal length
let ss2 = Vector.split [2; 4] (vector [1.; 2.; 3.; 4.; 5.; 6.]) // Split into vectors of given lengths
let cc1 = Vector.concat ss1 // Concatenate vectors into one
val ss1 : seq<Vector<float>>
val ss2 : seq<Vector<float>>
val cc1 : Vector<float> = Vector [|1.0; 2.0; 3.0; 4.0; 5.0; 6.0|]

Mathematica and MATLAB Strings

You can generate string representations of vectors that you can copy and paste into Mathematica notebooks or MATLAB.

1: 
2: 
let s1 = v1.ToMathematicaString()
let s2 = v1.ToMatlabString()
val s1 : string = "{1.00, 2.00, 3.00}"
val s2 : string = "[1.00 2.00 3.00]"

Other Operations

The following are just a selection of other operations. Please refer to API Reference for a full list of supported operations.

1: 
2: 
3: 
4: 
5: 
6: 
7: 
let len = Vector.length v1 // Length of vector
let min = Vector.min v1  // Minimum element of vector
let max = Vector.max v1  // Maximum element of vector
let sum = Vector.sum v1  // Sum elements of vector
let v17 = Vector.unitVector v1 // Get unit vector codirectional with v1
let v18 = Vector.map (fun x -> sin x) v1 // Map function to vector
let v19 = Vector.fold (fun s x -> s + sin x) 0. v1 // Fold a vector
val len : int = 3
val min : float = 1.0
val max : float = 3.0
val sum : float = 6.0
val v17 : Vector<float> = Vector [|0.2672612419; 0.5345224838; 0.8017837257|]
val v18 : Vector<float> = Vector [|0.8414709848; 0.9092974268; 0.1411200081|]
val v19 : float = 1.89188842
namespace FsAlg
namespace FsAlg.Generic
val v1 : Vector<float>

Full name: Vector-operations.v1
val vector : v:seq<'T> -> Vector<'T> (requires member get_Zero and member get_One and member ( + ) and member ( - ) and member ( * ) and member ( / ) and member ( ~- ) and member Abs and member Pow and member Sqrt and member op_Explicit and comparison)

Full name: FsAlg.Generic.Ops.vector
val v2 : Vector<float>

Full name: Vector-operations.v2
Multiple items
union case Vector.Vector: 'T [] -> Vector<'T>

--------------------
module Vector

from FsAlg.Generic

--------------------
type Vector<'T (requires member get_Zero and member get_One and member ( + ) and member ( - ) and member ( * ) and member ( / ) and member ( ~- ) and member Abs and member Pow and member Sqrt and member op_Explicit and comparison)> =
  | ZeroVector of 'T
  | Vector of 'T []
  member Convert : f:('T -> 'a) -> Vector<'a> (requires member get_Zero and member get_One and member ( + ) and member ( - ) and member ( * ) and member ( / ) and member ( ~- ) and member Abs and member Pow and member Sqrt and member op_Explicit and comparison)
  member Copy : unit -> Vector<'T>
  member GetEnumerator : unit -> IEnumerator<'T>
  member GetL1Norm : unit -> 'T
  member GetL2Norm : unit -> 'T
  member GetL2NormSq : unit -> 'T
  member GetLPNorm : p:'T -> 'T
  member GetMax : unit -> 'T
  member GetMaxBy : f:('T -> 'a1) -> 'T (requires comparison)
  member GetMin : unit -> 'T
  member GetMinBy : f:('T -> 'a1) -> 'T (requires comparison)
  member GetSlice : lower:int option * upper:int option -> Vector<'T>
  member GetSubVector : s:int * c:int -> Vector<'T>
  member GetUnitVector : unit -> Vector<'T>
  member Split : n:seq<int> -> seq<Vector<'T>>
  member SplitEqual : n:int -> seq<Vector<'T>>
  member ToArray : unit -> 'T []
  member ToMathematicaString : unit -> string
  member ToMatlabString : unit -> string
  member ToSeq : unit -> seq<'T>
  member FirstItem : 'T
  member Item : i:int -> 'T with get
  member Length : int
  member Item : i:int -> 'T with set
  static member Zero : Vector<'T>
  static member ( + ) : a:'T * b:Vector<'T> -> Vector<'T>
  static member ( + ) : a:Vector<'T> * b:'T -> Vector<'T>
  static member ( + ) : a:Vector<'T> * b:Vector<'T> -> Vector<'T>
  static member ( / ) : a:'T * b:Vector<'T> -> Vector<'T>
  static member ( / ) : a:Vector<'T> * b:'T -> Vector<'T>
  static member ( ./ ) : a:Vector<'T> * b:Vector<'T> -> Vector<'T>
  static member ( .* ) : a:Vector<'T> * b:Vector<'T> -> Vector<'T>
  static member op_Explicit : v:Vector<'T> -> float []
  static member ( * ) : a:'T * b:Vector<'T> -> Vector<'T>
  static member ( * ) : a:Vector<'T> * b:'T -> Vector<'T>
  static member ( * ) : a:Vector<'T> * b:Vector<'T> -> 'T
  static member ( %* ) : a:Vector<'T> * b:Vector<'T> -> Vector<'T>
  static member ( - ) : a:'T * b:Vector<'T> -> Vector<'T>
  static member ( - ) : a:Vector<'T> * b:'T -> Vector<'T>
  static member ( - ) : a:Vector<'T> * b:Vector<'T> -> Vector<'T>
  static member ( ~- ) : a:Vector<'T> -> Vector<'T>

Full name: FsAlg.Generic.Vector<_>
val create : n:int -> v:'T -> Vector<'T> (requires member get_Zero and member get_One and member ( + ) and member ( - ) and member ( * ) and member ( / ) and member ( ~- ) and member Abs and member Pow and member Sqrt and member op_Explicit and comparison)

Full name: FsAlg.Generic.Vector.create
val v3 : Vector<float>

Full name: Vector-operations.v3
val init : n:int -> f:(int -> 'T) -> Vector<'T> (requires member get_Zero and member get_One and member ( + ) and member ( - ) and member ( * ) and member ( / ) and member ( ~- ) and member Abs and member Pow and member Sqrt and member op_Explicit and comparison)

Full name: FsAlg.Generic.Vector.init
val i : int
val exp : value:'T -> 'T (requires member Exp)

Full name: Microsoft.FSharp.Core.Operators.exp
Multiple items
val float : value:'T -> float (requires member op_Explicit)

Full name: Microsoft.FSharp.Core.Operators.float

--------------------
type float = System.Double

Full name: Microsoft.FSharp.Core.float

--------------------
type float<'Measure> = float

Full name: Microsoft.FSharp.Core.float<_>
val printf : format:Printf.TextWriterFormat<'T> -> 'T

Full name: Microsoft.FSharp.Core.ExtraTopLevelOperators.printf
val v4 : Vector<float>

Full name: Vector-operations.v4
val v5 : Vector<float>

Full name: Vector-operations.v5
val v6 : float

Full name: Vector-operations.v6
val v7 : Vector<float>

Full name: Vector-operations.v7
val v8 : Vector<float>

Full name: Vector-operations.v8
val v9 : Vector<float>

Full name: Vector-operations.v9
val v10 : Vector<float>

Full name: Vector-operations.v10
val v11 : Vector<float>

Full name: Vector-operations.v11
val v12 : Vector<float>

Full name: Vector-operations.v12
val v13 : Vector<float>

Full name: Vector-operations.v13
val v14 : Vector<float>

Full name: Vector-operations.v14
val v15 : Vector<float>

Full name: Vector-operations.v15
val v16 : Vector<float>

Full name: Vector-operations.v16
val n1 : float

Full name: Vector-operations.n1
val l1norm : v:Vector<'T> -> 'T (requires member get_Zero and member get_One and member ( + ) and member ( - ) and member ( * ) and member ( / ) and member ( ~- ) and member Abs and member Pow and member Sqrt and member op_Explicit and comparison)

Full name: FsAlg.Generic.Vector.l1norm
val n2 : float

Full name: Vector-operations.n2
val l2norm : v:Vector<'T> -> 'T (requires member get_Zero and member get_One and member ( + ) and member ( - ) and member ( * ) and member ( / ) and member ( ~- ) and member Abs and member Pow and member Sqrt and member op_Explicit and comparison)

Full name: FsAlg.Generic.Vector.l2norm
val n3 : float

Full name: Vector-operations.n3
val l2normSq : v:Vector<'T> -> 'T (requires member get_Zero and member get_One and member ( + ) and member ( - ) and member ( * ) and member ( / ) and member ( ~- ) and member Abs and member Pow and member Sqrt and member op_Explicit and comparison)

Full name: FsAlg.Generic.Vector.l2normSq
val n4 : float

Full name: Vector-operations.n4
val lpnorm : p:'T -> v:Vector<'T> -> 'T (requires member get_Zero and member get_One and member ( + ) and member ( - ) and member ( * ) and member ( / ) and member ( ~- ) and member Abs and member Pow and member Sqrt and member op_Explicit and comparison)

Full name: FsAlg.Generic.Vector.lpnorm
val e1 : float

Full name: Vector-operations.e1
val e2 : float

Full name: Vector-operations.e2
val get : v:Vector<'T> -> i:int -> 'T (requires member get_Zero and member get_One and member ( + ) and member ( - ) and member ( * ) and member ( / ) and member ( ~- ) and member Abs and member Pow and member Sqrt and member op_Explicit and comparison)

Full name: FsAlg.Generic.Vector.get
val e3 : Vector<float>

Full name: Vector-operations.e3
val e4 : Vector<float>

Full name: Vector-operations.e4
val v20 : Vector<float>

Full name: Vector-operations.v20
val ofSeq : s:seq<'T> -> Vector<'T> (requires member get_Zero and member get_One and member ( + ) and member ( - ) and member ( * ) and member ( / ) and member ( ~- ) and member Abs and member Pow and member Sqrt and member op_Explicit and comparison)

Full name: FsAlg.Generic.Vector.ofSeq
val v21 : Vector<float>

Full name: Vector-operations.v21
val v22 : Vector<float>

Full name: Vector-operations.v22
val v23 : Vector<float>

Full name: Vector-operations.v23
Multiple items
val seq : sequence:seq<'T> -> seq<'T>

Full name: Microsoft.FSharp.Core.Operators.seq

--------------------
type seq<'T> = System.Collections.Generic.IEnumerable<'T>

Full name: Microsoft.FSharp.Collections.seq<_>
val aa1 : float []

Full name: Vector-operations.aa1
val toArray : v:Vector<'T> -> 'T [] (requires member get_Zero and member get_One and member ( + ) and member ( - ) and member ( * ) and member ( / ) and member ( ~- ) and member Abs and member Pow and member Sqrt and member op_Explicit and comparison)

Full name: FsAlg.Generic.Vector.toArray
val sq1 : seq<float>

Full name: Vector-operations.sq1
val toSeq : v:Vector<'T> -> seq<'T> (requires member get_Zero and member get_One and member ( + ) and member ( - ) and member ( * ) and member ( / ) and member ( ~- ) and member Abs and member Pow and member Sqrt and member op_Explicit and comparison)

Full name: FsAlg.Generic.Vector.toSeq
val replace : f:('T -> 'T) -> v:Vector<'T> -> unit (requires member get_Zero and member get_One and member ( + ) and member ( - ) and member ( * ) and member ( / ) and member ( ~- ) and member Abs and member Pow and member Sqrt and member op_Explicit and comparison)

Full name: FsAlg.Generic.Vector.replace
val x : float
val replacei : f:(int -> 'T -> 'T) -> v:Vector<'T> -> unit (requires member get_Zero and member get_One and member ( + ) and member ( - ) and member ( * ) and member ( / ) and member ( ~- ) and member Abs and member Pow and member Sqrt and member op_Explicit and comparison)

Full name: FsAlg.Generic.Vector.replacei
val replace2 : f:('T1 -> 'T2 -> 'T1) -> v1:Vector<'T1> -> v2:Vector<'T2> -> unit (requires member get_Zero and member get_One and member ( + ) and member ( - ) and member ( * ) and member ( / ) and member ( ~- ) and member Abs and member Pow and member Sqrt and member op_Explicit and comparison and member get_Zero and member get_One and member ( + ) and member ( - ) and member ( * ) and member ( / ) and member ( ~- ) and member Abs and member Pow and member Sqrt and member op_Explicit and comparison)

Full name: FsAlg.Generic.Vector.replace2
val y : float
val replacei2 : f:(int -> 'T1 -> 'T2 -> 'T1) -> v1:Vector<'T1> -> v2:Vector<'T2> -> unit (requires member get_Zero and member get_One and member ( + ) and member ( - ) and member ( * ) and member ( / ) and member ( ~- ) and member Abs and member Pow and member Sqrt and member op_Explicit and comparison and member get_Zero and member get_One and member ( + ) and member ( - ) and member ( * ) and member ( / ) and member ( ~- ) and member Abs and member Pow and member Sqrt and member op_Explicit and comparison)

Full name: FsAlg.Generic.Vector.replacei2
val replaceWith : v1:Vector<'T> -> v2:Vector<'T> -> unit (requires member get_Zero and member get_One and member ( + ) and member ( - ) and member ( * ) and member ( / ) and member ( ~- ) and member Abs and member Pow and member Sqrt and member op_Explicit and comparison)

Full name: FsAlg.Generic.Vector.replaceWith
val ss1 : seq<Vector<float>>

Full name: Vector-operations.ss1
val splitEqual : n:int -> v:Vector<'T> -> seq<Vector<'T>> (requires member get_Zero and member get_One and member ( + ) and member ( - ) and member ( * ) and member ( / ) and member ( ~- ) and member Abs and member Pow and member Sqrt and member op_Explicit and comparison)

Full name: FsAlg.Generic.Vector.splitEqual
val ss2 : seq<Vector<float>>

Full name: Vector-operations.ss2
val split : n:seq<int> -> v:Vector<'T> -> seq<Vector<'T>> (requires member get_Zero and member get_One and member ( + ) and member ( - ) and member ( * ) and member ( / ) and member ( ~- ) and member Abs and member Pow and member Sqrt and member op_Explicit and comparison)

Full name: FsAlg.Generic.Vector.split
val cc1 : Vector<float>

Full name: Vector-operations.cc1
val concat : v:seq<Vector<'T>> -> Vector<'T> (requires member get_Zero and member get_One and member ( + ) and member ( - ) and member ( * ) and member ( / ) and member ( ~- ) and member Abs and member Pow and member Sqrt and member op_Explicit and comparison)

Full name: FsAlg.Generic.Vector.concat
val s1 : string

Full name: Vector-operations.s1
member Vector.ToMathematicaString : unit -> string
val s2 : string

Full name: Vector-operations.s2
member Vector.ToMatlabString : unit -> string
val len : int

Full name: Vector-operations.len
val length : v:Vector<'T> -> int (requires member get_Zero and member get_One and member ( + ) and member ( - ) and member ( * ) and member ( / ) and member ( ~- ) and member Abs and member Pow and member Sqrt and member op_Explicit and comparison)

Full name: FsAlg.Generic.Vector.length
val min : float

Full name: Vector-operations.min
val min : v:Vector<'T> -> 'T (requires member get_Zero and member get_One and member ( + ) and member ( - ) and member ( * ) and member ( / ) and member ( ~- ) and member Abs and member Pow and member Sqrt and member op_Explicit and comparison)

Full name: FsAlg.Generic.Vector.min
val max : float

Full name: Vector-operations.max
val max : v:Vector<'T> -> 'T (requires member get_Zero and member get_One and member ( + ) and member ( - ) and member ( * ) and member ( / ) and member ( ~- ) and member Abs and member Pow and member Sqrt and member op_Explicit and comparison)

Full name: FsAlg.Generic.Vector.max
val sum : float

Full name: Vector-operations.sum
val sum : v:Vector<'T> -> 'T (requires member get_Zero and member get_One and member ( + ) and member ( - ) and member ( * ) and member ( / ) and member ( ~- ) and member Abs and member Pow and member Sqrt and member op_Explicit and comparison)

Full name: FsAlg.Generic.Vector.sum
val v17 : Vector<float>

Full name: Vector-operations.v17
val unitVector : v:Vector<'T> -> Vector<'T> (requires member get_Zero and member get_One and member ( + ) and member ( - ) and member ( * ) and member ( / ) and member ( ~- ) and member Abs and member Pow and member Sqrt and member op_Explicit and comparison)

Full name: FsAlg.Generic.Vector.unitVector
val v18 : Vector<float>

Full name: Vector-operations.v18
val map : f:('T -> 'U) -> v:Vector<'T> -> Vector<'U> (requires member get_Zero and member get_One and member ( + ) and member ( - ) and member ( * ) and member ( / ) and member ( ~- ) and member Abs and member Pow and member Sqrt and member op_Explicit and comparison and member get_Zero and member get_One and member ( + ) and member ( - ) and member ( * ) and member ( / ) and member ( ~- ) and member Abs and member Pow and member Sqrt and member op_Explicit and comparison)

Full name: FsAlg.Generic.Vector.map
val sin : value:'T -> 'T (requires member Sin)

Full name: Microsoft.FSharp.Core.Operators.sin
val v19 : float

Full name: Vector-operations.v19
val fold : f:('S -> 'T -> 'S) -> s:'S -> v:Vector<'T> -> 'S (requires member get_Zero and member get_One and member ( + ) and member ( - ) and member ( * ) and member ( / ) and member ( ~- ) and member Abs and member Pow and member Sqrt and member op_Explicit and comparison)

Full name: FsAlg.Generic.Vector.fold
val s : float