Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix #22168, im*I+I should be shown as (1+1im)*I #22169

Merged
merged 4 commits into from
Jun 6, 2017

Conversation

sunoru
Copy link
Contributor

@sunoru sunoru commented Jun 1, 2017

No description provided.

@ararslan ararslan added complex Complex numbers display and printing Aesthetics and correctness of printed representations of objects. labels Jun 1, 2017
Copy link
Member

@ararslan ararslan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me. Thanks for the contribution!

@@ -31,6 +31,7 @@ eltype(::Type{UniformScaling{T}}) where {T} = T
ndims(J::UniformScaling) = 2
getindex(J::UniformScaling, i::Integer,j::Integer) = ifelse(i==j,J.λ,zero(J.λ))

show(io::IO, J::UniformScaling{T}) where {T<:Complex} = print(io, "$(typeof(J))\n($(J.λ))*I")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we do a check for the number of terms instead? Minor use case but this would still be wrong for quaternions.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's true. But if we do such a check we have to deal with strings. Don't know if it's OK to do so.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Aren't we already dealing with strings? My idea was to add the parentheses if something like ismatch(r"[0-9]+ ?\+|\- ?[0-9]+", "$(J.λ)")

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh I see. How about just checking r"\w+\s*\+|\-\s*\w+" as there must be + or - between terms if there are more than one term.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That was also my first version but I think negative reals become false positives here

julia> ismatch(r"\w+\s*\+|\-\s*\w+", "-1")
true

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah I'm sorry. It's because of the misuse of | and we should use r"\w+\s*[\+\-]\s*\w+".

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you perhaps add tests for the things that wasn't completely right with the first regexp.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know what common things can be wrong with the first regexp, but I believe there could be situations when users define a type that doesn't contain digit-[plus/minus]-digit pattern, such as something like 1i+2j. (The current displays of complex and quaternion numbers have something like 0+0im so the first regexp always works with them.)

@KristofferC KristofferC merged commit e9b73ec into JuliaLang:master Jun 6, 2017
tkelman pushed a commit that referenced this pull request Jun 6, 2017
* 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)
@sunoru sunoru deleted the patch-1 branch July 12, 2022 19:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
complex Complex numbers display and printing Aesthetics and correctness of printed representations of objects.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants