Skip to content

Commit e9b73ec

Browse files
sunoruKristofferC
authored andcommitted
fix #22168, im*I+I should be shown as (1+1im)*I (#22169)
* fix #22168, `im*I+I` should be shown as `(1+1im)*I` * add test for `show(UniformScaling)` * check the number of terms in `show(::UniformScaling)` * fix the misuse of `|`
1 parent 54bca01 commit e9b73ec

File tree

2 files changed

+8
-1
lines changed

2 files changed

+8
-1
lines changed

base/linalg/uniformscaling.jl

+7-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,13 @@ eltype(::Type{UniformScaling{T}}) where {T} = T
3131
ndims(J::UniformScaling) = 2
3232
getindex(J::UniformScaling, i::Integer,j::Integer) = ifelse(i==j,J.λ,zero(J.λ))
3333

34-
show(io::IO, J::UniformScaling) = print(io, "$(typeof(J))\n$(J.λ)*I")
34+
function show(io::IO, J::UniformScaling)
35+
s = "$(J.λ)"
36+
if ismatch(r"\w+\s*[\+\-]\s*\w+", s)
37+
s = "($s)"
38+
end
39+
print(io, "$(typeof(J))\n$s*I")
40+
end
3541
copy(J::UniformScaling) = UniformScaling(J.λ)
3642

3743
transpose(J::UniformScaling) = J

test/linalg/uniformscaling.jl

+1
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ end
3838
end
3939

4040
@test copy(UniformScaling(one(Float64))) == UniformScaling(one(Float64))
41+
@test sprint(show,UniformScaling(one(Complex128))) == "UniformScaling{Complex{Float64}}\n(1.0 + 0.0im)*I"
4142
@test sprint(show,UniformScaling(one(Float32))) == "UniformScaling{Float32}\n1.0*I"
4243

4344
λ = complex(randn(),randn())

0 commit comments

Comments
 (0)