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 asvec
with undefined contents. -
vec.clone()
yields a new vector of the same vector space asvec
and with all its components set to the corresponding values of the components ofvec
. -
dst.copy(src)
copies the values of the components of vectorsrc
into vectordst
. -
vec.norm2()
yields the Euclidean norm ofvec
which is the square root of the sum of the squared value of its components. -
vec.norm1()
yields the L1 norm ofvec
which is the sum of the absolute value of its components. -
vec.norm2()
yields the infinite norm ofvec
which is the maximum absolute value of its components. -
x.dot(y)
yields the dot product of vectorsx
andy
. -
w.dot(x,y)
yields the dot product of vectorsx
andy
weighted byw
. This dot product can be expressed asx'.W.y
whereW = diag(w)
is a diagonal weighting operator. -
x.swap(y)
exchanges the values of the corresponding components of vectorsx
andy
. -
vec.fill(val)
set all components of vectorvec
to 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 ofdst
the components of the vectorx
multiplied by the scalaralpha
. -
dst.combine(alpha, x, beta, y)
stores indst
the sum ofalpha
timesx
plusbeta
timesy
. -
dst.combine(alpha, x, beta, y, gamma, z)
-
vec.scale(val)
scales all the components of the vectorvec
by the scalaralpha
-
dst.scale(alpha, src)
stores in vectordst
the result of scaling vectorsrc
by the scalaralpha
. -
dst.multiply(w)
stores in vectordst
the result of the component-wise multiplication ofdst
by the vectorw
. -
dst.multiply(w, x)
stores in vectordst
the result of the component-wise multiplication of vectorsw
andx
.
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 vectorvec
to 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
.