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 15 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: 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
67 changes: 67 additions & 0 deletions benchmark/benchmarks.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
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()
S["Taylor1{$i,Float64} + Float64"] = @benchmarkable f(+, $x, $y, $n)
Copy link
Contributor

Choose a reason for hiding this comment

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

You should be able to use metaprogramming to script these tests.

S["STaylor1{$i,Float64} + Float64"] = @benchmarkable f(+, $q, $y, $n)
S["Taylor1{$i,Float64} - Float64"] = @benchmarkable f(-, $x, $y, $n)
S["STaylor1{$i,Float64} - Float64"] = @benchmarkable f(-, $q, $y, $n)
S["Taylor1{$i,Float64} / Float64"] = @benchmarkable f(/, $x, $y, $n)
S["STaylor1{$i,Float64} / Float64"] = @benchmarkable f(/, $q, $y, $n)
S["Taylor1{$i,Float64} * Float64"] = @benchmarkable f(*, $x, $y, $n)
S["STaylor1{$i,Float64} * Float64"] = @benchmarkable f(*, $q, $y, $n)
S["Taylor1{$i,Float64} + Taylor1{$i,Float64}"] = @benchmarkable f(+, $x, $x2, $n)
S["STaylor1{$i,Float64} + STaylor1{$i,Float64}"] = @benchmarkable f(+, $q, $q2, $n)
S["Taylor1{$i,Float64} - Taylor1{$i,Float64}"] = @benchmarkable f(-, $x, $x2, $n)
S["STaylor1{$i,Float64} - STaylor1{$i,Float64}"] = @benchmarkable f(-, $q, $q2, $n)
S["Taylor1{$i,Float64} * Taylor1{$i,Float64}"] = @benchmarkable f(*, $x, $x2, $n)
S["STaylor1{$i,Float64} * STaylor1{$i,Float64}"] = @benchmarkable f(*, $q, $q2, $n)
S["Taylor1{$i,Float64} / Taylor1{$i,Float64}"] = @benchmarkable f(/, $x, $x2, $n)
S["STaylor1{$i,Float64} / STaylor1{$i,Float64}"] = @benchmarkable f(/, $q, $q2, $n)
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":[]}]]]
2 changes: 1 addition & 1 deletion src/TaylorSeries.jl
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ import Base: zero, one, zeros, ones, isinf, isnan, iszero,
power_by_squaring,
rtoldefault, isfinite, isapprox, rad2deg, deg2rad

export Taylor1, TaylorN, HomogeneousPolynomial, AbstractSeries
export Taylor1, TaylorN, STaylor1, HomogeneousPolynomial, AbstractSeries

export getcoeff, derivative, integrate, differentiate,
evaluate, evaluate!, inverse, set_taylor1_varname,
Expand Down
131 changes: 131 additions & 0 deletions src/arithmetic.jl
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ for T in (:Taylor1, :TaylorN)
end
end

==(a::STaylor1, b::STaylor1) = (a.coeffs == b.coeffs)

function ==(a::HomogeneousPolynomial, b::HomogeneousPolynomial)
a.order == b.order && return a.coeffs == b.coeffs
return iszero(a.coeffs) && iszero(b.coeffs)
Expand All @@ -31,11 +33,23 @@ end
for T in (:Taylor1, :HomogeneousPolynomial, :TaylorN)
@eval iszero(a::$T) = iszero(a.coeffs)
end
function iszero(a::STaylor1)
Copy link
Contributor

Choose a reason for hiding this comment

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

all(iszero, a.coeffs) perhaps?

Copy link
Author

Choose a reason for hiding this comment

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

Done!

flag = true
for i in eachindex(a)
flag &= iszero(a[i])
if ~flag
break
end
end
flag
end

## zero and one ##
for T in (:Taylor1, :TaylorN), f in (:zero, :one)
@eval ($f)(a::$T) = $T(($f)(a[0]), a.order)
end
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))

function zero(a::HomogeneousPolynomial{T}) where {T<:Number}
v = zero.(a.coeffs)
Expand Down Expand Up @@ -205,6 +219,48 @@ end
-(a::Taylor1{T}, b::TaylorN{S}) where {T<:NumberNotSeries,S<:NumberNotSeries} =
-(promote(a,b)...)

@inline +(a::STaylor1{N,T}, b::STaylor1{N,T}) where {N, T<:Number} = STaylor1(add_tuples(a.coeffs, b.coeffs))
Copy link
Contributor

Choose a reason for hiding this comment

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

Can you just use STaylor1(a.coeffs .+ b.coeffs)?

Copy link
Author

Choose a reason for hiding this comment

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

Done!

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

@generated function add_first_tuple(tup::NTuple{N,T}, x::T) where {N, T<:Number}
ex = :((tup[1] + x),)
append!(ex.args, Any[:(tup[$i]) for i in 2:N])
return quote
Base.@_inline_meta
@inbounds return $ex
end
end

@generated function sub_first_tuple(tup::NTuple{N,T}, x::T) where {N, T<:Number}
ex = :((tup[1] - x),)
append!(ex.args, Any[:(tup[$i]) for i in 2:N])
return quote
Base.@_inline_meta
@inbounds return $ex
end
end

