Skip to content

Commit b9aeafa

Browse files
authored
Overload Base.literal_pow for AbstractQ (#54010)
1 parent a721658 commit b9aeafa

File tree

2 files changed

+10
-0
lines changed

2 files changed

+10
-0
lines changed

stdlib/LinearAlgebra/src/abstractq.jl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ transpose(Q::AbstractQ{<:Real}) = AdjointQ(Q)
1818
transpose(Q::AbstractQ) = error("transpose not implemented for $(typeof(Q)). Consider using adjoint instead of transpose.")
1919
adjoint(adjQ::AdjointQ) = adjQ.Q
2020

21+
(^)(Q::AbstractQ, p::Integer) = p < 0 ? power_by_squaring(inv(Q), -p) : power_by_squaring(Q, p)
22+
@inline Base.literal_pow(::typeof(^), Q::AbstractQ, ::Val{1}) = Q
23+
@inline Base.literal_pow(::typeof(^), Q::AbstractQ, ::Val{-1}) = inv(Q)
24+
2125
# promotion with AbstractMatrix, at least for equal eltypes
2226
promote_rule(::Type{<:AbstractMatrix{T}}, ::Type{<:AbstractQ{T}}) where {T} =
2327
(@inline; Union{AbstractMatrix{T},AbstractQ{T}})

stdlib/LinearAlgebra/test/abstractq.jl

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,12 @@ n = 5
3939
@test Q'*I Q.Q'*I rtol=2eps(real(T))
4040
@test I*Q Q.Q*I rtol=2eps(real(T))
4141
@test I*Q' I*Q.Q' rtol=2eps(real(T))
42+
@test Q^3 Q*Q*Q
43+
@test Q^2 Q*Q
44+
@test Q^1 == Q
45+
@test Q^(-1) == Q'
46+
@test (Q')^(-1) == Q
47+
@test (Q')^2 Q'*Q'
4248
@test abs(det(Q)) 1
4349
@test logabsdet(Q)[1] 0 atol=2n*eps(real(T))
4450
y = rand(T, n)

0 commit comments

Comments
 (0)