Skip to content

Specialize lmul!/rmul! for adjoint/transpose #1388

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

Merged
merged 6 commits into from
Aug 21, 2025
Merged
Show file tree
Hide file tree
Changes from all 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
5 changes: 5 additions & 0 deletions src/adjtrans.jl
Original file line number Diff line number Diff line change
Expand Up @@ -513,6 +513,11 @@ function _dot_nonrecursive(u, v)
end
end

rmul!(X::Transpose{<:Union{Real,Complex}}, s::Union{Real,Complex}) = (lmul!(s, parent(X)); X)
rmul!(X::Adjoint, s::Number) = (lmul!(s', parent(X)); X)
lmul!(s::Union{Real,Complex}, X::Transpose{<:Union{Real,Complex}}) = (rmul!(parent(X), s); X)
lmul!(s::Number, X::Adjoint) = (rmul!(parent(X), s'); X)

# Adjoint/Transpose-vector * vector
*(u::AdjointAbsVec{<:Number}, v::AbstractVector{<:Number}) = dot(u.parent, v)
*(u::TransposeAbsVec{T}, v::AbstractVector{T}) where {T<:Real} = dot(u.parent, v)
Expand Down
28 changes: 28 additions & 0 deletions test/adjtrans.jl
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ isdefined(Main, :LinearAlgebraTestHelpers) || Base.include(Main, TESTHELPERS)

using Main.LinearAlgebraTestHelpers.OffsetArrays
using Main.LinearAlgebraTestHelpers.ImmutableArrays
using Main.LinearAlgebraTestHelpers.Quaternions

@testset "Adjoint and Transpose inner constructor basics" begin
intvec, intmat = [1, 2], [1 2; 3 4]
Expand Down Expand Up @@ -809,6 +810,33 @@ end
end
end

@testset "lmul!/rmul! by numbers" begin
@testset "$(eltype(A))" for A in (rand(4, 4), rand(ComplexF64,4,4),
fill([1 2; 3 4], 4, 4),
fill(Quaternion(1,2,3,4), 4, 4))
B = copy(A)
@testset for op in (transpose, adjoint)
A .= B
@test lmul!(2, op(A)) == 2 * op(B)
A .= B
@test rmul!(op(A), 2) == op(B) * 2
if eltype(A) <: Complex
A .= B
@test lmul!(-2im, op(A)) == -2im * op(B)
A .= B
@test rmul!(op(A), -2im) == op(B) * -2im
end
if eltype(A) <: Quaternion
A .= B
q = Quaternion(0,1,4,7)
@test lmul!(q, op(A)) == q * op(B)
A .= B
@test rmul!(op(A), q) == op(B) * q
end
end
end
end

@testset "fillband!" begin
for A in (rand(4, 4), rand(ComplexF64,4,4))
B = similar(A)
Expand Down