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

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

jishnub
Copy link
Member

@jishnub jishnub commented Jun 19, 2025

This ensures that the scaling happens in a cache-friendly manner, which improves performance.

julia> A = adjoint(rand(100,100));
 
julia> @btime rmul!($A, 2.0);
  2.877 μs (0 allocations: 0 bytes) # nightly
  961.385 ns (0 allocations: 0 bytes) # this PR

@jishnub jishnub force-pushed the jishnub/lrmul_adjtrans branch from de35d8c to a359c7c Compare June 19, 2025 18:43
Copy link

codecov bot commented Jun 19, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 93.82%. Comparing base (c5d7122) to head (a462e5a).
Report is 2 commits behind head on master.

Additional details and impacted files
@@           Coverage Diff           @@
##           master    #1388   +/-   ##
=======================================
  Coverage   93.81%   93.82%           
=======================================
  Files          34       34           
  Lines       15722    15724    +2     
=======================================
+ Hits        14750    14753    +3     
+ Misses        972      971    -1     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@@ -809,4 +809,23 @@ end
end
end

@testset "lmul!/rmul! by numbers" begin
Copy link
Member

Choose a reason for hiding this comment

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

Do we have some sort of Quaternions available? We should test this on something that is not commutative, because otherwise the whole left- and right-multiplication difference doesn't become visible.

Copy link
Member Author

Choose a reason for hiding this comment

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

Turns out that this only holds for commutative numbers, so I have restricted this to real and complex numbers. This should cover most use cases.

Copy link
Member

Choose a reason for hiding this comment

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

For the adjoint case, this should work, because we still have (xy)' = y'x'.

Comment on lines +517 to +518
rmul!(X::AdjOrTrans{<:Union{Real,Complex}}, s::Union{Real,Complex}) = (lmul!(wrapperop(X)(s), parent(X)); X)
lmul!(s::Union{Real,Complex}, X::AdjOrTrans{<:Union{Real,Complex}}) = (rmul!(parent(X), wrapperop(X)(s)); X)
Copy link
Member

Choose a reason for hiding this comment

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

I'd suggest to split this and make it available to non-commutative types in the adjoint case.

Suggested change
rmul!(X::AdjOrTrans{<:Union{Real,Complex}}, s::Union{Real,Complex}) = (lmul!(wrapperop(X)(s), parent(X)); X)
lmul!(s::Union{Real,Complex}, X::AdjOrTrans{<:Union{Real,Complex}}) = (rmul!(parent(X), wrapperop(X)(s)); X)
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)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants