From 1432c5a08531aab153953848e5e8015faed2bb8d Mon Sep 17 00:00:00 2001 From: Mason Protter Date: Wed, 11 Dec 2019 01:20:32 -0700 Subject: [PATCH] added ^ method for UniformScaling (#34008) Co-Authored-By: simeonschaub Co-Authored-By: Fredrik Ekre --- NEWS.md | 2 ++ stdlib/LinearAlgebra/src/uniformscaling.jl | 10 +++++++++- stdlib/LinearAlgebra/test/uniformscaling.jl | 9 +++++++++ 3 files changed, 20 insertions(+), 1 deletion(-) diff --git a/NEWS.md b/NEWS.md index 664d8f615dbc6..109c236be5351 100644 --- a/NEWS.md +++ b/NEWS.md @@ -65,6 +65,8 @@ Standard library changes * `cond(A, p)` with `p=1` or `p=Inf` now computes the exact condition number instead of an estimate ([#33547]). +* `UniformScaling` objects may now be exponentiated such that `(a*I)^x = a^x * I`. + #### Markdown * Tables now have the `align` attribute set when `show`n as HTML ([#33849]). diff --git a/stdlib/LinearAlgebra/src/uniformscaling.jl b/stdlib/LinearAlgebra/src/uniformscaling.jl index d268120262e94..73b216f52f899 100644 --- a/stdlib/LinearAlgebra/src/uniformscaling.jl +++ b/stdlib/LinearAlgebra/src/uniformscaling.jl @@ -1,7 +1,7 @@ # This file is a part of Julia. License is MIT: https://julialang.org/license import Base: copy, adjoint, getindex, show, transpose, one, zero, inv, - hcat, vcat, hvcat + hcat, vcat, hvcat, ^ """ UniformScaling{T<:Number} @@ -227,6 +227,14 @@ Broadcast.broadcasted(::typeof(*), J::UniformScaling,x::Number) = UniformScaling Broadcast.broadcasted(::typeof(/), J::UniformScaling,x::Number) = UniformScaling(J.λ/x) +(^)(J::UniformScaling, x::Number) = UniformScaling((J.λ)^x) +Base.literal_pow(::typeof(^), J::UniformScaling, x::Val) = UniformScaling(Base.literal_pow(^, J.λ, x)) + +Broadcast.broadcasted(::typeof(^), J::UniformScaling, x::Number) = UniformScaling(J.λ^x) +function Broadcast.broadcasted(::typeof(Base.literal_pow), ::typeof(^), J::UniformScaling, x::Val) + UniformScaling(Base.literal_pow(^, J.λ, x)) +end + ==(J1::UniformScaling,J2::UniformScaling) = (J1.λ == J2.λ) ## equality comparison with UniformScaling diff --git a/stdlib/LinearAlgebra/test/uniformscaling.jl b/stdlib/LinearAlgebra/test/uniformscaling.jl index b39f07b997994..ce6625443e824 100644 --- a/stdlib/LinearAlgebra/test/uniformscaling.jl +++ b/stdlib/LinearAlgebra/test/uniformscaling.jl @@ -66,6 +66,15 @@ end @test UniformScaling(α)./α == UniformScaling(1.0) @test α * UniformScaling(1.0) == UniformScaling(1.0) * α @test UniformScaling(α)/α == UniformScaling(1.0) + + β = rand() + @test (α*I)^2 == UniformScaling(α^2) + @test (α*I)^(-2) == UniformScaling(α^(-2)) + @test (α*I)^(.5) == UniformScaling(α^(.5)) + @test (α*I)^β == UniformScaling(α^β) + + @test (α * I) .^ 2 == UniformScaling(α^2) + @test (α * I) .^ β == UniformScaling(α^β) end @testset "det and logdet" begin