-
Notifications
You must be signed in to change notification settings - Fork 17
/
arithmetic.jl
355 lines (286 loc) · 11.1 KB
/
arithmetic.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
345
346
347
348
349
350
351
352
353
354
355
# arithmetic.jl
# Addition, substraction and other functions
for TM in tupleTMs
@eval begin
tmdata(f::$TM) = (expansion_point(f), domain(f))
zero(a::$TM) = $TM(zero(a.pol), zero(remainder(a)), expansion_point(a), domain(a))
one(a::$TM) = $TM(one(a.pol), zero(remainder(a)), expansion_point(a), domain(a))
# iszero(a::$TM) = iszero(a.pol) && iszero(zero(remainder(a)))
findfirst(a::$TM) = findfirst(a.pol)
==(a::$TM, b::$TM) =
a.pol == b.pol && remainder(a) == remainder(b) && tmdata(a) == tmdata(b)
# expansion_point(a) == expansion_point(b) && domain(a) == domain(b)
# Addition
+(a::$TM) = $TM(a.pol, remainder(a), expansion_point(a), domain(a))
function +(a::$TM, b::$TM)
a, b = fixorder(a, b)
return $TM(a.pol+b.pol, remainder(a)+remainder(b), expansion_point(a), domain(a))
end
+(a::$TM, b::T) where {T<:NumberNotSeries} = $TM(a.pol+b, remainder(a),
expansion_point(a), domain(a))
+(b::T, a::$TM) where {T<:NumberNotSeries} = $TM(b+a.pol, remainder(a),
expansion_point(a), domain(a))
# Substraction
-(a::$TM) = $TM(-a.pol, -remainder(a), expansion_point(a), domain(a))
function -(a::$TM, b::$TM)
a, b = fixorder(a, b)
return $TM(a.pol-b.pol, remainder(a)-remainder(b), expansion_point(a), domain(a))
end
-(a::$TM, b::T) where {T<:NumberNotSeries} = $TM(a.pol-b, remainder(a),
expansion_point(a), domain(a))
-(b::T, a::$TM) where {T<:NumberNotSeries} = $TM(b-a.pol, -remainder(a),
expansion_point(a), domain(a))
# Basic division
function basediv(a::$TM, b::$TM)
# invb = rpa(x->inv(x), b)
invb = inv(b)
return a * invb
end
# Multiplication by numbers
*(a::$TM, b::T) where {T<:NumberNotSeries} = $TM(a.pol*b, b*remainder(a),
expansion_point(a), domain(a))
*(b::T, a::$TM) where {T<:NumberNotSeries} = $TM(a.pol*b, b*remainder(a),
expansion_point(a), domain(a))
# Multiplication
function *(a::$TM, b::$TM)
@assert tmdata(a) == tmdata(b)
a_order = get_order(a)
b_order = get_order(b)
rnegl_order = a_order + b_order
aux = centered_dom(a)
# Returned polynomial
a_pol = polynomial(a)
b_pol = polynomial(b)
res = a_pol * b_pol
order = get_order(res)
# Remainder of the product
if $TM == TaylorModel1
# Remaining terms of the product as reduced Taylor1 (factored polynomial)
rnegl = Taylor1(zero(res[0]), rnegl_order)
for k in order+1:rnegl_order
@inbounds for i = 0:k
(i > a_order || k-i > b_order) && continue
rnegl[k] += a_pol[i] * b_pol[k-i]
end
end
# Bound for the neglected part of the product of polynomials
Δnegl = rnegl(aux)
Δ = remainder_product(a, b, aux, Δnegl)
else
# Remaining terms of the product as reduced Taylor1 (factored polynomial)
rnegl = Taylor1(zero(res[0]), rnegl_order-order)
for k in order+1:rnegl_order
@inbounds for i = 0:k
(i > a_order || k-i > b_order) && continue
rnegl[k-order-1] += a_pol[i] * b_pol[k-i]
end
end
Δnegl = rnegl(aux)
Δ = remainder_product(a, b, aux, Δnegl, order)
end
return $TM(res, Δ, expansion_point(a), domain(a))
end
# Division by numbers
/(a::$TM, b::T) where {T<:NumberNotSeries} = a * inv(b)
/(b::T, a::$TM) where {T<:NumberNotSeries} = b * inv(a)
# Power
function ^(a::$TM, r::Number)
r == zero(r) && return one(a)
r == 1 && return a
r == 2 && return a*a
return rpa(x->x^r, a)
end
function ^(a::$TM, n::Integer)
n == 0 && return one(a)
n == 1 && return a
n == 2 && return a*a
return rpa(x->x^n, a)
end
end
end
# Remainder of the product
# TaylorModel1
function remainder_product(a, b, aux, Δnegl)
Δa = a.pol(aux)
Δb = b.pol(aux)
a_rem = remainder(a)
b_rem = remainder(b)
Δ = Δnegl + Δb * a_rem + Δa * b_rem + a_rem * b_rem
return Δ
end
function remainder_product(a::TaylorModel1{TaylorN{T}, S},
b::TaylorModel1{TaylorN{T}, S},
auxT, Δnegl) where {T, S}
N = get_numvars()
# An N-dimensional symmetrical IntervalBox is assumed
# to bound the TaylorN part
auxQ = IntervalBox(-1 .. 1, Val(N))
Δa = a.pol(auxT)(auxQ)
Δb = b.pol(auxT)(auxQ)
a_rem = remainder(a)
b_rem = remainder(b)
Δ = Δnegl(auxQ) + Δb * a_rem + Δa * b_rem + a_rem * b_rem
return Δ
end
function remainder_product(a::TaylorModel1{TaylorModelN{N,T,S},S},
b::TaylorModel1{TaylorModelN{N,T,S},S}, aux, Δnegl) where {N,T,S}
Δa = a.pol(aux)
Δb = b.pol(aux)
a_rem = remainder(a)
b_rem = remainder(b)
Δ = Δnegl + Δb * a_rem + Δa * b_rem + a_rem * b_rem
# Evaluate at the TMN centered domain
auxN = centered_dom(a[0])
ΔN = Δ(auxN)
return ΔN
end
# RTaylorModel1
function remainder_product(a, b, aux, Δnegl, order)
Δa = a.pol(aux)
Δb = b.pol(aux)
V = aux^(order+1)
a_rem = remainder(a)
b_rem = remainder(b)
Δ = Δnegl + Δb * a.rem + Δa * b.rem + a.rem * b.rem * V
return Δ
end
function remainder_product(a::RTaylorModel1{TaylorN{T},S}, b::RTaylorModel1{TaylorN{T},S},
aux, Δnegl, order) where {T, S}
N = get_numvars()
# An N-dimensional symmetrical IntervalBox is assumed
# to bound the TaylorN part
auxQ = symmetric_box(N, T)
Δa = a.pol(aux)(auxQ)
Δb = b.pol(aux)(auxQ)
V = aux^(order+1)
a_rem = remainder(a)
b_rem = remainder(b)
Δ = Δnegl(auxQ) + Δb * a_rem + Δa * b_rem + a_rem * b_rem * V
return Δ
end
# Division
function /(a::TaylorModel1, b::TaylorModel1)
@assert tmdata(a) == tmdata(b)
return basediv(a, b)
end
function /(a::RTaylorModel1, b::RTaylorModel1)
@assert tmdata(a) == tmdata(b)
# DetermineRootOrderUpperBound seems equivalent (optimized?) to `findfirst`
bk = findfirst(b)
bk ≤ 0 && return basediv(a, b)
# In 2.3.12, `a` and `b` are now extended in order by `bk`,
# but how can we do this without knowing the explicit function
# that created them?
#
# Below we reduce the original order by bk.
#
order = get_order(a)
ared = truncate_taylormodel(
RTaylorModel1(Taylor1(a.pol.coeffs[bk+1:order+1]), remainder(a),
expansion_point(a), domain(a)), order-bk)
order = get_order(b)
bred = truncate_taylormodel(
RTaylorModel1(Taylor1(b.pol.coeffs[bk+1:order+1]), remainder(b),
expansion_point(b), domain(b)), order-bk)
return basediv( ared, bred )
end
"""
truncate_taylormodel(a::RTaylorModel1, m::Integer)
Truncates `a::RTaylorModel1` to order `m`.
"""
function truncate_taylormodel(a::RTaylorModel1, m::Integer)
order = get_order(a)
m ≥ order && return a
apol_coeffs = polynomial(a).coeffs
apol = Taylor1(copy(apol_coeffs[1:m+1]))
bpol = Taylor1(copy(apol_coeffs))
aux = centered_dom(a)
Δnegl = bound_truncation(RTaylorModel1, bpol, aux, m)
Δ = Δnegl + remainder(a) * (aux)^(order-m)
return RTaylorModel1( apol, Δ, expansion_point(a), domain(a) )
end
# Same as above, for TaylorModelN
tmdata(f::TaylorModelN) = (expansion_point(f), domain(f))
zero(a::TaylorModelN) = TaylorModelN(zero(a.pol), zero(remainder(a)),
expansion_point(a), domain(a))
one(a::TaylorModelN) = TaylorModelN(one(a.pol), zero(remainder(a)),
expansion_point(a), domain(a))
# iszero(a::TaylorModelN) = iszero(a.pol) && iszero(zero(remainder(a)))
findfirst(a::TaylorModelN) = findfirst(a.pol)
==(a::TaylorModelN, b::TaylorModelN) =
a.pol == b.pol && remainder(a) == remainder(b) && tmdata(a) == tmdata(b)
# expansion_point(a) == expansion_point(b) && domain(a) == domain(b)
# Addition and substraction
for op in (:+, :-)
@eval begin
$(op)(a::TaylorModelN) = TaylorModelN($(op)(a.pol), $(op)(remainder(a)),
expansion_point(a), domain(a))
function $(op)(a::TaylorModelN, b::TaylorModelN)
a, b = fixorder(a, b)
return TaylorModelN($(op)(a.pol, b.pol), $(op)(remainder(a), remainder(b)),
expansion_point(a), domain(a))
end
$(op)(a::TaylorModelN, b::T) where {T<:NumberNotSeries} =
TaylorModelN($(op)(a.pol, b), remainder(a), expansion_point(a), domain(a))
$(op)(b::T, a::TaylorModelN) where {T<:NumberNotSeries} = TaylorModelN($(
op)(b, a.pol), $(op)(remainder(a)), expansion_point(a), domain(a))
end
end
# Multiplication
function *(a::TaylorModelN, b::TaylorModelN)
@assert tmdata(a) == tmdata(b)
a_order = get_order(a)
b_order = get_order(b)
rnegl_order = a_order + b_order
@assert rnegl_order ≤ get_order()
aux = centered_dom(a)
# Returned polynomial
a_pol = polynomial(a)
b_pol = polynomial(b)
res = a_pol * b_pol
order = get_order(res)
# Remaing terms of the product
vv = Array{HomogeneousPolynomial{TS.numtype(res)}}(undef, rnegl_order-order)
suma = Array{promote_type(TS.numtype(res), TS.numtype(domain(a)))}(undef, rnegl_order-order)
for k in order+1:rnegl_order
vv[k-order] = HomogeneousPolynomial(zero(TS.numtype(res)), k)
@inbounds for i = 0:k
(i > a_order || k-i > b_order) && continue
TaylorSeries.mul!(vv[k-order], a_pol[i], b_pol[k-i])
end
suma[k-order] = vv[k-order](aux)
end
# Bound for the neglected part of the product of polynomials
Δnegl = sum( sort!(suma, by=abs2) )
Δ = remainder_product(a, b, aux, Δnegl)
return TaylorModelN(res, Δ, expansion_point(a), domain(a))
end
# Multiplication by numbers
function *(b::T, a::TaylorModelN) where {T<:NumberNotSeries}
pol = a.pol * b
rem = b * remainder(a)
return TaylorModelN(pol, rem, expansion_point(a), domain(a))
end
*(a::TaylorModelN, b::T) where {T<:NumberNotSeries} = b * a
# Basic division
function basediv(a::TaylorModelN, b::TaylorModelN)
invb = rpa(x->inv(x), b)
return a * invb
end
# /(a::TaylorModelN, b::TaylorModelN) = basediv(a, b)
# Division by numbers
/(a::TaylorModelN, b::T) where {T<:Number} = a * inv(b)
/(b::T, a::TaylorModelN) where {T<:NumberNotSeries} = b * inv(a)
# Power
function ^(a::TaylorModelN{N,T,S}, r::Number) where {N,T,S}
r == 0 && return one(a)
r == 1 && return a
r == 2 && return a*a
return rpa(x->x^r, a)
end
function ^(a::TaylorModelN{N,T,S}, n::Integer) where {N,T,S}
n == 0 && return one(a)
n == 1 && return a
n == 2 && return a*a
return rpa(x->x^n, a)
end