Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add mul! for matrices #330

Merged
merged 3 commits into from
May 16, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 50 additions & 0 deletions src/adjtrans.jl
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,25 @@ function mul!(
conj!(res)
end

function mul!(
res::AbstractMatrix,
op::AdjointLinearOperator{T, S},
m::AbstractMatrix,
α,
β,
) where {T, S}
p = op.parent
(size(m, 1) == size(p, 1) && size(res, 1) == size(p, 2) && size(m, 2) == size(res, 2)) ||
throw(LinearOperatorException("shape mismatch"))
if ishermitian(p)
return mul!(res, p, m, α, β)
elseif p.ctprod! !== nothing
return p.ctprod!(res, m, α, β)
else
error("Not implemented")
end
end

function mul!(
res::AbstractVector,
op::TransposeLinearOperator{T, S},
Expand Down Expand Up @@ -188,6 +207,25 @@ function mul!(
conj!(res)
end

function mul!(
res::AbstractMatrix,
op::TransposeLinearOperator{T, S},
m::AbstractMatrix,
α,
β,
) where {T, S}
p = op.parent
(size(m, 1) == size(p, 1) && size(res, 1) == size(p, 2) && size(m, 2) == size(res, 2)) ||
throw(LinearOperatorException("shape mismatch"))
if issymmetric(p)
return mul!(res, p, m, α, β)
elseif p.tprod! !== nothing
return p.tprod!(res, m, α, β)
else
error("Not implemented")
end
end

function mul!(
res::AbstractVector,
op::ConjugateLinearOperator{T, S},
Expand All @@ -200,6 +238,18 @@ function mul!(
conj!(res)
end

function mul!(
res::AbstractMatrix,
op::ConjugateLinearOperator{T, S},
v::AbstractMatrix,
α,
β,
) where {T, S}
p = op.parent
mul!(res, p, conj.(v), α, β)
conj!(res)
end

-(op::AdjointLinearOperator) = adjoint(-op.parent)
-(op::TransposeLinearOperator) = transpose(-op.parent)
-(op::ConjugateLinearOperator) = conj(-op.parent)
Expand Down
2 changes: 2 additions & 0 deletions src/constructors.jl
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,8 @@ op = LinearOperator(Float64, 2, 2, false, false,
(res, v) -> mul!(res, A, v),
(res, w) -> mul!(res, A', w))
```

The 3-args `mul!` also works when applying the operator on a matrix.
"""
function LinearOperator(
::Type{T},
Expand Down
36 changes: 30 additions & 6 deletions src/operations.jl
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,11 @@ function mul!(res::AbstractVector, op::AbstractLinearOperator{T}, v::AbstractVec
end
end

function mul!(res::AbstractVector, op::AbstractLinearOperator, v::AbstractVector{T}) where {T}
function mul!(res::AbstractMatrix, op::AbstractLinearOperator, m::AbstractMatrix{T}, α, β) where {T}
op.prod!(res, m, α, β)
end

function mul!(res::AbstractVecOrMat, op::AbstractLinearOperator, v::AbstractVecOrMat{T}) where {T}
mul!(res, op, v, one(T), zero(T))
end

Expand Down Expand Up @@ -73,6 +77,26 @@ function *(
return v_wrapper(res)
end

# Apply an operator to a matrix (only in-place, since operator * matrix is a matrix).

function mul!(
res::Adjoint{S1, M1},
m::Adjoint{S2, M2},
op::AbstractLinearOperator{T},
) where {T, S1, S2, M1 <: AbstractMatrix{S1}, M2 <: AbstractMatrix{S2}}
mul!(adjoint(res), adjoint(op), adjoint(m))
return res
end

function mul!(
res::Transpose{S1, M1},
m::Transpose{S2, M2},
op::AbstractLinearOperator{T},
) where {T, S1, S2, M1 <: AbstractMatrix{S1}, M2 <: AbstractMatrix{S2}}
mul!(transpose(res), transpose(op), transpose(m))
return res
end

# Unary operations.
+(op::AbstractLinearOperator) = op

Expand All @@ -95,11 +119,11 @@ function -(op::AbstractLinearOperator{T}) where {T}
end

function prod_op!(
res::AbstractVector,
res::AbstractVecOrMat,
op1::AbstractLinearOperator,
op2::AbstractLinearOperator,
vtmp::AbstractVector,
v::AbstractVector,
vtmp::AbstractVecOrMat,
v::AbstractVecOrMat,
α,
β,
)
Expand Down Expand Up @@ -162,10 +186,10 @@ end
# Operator + operator.

function sum_prod!(
res::AbstractVector,
res::AbstractVecOrMat,
op1::AbstractLinearOperator,
op2::AbstractLinearOperator{T},
v::AbstractVector,
v::AbstractVecOrMat,
α,
β,
) where {T}
Expand Down
2 changes: 2 additions & 0 deletions src/special-operators.jl
Original file line number Diff line number Diff line change
Expand Up @@ -289,3 +289,5 @@ function BlockDiagonalOperator(ops...; S = promote_type(storage_type.(ops)...))
args5 = all((has_args5(op) for op ∈ ops))
CompositeLinearOperator(T, nrow, ncol, symm, herm, prod!, tprod!, ctprod!, args5, S = S)
end

# TODO: overload the above for matrices?
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you remove this comment and open the issue for some functionalities that are not supported with matrices?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will merge the PR after that 🙂

Copy link
Contributor Author

@gdalle gdalle May 16, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comment removed, issue opened in #331

12 changes: 12 additions & 0 deletions test/test_linop.jl
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,18 @@ function test_linop()
@test(norm(transpose(u) * A - transpose(u) * op) <= rtol * norm(u))
@test(typeof(u' * op * v) <: Number)
@test(norm(u' * A * v - u' * op * v) <= rtol * norm(u))

mv = hcat(v, -2v)
mu = hcat(u, -2u)
res_mat = similar(mu)
res_trans = similar(mv)
res_adj = similar(mv)
mul!(res_mat, op, mv)
mul!(res_trans, transpose(op), mu)
mul!(res_adj, op', mu)
@test(norm(A * mv - res_mat) <= rtol * norm(mv))
@test(norm(transpose(A) * mu - res_trans) <= rtol * norm(mu))
@test(norm(A' * mu - res_adj) <= rtol * norm(mu))
end

A3 = Hermitian(A2' * A2)
Expand Down
Loading