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

Preliminary work on static analogs for Taylor1 #241

Open
wants to merge 23 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
b05db52
add STaylor1 definition, a few arithmetic operators, and benchmarking
mewilhel Apr 3, 2020
20d0c47
Update *, add getindex
mewilhel Apr 4, 2020
fcbe189
Add size, length, iteration functions
mewilhel Apr 4, 2020
63195c9
STaylor1: add promotions, conversions, real, img, conj, isinf, isnan
mewilhel Apr 4, 2020
51e80c1
STaylor1: Add size, length, iteration functions
mewilhel Apr 4, 2020
b4eafca
STaylor1: add promotions, conversions, real, img, conj, isinf, isnan
mewilhel Apr 4, 2020
fdf33dc
Merge remote-tracking branch 'mewilhel/master'
mewilhel Apr 4, 2020
6ca0206
STaylor1: add iszero, zero, one, ==
mewilhel Apr 4, 2020
d4a5117
STaylor1: +(STaylor,Number), -(STaylor,Number), ... and additional tests
mewilhel Apr 4, 2020
7910462
STaylor1: add unit tests, exp, temp fix promotions
mewilhel Apr 6, 2020
aa20279
STaylor1: fix zero/one, onevariable tests on STaylor1 only (temporary)
mewilhel Apr 6, 2020
ec4940e
STaylor1: Add evaluate and tests
mewilhel Apr 6, 2020
c495b89
STaylor1: add findfirst, findlast, *Num, /Num, construtor docs
mewilhel Apr 8, 2020
5e051c6
STaylor1: add division, abs, rad2deg, deg2rad; fix real: imag, conj
mewilhel Apr 8, 2020
66fa8ce
Enable non-STaylor1 unit tests
mewilhel Apr 10, 2020
71f8fc8
Simplify computations
mewilhel Apr 14, 2020
409c2ae
Fix failing test
mewilhel Apr 14, 2020
e8c6121
Simplify Benchmarking Suite
mewilhel Apr 15, 2020
8402997
STaylor1: Add square function
mewilhel Apr 16, 2020
38e9024
Update version conversion and constructor
mewilhel Apr 23, 2020
1778a92
Fix allocations in ntuple
mewilhel Apr 23, 2020
ed558cd
STaylor1: add log
mewilhel Apr 23, 2020
e169d1f
Remove STaylor1
mewilhel Sep 16, 2020
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
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "TaylorSeries"
uuid = "6aa5eb33-94cf-58f4-a9d0-e4b2c4fc25ea"
repo = "https://github.com/JuliaDiff/TaylorSeries.jl.git"
version = "0.10.3"
version = "0.11.0"

[deps]
InteractiveUtils = "b77e0a4c-d291-57a0-90e8-8db25a27a240"
Expand Down
2 changes: 2 additions & 0 deletions benchmark/REQUIRE
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
BenchmarkTools 0.3.1
PkgBenchmark 0.1.1
57 changes: 57 additions & 0 deletions benchmark/benchmarks.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
using BenchmarkTools, TaylorSeries

const SUITE = BenchmarkGroup()

S = SUITE["Constructors"] = BenchmarkGroup()
for i in (05,10,20,50)
x = rand(i)
t = tuple(x...)
S["STaylor1(x::Array[len = $i])"] = @benchmarkable STaylor1($x)
S["STaylor1(x::Float64, Val($i))"] = @benchmarkable STaylor1($t)
end

function f(g, x, y, n)
t = x
for i=1:n
t = g(x,y)
end
t
end
function f(g, x, n)
t = x
for i=1:n
t = g(t)
end
t
end
n = 100
dims = (5,20)

S = SUITE["arithmetic"] = BenchmarkGroup()
for i in dims
r = rand(i)
x = Taylor1(r)
x2 = Taylor1(r)
q = STaylor1(r)
q2 = STaylor1(r)
y = rand()
for g in (+, -, /, *)
S["Taylor1{$i,Float64} $(Symbol(g)) Float64"] = @benchmarkable f($g, $x, $y, $n)
S["STaylor1{$i,Float64} $(Symbol(g)) Float64"] = @benchmarkable f($g, $q, $y, $n)
S["Taylor1{$i,Float64} $(Symbol(g)) Taylor1{$i,Float64}"] = @benchmarkable f($g, $x, $x2, $n)
S["STaylor1{$i,Float64} $(Symbol(g)) STaylor1{$i,Float64}"] = @benchmarkable f($g, $q, $q2, $n)
end
end

