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 ofxandy; that is, the sum ofconj(x[i])*y[i]or, ifwis specified, the sum ofw[i]*conj(x[i])*y[i], for all indicesi. Optional argumentTis the type of the result; for real valued vectors,Tis a floating-point type; for complex valued vectors,Tcan 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 ∈ selwhereselis 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 argumentTis 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 argumentTis the floating-point type of the result.vnorminf([T,]x)L-∞ norm ofx, that is the maximal absolute values of the components ofx. Optional argumentTis the floating-point type of the resultvcreate(x)yields a new variable instance similar tox. Ifxis an array, the element type of the result is a floating-point type.vcopy!(dst,src)copies the contents ofsrcintodstand returnsdst.vcopy(x)yields a fresh copy of the vectorx.vswap!(x,y)exchanges the contents ofxandy(which must have the same type and size if they are arrays).vfill!(x,α)sets all elements ofxwith the scalar valueαand returnx.vzero!(x)fillsxwith zeros and returns it.vscale!(dst,α,src)overwritesdstwithα*srcand returnsdst. The convention is that, ifα = 0, thendstis filled with zeros whatever the contents ofsrc.vscale!(x,α)andvscale!(α,x)overwritexwithα*xand returnsx. The convention is that, ifα = 0, thenxis filled with zeros whatever its prior contents.vscale(α,x)andvscale(x,α)yield a new vector whose elements are those ofxmultiplied by the scalarα.vproduct!(dst,[sel,]x,y)overwritesdstwith the elementwise multiplication ofxbyy. Optional argumentselis a selection of indices to consider.vproduct(x,y)yields the elementwise multiplication ofxbyy.vupdate!(y,[sel,]α,x)overwritesywithα*x + yand returnsy. Optional argumentselis 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α*xorα*x + β*y.vcombine!(dst,α,x,β,y)overwritesdstwith the linear combinationdst = α*xordst = α*x + β*yand 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 (
vnorm2method, based onvdotby 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); vscaleandvcopymethods are implemented withvcreateand 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) -> dstmethods that may be implemented:
vscale!(alpha::Real, x::V) -> xFor mappings and linear operators (see Implementation of new mappings for details), implement:
apply!(α::Scalar, P::Type{<:Operations}, A::Ta, x::Tx, β::Scalar, y::Ty) -> yand
vcreate(P::Type{P}, A::Ta, x::Tx) -> yfor Ta<:Mapping and the supported operations P<:Operations.