Skip to content

Commit

Permalink
Add lookupvar and evaluate methods for Pair with tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Luis Benet committed Feb 23, 2018
1 parent 41dc075 commit 5b3a3f4
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 19 deletions.
18 changes: 6 additions & 12 deletions src/calculus.jl
Original file line number Diff line number Diff line change
Expand Up @@ -131,8 +131,7 @@ function derivative(a::HomogeneousPolynomial, r::Int)

return HomogeneousPolynomial{T}(coeffs, a.order-1)
end
derivative(a::HomogeneousPolynomial, s::Symbol) =
derivative(a, findfirst(get_variable_symbols(), s))
derivative(a::HomogeneousPolynomial, s::Symbol) = derivative(a, lookupvar(s))

"""
derivative(a, [r=1])
Expand All @@ -149,8 +148,7 @@ function derivative(a::TaylorN, r=1::Int)
end
return TaylorN{T}( coeffs, a.order )
end
derivative(a::TaylorN, s::Symbol) =
derivative(a, findfirst(get_variable_symbols(), s))
derivative(a::TaylorN, s::Symbol) = derivative(a, lookupvar(s))

## Gradient, jacobian and hessian
"""
Expand Down Expand Up @@ -318,8 +316,7 @@ function integrate(a::HomogeneousPolynomial, r::Int)

return HomogeneousPolynomial{T}(coeffs, a.order+1)
end
integrate(a::HomogeneousPolynomial, s::Symbol) =
integrate(a, findfirst(get_variable_symbols(), s))
integrate(a::HomogeneousPolynomial, s::Symbol) = integrate(a, lookupvar(s))


