Using vectors in TiPi
This section describes the objects used in TiPi to store the variables (but also the data and their weights) of an optimization problem.
Operations involving vector spaces
Vectors can be created by their vector space with either undefined contents of
with their components set to given values. For instance, to create a new
vector of the vector space vsp:
vsp.create() // yields a new vector with undefined contents
vsp.create(val) // yields a new vector with all components set to `val`
vsp.one() // is equivalent to space.create(1)
vsp.zero() // is equivalent to space.create(0)
To check whether a vector vec belongs to the vector space vsp, the
following expressions can be used:
vsp.owns(vec)
vec.belongsTo(vsp)
vec.getOwner() == vsp
The call:
vsp.check(vec)
checks whether vector vec belongs to the vector space vsp and throws an
IncorrectSpaceException exception otherwise.
Methods implemented by vectors
Many methods are available to directly manipulate vectors. These methods are sufficient to implement the iterative optimization algorithms provided by TiPi.
The following methods are assumed to be efficient:
-
vec.create()yields a new vector of the same vector space asvecwith undefined contents. -
vec.clone()yields a new vector of the same vector space asvecand with all its components set to the corresponding values of the components ofvec. -
dst.copy(src)copies the values of the components of vectorsrcinto vectordst. -
vec.norm2()yields the Euclidean norm ofvecwhich is the square root of the sum of the squared value of its components. -
vec.norm1()yields the L1 norm ofvecwhich is the sum of the absolute value of its components. -
vec.norm2()yields the infinite norm ofvecwhich is the maximum absolute value of its components. -
x.dot(y)yields the dot product of vectorsxandy. -
w.dot(x,y)yields the dot product of vectorsxandyweighted byw. This dot product can be expressed asx'.W.ywhereW = diag(w)is a diagonal weighting operator. -
x.swap(y)exchanges the values of the corresponding components of vectorsxandy. -
vec.fill(val)set all components of vectorvecto the valueval. -
vec.zero()is the same asx.fill(0)
Various linear combination of vectors can be formed:
-
dst.add(alpha, x)add to the components ofdstthe components of the vectorxmultiplied by the scalaralpha. -
dst.combine(alpha, x, beta, y)stores indstthe sum ofalphatimesxplusbetatimesy. -
dst.combine(alpha, x, beta, y, gamma, z) -
vec.scale(val)scales all the components of the vectorvecby the scalaralpha -
dst.scale(alpha, src)stores in vectordstthe result of scaling vectorsrcby the scalaralpha. -
dst.multiply(w)stores in vectordstthe result of the component-wise multiplication ofdstby the vectorw. -
dst.multiply(w, x)stores in vectordstthe result of the component-wise multiplication of vectorswandx.
The above methods manipulate vectors globally are supposed to be efficient. Two methods are provided to examine or set individually the components of a vector. These methods are mainly used for debugging or informative purposes and are not meant to be efficient (although it does not hurt if they are!).
-
vec.get(i)get the value ofi-th component of vectorvec. -
vec.set(i, val)set thei-th component of vectorvecto the valueval.
where index i is an integer between 0 for the first component and n - 1
for the last component (n being the number of components of the vector).
-
vec.getNumber()andvec.length()yields the number of components of the vectorvec. -
vec.getOwner()andvec.getSpace()yields the vector space to which belongs vectorvec.