Vectors
This library provides support for creating and manipulating vectors of real numbers, including some math functions on vectors.
Basic Usage
val v = Mlvector.make (5, 0.0)
(* v is a vector of length 5 containing all 0s. *)
val _ = Mlvector.set (v, 0, 2.0)
(* v now has a 2.0 at index 0. *)
val v2 = Mlvector.fromList [1.0, 2.0, 3.0, 4.0, 5.0]
val d = Mlvector.dot (v2, v2)
(* d is 55.0, the result of taking the dot product of v2 and itself *)
Interface
Types
type scalar = real- The
scalartype is a synonym forreal.
- The
type mlvector- The type of a vector.
Methods
val size : mlvector -> intval make : (int * scalar) -> mlvectorval makeInit : (int -> scalar) * int -> mlvectorval sub : mlvector * int -> scalarval set : mlvector * int * scalar -> unitval update : (scalar -> scalar) * mlvector * int -> unitval modify : (scalar -> scalar) * mlvector -> unitval modifyi : ((int * scalar) -> scalar) * mlvector -> unitval foldl : (('a * real) -> 'a) * 'a * mlvector -> 'aval foldli : (('a * int * real) -> 'a) * 'a * mlvector -> 'aval copy : mlvector -> mlvectorval squeeze : mlvector -> scalarval map : (scalar -> scalar) * mlvector -> mlvectorval mapi : ((int * scalar) -> scalar) * mlvector -> mlvectorval map2: ((scalar * scalar) -> scalar) * mlvector * mlvector -> mlvectorval elemwise : mlvector * mlvector -> mlvectorval add : mlvector * mlvector -> mlvectorval addModify : mlvector * mlvector -> unitval mulScalar : mlvector * scalar -> mlvectorval dot : mlvector * mlvector -> realval toStringF : (scalar -> string) * mlvector -> stringval toString : mlvector -> stringval fromList : real list -> mlvector
Method Overview
size vec- Returns the length of the vector.
make (len, default)- Creates a vector of length
lenwith every value set todefault.
- Creates a vector of length
makeInit (f, len)- Creates a vector of length
lenwith the value at indexiset tof i.
- Creates a vector of length
sub (vec, n)- Returns the value at index
nin the vector.
- Returns the value at index
set (vec, n, r)- Sets the value at index
ninvecto ber(e.g.,vec[n] := r). This modifiesvec.
- Sets the value at index
update (f, vec, n).- Sets the value at index
ninvecto be the result of applyingfto the original value at that index (e.g.,vec[n] := f(vec[n])). This modifiesvec.
- Sets the value at index
modify (f, vec)- Sets the value at each index in
vecto be the result of applyingfto the original value. This modifiesvec.
- Sets the value at each index in
modifyi (f, vec)- Like modify, but
falso take the index as input.
- Like modify, but
A foldl(((A, real) -> A) f, A default, mlvector vec). Fold a function over the elements of a vector.-
A foldli(((A, int, real) -> A) f, A default, mlvector vec). Like fold, but the recursive function also takes indices as input. copy vec- Creates a new vector with the same contents as
vec.
- Creates a new vector with the same contents as
squeeze vec- Returns the value at the 0th index in
vec.
- Returns the value at the 0th index in
map (f, vec)- Creates a new vector where the value at each index is the result of applying
fto the value at the same index invec.
- Creates a new vector where the value at each index is the result of applying
mapi (f, vec)- Like map, but
falso takes the index as input.
- Like map, but
map2 (f, vec1, vec2)- Creates a new vector where the value at each index is the result of applying
fto the pair of values at the same index invec1andvec2. The resulting vector is the same length as the smaller ofvec1andvec2.
- Creates a new vector where the value at each index is the result of applying
elemwise (vec1, vec2)- Creates a new vector whose contents are the element-wise product of
vec1andvec2.
- Creates a new vector whose contents are the element-wise product of
add (vec1, vec2)- Creates a new vector whose contents are the element-wise sum of
vec1andvec2.
- Creates a new vector whose contents are the element-wise sum of
addModify (vec1, vec2).- Updates
vec1so that it is contains the element-wise sum of the original value ofvec1andvec2. This modifiesvec1.
- Updates
mulScalar (vec, a)- Creates a new vector where each element is equal to the product of
aand the element of the same index invec.
- Creates a new vector where each element is equal to the product of
dot (vec1, vec2)- Returns the dot product of
vec1andvec2.
- Returns the dot product of
toString vec- Converts
vecto a string.
- Converts
fromList lst- Creates a vector whose contents are the same as that of the list
lst.
- Creates a vector whose contents are the same as that of the list