"""
Expand Down Expand Up @@ -352,9 +349,6 @@ end
integrate(a::TaylorN, r::Int, x0::NumberNotSeries) =
integrate(a,r,TaylorN(HomogeneousPolynomial([convert(eltype(a),x0)], 0)))

integrate(a::TaylorN, s::Symbol) =
integrate(a, findfirst(get_variable_symbols(), s))
integrate(a::TaylorN, s::Symbol, x0::TaylorN) =
integrate(a, findfirst(get_variable_symbols(), s), x0)
integrate(a::TaylorN, s::Symbol, x0::NumberNotSeries) =
integrate(a, findfirst(get_variable_symbols(), s), x0)
integrate(a::TaylorN, s::Symbol) = integrate(a, lookupvar(s))
integrate(a::TaylorN, s::Symbol, x0::TaylorN) = integrate(a, lookupvar(s), x0)
integrate(a::TaylorN, s::Symbol, x0::NumberNotSeries) = integrate(a, lookupvar(s), x0)
5 changes: 4 additions & 1 deletion src/evaluate.jl
Original file line number Diff line number Diff line change
Expand Up @@ -332,10 +332,12 @@ end
function evaluate(a::TaylorN{T}, s::Symbol, val::S) where
{T<:Number, S<:NumberNotSeriesN}
vars = get_variables()
ind = findfirst(get_variable_symbols(), s)
ind = lookupvar(s)
vars[ind] = val
evaluate(a, vars)
end
evaluate(a::TaylorN{T}, x::Pair{Symbol,S}) where {T<:Number, S<:NumberNotSeriesN} =
evaluate(p, first(x), last(x))

evaluate(a::TaylorN{T}) where {T<:Number} = a[0][1]

Expand Down Expand Up @@ -378,6 +380,7 @@ evaluate(A::SubArray{TaylorN{T},2}) where {T<:Number} = evaluate.(A)
(p::TaylorN)(x) = evaluate(p, x)
(p::TaylorN)() = evaluate(p)
(p::TaylorN)(s::Symbol, x) = evaluate(p, s, x)
(p::TaylorN)(x::Pair) = evaluate(p, first(x), last(x))

#function-like behavior for Vector{TaylorN}
(p::Array{TaylorN{T},1})(x) where {T<:Number} = evaluate(p, x)
Expand Down
3 changes: 2 additions & 1 deletion src/parameters.jl
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,12 @@ ParamsTaylorN(order, num_vars, variable_names) = ParamsTaylorN(order, num_vars,
const _params_TaylorN_ = ParamsTaylorN(6, 2, ["x₁", "x₂"])


## Utilities to get the maximum order and number of variables
## Utilities to get the maximum order, number of variables, their names and symbols
get_order() = _params_TaylorN_.order
get_numvars() = _params_TaylorN_.num_vars
get_variable_names() = _params_TaylorN_.variable_names
get_variable_symbols() = _params_TaylorN_.variable_symbols
lookupvar(s::Symbol) = findfirst(_params_TaylorN_.variable_symbols, s)

function set_variable_names(varnames::Vector{T}) where {T<:AbstractString}
_params_TaylorN_.variable_names = varnames
Expand Down
13 changes: 8 additions & 5 deletions test/manyvariables.jl
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ end
@test TaylorSeries._params_TaylorN_.variable_names == ["x","y"]
@test TaylorSeries._params_TaylorN_.variable_symbols == [:x, :y]
@test get_variable_symbols() == [:x, :y]
@test TaylorSeries.lookupvar(:x) == 1
@test TaylorSeries.lookupvar() == 0
@test TaylorSeries.get_variable_names() == ["x", "y"]
@test x == HomogeneousPolynomial(Float64, 1)
@test x == HomogeneousPolynomial(1)
Expand Down Expand Up @@ -140,8 +142,8 @@ end
@test (rem(1+xT,1.0))[0] == 0
@test abs(1-xT) == 1-xT
@test abs(-1-xT) == 1+xT
@test derivative(yH,1) == derivative(xH,:x₂)
@test derivative(mod2pi(2pi+yT^3),2) == derivative(yT^3,:x₂)
@test derivative(yH,1) == derivative(xH, :x₂)
@test derivative(mod2pi(2pi+yT^3),2) == derivative(yT^3, :x₂)
@test derivative(yT) == zeroT
@test -xT/3im == im*xT/3
@test (xH/3im)' == im*xH/3
Expand All @@ -157,7 +159,7 @@ end
vr = rand(2)
@test hp(vr) == evaluate(hp, vr)

@test integrate(yH,1) == integrate(xH,:x₂)
@test integrate(yH,1) == integrate(xH, :x₂)
p = (xT-yT)^6
@test integrate(derivative(p, 1), 1, yT^6) == p
@test integrate(derivative(p, :x₁), :x₁, yT^6) == p
Expand Down Expand Up @@ -290,13 +292,14 @@ end
@test evaluate(exy) == 1
@test evaluate(exy,[0.1im,0.01im]) == exp(0.11im)
@test isapprox(evaluate(exy, [1,1]), eeuler^2)
@test exy(:x₁, 1) exp(1+yT)
@test exy(:x₁, 0.0) == exp(yT)
txy = tan(xT+yT)
@test getcoeff(txy,[8,7]) == 929569/99225
ptxy = xT + yT + (1/3)*( xT^3 + yT^3 ) + xT^2*yT + xT*yT^2
@test getindex(tan(TaylorN(1)+TaylorN(2)),0:4) == ptxy.coeffs[1:5]
@test evaluate(xH*yH,[1.0,2.0]) == 2.0
@test evaluate(xH*yH, [1.0,2.0]) == 2.0
@test ptxy(:x₁, -1.0) == -1 + yT + (-1.0+yT^3)/3 + yT - yT^2
@test ptxy(:x₁ => -1.0) == -1 + yT + (-1.0+yT^3)/3 + yT - yT^2
v = zeros(Int, 2)
@test evaluate!([xT, yT], ones(Int, 2), v) == nothing
@test v == ones(2)
Expand Down

0 comments on commit 5b3a3f4

Please sign in to comment.