Skip to content

Commit

Permalink
Fixed * for mixtures of TaylorN{Taylo1{T}} and fixed some tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Luis Benet committed Mar 7, 2017
1 parent 0a91842 commit edcac00
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 37 deletions.
18 changes: 11 additions & 7 deletions src/arithmetic.jl
Original file line number Diff line number Diff line change
Expand Up @@ -175,23 +175,27 @@ for T in (:Taylor1, :HomogeneousPolynomial, :TaylorN)
end
*(b::$T, a::RealOrComplex) = a * b
if $T != Taylor1
function *{T<:RealOrComplex,R<:RealOrComplex}(
a::Taylor1{R}, b::$T{Taylor1{T}})
function *{T<:RealOrComplex}(a::Taylor1{T}, b::$T{Taylor1{T}})
@inbounds aux = a * b.coeffs[1]
S = typeof(aux)
coeffs = Array{S}(length(a.coeffs))
coeffs = Array{S}(length(b.coeffs))
@simd for i in eachindex(coeffs)
@inbounds coeffs[i] = a * b.coeffs[i]
end
return $T(coeffs, a.order)
return $T(coeffs, b.order)
end
*{T<:RealOrComplex,R<:RealOrComplex}(
b::$T{Taylor1{T}}, a::Taylor1{R}) = a * b
*{T<:RealOrComplex}(b::$T{Taylor1{T}}, a::Taylor1{T}) = a * b
function *{T<:RealOrComplex,R<:RealOrComplex}(a::Taylor1{T},
b::$T{Taylor1{return }})
S = promote_type(T,R)
return convert(Taylor1{S}, a) * convert($T{Taylor1{S}}, b)
end
*{T<:RealOrComplex,R<:RealOrComplex}(b::$T{Taylor1{T}},
a::Taylor1{R}) = a * b
end
end
end


