1
- function Base. show (io:: IO , v:: AbstractVariable )
2
- print (io, name (v))
1
+ Base. print (io:: IO , v:: AbstractPolynomialLike ) = show (io, MIME " text/print" (), v)
2
+
3
+ # VARIABLES
4
+ Base. show (io:: IO , v:: AbstractVariable ) = print_var (io, MIME " text/plain" (), v)
5
+ Base. show (io:: IO , mime:: MIME"text/plain" , v:: AbstractVariable ) = print_var (io, mime, v)
6
+ Base. show (io:: IO , mime:: MIME"text/latex" , v:: AbstractVariable ) = print_var (io, mime, v)
7
+ Base. show (io:: IO , mime:: MIME"text/print" , v:: AbstractVariable ) = print_var (io, mime, v)
8
+
9
+ function print_var (io:: IO , mime:: MIME , var:: AbstractVariable )
10
+ base, indices = name_base_indices (var)
11
+ if isempty (indices)
12
+ print (io, base)
13
+ else
14
+ print (io, base)
15
+ print_subscript (io, mime, indices)
16
+ end
3
17
end
18
+ print_var (io:: IO , mime:: MIME"text/print" , var:: AbstractVariable ) = print (io, name (var))
4
19
5
- if VERSION < v " 0.7.0-DEV.1144 " # Define `isone` for base types JuliaLang/julia#22846
6
- isone (x :: T ) where T = x == one (T )
20
+ function print_subscript (io :: IO , :: MIME"text/latex" , index)
21
+ print (io, " _{ " , join (index, " , " ), " } " )
7
22
end
23
+ function print_subscript (io:: IO , mime, indices)
24
+ if length (indices) == 1
25
+ print (io, unicode_subscript (indices[1 ]))
26
+ else
27
+ print (io, join (unicode_subscript .(indices), " \u 208B" ))
28
+ end
29
+ end
30
+
31
+ const unicode_subscripts = (" ₀" ," ₁" ," ₂" ," ₃" ," ₄" ," ₅" ," ₆" ," ₇" ," ₈" ," ₉" )
32
+ unicode_subscript (i) = unicode_subscripts[i+ 1 ]
8
33
9
- function Base. show (io:: IO , m:: AbstractMonomial )
34
+ # MONOMIALS
35
+
36
+ Base. show (io:: IO , mime:: MIME"text/latex" , m:: AbstractMonomial ) = print_monomial (io, mime, m)
37
+ Base. show (io:: IO , mime:: MIME"text/plain" , m:: AbstractMonomial ) = print_monomial (io, mime, m)
38
+ Base. show (io:: IO , mime:: MIME"text/print" , m:: AbstractMonomial ) = print_monomial (io, mime, m)
39
+ Base. show (io:: IO , m:: AbstractMonomial ) = print_monomial (io, MIME " text/plain" (), m)
40
+
41
+ function print_monomial (io:: IO , mime, m:: AbstractMonomial )
10
42
if isconstant (m)
11
43
print (io, ' 1' )
12
44
else
13
- for (var, exp) in zip (variables (m), exponents (m))
45
+ printed_var = false
46
+ vars = variables (m)
47
+ n = length (vars)
48
+ for (i, var, exp) in zip (1 : n, vars, exponents (m))
14
49
if ! iszero (exp)
15
- print (io, var)
50
+ if mime isa MIME " text/print" && printed_var && i > 0 &&
51
+ print (io," *" )
52
+ end
53
+ print_var (io, mime, var)
54
+ printed_var = true
16
55
if ! isone (exp)
17
- print (io, ' ^ ' , exp)
56
+ print_exponent (io, mime , exp)
18
57
end
19
58
end
20
59
end
21
60
end
22
61
end
62
+ #
63
+ print_exponent (io:: IO , :: MIME"text/latex" , exp) = print (io, " ^{" , exp, " }" )
64
+ print_exponent (io:: IO , :: MIME"text/print" , exp) = print (io, " ^" , exp)
65
+ function print_exponent (io:: IO , mime, exp)
66
+ print (io, join (unicode_superscript .(reverse (digits (exp)))))
67
+ end
23
68
24
- should_print_coefficient (x) = true # By default, just print all coefficients
25
- should_print_coefficient (x:: Number ) = ! isone (x) # For numbers, we omit any "one" coefficients
26
- print_coefficient (io:: IO , coeff:: Real ) = print (io, coeff)
27
- print_coefficient (io:: IO , coeff) = print (io, " (" , coeff, " )" )
69
+ const unicode_superscripts = (" ⁰" ," ¹" ," ²" ," ³" ," ⁴" ," ⁵" ," ⁶" ," ⁷" ," ⁸" ," ⁹" )
70
+ unicode_superscript (i) = unicode_superscripts[i+ 1 ]
28
71
29
- function Base. show (io:: IO , t:: AbstractTerm )
72
+ # TERM
73
+
74
+ Base. show (io:: IO , t:: AbstractTerm ) = print_term (io, MIME " text/plain" (), t)
75
+ Base. show (io:: IO , mime:: MIME"text/latex" , t:: AbstractTerm ) = print_term (io, mime, t)
76
+ Base. show (io:: IO , mime:: MIME"text/plain" , t:: AbstractTerm ) = print_term (io, mime, t)
77
+ Base. show (io:: IO , mime:: MIME"text/print" , t:: AbstractTerm ) = print_term (io, mime, t)
78
+
79
+ function print_term (io:: IO , mime, t:: AbstractTerm )
30
80
if isconstant (t)
31
81
print_coefficient (io, coefficient (t))
32
82
else
@@ -38,36 +88,55 @@ function Base.show(io::IO, t::AbstractTerm)
38
88
end
39
89
end
40
90
if ! iszero (t)
41
- print (io, monomial (t))
91
+ show (io, mime , monomial (t))
42
92
end
43
93
end
44
94
end
45
95
46
- isnegative (x:: Real ) = x < 0
47
- isnegative (x) = false
96
+ should_print_coefficient (x) = true # By default, just print all coefficients
97
+ should_print_coefficient (x:: Number ) = ! isone (x) # For numbers, we omit any "one" coefficients
98
+ print_coefficient (io:: IO , coeff:: Real ) = print (io, coeff)
99
+ print_coefficient (io:: IO , coeff) = print (io, " (" , coeff, " )" )
48
100
49
- function Base. show (io:: IO , p:: AbstractPolynomial{T} ) where T
101
+ # POLYNOMIAL
102
+
103
+ Base. show (io:: IO , t:: AbstractPolynomial ) = print_poly (io, MIME " text/plain" (), t)
104
+ Base. show (io:: IO , mime:: MIME"text/plain" , t:: AbstractPolynomial ) = print_poly (io, mime, t)
105
+ Base. show (io:: IO , mime:: MIME"text/latex" , t:: AbstractPolynomial ) = print_poly (io, mime, t)
106
+ Base. show (io:: IO , mime:: MIME"text/print" , t:: AbstractPolynomial ) = print_poly (io, mime, t)
107
+
108
+ function print_poly (io:: IO , mime, p:: AbstractPolynomial{T} ) where T
50
109
ts = terms (p)
51
110
if isempty (ts)
52
111
print (io, zero (T))
53
112
else
54
- print (io, first (ts))
113
+ print_term (io, mime , first (ts))
55
114
for t in Iterators. drop (ts, 1 )
56
115
if isnegative (coefficient (t))
57
116
print (io, " - " )
58
- print (io, abs (coefficient (t)) * monomial (t))
117
+ show (io, mime , abs (coefficient (t)) * monomial (t))
59
118
else
60
119
print (io, " + " )
61
- print (io, t)
120
+ show (io, mime , t)
62
121
end
63
122
end
64
123
end
65
124
end
66
125
67
- function Base. show (io:: IO , p:: RationalPoly )
126
+ isnegative (x:: Real ) = x < 0
127
+ isnegative (x) = false
128
+
129
+ # RATIONAL POLY
130
+
131
+ Base. show (io:: IO , t:: RationalPoly ) = print_ratpoly (io, MIME " text/plain" (), t)
132
+ Base. show (io:: IO , mime:: MIME"text/plain" , t:: RationalPoly ) = print_ratpoly (io, mime, t)
133
+ Base. show (io:: IO , mime:: MIME"text/latex" , t:: RationalPoly ) = print_ratpoly (io, mime, t)
134
+ Base. show (io:: IO , mime:: MIME"text/print" , t:: RationalPoly ) = print_ratpoly (io, mime, t)
135
+
136
+ function print_ratpoly (io:: IO , mime, p:: RationalPoly )
68
137
print (io, " (" )
69
- print (io, p. num)
138
+ show (io, mime , p. num)
70
139
print (io, " ) / (" )
71
- print (io, p. den)
140
+ show (io, mime , p. den)
72
141
print (io, " )" )
73
142
end
0 commit comments