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
scalar
type is a synonym forreal
.
- The
type mlvector
- The type of a vector.
Methods
val size : mlvector -> int
val make : (int * scalar) -> mlvector
val makeInit : (int -> scalar) * int -> mlvector
val sub : mlvector * int -> scalar
val set : mlvector * int * scalar -> unit
val update : (scalar -> scalar) * mlvector * int -> unit
val modify : (scalar -> scalar) * mlvector -> unit
val modifyi : ((int * scalar) -> scalar) * mlvector -> unit
val foldl : (('a * real) -> 'a) * 'a * mlvector -> 'a
val foldli : (('a * int * real) -> 'a) * 'a * mlvector -> 'a
val copy : mlvector -> mlvector
val squeeze : mlvector -> scalar
val map : (scalar -> scalar) * mlvector -> mlvector
val mapi : ((int * scalar) -> scalar) * mlvector -> mlvector
val map2: ((scalar * scalar) -> scalar) * mlvector * mlvector -> mlvector
val elemwise : mlvector * mlvector -> mlvector
val add : mlvector * mlvector -> mlvector
val addModify : mlvector * mlvector -> unit
val mulScalar : mlvector * scalar -> mlvector
val dot : mlvector * mlvector -> real
val toStringF : (scalar -> string) * mlvector -> string
val toString : mlvector -> string
val fromList : real list -> mlvector
Method Overview
size vec
- Returns the length of the vector.
make (len, default)
- Creates a vector of length
len
with every value set todefault
.
- Creates a vector of length
makeInit (f, len)
- Creates a vector of length
len
with the value at indexi
set tof i
.
- Creates a vector of length
sub (vec, n)
- Returns the value at index
n
in the vector.
- Returns the value at index
set (vec, n, r)
- Sets the value at index
n
invec
to ber
(e.g.,vec[n] := r
). This modifiesvec
.
- Sets the value at index
update (f, vec, n)
.- Sets the value at index
n
invec
to be the result of applyingf
to 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
vec
to be the result of applyingf
to the original value. This modifiesvec
.
- Sets the value at each index in
modifyi (f, vec)
- Like modify, but
f
also 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
f
to 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
f
also 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
f
to the pair of values at the same index invec1
andvec2
. The resulting vector is the same length as the smaller ofvec1
andvec2
.
- 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
vec1
andvec2
.
- 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
vec1
andvec2
.
- Creates a new vector whose contents are the element-wise sum of
addModify (vec1, vec2)
.- Updates
vec1
so that it is contains the element-wise sum of the original value ofvec1
andvec2
. This modifiesvec1
.
- Updates
mulScalar (vec, a)
- Creates a new vector where each element is equal to the product of
a
and 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
vec1
andvec2
.
- Returns the dot product of
toString vec
- Converts
vec
to 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