Skip to content

Commit

Permalink
Avoid irrelevant checks in getindex and setindex! (#143)
Browse files Browse the repository at this point in the history
* Avoid irrelevant checks in getindex and setindex!

* Simplify code and remove unused `zero_korder` and `order_posTb`
  • Loading branch information
lbenet authored Dec 5, 2017
1 parent 008b67d commit 10ab42c
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 67 deletions.
2 changes: 0 additions & 2 deletions docs/src/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,8 @@ generate_tables
generate_index_vectors
in_base
make_inverse_dict
order_posTb
resize_coeffs1!
resize_coeffsHP!
zero_korder
constant_term
mul!
mul!(c::HomogeneousPolynomial, a::HomogeneousPolynomial, b::HomogeneousPolynomial)
Expand Down
8 changes: 4 additions & 4 deletions src/arithmetic.jl
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ for T in (:Taylor1, :HomogeneousPolynomial, :TaylorN)
*(a::$T, b::Bool) = b * a

function *(a::T, b::$T) where {T<:NumberNotSeries}
@inbounds aux = a * b[1]
@inbounds aux = a * b.coeffs[1]
v = Array{typeof(aux)}(length(b.coeffs))
@__dot__ v = a * b.coeffs
return $T(v, b.order)
Expand Down Expand Up @@ -286,7 +286,7 @@ end
for T in (:Taylor1, :TaylorN)
@eval @inline function mul!(c::$T, a::$T, b::$T, k::Int)

# c[k+1] = zero( a[k+1] )
# c[k] = zero( a[k] )
@inbounds for i = 0:k
if $T == Taylor1
c[k] += a[i] * b[k-i]
Expand All @@ -311,7 +311,7 @@ end
doc"""
mul!(c, a, b, k::Int) --> nothing
Update the `k`-th expansion coefficient `c[k+1]` of `c = a * b`,
Update the `k`-th expansion coefficient `c[k]` of `c = a * b`,
where all `c`, `a`, and `b` are either `Taylor1` or `TaylorN`.
The coefficients are given by
Expand Down Expand Up @@ -467,7 +467,7 @@ end
doc"""
div!(c, a, b, k::Int, ordfact::Int=0)
Compute the `k-th` expansion coefficient `c[k+1]` of `c = a / b`,
Compute the `k-th` expansion coefficient `c[k]` of `c = a / b`,
where all `c`, `a` and `b` are either `Taylor1` or `TaylorN`.
The coefficients are given by
Expand Down
68 changes: 8 additions & 60 deletions src/auxiliary.jl
Original file line number Diff line number Diff line change
Expand Up @@ -73,29 +73,14 @@ Return the coefficient of order `n::Int` of a `a::Taylor1` polynomial.
"""
getcoeff(a::Taylor1, n::Int) = (@assert 0 n a.order; return a[n])

function getindex(a::Taylor1, n::Int)
@assert 0 n length(a.coeffs)
nn = n == length(a.coeffs) ? n : n+1
return a.coeffs[nn]
end
function getindex(a::Taylor1, u::UnitRange)
u_stop = u.stop == length(a.coeffs) ? u.stop : u.stop+1
view(a.coeffs, (1+u.start):u_stop )
end
getindex(a::Taylor1, n::Int) = a.coeffs[n+1]
getindex(a::Taylor1, u::UnitRange) = view(a.coeffs, u+1 )
getindex(a::Taylor1, c::Colon) = view(a.coeffs, c)

function setindex!(a::Taylor1{T}, x::T, n::Int) where {T<:Number}
@assert 0 n a.order
a.coeffs[n+1] = x
end
function setindex!(a::Taylor1{T}, x::T, u::UnitRange) where {T<:Number}
u_stop = u.stop == length(a.coeffs) ? u.stop : u.stop+1
a.coeffs[(u.start+1):u_stop] = x
end
function setindex!(a::Taylor1{T}, x::Array{T,1}, u::UnitRange) where {T<:Number}
u_stop = u.stop == length(a.coeffs) ? u.stop : u.stop+1
a.coeffs[(u.start+1):u_stop] .= x
end
setindex!(a::Taylor1{T}, x::T, n::Int) where {T<:Number} = a.coeffs[n+1] = x
setindex!(a::Taylor1{T}, x::T, u::UnitRange) where {T<:Number} = a.coeffs[u+1] = x
setindex!(a::Taylor1{T}, x::Array{T,1}, u::UnitRange) where {T<:Number} =
a.coeffs[u+1] .= x
setindex!(a::Taylor1{T}, x::T, c::Colon) where {T<:Number} = a.coeffs[c] = x
setindex!(a::Taylor1{T}, x::Array{T,1}, c::Colon) where {T<:Number} = a.coeffs[c] = x

Expand Down Expand Up @@ -141,15 +126,8 @@ function getcoeff(a::TaylorN, v::Array{Int,1})
getcoeff(a[order], v)
end

function getindex(a::TaylorN, n::Int)
# @assert 0 ≤ n ≤ length(a.coeffs)
nn = n == length(a.coeffs) ? n : n+1
return a.coeffs[nn]
end
function getindex(a::TaylorN, u::UnitRange)
u_stop = u.stop == length(a.coeffs) ? u.stop : u.stop+1
view(a.coeffs, (1+u.start):u_stop )
end
getindex(a::TaylorN, n::Int) = a.coeffs[n+1]
getindex(a::TaylorN, u::UnitRange) = view(a.coeffs, u+1 )
getindex(a::TaylorN, c::Colon) = view(a.coeffs, c)

function setindex!(a::TaylorN{T}, x::HomogeneousPolynomial{T}, n::Int) where
Expand Down Expand Up @@ -227,40 +205,10 @@ function fixorder(a::HomogeneousPolynomial, b::HomogeneousPolynomial)
end


"""
zero_korder(a)
For `a::Taylor1` returns `zero(a[1])` while for `a::TaylorN` returns
a zero of a k-th order `HomogeneousPolynomial` of proper type.
"""
zero_korder(a::Taylor1, ::Int) = zero(a[0])

zero_korder(a::TaylorN, k::Int) = HomogeneousPolynomial(zero(constant_term(a)), k)


# Finds the first non zero entry; extended to Taylor1
Base.findfirst(a::Taylor1{T}) where {T<:Number} = findfirst(a.coeffs)-1


"""
order_posTb(order, nv, ord)
Return a vector with the positions, in a `HomogeneousPolynomial` of
order `order`, where the variable `nv` has order `ord`.
"""
function order_posTb(order::Int, nv::Int, ord::Int)
@assert order get_order()
@inbounds indTb = coeff_table[order+1]
@inbounds num_coeffs = size_table[order+1]
posV = Int[]
for pos = 1:num_coeffs
@inbounds indTb[pos][nv] != ord && continue
push!(posV, pos)
end
posV
end


"""
constant_term(a)
Expand Down
2 changes: 1 addition & 1 deletion test/manyvariables.jl
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,7 @@ end
TaylorSeries.subst!(xx, 5.0, 0)
@test xx[0] == HomogeneousPolynomial([-5.0])
TaylorSeries.subst!(xx, -5.0, 1)
@test xx[1] == zero(xx[2])
@test xx[1] == zero(xx[end])
TaylorSeries.div!(xx, 1.0+xT, 1.0+xT, 0)
@test xx[0] == 1.0
TaylorSeries.pow!(xx, 1.0+xT, 1.5, 0)
Expand Down

0 comments on commit 10ab42c

Please sign in to comment.