Skip to content

Commit

Permalink
Overload Base.literal_pow for AbstractQ (#54010)
Browse files Browse the repository at this point in the history
(cherry picked from commit b9aeafa)
  • Loading branch information
dkarrasch authored and KristofferC committed May 25, 2024
1 parent b0f607a commit 525b3a2
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 0 deletions.
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)

# 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

0 comments on commit 525b3a2

Please sign in to comment.