From ef0bcf32d09b18d9a86695d2725eed2aba2e21ff Mon Sep 17 00:00:00 2001 From: Jishnu Bhattacharya Date: Fri, 30 May 2025 18:08:34 +0530 Subject: [PATCH 1/2] Faster `Adjoint`/`Transpose`-`UniformScaling` arithmetic --- src/uniformscaling.jl | 16 ++++++++++++++++ test/uniformscaling.jl | 13 +++++++++++++ 2 files changed, 29 insertions(+) diff --git a/src/uniformscaling.jl b/src/uniformscaling.jl index 4422799f..5ebe469e 100644 --- a/src/uniformscaling.jl +++ b/src/uniformscaling.jl @@ -217,6 +217,22 @@ function (-)(J::UniformScaling{<:Complex}, A::Hermitian) return B end +for f in (:+, :-) + @eval begin + function $f(A::AdjOrTransAbsMat, J::UniformScaling) + checksquare(A) + op = wrapperop(A) + op($f(op(A), op(J))) + end + + function $f(J::UniformScaling, A::AdjOrTransAbsMat) + checksquare(A) + op = wrapperop(A) + op($f(op(J), op(A))) + end + end +end + function (+)(A::AbstractMatrix, J::UniformScaling) checksquare(A) B = copymutable_oftype(A, Base.promote_op(+, eltype(A), typeof(J))) diff --git a/test/uniformscaling.jl b/test/uniformscaling.jl index 21bd4b80..047848de 100644 --- a/test/uniformscaling.jl +++ b/test/uniformscaling.jl @@ -335,6 +335,19 @@ let @test @inferred(J - T) == J - Array(T) end + @testset for f in (transpose, adjoint) + if isa(A, Array) + T = f(randn(ComplexF64,3,3)) + else + T = f(view(randn(ComplexF64,3,3), 1:3, 1:3)) + end + TA = Array(T) + @test @inferred(T + J) == TA + J + @test @inferred(J + T) == J + TA + @test @inferred(T - J) == TA - J + @test @inferred(J - T) == J - TA + end + @test @inferred(I\A) == A @test @inferred(A\I) == inv(A) @test @inferred(λ\I) === UniformScaling(1/λ) From d4201d6e8ea28cca655bd1d4078a9cd8ee9d6cc7 Mon Sep 17 00:00:00 2001 From: Jishnu Bhattacharya Date: Sat, 31 May 2025 00:20:37 +0530 Subject: [PATCH 2/2] Only define for one ordering --- src/uniformscaling.jl | 23 +++++++++-------------- 1 file changed, 9 insertions(+), 14 deletions(-) diff --git a/src/uniformscaling.jl b/src/uniformscaling.jl index 5ebe469e..b5874d2d 100644 --- a/src/uniformscaling.jl +++ b/src/uniformscaling.jl @@ -217,20 +217,15 @@ function (-)(J::UniformScaling{<:Complex}, A::Hermitian) return B end -for f in (:+, :-) - @eval begin - function $f(A::AdjOrTransAbsMat, J::UniformScaling) - checksquare(A) - op = wrapperop(A) - op($f(op(A), op(J))) - end - - function $f(J::UniformScaling, A::AdjOrTransAbsMat) - checksquare(A) - op = wrapperop(A) - op($f(op(J), op(A))) - end - end +function (+)(A::AdjOrTransAbsMat, J::UniformScaling) + checksquare(A) + op = wrapperop(A) + op(op(A) + op(J)) +end +function (-)(J::UniformScaling, A::AdjOrTransAbsMat) + checksquare(A) + op = wrapperop(A) + op(op(J) - op(A)) end function (+)(A::AbstractMatrix, J::UniformScaling)