Skip to content

Commit

Permalink
STaylor1: add findfirst, findlast, *Num, /Num, construtor docs
Browse files Browse the repository at this point in the history
  • Loading branch information
mewilhel committed Apr 8, 2020
1 parent ec4940e commit c495b89
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 10 deletions.
26 changes: 18 additions & 8 deletions benchmark/benchmarks.jl
Original file line number Diff line number Diff line change
Expand Up @@ -13,23 +13,22 @@ end
function f(g, x, y, n)
t = x
for i=1:n
t = g(t,x)
t = g(t,y)
t = g(x,y)
end
t
end
function f(g, x, n)
t = x
for i=1:n
t = g(t)
t = g(t)
end
t
end
n = 100
dims = (5,20)

S = SUITE["+"] = BenchmarkGroup()
for i in (5,10,20,50)
S = SUITE["arithmetic"] = BenchmarkGroup()
for i in dims
r = rand(i)
x = Taylor1(r)
x2 = Taylor1(r)
Expand All @@ -40,20 +39,31 @@ for i in (5,10,20,50)
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)

end

S = SUITE["functions"] = BenchmarkGroup()
for i in (5,10,20,50)
for i in dims
r = rand(i)
x = Taylor1(r)
q = STaylor1(r)
y = rand()
S["Taylor1{$i,Float64}"] = @benchmarkable f(exp, $x, $n)
S["STaylor1{$i,Float64}"] = @benchmarkable f(exp, $q, $n)
S["exp(Taylor1{Float64}), len = $i"] = @benchmarkable f(exp, $x, $n)
S["exp(STaylor1{$i,Float64}),"] = @benchmarkable f(exp, $q, $n)
S["zero(Taylor1{Float64}), len = $i"] = @benchmarkable f(zero, $x, $n)
S["zero(STaylor1{$i,Float64}),"] = @benchmarkable f(zero, $q, $n)
S["one(Taylor1{Float64}), len = $i"] = @benchmarkable f(one, $x, $n)
S["one(STaylor1{$i,Float64}),"] = @benchmarkable f(one, $q, $n)
S["iszero(Taylor1{Float64}), len = $i"] = @benchmarkable f(iszero, $x, $n)
S["iszero(STaylor1{$i,Float64}),"] = @benchmarkable f(iszero, $q, $n)
end

#=
Expand Down
12 changes: 12 additions & 0 deletions src/arithmetic.jl
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,14 @@ end
end
end


function *(a::STaylor1{N,T}, b::T) where {N, T<:Number}
STaylor1{N,T}(scale_tuple(a.coeffs, b))
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 @@ -508,6 +516,10 @@ 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

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

Expand Down
12 changes: 12 additions & 0 deletions src/auxiliary.jl
Original file line number Diff line number Diff line change
Expand Up @@ -286,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 Down
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
22 changes: 21 additions & 1 deletion src/constructors.jl
Original file line number Diff line number Diff line change
Expand Up @@ -72,12 +72,31 @@ Taylor1(::Type{T}, order::Int) where {T<:Number} = Taylor1( [zero(T), one(T)], o
Taylor1(order::Int) = Taylor1(Float64, order)

######################### STaylor1
"""
Taylor1{N,T<:Number} <: AbstractSeries{T}
DataType for polynomial expansions in one independent variable.
**Fields:**
- `coeffs :: NTuple{N,T}` Expansion coefficients; the ``i``-th
component is the coefficient of degree ``i-1`` of the expansion.
Note that `STaylor1` variables are callable. For more information, see
[`evaluate`](@ref).
"""
struct STaylor1{N,T<:Number} <: AbstractSeries{T}
coeffs::NTuple{N,T}
end

## Outer constructors ##
@inline STaylor1(x::STaylor1{N,T}) where {N,T<:Number} = x

"""
STaylor1(x::T, v::Val{N})
Shortcut to define the independent variable of a `STaylor1{N,T}` polynomial of
given `N` with constant term equal to `x`.
"""
@generated function STaylor1(x::T, v::Val{N}) where {N,T<:Number}
y = Any[zero(T) for i=1:N]
tup = :((x,))
Expand All @@ -90,6 +109,7 @@ end
@inline function STaylor1(coeffs::Vector{T}) where {T<:Number}
STaylor1{length(coeffs),T}(tuple(coeffs...))
end
@inline STaylor1(x::STaylor1{N,T}) where {N,T<:Number} = x

######################### HomogeneousPolynomial
"""
Expand Down
4 changes: 4 additions & 0 deletions test/onevariable.jl
Original file line number Diff line number Diff line change
Expand Up @@ -581,6 +581,10 @@ end
t1 = STaylor1([1.1, 2.1, 3.1])
t2 = Taylor1([1.1, 2.1, 3.1])
@test test_vs_Taylor1(exp(t1), exp(t2))

a = STaylor1([0.0, 1.2, 2.3, 4.5, 0.0])
@test findfirst(a) == 1
@test findlast(a) == 3
end

#=
Expand Down

0 comments on commit c495b89

Please sign in to comment.