Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve product of TaylorModelNs #31

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 57 additions & 19 deletions src/arithmetic.jl
Original file line number Diff line number Diff line change
Expand Up @@ -208,28 +208,66 @@ end
# Multiplication
function *(a::TaylorModelN, b::TaylorModelN)
@assert a.x0 == b.x0 && a.I == b.I

# Polynomial product with extended order
order = max(get_order(a), get_order(b))
@assert 2*order ≤ get_order()
aext = TaylorN(copy(a.pol.coeffs), 2*order)
bext = TaylorN(copy(b.pol.coeffs), 2*order)
res = aext * bext

# Returned polynomial
bext = TaylorN( copy(res.coeffs[1:order+1]) )

# Bound for the neglected part of the product of polynomials
res[0:order] .= zero(eltype(res))
aux = a.I - a.x0
Δnegl = res(aux)

# Remainder of the product
Δa = a.pol(aux)
Δb = b.pol(aux)
Δ = Δnegl + Δb * a.rem + Δa * b.rem + a.rem * b.rem
# Some definitions related to the order of some polynomials
orderTS = get_order() # maximum order, fixed by `TaylorSeries._params_TaylorN_`
order_a = get_order(a)
order_b = get_order(b)
order_prod = 2 * max(order_a, order_b)

# The returned polynomial has no terms beyond `orderTS`
if order_prod ≤ orderTS
apol = TaylorN(a.pol.coeffs, order_prod)
bpol = TaylorN(b.pol.coeffs, order_prod)
res = apol * bpol
Δa = a.pol(aux)
Δb = b.pol(aux)
Δ = Δb * a.rem + Δa * b.rem + a.rem * b.rem
return TaylorModelN(res, Δ, a.x0, a.I)
end

# The resulting product has a part whose order is larger than `orderTS`;
# a bound for this polynomial has to be included
apol = TaylorN(a.pol.coeffs, orderTS)
lbenet marked this conversation as resolved.
Show resolved Hide resolved
bpol = TaylorN(b.pol.coeffs, orderTS)
res = apol * bpol # Trunctated polynomial to order `orderTS`
Δa = apol(aux)
Δb = bpol(aux)
Δ = Δb * a.rem + Δa * b.rem + a.rem * b.rem
N = get_numvars()

# We evaluate *explicitely* each term of the product of coefficients at `aux`
Δnegl = zero(Δ)
for order = orderTS+1:order_prod
orderini = order - orderTS
for inda = orderini:order_a
indb = order - inda
# Δnegl += evaluate(apol[inda], aux) * evaluate(bpol[indb], aux)
Copy link
Member

Choose a reason for hiding this comment

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

You could try evaluating all of these bounds once before the loop (and storing them in new arrays), and then just indexing into the arrays inside the for loops. This should be much faster.

What I had done previously was store these bounds in the TaylorModel object itself when it is created (so that they are calculated exactly once in the history of the object).


# Coefficient tables of corresponding `HomogeneousPolynomial`s
@inbounds cta = TaylorSeries.coeff_table[inda+1]
@inbounds ctb = TaylorSeries.coeff_table[indb+1]

for i in eachindex(cta)
@inbounds a_coeff = apol.coeffs[inda+1][i]
iszero(a_coeff) && continue
for j in eachindex(ctb)
@inbounds b_coeff = bpol.coeffs[indb+1][j]
iszero(b_coeff) && continue

# Evaluation of monomials
tmp = one(Δ)
for n in 1:N
@inbounds tmp *= aux[n]^(cta[i][n]+ctb[j][n])
end
Δnegl += a_coeff * b_coeff * tmp
end
end
end
end

return TaylorModelN(bext, Δ, a.x0, a.I)
return TaylorModelN(res, Δ+Δnegl, a.x0, a.I)
end


Expand Down
8 changes: 3 additions & 5 deletions test/TMN.jl
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@ function check_containment(ftest, xx::TaylorModelN{N,T,S}, tma::TaylorModelN{N,T
end

const _order = 2
const _order_max = 2*(_order+1)
set_variables(Interval{Float64}, [:x, :y], order=_order_max)
set_variables(Interval{Float64}, [:x, :y], order=_order)

@testset "Tests for TaylorModelN " begin
b0 = Interval(0.0) × Interval(0.0)
Expand Down Expand Up @@ -53,7 +52,7 @@ set_variables(Interval{Float64}, [:x, :y], order=_order_max)
@test_throws BoundsError TaylorModelN(5, _order, b0, ib0) # wrong variable number

# Tests for get_order and remainder
@test get_order() == 6
@test get_order() == 2
@test get_order(xm) == 2
@test remainder(ym) == zi
end
Expand All @@ -79,8 +78,6 @@ set_variables(Interval{Float64}, [:x, :y], order=_order_max)
b = b1[2] * a
@test b == TaylorModelN( b1[2]*a.pol, Δ*b1[2], b1, ib1 )
@test b / b1[2] == a
@test_throws AssertionError TaylorModelN(TaylorN(1, order=_order_max), zi, b1, ib1) *
TaylorModelN(TaylorN(2, order=_order_max), zi, b1, ib1)

remt = remainder(1/(1-TaylorModel1(_order, b1[1], ib1[1])))
@test remainder(1 / (1-xm)) == remt
Expand All @@ -100,6 +97,7 @@ set_variables(Interval{Float64}, [:x, :y], order=_order_max)
@test rpa(x->5*x, ym) == 5*ym
remT = remainder(5*TaylorModel1(2, b1[1], ib1[1])^4)
@test rpa(x->5*x^4, xm) == TaylorModelN(zero(xT), remT, b1, ib1)
# remT = 5 * (ib1[1]-b1[1])^2 *(ib1[2]-b1[2]) * (2+(ib1[2]-b1[2]))
remT = 5 * (ib1[1]-b1[1])^2 * (2*(ib1[2]-b1[2])+(ib1[2]-b1[2])^2)
@test rpa(x->5*x^2, xm*ym) == TaylorModelN( 5*xT^2, remT, b1, ib1)

Expand Down