S = SUITE["functions"] = BenchmarkGroup()
for i in dims
r = rand(i)
x = Taylor1(r)
q = STaylor1(r)
y = rand()
for g in (exp, abs, zero, one, real, imag, conj, adjoint, iszero, isnan,
isinf, deg2rad, rad2deg)
S["$(Symbol(g))(Taylor1{Float64}), len = $i"] = @benchmarkable f($g, $x, $n)
S["$(Symbol(g))(STaylor1{$i,Float64}),"] = @benchmarkable f($g, $q, $n)
end
end
12 changes: 12 additions & 0 deletions benchmark/run_benchmark.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using PkgBenchmark
results = benchmarkpkg("TaylorSeries")
show(results)

#=
# specify tag and uncommit to benchmark versus prior tagged version
tag =
results = judge("TaylorSeries", tag)
show(results)
=#

export_markdown("results.md", results)
1 change: 1 addition & 0 deletions benchmark/tune.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[{"Julia":"1.3.1","BenchmarkTools":"0.4.3"},[["BenchmarkGroup",{"data":{"Constructors":["BenchmarkGroup",{"data":{"STaylor1(x::Float64, Val(5))":["BenchmarkTools.Parameters",{"gctrial":true,"time_tolerance":0.05,"samples":10000,"evals":12,"gcsample":false,"seconds":5.0,"overhead":0.0,"memory_tolerance":0.01}],"STaylor1(x::Array[length = 10])":["BenchmarkTools.Parameters",{"gctrial":true,"time_tolerance":0.05,"samples":10000,"evals":15,"gcsample":false,"seconds":5.0,"overhead":0.0,"memory_tolerance":0.01}],"STaylor1(x::Float64, Val(10))":["BenchmarkTools.Parameters",{"gctrial":true,"time_tolerance":0.05,"samples":10000,"evals":24,"gcsample":false,"seconds":5.0,"overhead":0.0,"memory_tolerance":0.01}],"STaylor1(x::Float64, Val(50))":["BenchmarkTools.Parameters",{"gctrial":true,"time_tolerance":0.05,"samples":10000,"evals":22,"gcsample":false,"seconds":5.0,"overhead":0.0,"memory_tolerance":0.01}],"STaylor1(x::Array[length = 5])":["BenchmarkTools.Parameters",{"gctrial":true,"time_tolerance":0.05,"samples":10000,"evals":10,"gcsample":false,"seconds":5.0,"overhead":0.0,"memory_tolerance":0.01}],"STaylor1(x::Array[length = 50])":["BenchmarkTools.Parameters",{"gctrial":true,"time_tolerance":0.05,"samples":10000,"evals":10,"gcsample":false,"seconds":5.0,"overhead":0.0,"memory_tolerance":0.01}]},"tags":[]}]},"tags":[]}]]]
1 change: 0 additions & 1 deletion src/arithmetic.jl
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,6 @@ end
-(a::Taylor1{T}, b::TaylorN{S}) where {T<:NumberNotSeries,S<:NumberNotSeries} =
-(promote(a,b)...)


## Multiplication ##
for T in (:Taylor1, :HomogeneousPolynomial, :TaylorN)

