Skip to content

Commit 6303d8c

Browse files
authored
Better printing of floating point coefficients (#244)
1 parent c9cbca9 commit 6303d8c

File tree

5 files changed

+21
-4
lines changed

5 files changed

+21
-4
lines changed

perf/Project.toml

+3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
[deps]
22
BenchmarkTools = "6e4b80f9-dd63-53aa-95a3-0cdb28fa8baf"
33
DynamicPolynomials = "7c1d4256-1411-5781-91ec-d7bc3513ac07"
4+
MultivariatePolynomials = "102ac46a-7ee4-5c85-9060-abc95bfdeaa3"
5+
MutableArithmetics = "d8a4904e-b15c-11e9-3269-09a3773c0cb0"
6+
OrderedCollections = "bac558e1-5e72-5ebc-8fee-abe8a469f55d"
47
SIMDPolynomials = "4edc584b-a88b-4acf-80bb-891198a11e01"
58
TypedPolynomials = "afbbf031-7a57-5f58-a1b9-b774a0fad08d"

perf/gcd.jl

+2
Original file line numberDiff line numberDiff line change
@@ -97,3 +97,5 @@ function bench()
9797
bench(T)
9898
end
9999
end
100+
101+
bench()

src/gcd.jl

+2-2
Original file line numberDiff line numberDiff line change
@@ -432,11 +432,11 @@ function primitive_univariate_gcd!(p::APL, q::APL, algo::GeneralizedEuclideanAlg
432432
elseif isconstant(v)
433433
# `p` and `q` are primitive so if one of them is constant, it cannot
434434
# divide the content of the other one.
435-
return MA.operate!!(one, u)
435+
return MA.operate!(one, u)
436436
end
437437

438438
d_before = degree(leadingmonomial(u))
439-
r = MA.operate!!(rem_or_pseudo_rem, u, v, algo)
439+
r = MA.operate!(rem_or_pseudo_rem, u, v, algo)
440440
d_after = degree(leadingmonomial(r))
441441
if d_after == d_before
442442
not_divided_error(u, v)

src/show.jl

+8
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,14 @@ should_print_coefficient(x::Number) = !isone(x) # For numbers, we omit any "one"
114114
# We could add a check with `showable` if a `Real` subtype supports it and
115115
# the feature is requested.
116116
print_coefficient(io::IO, mime, coeff::Real) = print(io, coeff)
117+
# Scientific notation does not display well in LaTeX so we rewrite it
118+
function print_coefficient(io::IO, mime::MIME"text/latex", coeff::AbstractFloat)
119+
s = string(coeff)
120+
if occursin('e', s)
121+
s = replace(s, 'e' => " \\cdot 10^{") * '}'
122+
end
123+
print(io, s)
124+
end
117125
# JuMP expressions supports LaTeX output so `showable` will return `true`
118126
# for them. It is important for anonymous variables to display properly as well:
119127
# https://github.com/jump-dev/SumOfSquares.jl/issues/256

test/show.jl

+6-2
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,10 @@ Base.show(io::IO, ::MIME"text/latex", ::CustomLaTeXPrint) = print(io, "a_a")
3333
@test sprint(show, x^2 + (1.0 + 3.1im) * x) == "(1.0 + 3.1im)x + x²"
3434
@test sprint(show, x^2 - (1.0 + 3.1im) * x) == "(-1.0 - 3.1im)x + x²"
3535

36+
@test sprint(show, 1e-8 + 3.2e7 * x + 4.5 * x^2) == "1.0e-8 + 3.2e7x + 4.5x²"
37+
@test sprint(show, "text/plain", 1e-8 + 3.2e7 * x + 4.5 * x^2) == "1.0e-8 + 3.2e7x + 4.5x²"
38+
@test sprint(show, "text/latex", 1e-8 + 3.2e7 * x + 4.5 * x^2) == "\$\$ 1.0 \\cdot 10^{-8} + 3.2 \\cdot 10^{7}x + 4.5x^{2} \$\$"
39+
3640
Mod.@polyvar x[0:9]
3741
@test sprint(show, sum(i*x[i]^i for i=1:10)) == "x₀ + 2x₁² + 3x₂³ + 4x₃⁴ + 5x₄⁵ + 6x₅⁶ + 7x₆⁷ + 8x₇⁸ + 9x₈⁹ + 10x₉¹⁰"
3842
@test sprint(show, "text/latex", sum(i*x[i]^i for i=1:10)) == "\$\$ x_{0} + 2x_{1}^{2} + 3x_{2}^{3} + 4x_{3}^{4} + 5x_{4}^{5} + 6x_{5}^{6} + 7x_{6}^{7} + 8x_{7}^{8} + 9x_{8}^{9} + 10x_{9}^{10} \$\$"
@@ -45,6 +49,6 @@ Base.show(io::IO, ::MIME"text/latex", ::CustomLaTeXPrint) = print(io, "a_a")
4549
@test sprint(print, 2x[1]^2+3x[3]+1+x[4]) == "1 + x[4] + 3*x[3] + 2*x[1]^2"
4650

4751
a = CustomLaTeXPrint()
48-
@test sprint((io, x) -> show(io, MIME"text/latex"(), x), term(a, x[1]^2)) == "\$\$ (a_a)x_{1}^{2} \$\$"
49-
@test sprint((io, x) -> show(io, MIME"text/latex"(), x), polynomial([a, a], [x[1]^2, x[2]])) == "\$\$ (a_a)x_{2} + (a_a)x_{1}^{2} \$\$"
52+
@test sprint((io, x) -> show(io, "text/latex", x), term(a, x[1]^2)) == "\$\$ (a_a)x_{1}^{2} \$\$"
53+
@test sprint((io, x) -> show(io, "text/latex", x), polynomial([a, a], [x[1]^2, x[2]])) == "\$\$ (a_a)x_{2} + (a_a)x_{1}^{2} \$\$"
5054
end

0 commit comments

Comments
 (0)