function +(a::STaylor1{N,T}, b::T) where {N, T<:Number}
STaylor1{N,T}(add_first_tuple(a.coeffs, b))
end
function +(b::T, a::STaylor1{N,T}) where {N, T<:Number}
STaylor1{N,T}(add_first_tuple(a.coeffs, b))
end
function -(a::STaylor1{N,T}, b::T) where {N, T<:Number}
STaylor1{N,T}(sub_first_tuple(a.coeffs, b))
end
-(b::T, a::STaylor1{N,T}) where {N, T<:Number} = 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)...)

#-(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)...)


## Multiplication ##
for T in (:Taylor1, :HomogeneousPolynomial, :TaylorN)
Expand All @@ -225,6 +281,38 @@ for T in (:Taylor1, :HomogeneousPolynomial, :TaylorN)
end
end

@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}(scale_tuple(a.coeffs, b))
Copy link
Contributor

Choose a reason for hiding this comment

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

Again, should just be a.coeffs .* b

Copy link
Author

Choose a reason for hiding this comment

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

Done!

end
function *(b::T, a::STaylor1{N,T}) where {N, T<:Number}
STaylor1{N,T}(scale_tuple(a.coeffs, b))
end

for T in (:HomogeneousPolynomial, :TaylorN)

@eval begin
Expand Down Expand Up @@ -428,6 +516,49 @@ function /(a::Taylor1{T}, b::Taylor1{T}) where {T<:Number}
return c
end

function /(a::STaylor1{N,T}, b::T) where {N, T<:Number}
STaylor1{N,T}(div_tuple_by_scalar(a.coeffs, b))
end

@generated function /(a::STaylor1{N,T}, b::STaylor1{N,T}) where {N,T<:Number}

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]

# add error
ex_line = quote
if iszero(b[0])
throw(ArgumentError("""The 0th order STaylor1 coefficient
must be non-zero for b, (a/b)(x) is
not differentiable at x=0)."""))
end
end
ex_calc.args[1] = ex_line

# add recursion relation
for j = 0:(N-1)
ex_line = :(a[$(j)])
for k = 1:j
sym = syms[j-k+1]
ex_line = :($ex_line - $sym*b[$k])
end
sym = syms[j+1]
ex_line = :($sym = ($ex_line)/b[0])
ex_calc.args[j+2] = 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

/(a::TaylorN{T}, b::TaylorN{S}) where
{T<:NumberNotSeriesN, S<:NumberNotSeriesN} = /(promote(a,b)...)

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


# getindex STaylor1
getindex(a::STaylor1, n::Int) = a.coeffs[n+1]
getindex(a::STaylor1, u::UnitRange{Int}) = a.coeffs[u .+ 1]
getindex(a::STaylor1, c::Colon) = a.coeffs[c]
getindex(a::STaylor1, u::StepRange{Int,Int}) = a.coeffs[u .+ 1]

"""
getcoeff(a, v)

Expand Down Expand Up @@ -236,6 +242,15 @@ for T in (:Taylor1, :HomogeneousPolynomial, :TaylorN)
@inline axes(a::$T) = ()
end
end
@inline iterate(a::STaylor1{N,T}, state=0) where {N, T<:Number} = state > N-1 ? nothing : (a.coeffs[state+1], state+1)
@inline firstindex(a::STaylor1) = 0
@inline lastindex(a::STaylor1{N,T}) where {N, T<:Number} = N-1
@inline eachindex(s::STaylor1{N,T}) where {N, T<:Number} = UnitRange(0, N-1)
@inline size(s::STaylor1{N,T}) where {N, T<:Number} = N
@inline length(s::STaylor1{N,T}) where {N, T<:Number} = N
@inline get_order(s::STaylor1{N,T}) where {N, T<:Number} = N - 1
@inline eltype(s::STaylor1{N,T}) where {N, T<:Number} = T
@inline axes(a::STaylor1) = ()


## fixorder ##
Expand Down Expand Up @@ -271,6 +286,18 @@ function Base.findlast(a::Taylor1{T}) where {T<:Number}
return last-1
end

function Base.findfirst(a::STaylor1{N,T}) where {N, T<:Number}
first = findfirst(x->!iszero(x), a.coeffs)
isa(first, Nothing) && return -1
return first-1
end
# Finds the last non-zero entry; extended to Taylor1
function Base.findlast(a::STaylor1{N,T}) where {N, T<:Number}
last = findlast(x->!iszero(x), a.coeffs)
isa(last, Nothing) && return -1
return last-1
end


## copyto! ##
# Inspired from base/abstractarray.jl, line 665
Expand All @@ -297,6 +324,8 @@ and `TaylorN`. The fallback behavior is to return `a` itself if
"""
constant_term(a::Taylor1) = a[0]

constant_term(a::STaylor1) = a[0]

constant_term(a::TaylorN) = a[0][1]

constant_term(a::Vector{T}) where {T<:Number} = constant_term.(a)
Expand All @@ -317,3 +346,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
Loading