Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Custom printing for Taylor1 #229

Merged
merged 2 commits into from
Oct 21, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/src/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ isapprox
isfinite
displayBigO
use_show_default
set_taylor1_varname
```

## Internals
Expand Down
4 changes: 1 addition & 3 deletions src/TaylorSeries.jl
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@ using Requires
using LinearAlgebra: norm, mul!,
lu, lu!, LinearAlgebra.lutype, LinearAlgebra.copy_oftype,
LinearAlgebra.issuccess
# istriu, triu!, istril, tril!, UpperTriangular, LowerTriangular,
# LinearAlgebra.inv!, LinearAlgebra.checksquare

import LinearAlgebra: norm, mul!, lu

Expand All @@ -47,7 +45,7 @@ import Base: zero, one, zeros, ones, isinf, isnan, iszero,
export Taylor1, TaylorN, HomogeneousPolynomial, AbstractSeries

export getcoeff, derivative, integrate, differentiate,
evaluate, evaluate!, inverse,
evaluate, evaluate!, inverse, set_taylor1_varname,
show_params_TaylorN, show_monomials, displayBigO, use_show_default,
get_order, get_numvars,
set_variables, get_variables,
Expand Down
34 changes: 28 additions & 6 deletions src/parameters.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,32 @@
# Parameters for HomogeneousPolynomial and TaylorN


"""
ParamsTaylor1

DataType holding the current variable name for `Taylor1`.

**Field:**

- `var_name :: String` Names of the variables

These parameters can be changed using [`set_taylor1_varname`](@ref)
"""
mutable struct ParamsTaylor1
var_name :: String
end

const _params_Taylor1_ = ParamsTaylor1("t")

"""
set_taylor1_varname(var::String)

Change the displayed variable for `Taylor1` objects.
"""
set_taylor1_varname(var::String) = _params_Taylor1_.var_name = strip(var)



"""
ParamsTaylorN

Expand All @@ -13,7 +39,7 @@ DataType holding the current parameters for `TaylorN` and

- `order :: Int` Order (degree) of the polynomials
- `num_vars :: Int` Number of variables
- `variable_names :: Vector{String}` Names of the variables
- `variable_names :: Vector{String}` Names of the variables
- `variable_symbols :: Vector{Symbol}` Symbols of the variables

These parameters can be changed using [`set_variables`](@ref)
Expand Down Expand Up @@ -42,11 +68,7 @@ function lookupvar(s::Symbol)
return ind
end

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(T::Type, [order::Int=get_order()])

Expand Down
14 changes: 8 additions & 6 deletions src/printing.jl
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,17 @@ end

function pretty_print(a::Taylor1{T}) where {T<:NumberNotSeries}
z = zero(a[0])
var = _params_Taylor1_.var_name
space = string(" ")
bigO = bigOnotation[end] ?
string("+ 𝒪(t", superscriptify(a.order+1), ")") :
string("+ 𝒪(", var, superscriptify(a.order+1), ")") :
string("")
iszero(a) && return string(space, z, space, bigO)
strout::String = space
ifirst = true
for i in eachindex(a)
monom::String = i==0 ? string("") : i==1 ? string(" t") :
string(" t", superscriptify(i))
monom::String = i==0 ? string("") : i==1 ? string(" ", var) :
string(" ", var, superscriptify(i))
@inbounds c = a[i]
c == z && continue
cadena = numbr2str(c, ifirst)
Expand All @@ -43,16 +44,17 @@ end

function pretty_print(a::Taylor1{T} where {T <: AbstractSeries{S}}) where {S<:Number}
z = zero(a[0])
var = _params_Taylor1_.var_name
space = string(" ")
bigO = bigOnotation[end] ?
string("+ 𝒪(t", superscriptify(a.order+1), ")") :
string("+ 𝒪(", var, superscriptify(a.order+1), ")") :
string("")
iszero(a) && return string(space, z, space, bigO)
strout::String = space
ifirst = true
for i in eachindex(a)
monom::String = i==0 ? string("") : i==1 ? string(" t") :
string(" t", superscriptify(i))
monom::String = i==0 ? string("") : i==1 ? string(" ", var) :
string(" ", var, superscriptify(i))
@inbounds c = a[i]
c == z && continue
cadena = numbr2str(c, ifirst)
Expand Down
2 changes: 1 addition & 1 deletion test/manyvariables.jl
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ eeuler = Base.MathConstants.e
@test iterate(x, 7) == nothing

@test x.order == 6
@test TaylorSeries.set_variable_names(["x","y"]) == nothing
@test TaylorSeries.name_taylorNvar(1) == " x"
@test TaylorSeries._params_TaylorN_.variable_names == ["x","y"]
@test TaylorSeries._params_TaylorN_.variable_symbols == [:x, :y]
@test get_variable_symbols() == [:x, :y]
Expand Down
3 changes: 3 additions & 0 deletions test/onevariable.jl
Original file line number Diff line number Diff line change
Expand Up @@ -426,6 +426,9 @@ eeuler = Base.MathConstants.e
"Taylor1{Float64}([1.4142135623730951, 1.0, 0.0], 2)]"
use_show_default(false)
@test string(aa) == " 1.4142135623730951 + 1.0 t + 𝒪(t³)"
set_taylor1_varname(" x ")
@test string(aa) == " 1.4142135623730951 + 1.0 x + 𝒪(x³)"
set_taylor1_varname("t")
displayBigO(false)
@test string(ta(-3)) == " - 3 + 1 t "
@test string(ta(0)^3-3) == " - 3 + 1 t³ "
Expand Down