doc"""
```
*(a, b)
Expand Down
57 changes: 29 additions & 28 deletions test/mixtures.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,33 +5,34 @@ using TaylorSeries
using Base.Test

@testset "Tests with mixures of Taylor1 and TaylorN" begin
set_variables("x", numvars=2, order=17)
set_variables("x", numvars=2, order=6)
xH = HomogeneousPolynomial(Int, 1)
yH = HomogeneousPolynomial(Int, 2)
tN = Taylor1(TaylorN{Int}, 2)
@test eltype(tN) == TaylorN{Int}
@test tN.order == 2
@test string(zero(tN)) == " 0 + 𝒪(‖x‖¹) + 𝒪(t³)"
@test string(tN) == " ( 1 + 𝒪(‖x‖¹)) t + 𝒪(t³)"
@test string(tN + 3Taylor1(2)) == " ( 4.0 + 𝒪(‖x‖¹)) t + 𝒪(t³)"
tN = Taylor1(TaylorN{Float64}, 3)

tN = Taylor1([zero(TaylorN(Int,1)), one(TaylorN(Int,1))],2)
@test string(zero(tN)) == " 0 + 𝒪(‖x‖¹⁸) + 𝒪(t³)"
@test string(tN) == " ( 1 + 𝒪(‖x‖¹⁸)) t + 𝒪(t³)"
@test string(Taylor1([xH+yH])) == " 1 x₁ + 1 x₂ + 𝒪(t¹)"
@test string(Taylor1([zero(xH), xH*yH])) ==
" ( 1 x₁ x₂) t + 𝒪(t²)"
@test string(tN*Taylor1([0,TaylorN([xH+yH])])) ==
" ( 1 x₁ + 1 x₂ + 𝒪(‖x‖¹⁸)) t² + 𝒪(t³)"
@test eltype(tN) == TaylorN{Float64}
@test tN.order == 3
@test string(zero(tN)) == " 0.0 + 𝒪(‖x‖¹) + 𝒪(t⁴)"
@test string(tN) == " ( 1.0 + 𝒪(‖x‖¹)) t + 𝒪(t⁴)"
@test string(tN + 3Taylor1(Int, 2)) == " ( 4.0 + 𝒪(‖x‖¹)) t + 𝒪(t⁴)"
@test string(xH * tN) == " ( 1.0 x₁ + 𝒪(‖x‖²)) t + 𝒪(t⁴)"

t = Taylor1(2)
tN = Taylor1([zero(TaylorN(Float64,1)), one(TaylorN(Float64,1))], 3)
@test string(zero(tN)) == " 0.0 + 𝒪(‖x‖⁷) + 𝒪(t⁴)"
@test string(tN) == " ( 1.0 + 𝒪(‖x‖⁷)) t + 𝒪(t⁴)"
@test string(Taylor1([xH+yH])) == " 1 x₁ + 1 x₂ + 𝒪(t¹)"
@test string(Taylor1([zero(xH), xH*yH])) == " ( 1 x₁ x₂) t + 𝒪(t²)"
@test string(tN * Taylor1([0,TaylorN([xH+yH])])) ==
" ( 1.0 x₁ + 1.0 x₂ + 𝒪(‖x‖⁷)) t² + 𝒪(t⁴)"

t = Taylor1(3)
xHt = HomogeneousPolynomial(typeof(t), 1)
@test string(xHt) == " ( 1.0 + 𝒪(t¹)) x₁"
xHt = HomogeneousPolynomial([one(t), zero(t)])
yHt = HomogeneousPolynomial([zero(t), t])
@test string(xHt) == " ( 1.0 + 𝒪(t³)) x₁"
@test string(yHt) == " ( 1.0 t + 𝒪(t³)) x₂"
@test string(HomogeneousPolynomial([t])) == " ( 1.0 t + 𝒪(t³))"
@test string(xHt) == " ( 1.0 + 𝒪(t)) x₁"
@test string(yHt) == " ( 1.0 t + 𝒪(t)) x₂"
@test string(HomogeneousPolynomial([t])) == " ( 1.0 t + 𝒪(t))"
@test 3*xHt == HomogeneousPolynomial([3*one(t), zero(t)])
@test complex(0,1)*xHt == HomogeneousPolynomial([1im*one(t), zero(1im*t)])

Expand All @@ -41,29 +42,29 @@ using Base.Test
@test eltype(xHt) == Taylor1{Float64}
@test eltype(tN1) == Taylor1{Float64}
@test eltype(Taylor1([xH])) == HomogeneousPolynomial{Int64}
@test eltype(tN) == TaylorN{Int64}
@test eltype(tN1) == Taylor1{Float64}
@test get_order(HomogeneousPolynomial([Taylor1(1), 1.0+Taylor1(2)])) == 1
@test string(tN1) ==
" ( 1.0 t + 𝒪(t³)) + ( 1.0 + 𝒪(t³)) x₁ + ( 1.0 t² + 𝒪(t³)) x₂² + 𝒪(‖x‖³)"
" ( 1.0 t + 𝒪(t)) + ( 1.0 + 𝒪(t)) x₁ + ( 1.0 t² + 𝒪(t)) x₂² + 𝒪(‖x‖³)"
@test string(t1N) ==
" 1.0 x₁ + 𝒪(‖x‖³) + ( 1.0 + 𝒪(‖x‖³)) t + ( 1.0 x₂² + 𝒪(‖x‖³)) t² + 𝒪(t³)"
" 1.0 x₁ + 𝒪(‖x‖³) + ( 1.0 + 𝒪(‖x‖³)) t + ( 1.0 x₂² + 𝒪(‖x‖³)) t² + 𝒪(t)"
@test tN1 == ctN1
@test tN1+tN1 == 2*tN1
@test tN1+1im*tN1 == complex(1,1)*tN1
@test tN1+t == t+tN1
@test tN1-t == -t+tN1
@test tN1-tN1 == zero(tN1)
@test string(t1N*t1N) ==
" 1.0 x₁² + 𝒪(‖x‖³) + ( 2.0 x₁ + 𝒪(‖x‖³)) t + ( 1.0 + 𝒪(‖x‖³)) t² + 𝒪(t³)"
" 1.0 x₁² + 𝒪(‖x‖³) + ( 2.0 x₁ + 𝒪(‖x‖³)) t + ( 1.0 + 𝒪(‖x‖³)) t² + ( 2.0 x₂² + 𝒪(‖x‖³)) t³ + 𝒪(t⁴)"

@test mod(tN1+1,1.0) == 0+tN1
@test mod(tN1-1.125,2) == 0.875+tN1
@test mod(tN1+1,1.0) == 0+tN1
@test mod(tN1-1.125,2) == 0.875+tN1
@test (rem(tN1+1.125,1.0)).coeffs[1].coeffs[1] == 0.125 + t
@test (rem(tN1-1.125,2)).coeffs[1].coeffs[1] == -1.125 + t
@test mod2pi(-3pi+tN1).coeffs[1].coeffs[1].coeffs[1] pi
@test mod2pi(0.125+2pi+tN1).coeffs[1].coeffs[1].coeffs[1] 0.125
@test mod(t1N+1.125,1.0) == 0.125+t1N
@test mod(t1N-1.125,2) == 0.875+t1N
@test mod(t1N+1.125,1.0) == 0.125+t1N
@test mod(t1N-1.125,2) == 0.875+t1N
@test (rem(t1N+1.125,1.0)).coeffs[1] == 0.125 + t1N.coeffs[1]
@test (rem(t1N-1.125,2)).coeffs[1] == -1.125 + t1N.coeffs[1]
@test mod2pi(-3pi+t1N).coeffs[1].coeffs[1].coeffs[1] pi
Expand All @@ -79,7 +80,7 @@ using Base.Test
[tN1 tN1]

@test string(evaluate(t1N, 0.0)) == " 1.0 x₁ + 𝒪(‖x‖³)"
@test string(evaluate(t1N^2, 1.0)) == " 1.0 + 2.0 x₁ + 1.0 x₁² + 𝒪(‖x‖³)"
@test string(evaluate(t1N^2, 1.0)) == " 1.0 + 2.0 x₁ + 1.0 x₁² + 2.0 x₂² + 𝒪(‖x‖³)"
v = zeros(TaylorN{Float64},2)
@test evaluate!([t1N, t1N^2], 0.0, v) == nothing
@test v[1] == TaylorN([xHt])
Expand Down
8 changes: 6 additions & 2 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,12 @@
#
# Tests for TaylorSeries

testfiles = ("onevariable.jl", "manyvariables.jl", "mixtures.jl",
"identities_Euler.jl", "fateman40.jl")
testfiles = (
"onevariable.jl",
"manyvariables.jl",
"mixtures.jl",
"identities_Euler.jl",
"fateman40.jl")

for file in testfiles
include(file)
Expand Down

0 comments on commit edcac00

Please sign in to comment.