Skip to content

Commit

Permalink
Fixes, cleanup, and tests for broadcasting with mixtures
Browse files Browse the repository at this point in the history
  • Loading branch information
lbenet committed Apr 26, 2019
1 parent 3152be7 commit 2750cf3
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 16 deletions.
28 changes: 16 additions & 12 deletions src/broadcasting.jl
Expand Up @@ -29,6 +29,9 @@ BroadcastStyle(::Type{<:TaylorN{T}}) where {T} = TaylorNStyle{T}()
BroadcastStyle(::TaylorNStyle{T}, ::Base.Broadcast.DefaultArrayStyle{0}) where {T} = TaylorNStyle{T}()
BroadcastStyle(::TaylorNStyle{T}, ::Base.Broadcast.DefaultArrayStyle{1}) where {T} = Base.Broadcast.DefaultArrayStyle{1}()

# Precedence rules for mixtures
BroadcastStyle(::TaylorNStyle{Taylor1{T}}, ::Taylor1Style{T}) where {T} = TaylorNStyle{Taylor1{T}}()
BroadcastStyle(::Taylor1Style{TaylorN{T}}, ::TaylorNStyle{T}) where {T} = Taylor1Style{TaylorN{T}}()

# Extend eltypes so things like [1.0] .+ t work
Base.Broadcast.eltypes(t::Tuple{Taylor1,AbstractArray}) =
Expand All @@ -44,17 +47,18 @@ Base.Broadcast.eltypes(t::Tuple{TaylorN,AbstractArray}) =
Base.Broadcast.eltypes(t::Tuple{AbstractArray,TaylorN}) =
Tuple{Base.Broadcast._broadcast_getindex_eltype(t[1]), Base.Broadcast._broadcast_getindex_eltype([t[2]])}

# We follow https://docs.julialang.org/en/v1/manual/interfaces/#man-interface-iteration-1
"`A = find_taylor(As)` returns the first Taylor1 among the arguments."
find_taylor(bc::Broadcasted) = find_taylor(bc.args)
find_taylor(args::Tuple) = find_taylor(find_taylor(args[1]), Base.tail(args))
find_taylor(x) = x
find_taylor(a::Taylor1, rest) = a
find_taylor(a::HomogeneousPolynomial, rest) = a
find_taylor(a::TaylorN, rest) = a
find_taylor(::AbstractArray, rest) = find_taylor(rest)

# Extend similar
# # We follow https://docs.julialang.org/en/v1/manual/interfaces/#man-interface-iteration-1
# "`A = find_taylor(As)` returns the first Taylor1 among the arguments."
# find_taylor(bc::Broadcasted) = find_taylor(bc.args)
# find_taylor(args::Tuple) = find_taylor(find_taylor(args[1]), Base.tail(args))
# find_taylor(x) = x
# find_taylor(a::Taylor1, rest) = a
# find_taylor(a::HomogeneousPolynomial, rest) = a
# find_taylor(a::TaylorN, rest) = a
# find_taylor(::AbstractArray, rest) = find_taylor(rest)
#
# # Extend similar
# function similar(bc::Broadcasted{Taylor1Style{S}}, ::Type{T}) where {S, T}
# # Proper promotion
# R = Base.Broadcast.combine_eltypes(bc.f, bc.args)
Expand All @@ -63,7 +67,7 @@ find_taylor(::AbstractArray, rest) = find_taylor(rest)
# # Create the output
# return Taylor1(similar(A.coeffs, R), A.order)
# end

#
# function similar(bc::Broadcasted{HomogeneousPolynomialStyle{S}}, ::Type{T}) where {S, T}
# # Proper promotion
# # combine_eltypes(f, args::Tuple) = Base._return_type(f, eltypes(args))
Expand All @@ -73,7 +77,7 @@ find_taylor(::AbstractArray, rest) = find_taylor(rest)
# # Create the output
# return HomogeneousPolynomial(similar(A.coeffs, R), A.order)
# end

#
# function similar(bc::Broadcasted{TaylorNStyle{S}}, ::Type{T}) where {S, T}
# # Proper promotion
# R = Base.Broadcast.combine_eltypes(bc.f, bc.args)
Expand Down
67 changes: 63 additions & 4 deletions test/broadcasting.jl
Expand Up @@ -5,7 +5,7 @@ using TaylorSeries

using Test

@testset "Broadcasting for Taylor1 expansions" begin
@testset "Broadcasting with Taylor1 expansions" begin
t = Taylor1(Int, 5)

