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

Overload Base.literal_pow for AbstractQ #54010

Merged
merged 3 commits into from
Apr 13, 2024
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions stdlib/LinearAlgebra/src/abstractq.jl
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ transpose(Q::AbstractQ{<:Real}) = AdjointQ(Q)
transpose(Q::AbstractQ) = error("transpose not implemented for $(typeof(Q)). Consider using adjoint instead of transpose.")
adjoint(adjQ::AdjointQ) = adjQ.Q

(^)(Q::AbstractQ, p::Integer) = p < 0 ? power_by_squaring(inv(Q), -p) : power_by_squaring(Q, p)
@inline Base.literal_pow(::typeof(^), Q::AbstractQ, ::Val{1}) = Q
@inline Base.literal_pow(::typeof(^), Q::AbstractQ, ::Val{-1}) = inv(Q)
Comment on lines +22 to +23
Copy link
Sponsor Member

Choose a reason for hiding this comment

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

Suggested change
@inline Base.literal_pow(::typeof(^), Q::AbstractQ, ::Val{1}) = Q
@inline Base.literal_pow(::typeof(^), Q::AbstractQ, ::Val{-1}) = inv(Q)

Is there a reason for these? The fallback should work just fine here, no?


# promotion with AbstractMatrix, at least for equal eltypes
promote_rule(::Type{<:AbstractMatrix{T}}, ::Type{<:AbstractQ{T}}) where {T} =
(@inline; Union{AbstractMatrix{T},AbstractQ{T}})
Expand Down
6 changes: 6 additions & 0 deletions stdlib/LinearAlgebra/test/abstractq.jl
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,12 @@ n = 5
@test Q'*I Q.Q'*I rtol=2eps(real(T))
@test I*Q Q.Q*I rtol=2eps(real(T))
@test I*Q' I*Q.Q' rtol=2eps(real(T))
@test Q^3 Q*Q*Q
@test Q^2 Q*Q
@test Q^1 == Q
@test Q^(-1) == Q'
@test (Q')^(-1) == Q
@test (Q')^2 Q'*Q'
@test abs(det(Q)) 1
@test logabsdet(Q)[1] 0 atol=2n*eps(real(T))
y = rand(T, n)
Expand Down