Skip to content

Commit

Permalink
Merge 68ffb42 into 518eda6
Browse files Browse the repository at this point in the history
  • Loading branch information
saschatimme committed Aug 17, 2018
2 parents 518eda6 + 68ffb42 commit d5f0cfa
Show file tree
Hide file tree
Showing 2 changed files with 154 additions and 31 deletions.
159 changes: 141 additions & 18 deletions src/show.jl
Original file line number Diff line number Diff line change
@@ -1,32 +1,79 @@
# VARIABLES
function Base.show(io::IO, v::AbstractVariable)
print(io, name(v))
print_var(io, MIME"text/plain"(), v)
end
function Base.show(io::IO, mime::MIME"text/latex", v::AbstractVariable)
print_var(io, mime, v)
end

if VERSION < v"0.7.0-DEV.1144" # Define `isone` for base types JuliaLang/julia#22846
isone(x::T) where T = x == one(T)
function print_var(io::IO, mime::MIME, var::AbstractVariable)
print_var(io, mime, name(var))
end
function print_var(io::IO, mime::MIME, var::String)
m = match(r"([a-zA-Z]+)(?:\[((?:\d,)*\d)\])", var)
if m === nothing
print(io, var)
else
print(io, m.captures[1])
print_subscript(io, mime, parse.(Int, split(m.captures[2], ",")))
end
end
function print_subscript(io::IO, ::MIME"text/latex", index)
print(io, "_{", join(unicode_subscript.(index), ","), "}")
end
function print_subscript(io::IO, mime, index)
if length(index) == 1
print(io, unicode_subscript(index[1]))
else
print(io, join(unicode_subscript.(index), "\u208B"))
end
end

# MONOMIALS

function Base.show(io::IO, mime::MIME"text/latex", m::AbstractMonomial)
print_monomial(io, mime, m)
end
function Base.show(io::IO, mime::MIME"text/plain", m::AbstractMonomial)
print_monomial(io, mime, m)
end
function Base.show(io::IO, m::AbstractMonomial)
print_monomial(io, MIME"text/plain"(), m)
end

function print_monomial(io::IO, mime, m::AbstractMonomial)
if isconstant(m)
print(io, '1')
else
for (var, exp) in zip(variables(m), exponents(m))
if !iszero(exp)
print(io, var)
print_var(io, mime, var)
if !isone(exp)
print(io, '^', exp)
print_exponent(io, mime, exp)
end
end
end
end
end
#
print_exponent(io::IO, ::MIME"text/latex", exp) = print(io, "^{", exp, "}")
function print_exponent(io::IO, mime, exp)
print(io, join(unicode_superscript.(reverse(digits(exp)))))
end

should_print_coefficient(x) = true # By default, just print all coefficients
should_print_coefficient(x::Number) = !isone(x) # For numbers, we omit any "one" coefficients
print_coefficient(io::IO, coeff::Real) = print(io, coeff)
print_coefficient(io::IO, coeff) = print(io, "(", coeff, ")")
# TERM

function Base.show(io::IO, t::AbstractTerm)
print_term(io, MIME"text/plain"(), t)
end
function Base.show(io::IO, mime::MIME"text/latex", t::AbstractTerm)
print_term(io, mime, t)
end
function Base.show(io::IO, mime::MIME"text/plain", t::AbstractTerm)
print_term(io, mime, t)
end

function print_term(io::IO, mime, t::AbstractTerm)
if isconstant(t)
print_coefficient(io, coefficient(t))
else
Expand All @@ -43,31 +90,107 @@ function Base.show(io::IO, t::AbstractTerm)
end
end

isnegative(x::Real) = x < 0
isnegative(x) = false
should_print_coefficient(x) = true # By default, just print all coefficients
should_print_coefficient(x::Number) = !isone(x) # For numbers, we omit any "one" coefficients
print_coefficient(io::IO, coeff::Real) = print(io, coeff)
print_coefficient(io::IO, coeff) = print(io, "(", coeff, ")")

# POLYNOMIAL

function Base.show(io::IO, t::AbstractPolynomial)
print_poly(io, MIME"text/plain"(), t)
end
function Base.show(io::IO, mime::MIME"text/plain", t::AbstractPolynomial)
print_poly(io, mime, t)
end
function Base.show(io::IO, mime::MIME"text/latex", t::AbstractPolynomial)
print_poly(io, mime, t)
end

function Base.show(io::IO, p::AbstractPolynomial{T}) where T
function print_poly(io::IO, mime, p::AbstractPolynomial{T}) where T
ts = terms(p)
if isempty(ts)
print(io, zero(T))
else
print(io, first(ts))
print_term(io, mime, first(ts))
for t in Iterators.drop(ts, 1)
if isnegative(coefficient(t))
print(io, " - ")
print(io, abs(coefficient(t)) * monomial(t))
print_term(io, mime, abs(coefficient(t)) * monomial(t))
else
print(io, " + ")
print(io, t)
print_term(io, mime, t)
end
end
end
end

