Skip to content

Commit

Permalink
For ^(a::Taylor1, n::Integer) fall back to a^float(n) (#250)
Browse files Browse the repository at this point in the history
* Move a method oF ^ to intervals.jl

* Fall back ^(a::Taylor1, n::Integer) to a^float(n) and fix a test

This solves a subtle type unstability.

* Bump a patch version
  • Loading branch information
lbenet authored Oct 2, 2020
1 parent 6c98fff commit 0937596
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 32 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "TaylorSeries"
uuid = "6aa5eb33-94cf-58f4-a9d0-e4b2c4fc25ea"
repo = "https://github.com/JuliaDiff/TaylorSeries.jl.git"
version = "0.10.7"
version = "0.10.8"

[deps]
InteractiveUtils = "b77e0a4c-d291-57a0-90e8-8db25a27a240"
Expand Down
2 changes: 1 addition & 1 deletion src/dictmutfunct.jl
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ const _dict_binary_ops = Dict(
:- => [:subst!, (:_res, :_arg1, :_arg2, :_k), :(_res = _arg1 - _arg2)],
:* => [:mul!, (:_res, :_arg1, :_arg2, :_k), :(_res = _arg1 * _arg2)],
:/ => [:div!, (:_res, :_arg1, :_arg2, :_k), :(_res = _arg1 / _arg2)],
:^ => [:pow!, (:_res, :_arg1, :_arg2, :_k), :(_res = _arg1 ^ _arg2)],
:^ => [:pow!, (:_res, :_arg1, :_arg2, :_k), :(_res = _arg1 ^ float(_arg2))],
);

"""
Expand Down
11 changes: 11 additions & 0 deletions src/intervals.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
using .IntervalArithmetic

# Method used for Taylor1{Interval{T}}^n
function ^(a::Taylor1{T}, n::Integer) where {T<:Interval}
n == 0 && return one(a)
n == 1 && return copy(a)
n == 2 && return square(a)
n < 0 && return a^float(n)
return power_by_squaring(a, n)
end



function evaluate(a::Taylor1, dx::Interval)
order = a.order
uno = one(dx)
Expand Down
31 changes: 4 additions & 27 deletions src/power.jl
Original file line number Diff line number Diff line change
Expand Up @@ -15,34 +15,11 @@ function ^(a::HomogeneousPolynomial, n::Integer)
return power_by_squaring(a, n)
end

#= The following three methods are coded like that, to use
preferentially a^float(n), but for cases like Taylor1{Interval{T}}^n
power_by_squaring is used. The latter is important when the
0-th order coefficient is/contains zero.
#= The following method computes `a^float(n)` (except for cases like
Taylor1{Interval{T}}^n, where `power_by_squaring` is used), to
use internally `pow!`.
=#
function ^(a::Taylor1{T}, n::Integer) where {T<:Number}
n == 0 && return one(a)
n == 1 && return copy(a)
n == 2 && return square(a)
return a^float(n)
end

# Method used for Taylor1{Interval{T}}^n
function ^(a::Taylor1{T}, n::Integer) where {T<:Real}
n == 0 && return one(a)
n == 1 && return copy(a)
n == 2 && return square(a)
n < 0 && return a^float(n)
return power_by_squaring(a, n)
end

function ^(a::Taylor1{T}, n::Integer) where {T<:AbstractFloat}
n == 0 && return one(a)
n == 1 && return copy(a)
n == 2 && return square(a)
return a^float(n)
end

^(a::Taylor1, n::Integer) = a^float(n)

function ^(a::TaylorN{T}, n::Integer) where {T<:Number}
n == 0 && return one(a)
Expand Down
10 changes: 7 additions & 3 deletions test/broadcasting.jl
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,9 @@ using Test
@. ts = 3 * t^2 - 1
@test ts == 3 * t^2 - 1

tt = Taylor1([zero(t), one(t)], 2)
# `tt` has to be `Taylor1{Taylor1{Float64}}` (instead of `Taylor1{Taylor1{Int}}`)
# since the method a^n (n integer) is equivalent to `a^float(n).`
tt = Taylor1([zero(1.0*t), one(t)], 2)
tts = zero(tt)
@test tt .== tt
@. tts = 3 * tt^2 - 1
Expand All @@ -51,8 +53,10 @@ using Test
ttt = Taylor1([zero(tt), one(tt)])
ttts = zero(ttt)
@test ttt .≈ ttt
@. ttts = 3 * ttt^2 - 1
@test ttts == -1
@. ttts = 3 * ttt^1 - 1
@test ttts == 3 * ttt^1 - 1
@. ttts = 3 * ttt^3 - 1
@test ttts == - 1.0
end

@testset "Broadcasting with HomogeneousPolynomial and TaylorN" begin
Expand Down

2 comments on commit 0937596

@lbenet
Copy link
Member Author

@lbenet lbenet commented on 0937596 Oct 2, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/22332

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.10.8 -m "<description of version>" 0937596692f79f84cb55b00123117632e913112d
git push origin v0.10.8

Please sign in to comment.