Skip to content

Commit

Permalink
Variable symbols (#154)
Browse files Browse the repository at this point in the history
* Variable symbols

* Fix symbols

* Fix tests, export new functions, and extend set_variable_names (#152)
  • Loading branch information
dpsanders authored and lbenet committed Feb 22, 2018
1 parent ffc2436 commit 1c32f7c
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 12 deletions.
1 change: 1 addition & 0 deletions src/TaylorSeries.jl
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ export getcoeff, derivative, integrate,
show_params_TaylorN, show_monomials, displayBigO,
get_order, get_numvars,
set_variables, get_variables,
get_variable_names, get_variable_symbols,
∇, jacobian, jacobian!, hessian, hessian!,
taylor_expand, update!, constant_term

Expand Down
41 changes: 30 additions & 11 deletions src/parameters.jl
Original file line number Diff line number Diff line change
Expand Up @@ -11,29 +11,37 @@ DataType holding the current parameters for `TaylorN` and
**Fields:**
- `order :: Int` Order (degree) of the polynomials
- `num_vars :: Int` Number of variables
- `variable_names :: Array{String,1}` Name of the variables
- `order :: Int` Order (degree) of the polynomials
- `num_vars :: Int` Number of variables
- `variable_names :: Vector{String}` Names of the variables
- `variable_symbols :: Vector{Symbol}` Symbols of the variables
These parameters can be changed using `set_params_TaylorN(order, numVars)`.
"""
mutable struct ParamsTaylorN
order :: Int
num_vars :: Int
variable_names :: Array{String,1}
order :: Int
num_vars :: Int
variable_names :: Vector{String}
variable_symbols :: Vector{Symbol}
end

const _params_TaylorN_ = ParamsTaylorN(6, 2, String["x₁", "x₂"])

ParamsTaylorN(order, num_vars, variable_names) = ParamsTaylorN(order, num_vars, variable_names, Symbol.(variable_names))

const _params_TaylorN_ = ParamsTaylorN(6, 2, ["x₁", "x₂"])


## Utilities to get the maximum order and number of variables
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

set_variable_names(names::Vector{T}) where {T<:AbstractString} =
_params_TaylorN_.variable_names = names

function set_variable_names(varnames::Vector{T}) where {T<:AbstractString}
_params_TaylorN_.variable_names = varnames
_params_TaylorN_.variable_symbols = Symbol.(varnames)
nothing
end
"""
get_variables(;order=get_order())
Expand Down Expand Up @@ -85,6 +93,8 @@ function set_variables(R::Type, names::Vector{T}; order=get_order()) where
num_vars 1 || error("Number of variables must be at least 1")

_params_TaylorN_.variable_names = names
_params_TaylorN_.variable_symbols = Symbol.(names)


if !(order == get_order() && num_vars == get_numvars())
# if these are unchanged, no need to regenerate tables
Expand All @@ -105,9 +115,13 @@ function set_variables(R::Type, names::Vector{T}; order=get_order()) where
# return a list of the new variables
TaylorN{R}[TaylorN(R,i) for i in 1:get_numvars()]
end
set_variables(R::Type, symbs::Vector{T}; order=get_order()) where
{T<:Symbol} = set_variables(R, string.(symbs), order=order)

set_variables(names::Vector{T}; order=get_order()) where {T} =
set_variables(names::Vector{T}; order=get_order()) where {T<:AbstractString} =
set_variables(Float64, names, order=order)
set_variables(symbs::Vector{T}; order=get_order()) where {T<:Symbol} =
set_variables(Float64, symbs, order=order)

function set_variables(R::Type, names::T; order=get_order(), numvars=-1) where
{T<:AbstractString}
Expand All @@ -121,9 +135,13 @@ function set_variables(R::Type, names::T; order=get_order(), numvars=-1) where

set_variables(R, variable_names, order=order)
end
set_variables(R::Type, symbs::Symbol; order=get_order(), numvars=-1) =
set_variables(R, string(symbs), order=order, numvars=numvars)

set_variables(names::T; order=get_order(), numvars=-1) where {T<:AbstractString} =
set_variables(Float64, names, order=order, numvars=numvars)
set_variables(symbs::Symbol; order=get_order(), numvars=-1) =
set_variables(Float64, string(symbs), order=order, numvars=numvars)


"""
Expand All @@ -137,6 +155,7 @@ function show_params_TaylorN()
Maximum order = $(get_order())
Number of variables = $(get_numvars())
Variable names = $(get_variable_names())
Variable symbols = $(Symbol.(get_variable_names()))
""")
nothing
end
Expand Down
9 changes: 8 additions & 1 deletion test/manyvariables.jl
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ end
@test eltype(set_variables("x", numvars=2, order=6)) == TaylorN{Float64}
@test eltype(set_variables(BigInt, "x y", order=6)) == TaylorN{BigInt}
@test eltype(set_variables("x y", order=6)) == TaylorN{Float64}
@test eltype(set_variables(Int, :x, numvars=2, order=6)) == TaylorN{Int}
@test eltype(set_variables(:x, numvars=2, order=6)) == TaylorN{Float64}
@test eltype(set_variables(BigInt, [:x,:y], order=6)) == TaylorN{BigInt}
@test eltype(set_variables([:x,:y], order=6)) == TaylorN{Float64}
@test typeof(show_params_TaylorN()) == Void
@test typeof(show_monomials(2)) == Void

Expand All @@ -34,7 +38,10 @@ end

x, y = set_variables("x y", order=6)
@test x.order == 6
@test TaylorSeries.set_variable_names(["x","y"]) == ["x", "y"]
@test TaylorSeries.set_variable_names(["x","y"]) == nothing
@test TaylorSeries._params_TaylorN_.variable_names == ["x","y"]
@test TaylorSeries._params_TaylorN_.variable_symbols == [:x, :y]
@test get_variable_symbols() == [:x, :y]
@test TaylorSeries.get_variable_names() == ["x", "y"]
@test x == HomogeneousPolynomial(Float64, 1)
@test x == HomogeneousPolynomial(1)
Expand Down

0 comments on commit 1c32f7c

Please sign in to comment.