Skip to content

Commit

Permalink
Add a new method for evaluate (for Taylor1 vectors) and evaluate!
Browse files Browse the repository at this point in the history
… and tests
  • Loading branch information
Luis Benet committed Sep 5, 2016
1 parent 5e29918 commit e54b4e8
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 5 deletions.
1 change: 1 addition & 0 deletions docs/src/api.md
Expand Up @@ -25,6 +25,7 @@ set_variables
show_params_TaylorN
get_coeff
evaluate
evaluate!
derivative
integrate
gradient
Expand Down
33 changes: 33 additions & 0 deletions src/Taylor1.jl
Expand Up @@ -1049,6 +1049,39 @@ function evaluate{T<:Number,S<:Number}(a::Taylor1{T}, dx::S)
end
evaluate{T<:Number}(a::Taylor1{T}) = a.coeffs[1]

doc"""
evaluate(x, δt)
Evaluates each element of `x::Array{Taylor1{T},1}`, representing
the dependent variables of an ODE, at *time* δt.
"""
function evaluate{T<:Number, S<:Number}(x::Array{Taylor1{T},1}, δt::S)
R = promote_type(T,S)
return evaluate(convert(Array{Taylor1{R},1},x), convert(R,δt))
end
function evaluate{T<:Number}(x::Array{Taylor1{T},1}, δt::T)
xnew = Array{T}( length(x) )
evaluate!(x, δt, xnew)
return xnew
end
evaluate{T<:Number}(a::Array{Taylor1{T},1}) = evaluate(a, zero(T))

doc"""
evaluate!(x, δt, x0)
Evaluates each element of `x::Array{Taylor1{T},1}`,
representing the Taylor expansion for the dependent variables
of an ODE at *time* δt. It updates the vector `x0` with the
computed values.
"""
function evaluate!{T<:Number}(x::Array{Taylor1{T},1}, δt::T, x0::Array{T,1})
@assert length(x) == length(x0)
@inbounds for i in eachindex(x)
x0[i] = evaluate( x[i], δt )
end
nothing
end


"""
evaluate(a, x)
Expand Down
6 changes: 4 additions & 2 deletions src/TaylorSeries.jl
Expand Up @@ -24,11 +24,13 @@ import Base: zero, one, zeros, ones, isinf, isnan,
convert, promote_rule, promote, eltype, length, show,
real, imag, conj, ctranspose,
rem, mod, mod2pi, abs, gradient,
sqrt, exp, log, sin, cos, tan, asin, acos, atan, A_mul_B!
sqrt, exp, log, sin, cos, tan,
asin, acos, atan, A_mul_B!

export Taylor1, TaylorN, HomogeneousPolynomial

export get_coeff, derivative, integrate, evaluate,
export get_coeff, derivative, integrate,
evaluate, evaluate!,
show_params_TaylorN,
get_order, get_numvars,
set_variables, get_variables,
Expand Down
7 changes: 4 additions & 3 deletions test/runtests.jl
Expand Up @@ -101,10 +101,11 @@ facts("Tests for Taylor1 expansions") do
@fact atan(tan(tsquare)) == tsquare --> true
@fact asin(t) + acos(t) == pi/2 --> true
@fact derivative(acos(t)) == - 1/sqrt(1-t^2) --> true
@fact_throws ArgumentError asin(ta(1.0))
@fact_throws ArgumentError acos(ta(big(1.0)))
@fact_throws ArgumentError atan(ta(1//1*im))

v = [sin(t), exp(-t)]
@fact evaluate(v) == [0.0, 1.0] --> true
@fact evaluate(v, complex(0.0,0.2)) ==
[complex(0.0,sinh(0.2)),complex(cos(0.2),sin(-0.2))] --> true
@fact derivative(5, exp(ta(1.0))) == exp(1.0) --> true
@fact derivative(3, exp(ta(1.0pi))) == exp(1.0pi) --> true
@fact isapprox(derivative(10, exp(ta(1.0pi))) , exp(1.0pi) ) --> true
Expand Down

0 comments on commit e54b4e8

Please sign in to comment.