function Base.show(io::IO, p::RationalPoly)
isnegative(x::Real) = x < 0
isnegative(x) = false


function Base.show(io::IO, t::RationalPoly)
print_ratpoly(io, MIME"text/plain"(), t)
end
function Base.show(io::IO, mime::MIME"text/plain", t::RationalPoly)
print_ratpoly(io, mime, t)
end
function Base.show(io::IO, mime::MIME"text/latex", t::RationalPoly)
print_ratpoly(io, mime, t)
end

function print_ratpoly(io::IO, mime, p::RationalPoly)
print(io, "(")
print(io, p.num)
show(io, mime, p.num)
print(io, ") / (")
print(io, p.den)
show(io, mime, p.den)
print(io, ")")
end

function unicode_subscript(i)
if i == 0
"\u2080"
elseif i == 1
"\u2081"
elseif i == 2
"\u2082"
elseif i == 3
"\u2083"
elseif i == 4
"\u2084"
elseif i == 5
"\u2085"
elseif i == 6
"\u2086"
elseif i == 7
"\u2087"
elseif i == 8
"\u2088"
elseif i == 9
"\u2089"
end
end

function unicode_superscript(i)
if i == 0
"\u2070"
elseif i == 1
"\u00B9"
elseif i == 2
"\u00B2"
elseif i == 3
"\u00B3"
elseif i == 4
"\u2074"
elseif i == 5
"\u2075"
elseif i == 6
"\u2076"
elseif i == 7
"\u2077"
elseif i == 8
"\u2078"
elseif i == 9
"\u2079"
end
end
26 changes: 13 additions & 13 deletions test/show.jl
Original file line number Diff line number Diff line change
@@ -1,28 +1,28 @@
@testset "Show" begin
Mod.@polyvar x y z
@test sprint(show, (x*y^2 + x + 1 + y)) == "xy^2 + x + y + 1"
@test sprint(show, (x + 1 + y) / x^2) == "(x + y + 1) / (x^2)"
@test sprint(show, (x - y - x + y) / (x^2 - x)) == "(0) / (x^2 - x)"
@test sprint(show, (x*y^2 + x + 1 + y)) == "xy² + x + y + 1"
@test sprint(show, (x + 1 + y) / x^2) == "(x + y + 1) / (x²)"
@test sprint(show, (x - y - x + y) / (x^2 - x)) == "(0) / (x² - x)"
# Test taken from TypedPolynomials
@test sprint(show, x) == "x"
@test sprint(show, x^0) == "1"
@test sprint(show, x^1) == "x"
@test sprint(show, x^2) == "x^2"
@test sprint(show, 1x^2) == "x^2"
@test sprint(show, x^2) == "x²"
@test sprint(show, 1x^2) == "x²"
@test sprint(show, 5x) == "5x"
@test sprint(show, x * y) == "xy"
@test sprint(show, y * x) == "xy"
@test sprint(show, 5 + y + x) == "x + y + 5"
@test sprint(show, y + 5 + x) == "x + y + 5"
@test sprint(show, x + x^2) == "x^2 + x"
@test sprint(show, x^2 + x) == "x^2 + x"
@test sprint(show, x^2 - 3.0x) == "x^2 - 3.0x"
@test sprint(show, -x^3) == "-x^3"
@test sprint(show, -x^3 + y^2 - x) == "-x^3 + y^2 - x"
@test sprint(show, -2.0x^2) == "-2.0x^2"
@test sprint(show, x + x^2) == "x² + x"
@test sprint(show, x^2 + x) == "x² + x"
@test sprint(show, x^2 - 3.0x) == "x² - 3.0x"
@test sprint(show, -x^3) == "-x³"
@test sprint(show, -x^3 + y^2 - x) == "-x³ + y² - x"
@test sprint(show, -2.0x^2) == "-2.0x²"
@test sprint(show, (1.0 + 3.1im) * x*z) == "(1.0 + 3.1im)xz"
@test sprint(show, -(1.0 + 3.1im) * z*x) == "(-1.0 - 3.1im)xz"
@test sprint(show, x^2 + (1.0 + 3.1im) * x) == "x^2 + (1.0 + 3.1im)x"
@test sprint(show, x^2 - (1.0 + 3.1im) * x) == "x^2 + (-1.0 - 3.1im)x"
@test sprint(show, x^2 + (1.0 + 3.1im) * x) == "x² + (1.0 + 3.1im)x"
@test sprint(show, x^2 - (1.0 + 3.1im) * x) == "x² + (-1.0 - 3.1im)x"
@test sprint(show, [1.0, 2.0] * x) == "([1.0, 2.0])x"
end

0 comments on commit d5f0cfa

Please sign in to comment.