Methods for vectors
A vector is that which has the algebra of a vector space (Peano 1888, van der Waerden 1931). See talk by Jiahao Chen: Taking Vector Transposes Seriously at JuliaCon 2017.
Vectorized methods
Most necessary operations on the variables of interest are linear operations. Hence variables (whatever their specific type and size) are just called vectors in LazyAlgebra
. Numerical methods based on LazyAlgebra
manipulate the variables via a small number of vectorized methods:
vdot([T,][w,]x,y)
yields the inner product ofx
andy
; that is, the sum ofconj(x[i])*y[i]
or, ifw
is specified, the sum ofw[i]*conj(x[i])*y[i]
, for all indicesi
. Optional argumentT
is the type of the result; for real valued vectors,T
is a floating-point type; for complex valued vectors,T
can be a complex type (with floating-point parts) or a floating-point type to compute only the real part of the inner product.vdot([T,]sel,x,y)
yields the sum ofx[i]*y[i]
for alli ∈ sel
wheresel
is a selection of indices.vnorm1([T,]x)
yields the L-1 norm ofx
, that is the sum of the absolute values of the components ofx
. Optional argumentT
is the floating-point type of the result.vnorm2([T,]x)
yields the Euclidean (or L-2) norm ofx
, that is the square root of sum of the squared values of the components ofx
. Optional argumentT
is the floating-point type of the result.vnorminf([T,]x)
L-∞ norm ofx
, that is the maximal absolute values of the components ofx
. Optional argumentT
is the floating-point type of the resultvcreate(x)
yields a new variable instance similar tox
. Ifx
is an array, the element type of the result is a floating-point type.vcopy!(dst,src)
copies the contents ofsrc
intodst
and returnsdst
.vcopy(x)
yields a fresh copy of the vectorx
.vswap!(x,y)
exchanges the contents ofx
andy
(which must have the same type and size if they are arrays).vfill!(x,α)
sets all elements ofx
with the scalar valueα
and returnx
.vzero!(x)
fillsx
with zeros and returns it.vscale!(dst,α,src)
overwritesdst
withα*src
and returnsdst
. The convention is that, ifα = 0
, thendst
is filled with zeros whatever the contents ofsrc
.vscale!(x,α)
andvscale!(α,x)
overwritex
withα*x
and returnsx
. The convention is that, ifα = 0
, thenx
is filled with zeros whatever its prior contents.vscale(α,x)
andvscale(x,α)
yield a new vector whose elements are those ofx
multiplied by the scalarα
.vproduct!(dst,[sel,]x,y)
overwritesdst
with the elementwise multiplication ofx
byy
. Optional argumentsel
is a selection of indices to consider.vproduct(x,y)
yields the elementwise multiplication ofx
byy
.vupdate!(y,[sel,]α,x)
overwritesy
withα*x + y
and returnsy
. Optional argumentsel
is a selection of indices to which apply the operation (if an index is repeated, the operation will be performed several times at this location).vcombine(α,x,β,y)
yields the linear combinationα*x
orα*x + β*y
.vcombine!(dst,α,x,β,y)
overwritesdst
with the linear combinationdst = α*x
ordst = α*x + β*y
and returnsdst
.
Note that the names of these methods all start with a v
(for vector) as the conventions used by these methods may be particular. For instance, compared to copy!
and when applied to arrays, vcopy!
imposes that the two arguments have exactly the same dimensions. Another example is the vdot
method which has a slightly different semantics than Julia dot
method.
LazyAlgebra
already provides implementations of these methods for Julia arrays with floating-point type elements. This implementation assumes that an array is a valid vector providing it has suitable type and dimensions.
Implementing a new vector type
To have a numerical method based on LazyAlgebra
be applicable to a new given type of variables, it is sufficient to implement a subset of these basic methods specialized for this kind of variables.
The various operations that should be implemented for a vector are:
- compute the inner product of two vectors of the same kind (
vdot(x,y)
method); - create a vector of a given kind (
vcreate(x)
method); - copy a vector (
vcopy!(dst,src)
); - fill a vector with a given value (
vfill!(x,α)
method); - exchange the contents of two vectors (
vswap!(x,y)
method); - linearly combine several vectors (
vcombine!(dst,α,x,β,y)
method).
Derived methods are:
- compute the Euclidean norm of a vector (
vnorm2
method, based onvdot
by default); - multiply a vector by a scalar:
vscale!(dst,α,src)
and/orvscale!(x,α)
methods (based onvcombine!
by default); - update a vector by a scaled step:
vupdate!(y,α,x)
method (based onvcombine!
by default) and, for some constrained optimization methods,vupdate!(y,sel,α,x)
method; - erase a vector:
vzero!(x)
method (based onvfill!
by default); vscale
andvcopy
methods are implemented withvcreate
and respectivelyvscale!
andvcopy!
.
Other methods which may be required by some packages:
- compute the L-1 norm of a vector:
vnorm1(x)
method; - compute the L-∞ norm of a vector:
vnorminf(x)
method;
Methods that must be implemented (V
represent the vector type):
vdot(x::V, y::V)
vscale!(dst::V, alpha::Real, src::V) -> dst
methods that may be implemented:
vscale!(alpha::Real, x::V) -> x
For mappings and linear operators (see Implementation of new mappings for details), implement:
apply!(α::Scalar, P::Type{<:Operations}, A::Ta, x::Tx, β::Scalar, y::Ty) -> y
and
vcreate(P::Type{P}, A::Ta, x::Tx) -> y
for Ta<:Mapping
and the supported operations P<:Operations
.