Skip to content

Commit

Permalink
Merge pull request #243 from gustaphe/master
Browse files Browse the repository at this point in the history
Print imaginay unit as "i" with latex mimetype
  • Loading branch information
jverzani authored Aug 11, 2020
2 parents 6605bc5 + 5c22aeb commit ad67b84
Show file tree
Hide file tree
Showing 4 changed files with 127 additions and 84 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ RecipesBase = "3cdcf5f2-1ef4-517c-9805-6587b60abb01"

[compat]
Intervals = "0.5, 1.0, 1.3"
RecipesBase = "0.7, 0.8, 1"
OffsetArrays = "1"
RecipesBase = "0.7, 0.8, 1"
julia = "1"

[extras]
Expand Down
63 changes: 51 additions & 12 deletions src/show.jl
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,15 @@ function printproductsign(io::IO, pj::T, j, mimetype) where {T}
(showone(T) || pj != one(T)) && print(io, showop(mimetype, "*"))
end

function printproductsign(io::IO, pj::T, j, mimetype) where {T<:Complex}
j == 0 && return
(a,b) = reim(pj)
!iszero(a) && !iszero(b) && return # parentheses inserted, no * needed
!iszero(a) && return printproductsign(io, a, j, mimetype)
print(io, showop(mimetype, "*"))
end


# show a single term
# Other types can overload Polynomials.printcofficient with a mimetype
# or Base.show_unquoted(io, pj, indent, prec)
Expand Down Expand Up @@ -203,24 +212,51 @@ function printcoefficient(io::IO, a::Rational{T}, j, mimetype::MIME"text/latex")
end

# print complex numbers with parentheses as needed
function printcoefficient(io::IO, pj::Complex{T}, j, mimetype) where {T}
function printcoefficient(io::IO, pj::S, j, mimetype) where {T,S <: Complex{T}}

hasreal = abs(real(pj)) > 0 || isnan(real(pj)) || isinf(real(pj))
hasimag = abs(imag(pj)) > 0 || isnan(imag(pj)) || isinf(imag(pj))
(a,b) = reim(pj)
hasreal = !iszero(a) || isnan(a) || isinf(a)
hasimag = !iszero(b) || isnan(b) || isinf(b)

if hasreal && hasimag
Base.show_unquoted(io, pj, 0, Base.operator_precedence(:*))
iszero(j) || print(io, "(")
print(io, a)

# print b
if isone(b) || isone(-b)
if hasneg(S) && b < 0
print(io, showop(mimetype, "-"))
else
print(io, showop(mimetype, "+"))
end
else
if hasneg(S) && b < 0
print(io, showop(mimetype, "-"))
(showone(S) || !isone(-b)) && print(io, -b)
else
print(io, showop(mimetype,"+"))
print(io, b)
end
(isnan(b) || isinf(b)) && print(io, showop(mimetype, "*"))
end

print(io, imagsymbol(mimetype))
iszero(j) || print(io, ")")

elseif hasreal
a = real(pj)
(j==0 || showone(T) || a != one(T)) && printcoefficient(io, a, j, mimetype)

(iszero(j) || showone(T) || isone(a)) && printcoefficient(io, a, j, mimetype)

elseif hasimag
b = imag(pj)
(showone(T) || b != one(T)) && printcoefficient(io, b, j, mimetype)
(isnan(imag(pj)) || isinf(imag(pj))) && print(io, showop(mimetype, "*"))
print(io, im)
else
return nothing

(showone(T) || !isone(b)) && printcoefficient(io, b, j, mimetype)
(isnan(b) || isinf(b)) && print(io, showop(mimetype, "*"))
print(io, imagsymbol(mimetype))

end

return nothing

end

## show exponent
Expand Down Expand Up @@ -252,3 +288,6 @@ function unicode_subscript(io, j)
print(io, a[Int(i)-44])
end
end

imagsymbol(::Any) = "im"
imagsymbol(::MIME"text/latex") = "i"
5 changes: 3 additions & 2 deletions test/Poly.jl
Original file line number Diff line number Diff line change
Expand Up @@ -286,15 +286,16 @@ p = Poly([1.0, 2.0, 3.0, 1.0])
p = Poly([1, im])
@test repr(p) == "Poly(1 + im*x)"
p = Poly([1+im, 1-im, -1+im, -1 - im])# minus signs
@test repr(p) == "Poly((1 + 1im) + (1 - 1im)*x - (1 - 1im)*x^2 - (1 + 1im)*x^3)"
@test repr(p) == "Poly(1 + im + (1 - im)x - (1 - im)x^2 - (1 + im)x^3)"
p = Poly([1.0, 0 + NaN*im, NaN, Inf, 0 - Inf*im]) # handle NaN or Inf appropriately
@test repr(p) == "Poly(1.0 + NaN*im*x + NaN*x^2 + Inf*x^3 - Inf*im*x^4)"

p = Poly([1,2,3])

@test repr("text/latex", p) == "\$1 + 2\\cdot x + 3\\cdot x^{2}\$"
p = Poly([1//2, 2//3, 1])
@test repr("text/latex", p) == "\$\\frac{1}{2} + \\frac{2}{3}\\cdot x + x^{2}\$"
p = Poly([complex(1,1),complex(0,1),complex(1,0),complex(1,1)])
@test repr("text/latex", p) == "\$1 + i + i\\cdot x + x^{2} + (1 + i)x^{3}\$"

# customized printing with printpoly
function printpoly_to_string(args...; kwargs...)
Expand Down
Loading

2 comments on commit ad67b84

@jverzani
Copy link
Member Author

Choose a reason for hiding this comment

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

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

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

Registration pull request created: JuliaRegistries/General/19328

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v1.1.4 -m "<description of version>" ad67b84bd51d5a4ec3c9f8beb9c895c76f920a97
git push origin v1.1.4

Please sign in to comment.