Skip to content

Commit 4635c5e

Browse files
authored
Specialize lmul!/rmul! for adjoint/transpose (#1388)
1 parent bcc4638 commit 4635c5e

File tree

2 files changed

+33
-0
lines changed

2 files changed

+33
-0
lines changed

src/adjtrans.jl

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -513,6 +513,11 @@ function _dot_nonrecursive(u, v)
513513
end
514514
end
515515

516+
rmul!(X::Transpose{<:Union{Real,Complex}}, s::Union{Real,Complex}) = (lmul!(s, parent(X)); X)
517+
rmul!(X::Adjoint, s::Number) = (lmul!(s', parent(X)); X)
518+
lmul!(s::Union{Real,Complex}, X::Transpose{<:Union{Real,Complex}}) = (rmul!(parent(X), s); X)
519+
lmul!(s::Number, X::Adjoint) = (rmul!(parent(X), s'); X)
520+
516521
# Adjoint/Transpose-vector * vector
517522
*(u::AdjointAbsVec{<:Number}, v::AbstractVector{<:Number}) = dot(u.parent, v)
518523
*(u::TransposeAbsVec{T}, v::AbstractVector{T}) where {T<:Real} = dot(u.parent, v)

test/adjtrans.jl

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ isdefined(Main, :LinearAlgebraTestHelpers) || Base.include(Main, TESTHELPERS)
1212

1313
using Main.LinearAlgebraTestHelpers.OffsetArrays
1414
using Main.LinearAlgebraTestHelpers.ImmutableArrays
15+
using Main.LinearAlgebraTestHelpers.Quaternions
1516

1617
@testset "Adjoint and Transpose inner constructor basics" begin
1718
intvec, intmat = [1, 2], [1 2; 3 4]
@@ -809,6 +810,33 @@ end
809810
end
810811
end
811812

813+
@testset "lmul!/rmul! by numbers" begin
814+
@testset "$(eltype(A))" for A in (rand(4, 4), rand(ComplexF64,4,4),
815+
fill([1 2; 3 4], 4, 4),
816+
fill(Quaternion(1,2,3,4), 4, 4))
817+
B = copy(A)
818+
@testset for op in (transpose, adjoint)
819+
A .= B
820+
@test lmul!(2, op(A)) == 2 * op(B)
821+
A .= B
822+
@test rmul!(op(A), 2) == op(B) * 2
823+
if eltype(A) <: Complex
824+
A .= B
825+
@test lmul!(-2im, op(A)) == -2im * op(B)
826+
A .= B
827+
@test rmul!(op(A), -2im) == op(B) * -2im
828+
end
829+
if eltype(A) <: Quaternion
830+
A .= B
831+
q = Quaternion(0,1,4,7)
832+
@test lmul!(q, op(A)) == q * op(B)
833+
A .= B
834+
@test rmul!(op(A), q) == op(B) * q
835+
end
836+
end
837+
end
838+
end
839+
812840
@testset "fillband!" begin
813841
for A in (rand(4, 4), rand(ComplexF64,4,4))
814842
B = similar(A)

0 commit comments

Comments
 (0)