From 8e04fcd11fa4ddff70ba24c99fd5bb3908050382 Mon Sep 17 00:00:00 2001 From: Luis Benet Date: Mon, 16 Apr 2018 18:20:55 -0500 Subject: [PATCH 1/7] Add methods of ^, that distinguish Real from AbstractFloat --- src/power.jl | 24 ++++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/src/power.jl b/src/power.jl index 81297645..f30dd5a6 100644 --- a/src/power.jl +++ b/src/power.jl @@ -15,15 +15,35 @@ 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. +=# 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) instead of power_by_squaring(a,n) - # because it yields better accuracy 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 + + function ^(a::TaylorN{T}, n::Integer) where {T<:Number} n == 0 && return one(a) n == 1 && return copy(a) From 2f446fc3bf255097bef42e9045c319d8844250db Mon Sep 17 00:00:00 2001 From: Luis Benet Date: Mon, 16 Apr 2018 19:10:09 -0500 Subject: [PATCH 2/7] Add one new test --- test/onevariable.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/onevariable.jl b/test/onevariable.jl index 0c03238f..37a26b16 100644 --- a/test/onevariable.jl +++ b/test/onevariable.jl @@ -145,7 +145,7 @@ end @test (Rational(1,2)*tsquare)[2] == 1//2 @test t^2/tsquare == ot @test ((1+t)^(1/3))[2]+1/9 ≤ tol1 - @test 1-tsquare == (1+t)-t*(1+t) + @test (1.0-tsquare)^3 == (1.0-t)^3*(1.0+t)^3 @test (1-tsquare)^2 == (1+t)^2.0 * (1-t)^2.0 @test (sqrt(1+t))[2] == -1/8 @test ((1-tsquare)^(1//2))^2 == 1-tsquare From 0e2fca0a7c33b5bdf94bffc7cc430b1a81c26d51 Mon Sep 17 00:00:00 2001 From: Luis Benet Date: Tue, 17 Apr 2018 08:25:27 -0500 Subject: [PATCH 3/7] Add tests involving intervals --- test/intervals.jl | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 test/intervals.jl diff --git a/test/intervals.jl b/test/intervals.jl new file mode 100644 index 00000000..e33a7136 --- /dev/null +++ b/test/intervals.jl @@ -0,0 +1,37 @@ +# This file is part of TaylorSeries.jl, MIT licensed +# + +using TaylorSeries, IntervalArithmetic + +if VERSION < v"0.7.0-DEV.2004" + using Base.Test + eeuler = Base.e +else + using Test + eeuler = Base.MathConstants.e +end + +@testset "Tests Taylor1 and TaylorN expansions over Intervals" begin + ti = Taylor1(Interval{Float64}, 10) + @test eltype(ti) == Interval{Float64} + + a = 1..2 + b = -1 .. 1 + p4(x, a) = x^4 + 4*a*x^3 + 6*a^2*x^2 + 4*a^3*x + a^4 + p5(x, a) = x^5 + 5*a*x^4 + 10*a^2*x^3 + 10*a^3*x^2 + 5*a^4*x + a^5 + @test p4(ti,-a) == (ti-a)^4 + @test p5(ti,-a) == (ti-a)^5 + @test p4(ti,-b) == (ti-b)^4 + @test all((p5(ti,-b)).coeffs .⊆ ((ti-b)^5).coeffs) + + x, y = set_variables(Interval{Float64}, "x y") + @test eltype(x) == Interval{Float64} + + @test p4(x,-y) == (x-y)^4 + @test p5(x,-y) == (x-y)^5 + @test p4(x,-a) == (x-a)^4 + @test p5(x,-a) == (x-a)^5 + @test all((p4(ti,-b))[:] .⊆ ((ti-b)^4)[:]) + @test all((p5(ti,-b))[:] .⊆ ((ti-b)^5)[:]) + +end From 2a646b884e98bc739c512e51067e91d9a891a429 Mon Sep 17 00:00:00 2001 From: Luis Benet Date: Tue, 17 Apr 2018 16:05:07 -0500 Subject: [PATCH 4/7] Add test/REQUIRE and modify runtests.jl --- test/REQUIRE | 1 + test/runtests.jl | 3 ++- 2 files changed, 3 insertions(+), 1 deletion(-) create mode 100644 test/REQUIRE diff --git a/test/REQUIRE b/test/REQUIRE new file mode 100644 index 00000000..ff005721 --- /dev/null +++ b/test/REQUIRE @@ -0,0 +1 @@ +IntervalArithmetic 0.13.0 diff --git a/test/runtests.jl b/test/runtests.jl index 62306621..4abbb861 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -8,7 +8,8 @@ testfiles = ( "mixtures.jl", "mutatingfuncts.jl", "identities_Euler.jl", - "fateman40.jl" + "fateman40.jl", + "intervals.jl" ) for file in testfiles From 4d5ac348a807345e81c2c83d37ef8264657a57ec Mon Sep 17 00:00:00 2001 From: Luis Benet Date: Wed, 18 Apr 2018 12:09:11 -0500 Subject: [PATCH 5/7] Use ca^2 instead of ca*ca; important for Intervals --- src/power.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/power.jl b/src/power.jl index f30dd5a6..cbfed99e 100644 --- a/src/power.jl +++ b/src/power.jl @@ -348,7 +348,7 @@ Return `c = a*a` with no allocation; all parameters are `HomogeneousPolynomial`. iszero(ca) && continue inda = idxTb[na] pos = posTb[2*inda] - c[pos] += ca * ca + c[pos] += ca^2 @inbounds for nb = na+1:num_coeffs_a cb = a[nb] iszero(cb) && continue From 2b67e9ee9ec26cc73cff7f892253b573a4711f97 Mon Sep 17 00:00:00 2001 From: Luis Benet Date: Wed, 18 Apr 2018 12:15:03 -0500 Subject: [PATCH 6/7] Fix tests --- test/intervals.jl | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/test/intervals.jl b/test/intervals.jl index e33a7136..21d7e7fd 100644 --- a/test/intervals.jl +++ b/test/intervals.jl @@ -12,26 +12,28 @@ else end @testset "Tests Taylor1 and TaylorN expansions over Intervals" begin - ti = Taylor1(Interval{Float64}, 10) - @test eltype(ti) == Interval{Float64} - a = 1..2 b = -1 .. 1 p4(x, a) = x^4 + 4*a*x^3 + 6*a^2*x^2 + 4*a^3*x + a^4 p5(x, a) = x^5 + 5*a*x^4 + 10*a^2*x^3 + 10*a^3*x^2 + 5*a^4*x + a^5 + + ti = Taylor1(Interval{Float64}, 10) + x, y = set_variables(Interval{Float64}, "x y") + + @test eltype(ti) == Interval{Float64} + @test eltype(x) == Interval{Float64} + @test p4(ti,-a) == (ti-a)^4 @test p5(ti,-a) == (ti-a)^5 @test p4(ti,-b) == (ti-b)^4 @test all((p5(ti,-b)).coeffs .⊆ ((ti-b)^5).coeffs) - x, y = set_variables(Interval{Float64}, "x y") - @test eltype(x) == Interval{Float64} @test p4(x,-y) == (x-y)^4 @test p5(x,-y) == (x-y)^5 @test p4(x,-a) == (x-a)^4 @test p5(x,-a) == (x-a)^5 - @test all((p4(ti,-b))[:] .⊆ ((ti-b)^4)[:]) - @test all((p5(ti,-b))[:] .⊆ ((ti-b)^5)[:]) + @test p4(x,-b) == (x-b)^4 + @test all((p5(x,-b))[:] .⊆ ((x-b)^5)[:]) end From 8141256a7dda05e27e24d2ecbe07402989059fcb Mon Sep 17 00:00:00 2001 From: Luis Benet Date: Wed, 18 Apr 2018 12:39:45 -0500 Subject: [PATCH 7/7] Fix (again) a broken test --- test/intervals.jl | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/test/intervals.jl b/test/intervals.jl index 21d7e7fd..2b715799 100644 --- a/test/intervals.jl +++ b/test/intervals.jl @@ -34,6 +34,8 @@ end @test p4(x,-a) == (x-a)^4 @test p5(x,-a) == (x-a)^5 @test p4(x,-b) == (x-b)^4 - @test all((p5(x,-b))[:] .⊆ ((x-b)^5)[:]) + for ind in eachindex((p5(x,-b)).coeffs) + @test all(((p5(x,-b)).coeffs[ind]).coeffs .⊆ (((x-b)^5).coeffs[ind]).coeffs) + end end