Skip to content

Commit 5d73258

Browse files
authored
Improve nested Taylors (#381)
* Better display of Taylor1{Taylor1{...}}, and minor fixes in pretty_print * Fixes in promotion for nested Taylor1s, and total ordering * Add promotion methods for nested Taylor1, and refine tests * Add various methods involving nested Taylor1s and untangle some metaprogramming stuff * Methods for nested Taylor1s functions * Minor fixes in tests * Add tests for functions on nested Taylor1s * Minor fixes * Add specialized methods for pow!, sqr! and sqrt! * Comment seemingly unused (arithmetic) methods; minor fixes in abs * Minor improvements in power * Fix a bug in pow! * More minor fixes reducing use of memory * Improvements in evaluate and differentiate
1 parent a08f13b commit 5d73258

13 files changed

+1975
-857
lines changed

src/arithmetic.jl

Lines changed: 444 additions & 177 deletions
Large diffs are not rendered by default.

src/auxiliary.jl

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,17 @@ for T in (:HomogeneousPolynomial, :TaylorN)
291291
end
292292

293293

294+
## minlength
295+
@inline function minlength(a::Taylor1, b::Taylor1)
296+
length(eachindex(a)) < length(eachindex(b)) && return a
297+
return b
298+
end
299+
@inline function minlength(a::Taylor1, b::Taylor1, c::Taylor1)
300+
length(minlength(a, c)) < length(eachindex(b)) && return minlength(a, c)
301+
return b
302+
end
303+
304+
294305
## _isthinzero
295306
"""
296307
_isthinzero(x)

src/calculus.jl

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,15 +57,28 @@ p_k = (k+1) a_{k+1}.
5757
```
5858
5959
"""
60-
function differentiate!(p::Taylor1, a::Taylor1, k::Int)
60+
function differentiate!(p::Taylor1{T}, a::Taylor1{T}, k::Int) where
61+
{T<:NumberNotSeries}
6162
k >= a.order && return nothing
6263
@inbounds p[k] = (k+1)*a[k+1]
6364
return nothing
6465
end
6566
function differentiate!(p::Taylor1{TaylorN{T}}, a::Taylor1{TaylorN{T}}, k::Int) where
6667
{T<:NumberNotSeries}
6768
k >= a.order && return nothing
68-
@inbounds p[k] = (k+1)*a[k+1]
69+
@inbounds for ord in eachindex(p[k])
70+
zero!(p[k], ord)
71+
mul!(p[k], a[k+1], k+1, ord)
72+
end
73+
return nothing
74+
end
75+
function differentiate!(p::Taylor1{Taylor1{T}}, a::Taylor1{Taylor1{T}}, k::Int) where
76+
{T<:NumberNotSeriesN}
77+
k >= a.order && return nothing
78+
# @inbounds p[k] = (k+1)*a[k+1]
79+
@inbounds for ord in eachindex(p[k])
80+
mul!(p[k], a[k+1], k+1, ord)
81+
end
6982
return nothing
7083
end
7184

src/constructors.jl

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,3 +214,15 @@ const NumberNotSeriesN = Union{Real,Complex,Taylor1}
214214
## Additional Taylor1 and TaylorN outer constructor ##
215215
Taylor1{T}(x::S) where {T<:Number,S<:NumberNotSeries} = Taylor1([convert(T,x)], 0)
216216
TaylorN{T}(x::S) where {T<:Number,S<:NumberNotSeries} = TaylorN(convert(T, x), TaylorSeries.get_order())
217+
218+
219+
# """
220+
# get_numvars
221+
#
222+
# Return the number of variables of a `Taylor1`, `HomogeneousPolynomial`
223+
# or `TaylorN` object.
224+
# """
225+
get_numvars(t::Number) = 0
226+
get_numvars(t::Taylor1) = 1
227+
get_numvars(t::Taylor1{Taylor1{T}}) where {T<:Number} = get_numvars(t[0])+1
228+
get_numvars(::T) where {T<:Union{HomogeneousPolynomial, TaylorN}} = _params_TaylorN_.num_vars

src/conversion.jl

Lines changed: 33 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -187,20 +187,41 @@ promote_rule(::Type{Taylor1{TaylorN{T}}}, ::Type{TaylorN{Taylor1{S}}}) where
187187
promote_rule(::Type{TaylorN{Taylor1{T}}}, ::Type{Taylor1{TaylorN{S}}}) where
188188
{T<:NumberNotSeries, S<:NumberNotSeries} = Taylor1{TaylorN{promote_type(T,S)}}
189189

190-
# Nested Taylor1's
191-
function promote(a::Taylor1{Taylor1{T}}, b::Taylor1{T}) where {T<:NumberNotSeriesN}
192-
order_a = get_order(a)
193-
order_b = get_order(b)
194-
zb = zero(b)
195-
new_bcoeffs = similar(a.coeffs)
196-
new_bcoeffs[1] = b
197-
@inbounds for ind in 2:order_a+1
198-
new_bcoeffs[ind] = zb
190+
# Different nested Taylor1's promotion methods; consider to 4 levels of nesting
191+
let
192+
strucTT = :(Taylor1{T})
193+
strucTR = :(Taylor1{R})
194+
strucT = :(T)
195+
strucS = :(S)
196+
strucR = :(R)
197+
expr_elem = :(bnew[0])
198+
for j = 0:5
199+
for i = 1+j:6
200+
@eval function Base._promote(a::$strucTT, b::$strucS) where {T<:NumberNotSeries,
201+
S<:NumberNotSeries}
202+
R = promote_type(T, S)
203+
anew = convert($strucTR, a)
204+
bnew = zero(anew)
205+
$(expr_elem) = convert($strucR, b)
206+
return anew, bnew
207+
end
208+
@eval Base._promote(b::$strucS, a::$strucTT) where {T<:NumberNotSeries,
209+
S<:NumberNotSeries} = reverse(Base._promote(a, b))
210+
strucTT = :(Taylor1{$strucTT})
211+
strucTR = :(Taylor1{$strucTR})
212+
expr_elem = :(($(expr_elem))[0])
213+
end
214+
strucS = :(Taylor1{$strucS})
215+
strucR = :(Taylor1{$strucR})
216+
strucT = :(Taylor1{$strucT})
217+
strucTT = deepcopy(strucT)
218+
strucTT = :(Taylor1{$strucT})
219+
strucTR = deepcopy(strucR)
220+
strucTR = :(Taylor1{$strucTR})
221+
expr_elem = :(bnew[0])
199222
end
200-
return a, Taylor1(b, order_a)
201223
end
202-
promote(b::Taylor1{T}, a::Taylor1{Taylor1{T}}) where {T<:NumberNotSeriesN} =
203-
reverse(promote(a, b))
224+
204225

205226
# float
206227
float(::Type{Taylor1{T}}) where T<:Number = Taylor1{float(T)}

0 commit comments

Comments
 (0)