-
Notifications
You must be signed in to change notification settings - Fork 120
/
poly.jl
344 lines (278 loc) · 9.67 KB
/
poly.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
################################################################################
#
# Tropical polynomials
#
################################################################################
################################################################################
#
# Tropical alternatives to generic functions
#
################################################################################
###
# Alternatives to generic functions that use R(1) and R(0) instead of one(R) and zero(R)
###
one(R::Generic.PolyRing{<:TropicalSemiringElem}) = R(one(base_ring(R)))
zero(R::Generic.PolyRing{<:TropicalSemiringElem}) = R(zero(base_ring(R)))
one(R::MPolyRing{<:TropicalSemiringElem}) = R(one(base_ring(R)))
zero(R::MPolyRing{<:TropicalSemiringElem}) = R(zero(base_ring(R)))
function polynomial_ring(R::TropicalSemiring, s::Symbol; cached::Bool = true)
T = elem_type(R)
parent_obj = Oscar.Generic.PolyRing{T}(R, s, cached)
return parent_obj, parent_obj([zero(R), one(R)])
end
###
# Alternative to generic prints that display zero sums as 0
###
function AbstractAlgebra.expressify(@nospecialize(a::PolyRingElem{<:TropicalSemiringElem}), x = var(parent(a)); context = nothing)
if iszero(a)
return expressify(zero(base_ring(a)), context = context)
end
sum = Expr(:call, :+)
for k in degree(a):-1:0
c = coeff(a, k)
if !iszero(c)
xk = k < 1 ? expressify(one(base_ring(a)), context = context) : k == 1 ? x : Expr(:call, :^, x, k)
if isone(c)
push!(sum.args, Expr(:call, :*, xk))
else
push!(sum.args, Expr(:call, :*, expressify(c, context = context), xk))
end
end
end
return sum
end
function AbstractAlgebra.expressify(a::MPolyRingElem{<:TropicalSemiringElem}, x = symbols(parent(a)); context = nothing)
if iszero(a)
return expressify(zero(base_ring(a)), context = context)
end
sum = Expr(:call, :+)
n = nvars(parent(a))
for (c, v) in zip(coefficients(a), exponents(a))
prod = Expr(:call, :*)
if !isone(c)
push!(prod.args, expressify(c, context = context))
end
for i in 1:n
if v[i] > 1
push!(prod.args, Expr(:call, :^, x[i], v[i]))
elseif v[i] == 1
push!(prod.args, x[i])
end
end
# Capture empty products
if length(prod.args) == 1
prod = expressify(one(base_ring(a)), context = context)
end
push!(sum.args, prod)
end
return sum
end
###
# Disabling ideals in tropical polyomial rings
###
function MPolyIdeal(R::Ring, g::Vector{Generic.MPoly{TropicalSemiringElem{minOrMax}}}) where {minOrMax}
error("Ideals over tropical semirings not supported")
end
################################################################################
#
# @tropical macro
#
################################################################################
"""
@tropical(expr)
Translate the expression in the tropical world.
# Examples
```jldoctest
julia> T = tropical_semiring(min);
julia> Tx, x = polynomial_ring(T, "x" => 1:3);
julia> @tropical min(1, x[1], x[2], 2*x[3])
x[3]^2 + x[1] + x[2] + (1)
```
"""
macro tropical(expr)
e = _tropicalize(expr)
return quote
$(esc(e))
end
end
_tropicalize(x::Symbol) = x
_tropicalize(x::Int) = x
function _tropicalize(x::Expr)
if x.head == :call
if x.args[1] == :min
x.args[1] = :(+)
elseif x.args[1] == :(*)
length(x.args) <= 3 || error("Cannot convert")
x.args[1] = :(Oscar._tropical_mul)
elseif x.args[1] == :(+)
x.args[1] = :*
else
error("Cannot convert")
end
for i in 2:length(x.args)
x.args[i] = _tropicalize(x.args[i])
end
else
return x
end
return x
end
function _tropical_mul(x, y)
if x isa Union{Integer, Rational, QQFieldElem, ZZRingElem}
if x isa Rational || x isa QQFieldElem
_x = ZZ(x)
return y^_x
else
return y^x
end
elseif y isa Union{Integer, Rational, QQFieldElem, ZZRingElem}
if y isa Rational | y isa QQFieldElem
_y = ZZ(y)
return x^_y
else
return x^y
end
else
error("Cannot convert ", x, " * ", y)
end
end
################################################################################
#
# Conversion to tropical polynomial
#
################################################################################
@doc raw"""
tropical_polynomial(f::Union{<:MPolyRingElem,<:PolyRingElem},nu::TropicalSemiringMap)
Given a polynomial `f` and a tropical semiring map `nu`,
return the tropicalization of `f` as a polynomial over the tropical semiring.
# Examples
```jldoctest
julia> R, (x,y) = polynomial_ring(QQ,["x", "y"])
(Multivariate polynomial ring in 2 variables over QQ, QQMPolyRingElem[x, y])
julia> nu = tropical_semiring_map(QQ,7)
Map into Min tropical semiring encoding the 7-adic valuation on Rational field
julia> f = 7*x+y+49
7*x + y + 49
julia> tropical_polynomial(f,nu)
(1)*x + y + (2)
```
"""
function tropical_polynomial(f::Union{<:MPolyRingElem,<:PolyRingElem}, nu::Union{Nothing,TropicalSemiringMap}=nothing)
# if unspecified, set nu to be the trivial valuation + min convention
isnothing(nu) && (nu = tropical_semiring_map(coefficient_ring(f)))
T = tropical_semiring(nu)
Tx,x = polynomial_ring(T,[repr(x) for x in gens(parent(f))])
tropf = inf(T)
for (c,alpha) in zip(coefficients(f), exponents(f))
tropf = tropf + nu(c)*monomial(Tx,alpha)
end
return tropf
end
################################################################################
#
# Basic functions
#
################################################################################
@doc raw"""
newton_subdivision(f::Generic.MPoly{TropicalSemiringElem{minOrMax}}) where minOrMax<:Union{typeof(min),typeof(max)}
Return the dual subdivision on `exponents(f)` with weights `coefficients(f)` (min-convention) or `-coefficients(f)` (max-convention). It is dual to `tropical_hypersurface(f)`.
# Examples
```jldoctest
julia> _, (x,y) = polynomial_ring(tropical_semiring(),["x", "y"]);
julia> f = 1+x+y+x^2;
julia> Deltaf = newton_subdivision(f)
Subdivision of points in ambient dimension 2
julia> points(Deltaf)
4-element SubObjectIterator{PointVector{QQFieldElem}}:
[2, 0]
[1, 0]
[0, 1]
[0, 0]
julia> maximal_cells(Deltaf)
2-element SubObjectIterator{Vector{Int64}}:
[2, 3, 4]
[1, 2, 3]
```
"""
function newton_subdivision(f::Generic.MPoly{TropicalSemiringElem{minOrMax}}) where minOrMax<:Union{typeof(min),typeof(max)}
# preserve_ordering=true, since weights of regular subdivisions have to be in min-convention,
# e.g., [0 0; 1 0; 0 1; 2 0] decomposed into [0 0; 1 0; 0 1] and [1 0; 0 1; 2 0] has min_weight [+1,0,0,0]
# which is dual to the tropical hypersurface of min(+1, x, y, 2*x) or max(-1, x, y, 2*x)
weights = QQ.(coefficients(f); preserve_ordering=true)
points = matrix(QQ,collect(exponents(f)))
return subdivision_of_points(points,weights)
end
@doc raw"""
newton_subdivision(f::MPolyRingElem, nu::Union{Nothing,TropicalSemiringMap}=nothing)
Return the dual subdivision on `exponents(f)` with weights `nu.(coefficients(f))` (min-convention) or `-nu.(coefficients(f))` (max-convention). It is dual to `tropical_hypersurface(f,nu)`.
# Examples
```jldoctest
julia> _, (x,y) = QQ["x", "y"];
julia> nu = tropical_semiring_map(QQ,2)
Map into Min tropical semiring encoding the 2-adic valuation on Rational field
julia> f = 2+x+y+x^2;
julia> Deltaf = newton_subdivision(f,nu)
Subdivision of points in ambient dimension 2
julia> points(Deltaf)
4-element SubObjectIterator{PointVector{QQFieldElem}}:
[2, 0]
[1, 0]
[0, 1]
[0, 0]
julia> maximal_cells(Deltaf)
2-element SubObjectIterator{Vector{Int64}}:
[2, 3, 4]
[1, 2, 3]
```
"""
function newton_subdivision(f::MPolyRingElem, nu::Union{Nothing,TropicalSemiringMap}=nothing)
tropf = tropical_polynomial(f,nu)
return newton_subdivision(tropf)
end
################################################################################
#
# Outdated code
#
################################################################################
# # Disabled due to potential clash with
# # tropical_polynomial(f::MPolyRingElem, nu::Union{Nothing,TropicalSemiringMap}=nothing)
# @doc raw"""
# tropical_polynomial(f::MPolyRingElem,M::Union{typeof(min),typeof(max)}=min)
# Given a polynomial `f` over a field with an intrinsic valuation (i.e., a field
# on which a function `valuation` is defined such as `PadicField(7,2)`),
# return the tropicalization of `f` as a polynomial over the min tropical semiring
# (default) or the max tropical semiring.
# # Examples
# ```jldoctest
# julia> K = PadicField(7, 2)
# Field of 7-adic numbers
# julia> Kxy, (x,y) = K["x", "y"]
# (Multivariate polynomial ring in 2 variables over QQ_7, AbstractAlgebra.Generic.MPoly{PadicFieldElem}[x, y])
# julia> f = 7*x+y+49
# (7^1 + O(7^3))*x + y + 7^2 + O(7^4)
# julia> tropical_polynomial(f,min)
# (1)*x + y + (2)
# julia> tropical_polynomial(f,max)
# (-1)*x + y + (-2)
# ```
# """
# function tropical_polynomial(f::MPolyRingElem, M::Union{typeof(min),typeof(max)}=min)
# T = tropical_semiring(M)
# if M==min
# s=1
# else
# s=-1
# end
# Tx,x = polynomial_ring(T,[repr(x) for x in gens(parent(f))])
# tropf = inf(T)
# if base_ring(parent(f)) isa NonArchLocalField
# for (c,alpha) in zip(coefficients(f), exponents(f))
# tropf = tropf + T(s*valuation(c))*monomial(Tx,alpha)
# end
# else
# for alpha in exponents(f)
# tropf = tropf + T(0)*monomial(Tx,alpha)
# end
# end
# return tropf
# end