Skip to content

Commit 86933d7

Browse files
authored
Reduce memory allocation when squaring a nested Taylor1 (#388)
* Pass aux as argument to the nested Taylor1s-specialized mutating method of sqr * Add tests for the nested Taylor1s-specialized mutating version of pow * Bump nested Taylor1s mutating methods of subst, div and pow to NumberNotSeriesN as suggested by @lbenet * Include compatibility with IA v0.23 * Bump patch version
1 parent fc1dcbd commit 86933d7

File tree

4 files changed

+23
-8
lines changed

4 files changed

+23
-8
lines changed

Project.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name = "TaylorSeries"
22
uuid = "6aa5eb33-94cf-58f4-a9d0-e4b2c4fc25ea"
33
repo = "https://github.com/JuliaDiff/TaylorSeries.jl.git"
4-
version = "0.20.6"
4+
version = "0.20.7"
55

66
[deps]
77
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
@@ -22,7 +22,7 @@ TaylorSeriesSAExt = "StaticArrays"
2222

2323
[compat]
2424
Aqua = "0.8"
25-
IntervalArithmetic = "1"
25+
IntervalArithmetic = "0.23, 1"
2626
JLD2 = "0.5"
2727
LinearAlgebra = "<0.0.1, 1"
2828
Markdown = "<0.0.1, 1"

src/arithmetic.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -473,7 +473,7 @@ for (f, fc) in ((:+, :(add!)), (:-, :(subst!)))
473473
end
474474
end
475475

476-
function subst!(v::Taylor1{Taylor1{T}}, a::Taylor1{Taylor1{T}}, k::Int) where {T <: TS.NumberNotSeries}
476+
function subst!(v::Taylor1{Taylor1{T}}, a::Taylor1{Taylor1{T}}, k::Int) where {T <: NumberNotSeriesN}
477477
@inbounds for i in eachindex(v[k])
478478
v[k][i] = -a[k][i]
479479
end
@@ -1236,7 +1236,7 @@ function div!(c::Taylor1, a::NumberNotSeries, b::Taylor1)
12361236
end
12371237

12381238
@inline function div!(c::Taylor1{Taylor1{T}}, a::NumberNotSeries,
1239-
b::Taylor1{Taylor1{T}}, k::Int) where {T<:NumberNotSeries}
1239+
b::Taylor1{Taylor1{T}}, k::Int) where {T<:NumberNotSeriesN}
12401240
zero!(c, k)
12411241
iszero(a) && !iszero(b) && return nothing
12421242
# order and coefficient of first factorized term

src/power.jl

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -321,11 +321,11 @@ end
321321
return nothing
322322
end
323323

324-
@inline function pow!(c::Taylor1{T}, a::Taylor1{T}, aux::Taylor1{T},
324+
@inline function pow!(c::Taylor1{Taylor1{T}}, a::Taylor1{Taylor1{T}}, aux::Taylor1{Taylor1{T}},
325325
r::S, k::Int) where {T<:NumberNotSeriesN, S<:Real}
326326
(r == 0) && return one!(c, a, k)
327327
(r == 1) && return identity!(c, a, k)
328-
(r == 2) && return sqr!(c, a, k)
328+
(r == 2) && return sqr!(c, a, aux[k], k)
329329
(r == 0.5) && return sqrt!(c, a, k)
330330
# Sanity
331331
zero!(c, k)
@@ -553,7 +553,7 @@ end
553553
return nothing
554554
end
555555

556-
@inline function sqr!(c::Taylor1{Taylor1{T}}, a::Taylor1{Taylor1{T}},
556+
@inline function sqr!(c::Taylor1{Taylor1{T}}, a::Taylor1{Taylor1{T}}, aux::Taylor1{T},
557557
k::Int) where {T<:NumberNotSeriesN}
558558
if k == 0
559559
sqr_orderzero!(c, a)
@@ -564,7 +564,7 @@ end
564564
# Recursion formula
565565
kodd = k%2
566566
kend = (k - 2 + kodd) >> 1
567-
aux = zero(c[k])
567+
# aux = zero(c[k])
568568
@inbounds for i = 0:kend
569569
for j in eachindex(a[k])
570570
# c[k] += 2 * a[i] * a[k-i]

test/mixtures.jl

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -671,6 +671,21 @@ end
671671

672672
end
673673
end
674+
675+
for r in (0, 1, 2, 0.5)
676+
local inorder = 6
677+
local outorder = 25
678+
679+
x = Taylor1([Taylor1(rand(inorder+1), inorder) for _ in 1:outorder+1], outorder)
680+
y = zero(x)
681+
z = zero(x)
682+
683+
w = x^r
684+
for k in eachindex(z)
685+
TS.pow!(z, x, y, r, k)
686+
end
687+
@test norm(z - w, Inf) == 0.0
688+
end
674689
end
675690

676691
# Back to default

0 commit comments

Comments
 (0)