# @test t .= t
Expand All @@ -19,7 +19,7 @@ using Test
@test typeof(1.0 .+ [t]) == Vector{Taylor1{Float64}}
@test 1.0 .+ [t, 2t] == [1.0 + t, 1.0 + 2t]
@test [1.0,2.0] .+ [t 2t] == [1.0+t 1.0+2t; 2.0+t 2.0+2t]
@test [1.0] .+ t == [1.0 + t]
@test [1.0] .+ t == t .+ [1.0] == [1.0 + t]
@test 1.0 .* t == t
@test typeof(1.0 .* t) == Taylor1{Float64}

Expand Down Expand Up @@ -58,7 +58,7 @@ using Test
@test ttts == -1
end

@testset "Broadcasting for HomogeneousPolynomial and TaylorN" begin
@testset "Broadcasting with HomogeneousPolynomial and TaylorN" begin
x, y = set_variables("x y", order=3)
xH = x[1]
yH = y[1]
Expand All @@ -78,7 +78,7 @@ end

@test 1 .* xH == xH
@test 1 .* [xH] == [xH]
@test [1] .* xH == [xH]
@test [1] .* xH == xH .* [1] == [xH]

@test x .== x
@test y .≈ y
Expand All @@ -103,3 +103,62 @@ end
@test 1.0 .* y == y
@test typeof(1.0 .* x .* y) == TaylorN{Float64}
end

@testset "Broadcasting with mixtures Taylor1{TalorN{T}}" begin
x, y = set_variables("x", numvars=2, order=6)
tN = Taylor1(TaylorN{Float64}, 3)

@test tN .== tN
@test tN .≈ tN
@test tN .!= (1 + tN)

@test 1.0 .+ tN == 1.0 + tN
@test typeof(1.0 .+ tN) == Taylor1{TaylorN{Float64}}
@test 1.0 .+ [tN] == [1.0 + tN]
@test typeof(1.0 .+ [tN]) == Vector{Taylor1{TaylorN{Float64}}}
@test 1.0 .+ [tN, 2tN] == [1.0 + tN, 1.0 + 2tN]
@test [1.0,2.0] .+ [tN 2tN] == [1.0+tN 1.0+2tN; 2.0+tN 2.0+2tN]
@test [1.0] .+ tN == tN .+ [1.0] == [1.0 + tN]
@test 1.0 .* tN == 1.0 * tN
@test typeof(1.0 .* tN) == Taylor1{TaylorN{Float64}}

tNs = similar(tN);
@test get_order(tN) == get_order(tNs) == 3
@test typeof(tNs) == Taylor1{TaylorN{Float64}}
tNs .= tN
@test tNs == tN
@. tNs = y[1] * tN^2 - 1
@test tNs == y[1] * tN^2 - 1
@. tNs = y * tN^2 - 1
@test tNs == y * tN^2 - 1
end

@testset "Broadcasting with mixtures TaylorN{Talor1{T}}" begin
set_variables("x", numvars=2, order=6)
t = Taylor1(3)
xHt = HomogeneousPolynomial([one(t), zero(t)])
yHt = HomogeneousPolynomial([zero(t), t])
tN1 = TaylorN([HomogeneousPolynomial([t]),xHt,yHt^2])

@test tN1 .== tN1
@test tN1 .≈ tN1
@test tN1 .!= (1 + tN1)

@test 1.0 .+ tN1 == 1.0 + tN1
@test typeof(1.0 .+ tN1) == TaylorN{Taylor1{Float64}}
@test 1.0 .+ [tN1] == [1.0 + tN1]
@test typeof(1.0 .+ [tN1]) == Vector{TaylorN{Taylor1{Float64}}}
@test 1.0 .+ [tN1, 2tN1] == [1.0 + tN1, 1.0 + 2tN1]
@test [1.0, 2.0] .+ [tN1 2tN1] == [1.0+tN1 1.0+2tN1; 2.0+tN1 2.0+2tN1]
@test [1.0] .+ tN1 == tN1 .+ [1.0] == [1.0 + tN1]
@test 1.0 .* tN1 == tN1
@test typeof(1.0 .* tN1) == TaylorN{Taylor1{Float64}}

tN1s = similar(tN1);
@test get_order(tN1) == get_order(tN1s) == 2
@test typeof(tN1s) == TaylorN{Taylor1{Float64}}
tN1s .= tN1
@test tN1s == tN1
@. tN1s = t * tN1^2 - 1
@test tN1s == t * tN1^2 - 1
end

0 comments on commit 2750cf3

Please sign in to comment.