-
Notifications
You must be signed in to change notification settings - Fork 53
/
Copy pathTaylorSeriesIAExt.jl
300 lines (259 loc) · 9.3 KB
/
TaylorSeriesIAExt.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
module TaylorSeriesIAExt
using TaylorSeries
import Base: ^, log, asin, acos, acosh, atanh, power_by_squaring
import TaylorSeries: evaluate, _evaluate, normalize_taylor, square
isdefined(Base, :get_extension) ? (using IntervalArithmetic) : (using ..IntervalArithmetic)
for T in (:Taylor1, :TaylorN)
@eval begin
@eval ^(a::$T{Interval{T}}, n::Integer) where {T<:Real} = TS.power_by_squaring(a, n)
@eval ^(a::$T{Interval{T}}, r::Rational) where {T<:Real} = a^float(r)
@eval function ^(a::$T{Interval{T}}, r::S) where {T<:Real, S<:Real}
isinteger(r) && r ≥ 0 && return TS.power_by_squaring(a, Integer(r))
a0 = constant_term(a) ∩ Interval(zero(T), T(Inf))
@assert !isempty(a0)
aux = one(a0^r)
a[0] = aux * a0
r == 0.5 && return sqrt(a)
if $T == TaylorN
if iszero(a0)
throw(DomainError(a,
"""The 0-th order TaylorN coefficient must be non-zero
in order to expand `^` around 0."""))
end
end
return TS._pow(a, r)
end
# _pow
function TS._pow(a::$T{Interval{S}}, n::Integer) where {S<:Real}
n < 0 && return TS._pow(a, float(n))
return TS.power_by_squaring(a, n)
end
function TS._pow(a::$T{Interval{T}}, r::S) where {T<:Real, S<:Real}
isinteger(r) && r ≥ 0 && return TS.power_by_squaring(a, Integer(r))
a0 = constant_term(a) ∩ Interval(zero(T), T(Inf))
@assert !isempty(a0)
aux = one(a0^r)
a[0] = aux * a0
r == 0.5 && return sqrt(a)
if $T == Taylor1
l0 = findfirst(a)
# Index of first non-zero coefficient of the result; must be integer
!isinteger(r*l0) && throw(DomainError(a,
"""The 0-th order Taylor1 coefficient must be non-zero
to raise the Taylor1 polynomial to a non-integer exponent."""))
lnull = trunc(Int, r*l0 )
(lnull > a.order) && return $T( zero(aux), a.order)
c_order = l0 == 0 ? a.order : min(a.order, trunc(Int, r*a.order))
else
if iszero(a0)
throw(DomainError(a,
"""The 0-th order TaylorN coefficient must be non-zero
in order to expand `^` around 0."""))
end
c_order = a.order
end
#
c = $T(zero(aux), c_order)
aux0 = zero(c)
for k in eachindex(c)
TS.pow!(c, a, aux0, r, k)
end
return c
end
function TS.pow!(res::$T{Interval{T}}, a::$T{Interval{T}}, aux::$T{Interval{T}},
r::S, k::Int) where {T<:Real, S<:Integer}
(r == 0) && return TS.one!(res, a, k)
(r == 1) && return TS.identity!(res, a, k)
(r == 2) && return TS.sqr!(res, a, k)
TS.power_by_squaring!(res, a, aux, r)
return nothing
end
end
end
for T in (:Taylor1, :TaylorN)
@eval function log(a::$T{Interval{S}}) where {S<:Real}
iszero(constant_term(a)) && throw(DomainError(a,
"""The 0-th order coefficient must be non-zero in order to expand `log` around 0."""))
a0 = constant_term(a) ∩ Interval(S(0.0), S(Inf))
order = a.order
aux = log(a0)
aa = one(aux) * a
aa[0] = one(aux) * a0
c = $T( aux, order )
for k in eachindex(a)
TS.log!(c, aa, k)
end
return c
end
@eval function asin(a::$T{Interval{S}}) where {S<:Real}
a0 = constant_term(a) ∩ Interval(-one(S), one(S))
a0^2 == one(a0) && throw(DomainError(a,
"""Series expansion of asin(x) diverges at x = ±1."""))
order = a.order
aux = asin(a0)
aa = one(aux) * a
aa[0] = one(aux) * a0
c = $T( aux, order )
r = $T( sqrt(1 - a0^2), order )
for k in eachindex(a)
TS.asin!(c, aa, r, k)
end
return c
end
@eval function acos(a::$T{Interval{S}}) where {S<:Real}
a0 = constant_term(a) ∩ Interval(-one(S), one(S))
a0^2 == one(a0) && throw(DomainError(a,
"""Series expansion of asin(x) diverges at x = ±1."""))
order = a.order
aux = acos(a0)
aa = one(aux) * a
aa[0] = one(aux) * a0
c = $T( aux, order )
r = $T( sqrt(1 - a0^2), order )
for k in eachindex(a)
TS.acos!(c, aa, r, k)
end
return c
end
@eval function acosh(a::$T{Interval{S}}) where {S<:Real}
a0 = constant_term(a) ∩ Interval(one(S), S(Inf))
a0^2 == one(a0) && throw(DomainError(a,
"""Series expansion of acosh(x) diverges at x = ±1."""))
order = a.order
aux = acosh(a0)
aa = one(aux) * a
aa[0] = one(aux) * a0
c = $T( aux, order )
r = $T( sqrt(a0^2 - 1), order )
for k in eachindex(a)
TS.acosh!(c, aa, r, k)
end
return c
end
@eval function atanh(a::$T{Interval{S}}) where {S<:Real}
order = a.order
a0 = constant_term(a) ∩ Interval(-one(S), one(S))
aux = atanh(a0)
aa = one(aux) * a
aa[0] = one(aux) * a0
c = $T( aux, order)
r = $T(one(aux) - a0^2, order)
iszero(constant_term(r)) && throw(DomainError(a,
"""Series expansion of atanh(x) diverges at x = ±1."""))
for k in eachindex(a)
TS.atanh!(c, aa, r, k)
end
return c
end
end
function evaluate(a::Taylor1, dx::Interval{S}) where {S<:Real}
order = a.order
uno = one(dx)
dx2 = dx^2
if iseven(order)
kend = order-2
@inbounds sum_even = a[end]*uno
@inbounds sum_odd = a[end-1]*zero(dx)
else
kend = order-3
@inbounds sum_odd = a[end]*uno
@inbounds sum_even = a[end-1]*uno
end
@inbounds for k in kend:-2:0
sum_odd = sum_odd*dx2 + a[k+1]
sum_even = sum_even*dx2 + a[k]
end
return sum_even + sum_odd*dx
end
function evaluate(a::TaylorN, dx::IntervalBox{N,T}) where {T<:Real,N}
@assert N == get_numvars()
a_length = length(a)
suma = zero(constant_term(a)) + Interval{T}(0, 0)
@inbounds for homPol in reverse(eachindex(a))
suma += evaluate(a[homPol], dx)
end
return suma
end
function evaluate(a::HomogeneousPolynomial, dx::IntervalBox{N,T}) where {T<:Real,N}
@assert N == get_numvars()
dx == IntervalBox(-1..1, Val(N)) && return _evaluate(a, dx, Val(true))
dx == IntervalBox( 0..1, Val(N)) && return _evaluate(a, dx, Val(false))
return evaluate(a, dx...)
end
function _evaluate(a::HomogeneousPolynomial, dx::IntervalBox{N,T}, ::Val{true} ) where {T<:Real,N}
a.order == 0 && return a[1] + Interval{T}(0, 0)
ct = TS.coeff_table[a.order+1]
@inbounds suma = a[1]*Interval{T}(0,0)
Ieven = Interval{T}(0,1)
for (i, a_coeff) in enumerate(a.coeffs)
iszero(a_coeff) && continue
if isodd(sum(ct[i]))
tmp = dx[1]
else
tmp = Ieven
for n in eachindex(ct[i])
iseven(ct[i][n]) && continue
tmp *= dx[1]
end
end
suma += a_coeff * tmp
end
return suma
end
function _evaluate(a::HomogeneousPolynomial, dx::IntervalBox{N,T}, ::Val{false} ) where {T<:Real,N}
a.order == 0 && return a[1] + Interval{T}(0, 0)
@inbounds suma = zero(a[1])*dx[1]
@inbounds for homPol in a.coeffs
suma += homPol*dx[1]
end
return suma
end
"""
normalize_taylor(a::Taylor1, I::Interval, symI::Bool=true)
Normalizes `a::Taylor1` such that the interval `I` is mapped
by an affine transformation to the interval `-1..1` (`symI=true`)
or to `0..1` (`symI=false`).
"""
normalize_taylor(a::Taylor1, I::Interval{T}, symI::Bool=true) where {T<:Real} =
_normalize(a, I, Val(symI))
"""
normalize_taylor(a::TaylorN, I::IntervalBox, symI::Bool=true)
Normalize `a::TaylorN` such that the intervals in `I::IntervalBox`
are mapped by an affine transformation to the intervals `-1..1`
(`symI=true`) or to `0..1` (`symI=false`).
"""
normalize_taylor(a::TaylorN, I::IntervalBox{N,T}, symI::Bool=true) where {T<:Real,N} =
_normalize(a, I, Val(symI))
# I -> -1..1
function _normalize(a::Taylor1, I::Interval{T}, ::Val{true}) where {T<:Real}
order = get_order(a)
t = Taylor1(T, order)
tnew = mid(I) + t*radius(I)
return a(tnew)
end
# I -> 0..1
function _normalize(a::Taylor1, I::Interval{T}, ::Val{false}) where {T<:Real}
order = get_order(a)
t = Taylor1(T, order)
tnew = inf(I) + t*diam(I)
return a(tnew)
end
# I -> IntervalBox(-1..1, Val(N))
function _normalize(a::TaylorN, I::IntervalBox{N,T}, ::Val{true}) where {T<:Real,N}
order = get_order(a)
x = Vector{typeof(a)}(undef, N)
for ind in eachindex(x)
x[ind] = mid(I[ind]) + TaylorN(ind, order=order)*radius(I[ind])
end
return a(x)
end
# I -> IntervalBox(0..1, Val(N))
function _normalize(a::TaylorN, I::IntervalBox{N,T}, ::Val{false}) where {T<:Real,N}
order = get_order(a)
x = Vector{typeof(a)}(undef, N)
for ind in eachindex(x)
x[ind] = inf(I[ind]) + TaylorN(ind, order=order)*diam(I[ind])
end
return a(x)
end
end