Types and methods
Types and their constructors
None of the types below need to be constructed directly; they arise from performing operations between LinearMap objects or by calling the LinearMap constructor described next.
LinearMap
Abstract supertype
LinearMaps.LinearMap — TypeLinearMap(A::LinearMap; kwargs...)::WrappedMap
LinearMap(A::AbstractMatrix; kwargs...)::WrappedMap
LinearMap(J::UniformScaling, M::Int)::UniformScalingMap
LinearMap{T=Float64}(f, [fc,], M::Int, N::Int = M; kwargs...)::FunctionMapConstruct a linear map object, either from an existing LinearMap or AbstractMatrix A, with the purpose of redefining its properties via the keyword arguments kwargs; a UniformScaling object J with specified (square) dimension M; from a Number object to lazily represent filled matrices; or from a function or callable object f. In the latter case, one also needs to specify the size of the equivalent matrix representation (M, N), i.e., for functions f acting on length N vectors and producing length M vectors (with default value N=M). Preferably, also the eltype T of the corresponding matrix representation needs to be specified, i.e. whether the action of f on a vector will be similar to, e.g., multiplying by numbers of type T. If not specified, the devault value T=Float64 will be assumed. Optionally, a corresponding function fc can be specified that implements the adjoint (=transpose in the real case) of f.
The keyword arguments and their default values for the function-based constructor are:
issymmetric::Bool = false: whetherAorfacts as a symmetric matrixishermitian::Bool = issymmetric & T<:Real: whetherAorfacts as a Hermitian matrixisposdef::Bool = false: whetherAorfacts as a positive definite matrix.
For existing linear maps or matrices A, the default values will be taken by calling issymmetric, ishermitian and isposdef on the existing object A.
For the function-based constructor, there is one more keyword argument:
ismutating::Bool: flags whether the function acts as a mutating matrix multiplicationf(y,x)where the result vectoryis the first argument (in case oftrue), or as a normal matrix multiplication that is called asy=f(x)(in case offalse). The default value is guessed by looking at the number of arguments of the first occurrence offin the method table.
FunctionMap
Type for wrapping an arbitrary function that is supposed to implement the matrix-vector product as a LinearMap; see above.
WrappedMap
Type for wrapping an AbstractMatrix or LinearMap and to possible redefine the properties isreal, issymmetric, ishermitian and isposdef. An AbstractMatrix will automatically be converted to a WrappedMap when it is combined with other LinearMap objects via linear combination or composition (multiplication). Note that WrappedMap(mat1)*WrappedMap(mat2) will never evaluate mat1*mat2, since this is more costly than evaluating mat1*(mat2*x) and the latter is the only operation that needs to be performed by LinearMap objects anyway. While the cost of matrix addition is comparable to matrix-vector multiplication, this too is not performed explicitly since this would require new storage of the same amount as of the original matrices.
ScaledMap
Type for representing a scalar multiple of any LinearMap type. A ScaledMap will be automatically constructed if real or complex LinearMap objects are multiplied by real or complex scalars from the left or from the right.
UniformScalingMap
Type for representing a scalar multiple of the identity map (a.k.a. uniform scaling) of a certain size M=N, obtained simply as LinearMap(λI, M), where I is the LinearAlgebra.UniformScaling object. The type T of the resulting LinearMap object is inferred from the type of λ. A UniformScalingMap of the correct size will be automatically constructed if LinearMap objects are multiplied by scalars from the left or from the right (respecting the order of multiplication), if the scalar λ is either real or complex.
LinearCombination, CompositeMap, TransposeMap and AdjointMap
Used to add/multiply/transpose/adjoint LinearMap objects lazily, don't need to be constructed explicitly.
Base.:+ — Method+(A::LinearMap, B::LinearMap)::LinearCombinationConstruct a (lazy) representation of the sum/linear combination of the two operators. Sums of LinearMap/LinearCombination objects and LinearMap/LinearCombination objects are reduced to a single LinearCombination. In sums of LinearMaps and AbstractMatrix/UniformScaling objects, the latter get promoted to LinearMaps automatically.
Examples
julia> CS = LinearMap{Int}(cumsum, 3)::LinearMaps.FunctionMap;
julia> LinearMap(ones(Int, 3, 3)) + CS + I + rand(3, 3);Base.:* — Method*(A::LinearMap, B::LinearMap)::CompositeMapConstruct a (lazy) representation of the product of the two operators. Products of LinearMap/CompositeMap objects and LinearMap/CompositeMap objects are reduced to a single CompositeMap. In products of LinearMaps and AbstractMatrix/UniformScaling objects, the latter get promoted to LinearMaps automatically.
Examples
julia> CS = LinearMap{Int}(cumsum, 3)::LinearMaps.FunctionMap;
julia> LinearMap(ones(Int, 3, 3)) * CS * I * rand(3, 3);Base.transpose — Methodtranspose(A::LinearMap)Construct a lazy representation of the transpose of A. This can be either a TransposeMap wrapper of A, or a suitably redefined instance of the same type as A. For instance, for a linear combination of linear maps $A + B$, the transpose is given by $A^⊤ + B^⊤$, i.e., another linear combination of linear maps.
Base.adjoint — Methodadjoint(A::LinearMap)Construct a lazy representation of the adjoint of A. This can be either a AdjointMap wrapper of A, or a suitably redefined instance of the same type as A. For instance, for a linear combination of linear maps $A + B$, the adjoint is given by $A^* + B^*$, i.e., another linear combination of linear maps.
KroneckerMap and KroneckerSumMap
Types for representing Kronecker products and Kronecker sums, resp., lazily.
Base.kron — Methodkron(A::LinearMap, B::LinearMap)::KroneckerMap
kron(A, B, Cs...)::KroneckerMapConstruct a (lazy) representation of the Kronecker product A⊗B. One of the two factors can be an AbstractMatrix, which is then promoted to a LinearMap automatically.
To avoid fallback to the generic Base.kron in the multi-map case, there must be a LinearMap object among the first 8 arguments in usage like kron(A, B, Cs...).
For convenience, one can also use A ⊗ B or ⊗(A, B, Cs...) (typed as \otimes+TAB) to construct the KroneckerMap, even when all arguments are of AbstractMatrix type.
If A, B, C and D are linear maps of such size that one can form the matrix products A*C and B*D, then the mixed-product property (A⊗B)*(C⊗D) = (A*C)⊗(B*D) holds. Upon vector multiplication, this rule is checked for applicability.
Examples
julia> J = LinearMap(I, 2) # 2×2 identity map
2×2 LinearMaps.UniformScalingMap{Bool} with scaling factor: true
julia> E = spdiagm(-1 => trues(1)); D = E + E' - 2I;
julia> Δ = kron(D, J) + kron(J, D); # discrete 2D-Laplace operator
julia> Matrix(Δ)
4×4 Array{Int64,2}:
-4 1 1 0
1 -4 0 1
1 0 -4 1
0 1 1 -4LinearMaps.:⊗ — Function⊗(k::Integer)Construct a lazy representation of the k-th Kronecker power A^⊗(k) = A ⊗ A ⊗ ... ⊗ A, where A can be an AbstractMatrix or a LinearMap.
LinearMaps.kronsum — Functionkronsum(A, B)::KroneckerSumMap
kronsum(A, B, Cs...)::KroneckerSumMapConstruct a (lazy) representation of the Kronecker sum A⊕B = A ⊗ Ib + Ia ⊗ B of two square linear maps of type LinearMap or AbstractMatrix. Here, Ia and Ib are identity operators of the size of A and B, respectively. Arguments of type AbstractMatrix are automatically promoted to LinearMap.
For convenience, one can also use A ⊕ B or ⊕(A, B, Cs...) (typed as \oplus+TAB) to construct the KroneckerSumMap.
Examples
julia> J = LinearMap(I, 2) # 2×2 identity map
2×2 LinearMaps.UniformScalingMap{Bool} with scaling factor: true
julia> E = spdiagm(-1 => trues(1)); D = LinearMap(E + E' - 2I);
julia> Δ₁ = kron(D, J) + kron(J, D); # discrete 2D-Laplace operator, Kronecker sum
julia> Δ₂ = kronsum(D, D);
julia> Δ₃ = D^⊕(2);
julia> Matrix(Δ₁) == Matrix(Δ₂) == Matrix(Δ₃)
trueLinearMaps.:⊕ — Function⊕(k::Integer)Construct a lazy representation of the k-th Kronecker sum power A^⊕(k) = A ⊕ A ⊕ ... ⊕ A, where A can be a square AbstractMatrix or a LinearMap.
BlockMap and BlockDiagonalMap
Types for representing block (diagonal) maps lazily.
Base.hcat — Functionhcat(As::Union{LinearMap,UniformScaling,AbstractVecOrMat}...)::BlockMapConstruct a (lazy) representation of the horizontal concatenation of the arguments. All arguments are promoted to LinearMaps automatically.
Examples
julia> CS = LinearMap{Int}(cumsum, 3)::LinearMaps.FunctionMap;
julia> L = [CS LinearMap(ones(Int, 3, 3))]::LinearMaps.BlockMap;
julia> L * ones(Int, 6)
3-element Array{Int64,1}:
4
5
6Base.vcat — Functionvcat(As::Union{LinearMap,UniformScaling,AbstractVecOrMat}...)::BlockMapConstruct a (lazy) representation of the vertical concatenation of the arguments. All arguments are promoted to LinearMaps automatically.
Examples
julia> CS = LinearMap{Int}(cumsum, 3)::LinearMaps.FunctionMap;
julia> L = [CS; LinearMap(ones(Int, 3, 3))]::LinearMaps.BlockMap;
julia> L * ones(Int, 3)
6-element Array{Int64,1}:
1
2
3
3
3
3Base.hvcat — Functionhvcat(rows::Tuple{Vararg{Int}}, As::Union{LinearMap,UniformScaling,AbstractVecOrMat}...)::BlockMapConstruct a (lazy) representation of the horizontal-vertical concatenation of the arguments. The first argument specifies the number of arguments to concatenate in each block row. All arguments are promoted to LinearMaps automatically.
Examples
julia> CS = LinearMap{Int}(cumsum, 3)::LinearMaps.FunctionMap;
julia> L = [CS CS; CS CS]::LinearMaps.BlockMap;
julia> L.rows
(2, 2)
julia> L * ones(Int, 6)
6-element Array{Int64,1}:
2
4
6
2
4
6Base.cat — Functioncat(As::Union{LinearMap,AbstractVecOrMat}...; dims=(1,2))::BlockDiagonalMapConstruct a (lazy) representation of the diagonal concatenation of the arguments. To avoid fallback to the generic Base.cat, there must be a LinearMap object among the first 8 arguments.
SparseArrays.blockdiag — Functionblockdiag(As::Union{LinearMap,AbstractVecOrMat}...)::BlockDiagonalMapConstruct a (lazy) representation of the diagonal concatenation of the arguments. To avoid fallback to the generic SparseArrays.blockdiag, there must be a LinearMap object among the first 8 arguments.
FillMap
Type for lazily representing constantly filled matrices.
LinearMaps.FillMap — TypeFillMap(λ, (m, n))::FillMap
FillMap(λ, m, n)::FillMapConstruct a (lazy) representation of an operator whose matrix representation would be an m×n-matrix filled constantly with the value λ.
Methods
Multiplication methods
Base.:* — Method*(A::LinearMap, x::AbstractVector)::AbstractVectorCompute the action of the linear map A on the vector x.
In Julia versions v1.3 and above, objects L of any subtype of LinearMap are callable in the sense that L(x) = L*x for x::AbstractVector.
Examples
julia> A=LinearMap([1.0 2.0; 3.0 4.0]); x=[1.0, 1.0];
julia> A*x
2-element Array{Float64,1}:
3.0
7.0
julia> A(x)
2-element Array{Float64,1}:
3.0
7.0Base.:* — Method*(A::LinearMap, X::AbstractMatrix)::CompositeMapReturn the CompositeMap A*LinearMap(X), interpreting the matrix X as a linear operator, rather than a collection of column vectors. To compute the action of A on each column of X, call Matrix(A*X) or use the in-place multiplication mul!(Y, A, X[, α, β]) with an appropriately sized, preallocated matrix Y.
Examples
julia> A=LinearMap([1.0 2.0; 3.0 4.0]); X=[1.0 1.0; 1.0 1.0];
julia> A*X isa LinearMaps.CompositeMap
trueBase.:* — Method*(X::AbstractMatrix, A::LinearMap)::CompositeMapReturn the CompositeMap LinearMap(X)*A, interpreting the matrix X as a linear operator. To compute the right-action of A on each row of X, call Matrix(X*A) or mul!(Y, X, A) for the in-place version.
Examples
julia> X=[1.0 1.0; 1.0 1.0]; A=LinearMap([1.0 2.0; 3.0 4.0]);
julia> X*A isa LinearMaps.CompositeMap
trueLinearAlgebra.mul! — Methodmul!(Y::AbstractVecOrMat, A::LinearMap, B::AbstractVector) -> Y
mul!(Y::AbstractMatrix, A::LinearMap, B::AbstractMatrix) -> YCalculates the action of the linear map A on the vector or matrix B and stores the result in Y, overwriting the existing value of Y. Note that Y must not be aliased with either A or B.
Examples
julia> A=LinearMap([1.0 2.0; 3.0 4.0]); B=[1.0, 1.0]; Y = similar(B); mul!(Y, A, B);
julia> Y
2-element Array{Float64,1}:
3.0
7.0
julia> A=LinearMap([1.0 2.0; 3.0 4.0]); B=[1.0 1.0; 1.0 1.0]; Y = similar(B); mul!(Y, A, B);
julia> Y
2×2 Array{Float64,2}:
3.0 3.0
7.0 7.0LinearAlgebra.mul! — Methodmul!(C::AbstractVecOrMat, A::LinearMap, B::AbstractVector, α, β) -> C
mul!(C::AbstractMatrix, A::LinearMap, B::AbstractMatrix, α, β) -> CCombined inplace multiply-add $A B α + C β$. The result is stored in C by overwriting it. Note that C must not be aliased with either A or B.
Examples
julia> A=LinearMap([1.0 2.0; 3.0 4.0]); B=[1.0, 1.0]; C=[1.0, 3.0];
julia> mul!(C, A, B, 100.0, 10.0) === C
true
julia> C
2-element Array{Float64,1}:
310.0
730.0
julia> A=LinearMap([1.0 2.0; 3.0 4.0]); B=[1.0 1.0; 1.0 1.0]; C=[1.0 2.0; 3.0 4.0];
julia> mul!(C, A, B, 100.0, 10.0) === C
true
julia> C
2×2 Array{Float64,2}:
310.0 320.0
730.0 740.0LinearAlgebra.mul! — Methodmul!(C::AbstractMatrix, A::AbstractMatrix, B::LinearMap) -> CCalculates the matrix representation of A*B and stores the result in C, overwriting the existing value of C. Note that C must not be aliased with either A or B. The computation C = A*B is performed via C' = B'A'.
Examples
julia> A=[1.0 1.0; 1.0 1.0]; B=LinearMap([1.0 2.0; 3.0 4.0]); C = similar(A); mul!(C, A, B);
julia> C
2×2 Array{Float64,2}:
4.0 6.0
4.0 6.0Base.:* — Method*(x::LinearAlgebra.AdjointAbsVec, A::LinearMap)::AdjointAbsVecCompute the right-action of the linear map A on the adjoint vector x and return an adjoint vector.
Examples
julia> A=LinearMap([1.0 2.0; 3.0 4.0]); x=[1.0, 1.0]; x'A
1×2 Adjoint{Float64,Array{Float64,1}}:
4.0 6.0Base.:* — Method*(x::LinearAlgebra.TransposeAbsVec, A::LinearMap)::TransposeAbsVecCompute the right-action of the linear map A on the transpose vector x and return a transpose vector.
Examples
julia> A=LinearMap([1.0 2.0; 3.0 4.0]); x=[1.0, 1.0]; transpose(x)*A
1×2 Transpose{Float64,Array{Float64,1}}:
4.0 6.0Applying the adjoint or transpose of A (if defined) to x works exactly as in the usual matrix case: transpose(A) * x and mul!(y, A', x), for instance.
Conversion methods
Array,Matrixand associatedconvertmethodsCreate a dense matrix representation of the
LinearMapobject, by multiplying it with the successive basis vectors. This is mostly for testing purposes or if you want to have the explicit matrix representation of a linear map for which you only have a function definition (e.g. to be able to use itstransposeoradjoint). This way, one may conveniently makeAact on the columns of a matrixX, instead of interpretingA * Xas a composed linear map:Matrix(A * X). For generic code, that is supposed to handle bothA::AbstractMatrixandA::LinearMap, it is recommended to useconvert(Matrix, A*X).convert(Abstract[Matrix/Array], A::LinearMap)Create an
AbstractMatrixrepresentation of theLinearMap. This falls back toMatrix(A), but avoids explicit construction in case theLinearMapobject is matrix-based.SparseArrays.sparse(A::LinearMap)andconvert(SparseMatrixCSC, A::LinearMap)Create a sparse matrix representation of the
LinearMapobject, by multiplying it with the successive basis vectors. This is mostly for testing purposes or if you want to have the explicit sparse matrix representation of a linear map for which you only have a function definition (e.g. to be able to use itstransposeoradjoint).