Skip to content

Commit

Permalink
More fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
lbenet committed Aug 28, 2024
1 parent 8acc4ff commit 7879ceb
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 24 deletions.
19 changes: 11 additions & 8 deletions ext/TaylorSeriesIAExt.jl
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,25 @@ import TaylorSeries: evaluate, _evaluate, normalize_taylor, square
isdefined(Base, :get_extension) ? (using IntervalArithmetic) : (using ..IntervalArithmetic)


# TS._pow
# _pow
for T in (:Taylor1, :TaylorN)
@eval begin
# Use power_by_squaring!
# Uses TS.power_by_squaring! through `pow!`
function TS._pow(a::$T{Interval{S}}, n::Integer) where {S<:Real}
aux = one(constant_term(a))^n
a0 = constant_term(a)
aux = one(a0)^n
a[0] = aux * a0
c = $T( zero(aux), a.order)
caux = zero(c)
TS.power_by_squaring!(c, a, caux, n)
# TS.power_by_squaring!(c, a, caux, n)
TS.pow!(c, a, caux, n, 1)
return c
end
end
end

function TS._pow(a::Taylor1{Interval{T}}, r::S) where {T<:Real, S<:Real}
isinteger(r) && r >= 0 && return a^Int(r)
isinteger(r) && r >= 0 && return TS._pow(a, Int(r))
a0 = constant_term(a) Interval(zero(T), T(Inf))
aux = one(a0)^r
a[0] = aux * a0
Expand All @@ -34,15 +37,15 @@ function TS._pow(a::Taylor1{Interval{T}}, r::S) where {T<:Real, S<:Real}
(lnull > a.order) && return Taylor1( zero(aux), a.order)
c_order = l0 == 0 ? a.order : min(a.order, trunc(Int, r*a.order))
c = Taylor1(zero(aux), c_order)
aux0 = deepcopy(c)
aux0 = zero(c)
for k in eachindex(c)
TS.pow!(c, a, aux0, r, k)
end
return c
end

function TS._pow(a::TaylorN{Interval{T}}, r::S) where {T<:Real, S<:Real}
isinteger(r) && r >= 0 && return a^Int(r)
isinteger(r) && r >= 0 && return TS._pow(a, Int(r))
a0 = constant_term(a) Interval(zero(T), T(Inf))
aux = one(a0^r)
a[0] = aux * a0
Expand All @@ -59,7 +62,7 @@ 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."""))
"""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)
Expand Down
32 changes: 16 additions & 16 deletions src/power.jl
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,9 @@ for T in (:Taylor1, :TaylorN)
@eval ^(a::$T, z::T) where {T<:Complex} = exp( z*log(a) )
end

^(a::Taylor1{TaylorN{T}}, n::Integer) where {T<:NumberNotSeries} =
a^float(n)
^(a::Taylor1{TaylorN{T}}, n::Integer) where {T<:NumberNotSeries} = a^float(n)

^(a::Taylor1{TaylorN{T}}, r::Rational) where {T<:NumberNotSeries} =
a^float(r)
^(a::Taylor1{TaylorN{T}}, r::Rational) where {T<:NumberNotSeries} = a^float(r)


## Real power ##
Expand Down Expand Up @@ -87,7 +85,7 @@ function _pow(a::Taylor1{T}, r::S) where {T<:Number, S<:Real}
(lnull > a.order) && return Taylor1( zero(aux), a.order)
c_order = l0 == 0 ? a.order : min(a.order, trunc(Int, r*a.order))
c = Taylor1(zero(aux), c_order)
aux0 = deepcopy(c)
aux0 = zero(c)
for k in eachindex(c)
pow!(c, a, aux0, r, k)
end
Expand Down Expand Up @@ -272,22 +270,24 @@ end
end

# Uses power_by_squaring!
@inline function pow!(res::TaylorN{T}, a::TaylorN{T}, aux::TaylorN{T},
r::S, k::Int) where {T<:NumberNotSeriesN, S<:Integer}
(r == 0) && return one!(res, a, k)
(r == 1) && return identity!(res, a, k)
(r == 2) && return sqr!(res, a, k)
power_by_squaring!(res, a, aux, r)
return nothing
for T in (:Taylor1, :TaylorN)
@eval @inline function pow!(res::$T{T}, a::$T{T}, aux::$T{T},
r::S, k::Int) where {T<:NumberNotSeriesN, S<:Integer}
(r == 0) && return one!(res, a, k)
(r == 1) && return identity!(res, a, k)
(r == 2) && return sqr!(res, a, k)
power_by_squaring!(res, a, aux, r)
return nothing
end
end

@inline function pow!(res::Taylor1{TaylorN{T}}, a::Taylor1{TaylorN{T}},
aux::Taylor1{TaylorN{T}}, r::S, ordT::Int) where
{T<:NumberNotSeries, S<:Real}
(r == 0) && return one!(c, a, k)
(r == 1) && return identity!(c, a, k)
(r == 2) && return sqr!(c, a, k)
(r == 0.5) && return sqrt!(c, a, k)
(r == 0) && return one!(res, a, ordT)
(r == 1) && return identity!(res, a, ordT)
(r == 2) && return sqr!(res, a, ordT)
(r == 0.5) && return sqrt!(res, a, ordT)
# Sanity
zero!(res, ordT)
# First non-zero coefficient
Expand Down

0 comments on commit 7879ceb

Please sign in to comment.