Skip to content

Commit 917f80e

Browse files
sunorutkelman
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 `|` (cherry picked from commit e9b73ec)
1 parent 7c0a1b7 commit 917f80e

File tree

2 files changed

+8
-1
lines changed

2 files changed

+8
-1
lines changed

base/linalg/uniformscaling.jl

Lines changed: 7 additions & 1 deletion
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

Lines changed: 1 addition & 0 deletions
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)