Skip to content

Commit

Permalink
Add missing methods for UniformScaling (#53949)
Browse files Browse the repository at this point in the history
The methods `float`, `cis`, `sincos` and `sincosd` are defined for
matrices, so it makes sense for these to be defined for a
`UniformScaling` as well.
E.g.:
```julia
julia> float(2I)
UniformScaling{Float64}
2.0*I

julia> sincos(2I)
(UniformScaling{Float64}(0.9092974268256817), UniformScaling{Float64}(-0.4161468365471424))
```
  • Loading branch information
jishnub authored Apr 5, 2024
1 parent a931fbe commit e64fa86
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 3 deletions.
9 changes: 7 additions & 2 deletions stdlib/LinearAlgebra/src/uniformscaling.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# This file is a part of Julia. License is MIT: https://julialang.org/license

import Base: copy, adjoint, getindex, show, transpose, one, zero, inv,
import Base: copy, adjoint, getindex, show, transpose, one, zero, inv, float,
hcat, vcat, hvcat, ^

"""
Expand Down Expand Up @@ -124,6 +124,8 @@ conj(J::UniformScaling) = UniformScaling(conj(J.λ))
real(J::UniformScaling) = UniformScaling(real(J.λ))
imag(J::UniformScaling) = UniformScaling(imag(J.λ))

float(J::UniformScaling) = UniformScaling(float(J.λ))

transpose(J::UniformScaling) = J
adjoint(J::UniformScaling) = UniformScaling(conj(J.λ))

Expand Down Expand Up @@ -159,7 +161,7 @@ isposdef(J::UniformScaling) = isposdef(J.λ)
(-)(A::AbstractMatrix, J::UniformScaling) = A + (-J)

# matrix functions
for f in ( :exp, :log,
for f in ( :exp, :log, :cis,
:expm1, :log1p,
:sqrt, :cbrt,
:sin, :cos, :tan,
Expand All @@ -172,6 +174,9 @@ for f in ( :exp, :log,
:acsch, :asech, :acoth )
@eval Base.$f(J::UniformScaling) = UniformScaling($f(J.λ))
end
for f in (:sincos, :sincosd)
@eval Base.$f(J::UniformScaling) = map(UniformScaling, $f(J.λ))
end

# Unit{Lower/Upper}Triangular matrices become {Lower/Upper}Triangular under
# addition with a UniformScaling
Expand Down
7 changes: 6 additions & 1 deletion stdlib/LinearAlgebra/test/uniformscaling.jl
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ Random.seed!(1234543)
@test -one(UniformScaling(2)) == UniformScaling(-1)
@test opnorm(UniformScaling(1+im)) sqrt(2)
@test convert(UniformScaling{Float64}, 2I) === 2.0I
@test float(2I) === 2.0*I
end

@testset "getindex" begin
Expand Down Expand Up @@ -67,7 +68,7 @@ end

# on complex plane
J = UniformScaling(randn(ComplexF64))
for f in ( exp, log,
for f in ( exp, log, cis,
sqrt,
sin, cos, tan,
asin, acos, atan,
Expand All @@ -80,6 +81,10 @@ end
@test f(J) f(M(J))
end

for f in (sincos, sincosd)
@test all(splat(), zip(f(J), f(M(J))))
end

# on real axis
for (λ, fs) in (
# functions defined for x ∈ ℝ
Expand Down

0 comments on commit e64fa86

Please sign in to comment.