Expand Down
46 changes: 43 additions & 3 deletions src/auxiliary.jl
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,6 @@ function setindex!(a::Taylor1{T}, x::Array{T,1}, u::StepRange{Int,Int}) where {T
end
end


"""
getcoeff(a, v)

Expand Down Expand Up @@ -237,7 +236,6 @@ for T in (:Taylor1, :HomogeneousPolynomial, :TaylorN)
end
end


## fixorder ##
for T in (:Taylor1, :TaylorN)
@eval begin
Expand Down Expand Up @@ -271,7 +269,6 @@ function Base.findlast(a::Taylor1{T}) where {T<:Number}
return last-1
end


## copyto! ##
# Inspired from base/abstractarray.jl, line 665
for T in (:Taylor1, :HomogeneousPolynomial, :TaylorN)
Expand Down Expand Up @@ -317,3 +314,46 @@ linear_polynomial(a::TaylorN) = TaylorN([a[1]])
linear_polynomial(a::Vector{T}) where {T<:Number} = linear_polynomial.(a)

linear_polynomial(a::Number) = a


#=
Tuple operations taken from ForwardDiff.jl
Copy link
Contributor

Choose a reason for hiding this comment

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

I think all of these can be replaced by simple broadcasts these days.


Copyright (c) 2015: Jarrett Revels, Theodore Papamarkou, Miles Lubin, and other contributors

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

=#
function tupexpr(f, N)
ex = Expr(:tuple, [f(i) for i=1:N]...)
return quote
$(Expr(:meta, :inline))
@inbounds return $ex
end
end

@generated function scale_tuple(tup::NTuple{N}, x) where N
return tupexpr(i -> :(tup[$i] * x), N)
end

@generated function div_tuple_by_scalar(tup::NTuple{N}, x) where N
return tupexpr(i -> :(tup[$i] / x), N)
end

@generated function add_tuples(a::NTuple{N}, b::NTuple{N}) where N
return tupexpr(i -> :(a[$i] + b[$i]), N)
end

@generated function sub_tuples(a::NTuple{N}, b::NTuple{N}) where N
return tupexpr(i -> :(a[$i] - b[$i]), N)
end

@generated function minus_tuple(tup::NTuple{N}) where N
return tupexpr(i -> :(-tup[$i]), N)
end

@generated function mul_tuples(a::NTuple{N}, b::NTuple{N}, afactor, bfactor) where N
return tupexpr(i -> :((afactor * a[$i]) + (bfactor * b[$i])), N)
end
2 changes: 1 addition & 1 deletion src/broadcasting.jl
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import .Broadcast: BroadcastStyle, Broadcasted, broadcasted

# BroadcastStyle definitions and basic precedence rules
struct Taylor1Style{T} <: Base.Broadcast.AbstractArrayStyle{0} end
Taylor1Style{T}(::Val{N}) where {T, N}= Base.Broadcast.DefaultArrayStyle{N}()
Taylor1Style{T}(::Val{N}) where {T, N} = Base.Broadcast.DefaultArrayStyle{N}()
BroadcastStyle(::Type{<:Taylor1{T}}) where {T} = Taylor1Style{T}()
BroadcastStyle(::Taylor1Style{T}, ::Base.Broadcast.DefaultArrayStyle{0}) where {T} = Taylor1Style{T}()
BroadcastStyle(::Taylor1Style{T}, ::Base.Broadcast.DefaultArrayStyle{1}) where {T} = Base.Broadcast.DefaultArrayStyle{1}()
Expand Down
1 change: 0 additions & 1 deletion src/constructors.jl
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,6 @@ julia> Taylor1(Rational{Int}, 4)
Taylor1(::Type{T}, order::Int) where {T<:Number} = Taylor1( [zero(T), one(T)], order)
Taylor1(order::Int) = Taylor1(Float64, order)


######################### HomogeneousPolynomial
"""
HomogeneousPolynomial{T<:Number} <: AbstractSeries{T}
Expand Down
2 changes: 0 additions & 2 deletions src/conversion.jl
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ convert(::Type{Taylor1{T}}, b::T) where {T<:Number} = Taylor1([b], 0)

convert(::Type{Taylor1}, a::T) where {T<:Number} = Taylor1(a, 0)


convert(::Type{HomogeneousPolynomial{T}}, a::HomogeneousPolynomial) where {T<:Number} =
HomogeneousPolynomial(convert(Array{T,1}, a.coeffs), a.order)

Expand Down Expand Up @@ -171,7 +170,6 @@ promote_rule(::Type{Taylor1{T}}, ::Type{S}) where {T<:Number, S<:Number} =
promote_rule(::Type{Taylor1{Taylor1{T}}}, ::Type{Taylor1{T}}) where {T<:Number} =
Taylor1{Taylor1{T}}


promote_rule(::Type{HomogeneousPolynomial{T}},
::Type{HomogeneousPolynomial{S}}) where {T<:Number, S<:Number} =
HomogeneousPolynomial{promote_type(T,S)}
Expand Down
16 changes: 8 additions & 8 deletions src/evaluate.jl
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@
"""
evaluate(a, [dx])

Evaluate a `Taylor1` polynomial using Horner's rule (hand coded). If `dx` is
ommitted, its value is considered as zero. Note that the syntax `a(dx)` is
equivalent to `evaluate(a,dx)`, and `a()` is equivalent to `evaluate(a)`.
Evaluate a `Taylor1` (or `STaylor1{N,T}`) polynomial using Horner's rule (hand
coded). If `dx` is ommitted, its value is considered as zero. Note that the
syntax `a(dx)` is equivalent to `evaluate(a,dx)`, and `a()` is equivalent
to `evaluate(a)`.
"""
function evaluate(a::Taylor1{T}, dx::T) where {T<:Number}
@inbounds suma = a[end]
Expand All @@ -33,10 +34,10 @@ evaluate(a::Taylor1{T}) where {T<:Number} = a[0]
"""
evaluate(x, δt)

Evaluates each element of `x::Union{ Vector{Taylor1{T}}, Matrix{Taylor1{T}} }`,
representing the dependent variables of an ODE, at *time* δt. Note that the
syntax `x(δt)` is equivalent to `evaluate(x, δt)`, and `x()`
is equivalent to `evaluate(x)`.
Evaluates each element of `x::Union{Vector{Taylor1{T}}, Matrix{Taylor1{T}},
Vector{STaylor1{N,T}}, Matrix{STaylor1{N,T}}}`, representing the dependent
variables of an ODE, at *time* δt. Note that the syntax `x(δt)` is equivalent
to `evaluate(x, δt)`, and `x()` is equivalent to `evaluate(x)`.
"""
evaluate(x::Union{Array{Taylor1{T}}, SubArray{Taylor1{T}}}, δt::S) where
{T<:Number, S<:Number} = evaluate.(x, δt)
Expand Down Expand Up @@ -110,7 +111,6 @@ evaluate(p::Taylor1{T}, x::Array{S}) where {T<:Number, S<:Number} =

#function-like behavior for Taylor1
(p::Taylor1)(x) = evaluate(p, x)

(p::Taylor1)() = evaluate(p)

#function-like behavior for Vector{Taylor1}
Expand Down
2 changes: 0 additions & 2 deletions src/functions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,6 @@ for T in (:Taylor1, :TaylorN)
end
end


# Recursive functions (homogeneous coefficients)
for T in (:Taylor1, :TaylorN)
@eval begin
Expand Down Expand Up @@ -422,7 +421,6 @@ for T in (:Taylor1, :TaylorN)
end
end


@doc doc"""
inverse(f)

Expand Down
2 changes: 2 additions & 0 deletions src/intervals.jl
Original file line number Diff line number Diff line change
Expand Up @@ -146,3 +146,5 @@ function _normalize(a::TaylorN, I::IntervalBox{N,T}, ::Val{false}) where {N,T}
end
return a(x)
end

square(x::Interval{T}) where {T <: Real} = pow(x,2)
1 change: 0 additions & 1 deletion src/other_functions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ for T in (:Taylor1, :HomogeneousPolynomial, :TaylorN)
@eval isnan(a::$T) = any( isnan.(a.coeffs) )
end


## Division functions: rem and mod ##
for op in (:mod, :rem)
for T in (:Taylor1, :TaylorN)
Expand Down
3 changes: 2 additions & 1 deletion src/power.jl
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,6 @@ function ^(a::TaylorN, r::S) where {S<:Real}
return c
end


# Homogeneous coefficients for real power
@doc doc"""
pow!(c, a, r::Real, k::Int)
Expand Down Expand Up @@ -283,6 +282,8 @@ function square(a::HomogeneousPolynomial)
return res
end

#two(x::T) = convert(T, x)
square(x::T) where {T <: Real} = x^2

# Homogeneous coefficients for square
@doc doc"""
Expand Down
63 changes: 63 additions & 0 deletions src/static.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
==(a::STaylor1, b::STaylor1) = (a.coeffs == b.coeffs)

iszero(a::STaylor1) = all(iszero, a.coeffs)

zero(::STaylor1{N,T}) where {N, T<:Number} = STaylor1(zero(T), Val(N-1))
one(::STaylor1{N,T}) where {N, T<:Number} = STaylor1(one(T), Val(N-1))

@inline +(a::STaylor1{N,T}, b::STaylor1{N,T}) where {N, T<:Number} = STaylor1(a.coeffs .+ b.coeffs)
@inline -(a::STaylor1{N,T}, b::STaylor1{N,T}) where {N, T<:Number} = STaylor1(a.coeffs .- b.coeffs)
@inline +(a::STaylor1) = a
@inline -(a::STaylor1) = STaylor1(minus_tuple(a.coeffs))

function +(a::STaylor1{N,T}, b::T) where {N, T<:Number}
STaylor1{N,T}(ntuple(i -> i == 1 ? a.coeffs[1] + b : a.coeffs[i], Val(N)))
end
function +(b::T, a::STaylor1{N,T}) where {N, T<:Number}
STaylor1{N,T}(ntuple(i -> i == 1 ? a.coeffs[1] + b : a.coeffs[i], Val(N)))
end
function -(a::STaylor1{N,T}, b::T) where {N, T<:Number}
STaylor1{N,T}(ntuple(i -> i == 1 ? a.coeffs[1] - b : a.coeffs[i], Val(N)))
end
-(b::T, a::STaylor1{N,T}) where {N, T<:Number} = b + (-a)

#+(a::STaylor1{N,T}, b::S) where {N, T<:NumberNotSeries, S<:NumberNotSeries} = +(promote(a,b)...)
#+(a::STaylor1{N,T}, b::STaylor1{N,S}) where {N, T<:NumberNotSeries, S<:NumberNotSeries} = +(promote(a,b)...)
#+(a::STaylor1{N,T}, b::S) where {N, T<:NumberNotSeries, S<:NumberNotSeries} = +(promote(a,b)...)
#+(b::S, a::STaylor1{N,T}) where {N, T<:NumberNotSeries, S<:NumberNotSeries} = +(promote(b,a)...)

#-(a::STaylor1{N,T}, b::STaylor1{N,S}) where {N, T<:NumberNotSeries, S<:NumberNotSeries} = -(promote(a,b)...)
#-(a::STaylor1{N,T}, b::S) where {N, T<:NumberNotSeries, S<:NumberNotSeries} = -(promote(a,b)...)
#-(b::S, a::STaylor1{N,T}) where {N, T<:NumberNotSeries, S<:NumberNotSeries} = -(promote(b,a)...)

@generated function *(x::STaylor1{N,T}, y::STaylor1{N,T}) where {T<:Number,N}
ex_calc = quote end
append!(ex_calc.args, Any[nothing for i in 1:N])
syms = Symbol[Symbol("c$i") for i in 1:N]
for j = 1:N
ex_line = :(x.coeffs[1]*y.coeffs[$j])
for k = 2:j
ex_line = :($ex_line + x.coeffs[$k]*y.coeffs[$(j-k+1)])
end
sym = syms[j]
ex_line = :($sym = $ex_line)
ex_calc.args[j] = ex_line
end
exout = :(($(syms[1]),))
for i = 2:N
push!(exout.args, syms[i])
end
return quote
Base.@_inline_meta
$ex_calc
return STaylor1{N,T}($exout)
end
end


function *(a::STaylor1{N,T}, b::T) where {N, T<:Number}
STaylor1{N,T}(b .* a.coeffs)
end
function *(b::T, a::STaylor1{N,T}) where {N, T<:Number}
STaylor1{N,T}(b .* a.coeffs)
end
11 changes: 11 additions & 0 deletions test/onevariable.jl
Original file line number Diff line number Diff line change
Expand Up @@ -523,6 +523,17 @@ eeuler = Base.MathConstants.e
@test Taylor1{Int}(false) == Taylor1([0])
end

function test_vs_Taylor1(x,y)
flag = true
for i in 0:2
if x[i] !== y[i]
flag = false
break
end
end
flag
end

@testset "Test `inv` for `Matrix{Taylor1{Float64}}``" begin
t = Taylor1(5)
a = Diagonal(rand(0:10,3)) + rand(3, 3)
Expand Down