-
Notifications
You must be signed in to change notification settings - Fork 17
/
tropicalnumber.jl
159 lines (142 loc) · 7.2 KB
/
tropicalnumber.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
@testset "Polymake.TropicalNumber" begin
NumberTypes = [Int32, Int64, UInt64, BigInt, Float32, Float64, BigFloat, Polymake.Integer, Polymake.Rational]
AdditionTypes = [Polymake.Min, Polymake.Max]
@testset "Constructors/Conversions" begin
for T in [NumberTypes; Polymake.TropicalNumber{Polymake.Min}; Polymake.TropicalNumber{Polymake.Max}]
@test_throws ArgumentError Polymake.TropicalNumber(T(2))
end
for A in AdditionTypes
# constructors
for T in [NumberTypes; Polymake.TropicalNumber{Polymake.Min}; Polymake.TropicalNumber{Polymake.Max}]
@test Polymake.TropicalNumber{A}(T(1)) isa Polymake.TropicalNumber
@test Polymake.TropicalNumber{A}(T(1)) isa Polymake.TropicalNumber{A}
end
a = Polymake.TropicalNumber{A}(5)
# no copy conversion:
@test convert(Polymake.TropicalNumber{A}, a) === a
for R in [Base.Rational{Polymake.Int}, Polymake.Rational]
r = R(1//3)
@test convert(Polymake.TropicalNumber{Polymake.Max}, r) isa Polymake.TropicalNumber
tr = convert(Polymake.TropicalNumber{Polymake.Max}, r)
@test convert(R, tr) isa R
@test convert(R, tr) == r
end
# julia arrays
@test Vector{Any}([a,1])[1] isa Polymake.TropicalNumberAllocated
@test [a,a] isa Vector{Polymake.TropicalNumberAllocated{A,Polymake.Rational}}
end
end
@testset "Arithmetic" begin
for A in AdditionTypes
@testset "(In-)Equality $A" begin
a = Polymake.TropicalNumber{A}(Polymake.Rational(5))
for T in [NumberTypes; Polymake.TropicalNumber{Polymake.Min}; Polymake.TropicalNumber{Polymake.Max}]
b = Polymake.TropicalNumber{A}(T(5))
@test a == b
@test b == a
end
b = Polymake.TropicalNumber{A}(17)
@test a == a
@test a <= a
@test a >= a
@test a != b
@test a <= b
@test a < b
@test b >= a
@test b > a
end
@testset "Multiplication $A" begin
a = Polymake.TropicalNumber{A}(5)
b = Polymake.TropicalNumber{A}(17)
@test a * b isa Polymake.TropicalNumber{A}
@test a * b == b * a == Polymake.TropicalNumber{A}(22)
a *= b
@test a == Polymake.TropicalNumber{A}(22)
end
@testset "Division $A" begin
a = Polymake.TropicalNumber{A}(5)
b = Polymake.TropicalNumber{A}(17)
@test a // b isa Polymake.TropicalNumber{A}
@test a // b == Polymake.TropicalNumber{A}(-12)
@test b // a == Polymake.TropicalNumber{A}(12)
@test a / b isa Polymake.TropicalNumber{A}
@test a / b == Polymake.TropicalNumber{A}(-12)
@test b / a == Polymake.TropicalNumber{A}(12)
a //= b
@test a == Polymake.TropicalNumber{A}(-12)
a /= b
@test a == Polymake.TropicalNumber{A}(-29)
end
end
@testset "Addition" begin
a = Polymake.TropicalNumber{Polymake.Min}(5)
b = Polymake.TropicalNumber{Polymake.Min}(17)
c = Polymake.TropicalNumber{Polymake.Max}(5)
d = Polymake.TropicalNumber{Polymake.Max}(17)
@test a + b isa Polymake.TropicalNumber{Polymake.Min}
@test a + b == b + a == a
@test c + d isa Polymake.TropicalNumber{Polymake.Max}
@test c + d == d + c == d
a += b
@test a == Polymake.TropicalNumber{Polymake.Min}(5)
b += a
@test b == a
d += c
@test d == Polymake.TropicalNumber{Polymake.Max}(17)
c += d
@test c == d
end
@testset "Catching mismatching parameters" begin
a = Polymake.TropicalNumber{Polymake.Min}(5)
b = Polymake.TropicalNumber{Polymake.Max}(17)
@test_throws DomainError a + b
@test_throws DomainError b + a
@test_throws DomainError a * b
@test_throws DomainError b * a
@test_throws DomainError a // b
@test_throws DomainError b // a
@test_throws DomainError a / b
@test_throws DomainError b / a
@test_throws DomainError a < b
@test_throws DomainError b < a
@test_throws DomainError a > b
@test_throws DomainError b > a
end
end
@testset "zero / one" begin
ZEROmin = Polymake.TropicalNumber{Polymake.Min}()
ONEmin = Polymake.TropicalNumber{Polymake.Min}(0)
DZEROmin = Polymake.TropicalNumber{Polymake.Min}(-Inf)
ZEROmax = Polymake.TropicalNumber{Polymake.Max}()
ONEmax = Polymake.TropicalNumber{Polymake.Max}(0)
DZEROmax = Polymake.TropicalNumber{Polymake.Max}(Inf)
@test one(ZEROmin) isa Polymake.TropicalNumber{Polymake.Min}
@test zero(ZEROmin) isa Polymake.TropicalNumber{Polymake.Min}
@test Polymake.dual_zero(ZEROmin) isa Polymake.TropicalNumber{Polymake.Min}
@test one(Polymake.TropicalNumber{Polymake.Min}) isa Polymake.TropicalNumber{Polymake.Min}
@test zero(Polymake.TropicalNumber{Polymake.Min}) isa Polymake.TropicalNumber{Polymake.Min}
@test Polymake.dual_zero(Polymake.TropicalNumber{Polymake.Min}) isa Polymake.TropicalNumber{Polymake.Min}
@test one(ZEROmax) isa Polymake.TropicalNumber{Polymake.Max}
@test zero(ZEROmax) isa Polymake.TropicalNumber{Polymake.Max}
@test Polymake.dual_zero(ZEROmax) isa Polymake.TropicalNumber{Polymake.Max}
@test one(Polymake.TropicalNumber{Polymake.Max}) isa Polymake.TropicalNumber{Polymake.Max}
@test zero(Polymake.TropicalNumber{Polymake.Max}) isa Polymake.TropicalNumber{Polymake.Max}
@test Polymake.dual_zero(Polymake.TropicalNumber{Polymake.Max}) isa Polymake.TropicalNumber{Polymake.Max}
@test zero(Polymake.TropicalNumber{Polymake.Min}) == zero(ONEmin) == ZEROmin
@test Polymake.dual_zero(Polymake.TropicalNumber{Polymake.Min}) == Polymake.dual_zero(ONEmin) == Polymake.TropicalNumber{Polymake.Min}(-Inf)
@test one(Polymake.TropicalNumber{Polymake.Min}) == one(ZEROmin) == ONEmin
@test zero(Polymake.TropicalNumber{Polymake.Max}) == zero(ONEmax) == ZEROmax
@test Polymake.dual_zero(Polymake.TropicalNumber{Polymake.Max}) == Polymake.dual_zero(ONEmax) == Polymake.TropicalNumber{Polymake.Max}(Inf)
@test one(Polymake.TropicalNumber{Polymake.Max}) == one(ZEROmax) == ONEmax
@test Polymake.orientation(Polymake.TropicalNumber{Polymake.Min}) == Polymake.orientation(ZEROmin) == -Polymake.orientation(Polymake.TropicalNumber{Polymake.Max}) == - Polymake.orientation(ZEROmax) == 1
end
@testset "Promotion (equality)" begin
for A in AdditionTypes
for T in NumberTypes
a = Polymake.TropicalNumber{A}(5)
@test a == T(5)
@test T(5) == a
end
end
end
end