Skip to content

Commit

Permalink
Add displayBigO to set/unset printing the bigO notation (#139)
Browse files Browse the repository at this point in the history
* Add displayBigO to set/unset printing the bigO notation

This commit also addresses some deprecation warnings for Julia 0.7.

* Add docs [ci skip]
  • Loading branch information
lbenet authored Nov 27, 2017
1 parent 8c385c7 commit 5d9794f
Show file tree
Hide file tree
Showing 7 changed files with 62 additions and 11 deletions.
2 changes: 2 additions & 0 deletions docs/src/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ abs
norm
isapprox
isfinite
displayBigO
```

## Internals
Expand Down Expand Up @@ -79,6 +80,7 @@ atan!
sinhcosh!
tanh!
A_mul_B!
derivative!
```

## Index
Expand Down
13 changes: 11 additions & 2 deletions docs/src/userguide.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,15 @@ t = shift_taylor(0.0) # Independent variable `t`
```

Note that the information about the maximum order considered is displayed
using a big-𝒪 notation.
using a big-𝒪 notation. In some cases, it is desirable to not display
the big-𝒪 notation. The function [`displayBigO`](@ref) allows to
control whether it is displayed or not.
```@repl userguide
displayBigO(false) # turn-off displaying big O notation
t
displayBigO(true) # turn it on
t
```

The definition of `shift_taylor(a)` uses the method
[`Taylor1([::Type{Float64}], [order::Int64=1])`](@ref), which is a
Expand Down Expand Up @@ -170,7 +178,8 @@ equivalent to `evaluate(p)`. For more details about function-like behavior for a
given type in Julia, see the [Function-like objects](https://docs.julialang.org/en/stable/manual/methods/#Function-like-objects-1)
section of the Julia manual.

Useful shortcuts are `taylor_expand` are `update!`. The former returns
Useful shortcuts are [`taylor_expand`](@ref) are [`update!`](@ref).
The former returns
the expansion of a function around a given value `t0`. In turn, `update!`
provides an in-place update of a given Taylor polynomial, that is, it shifts
it further by the provided amount.
Expand Down
2 changes: 1 addition & 1 deletion src/TaylorSeries.jl
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export Taylor1, TaylorN, HomogeneousPolynomial, AbstractSeries

export getcoeff, derivative, integrate,
evaluate, evaluate!, inverse,
show_params_TaylorN, show_monomials,
show_params_TaylorN, show_monomials, displayBigO,
get_order, get_numvars,
set_variables, get_variables,
∇, jacobian, jacobian!, hessian, hessian!,
Expand Down
13 changes: 13 additions & 0 deletions src/parameters.jl
Original file line number Diff line number Diff line change
Expand Up @@ -140,3 +140,16 @@ function show_params_TaylorN()
""")
nothing
end


# Control the display of the big 𝒪 notation
const bigOnotation = Bool[true]

"""
displayBigO(d::Bool) --> nothing
Set/unset displaying of the big 𝒪 notation in the output
of `Taylor1` and `TaylorN` polynomials. The initial value is
`true`.
"""
displayBigO(d::Bool) = (bigOnotation[end] = d; d)
22 changes: 18 additions & 4 deletions src/printing.jl
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,10 @@ end
function pretty_print(a::Taylor1{T}) where {T<:Number}
z = zero(a[0])
space = string(" ")
bigO = string("+ 𝒪(t", superscriptify(a.order+1), ")")
# bigO = string("+ 𝒪(t", superscriptify(a.order+1), ")")
bigO = bigOnotation[end] ?
string("+ 𝒪(t", superscriptify(a.order+1), ")") :
string("")
a == zero(a) && return string(space, z, space, bigO)
strout::String = space
ifirst = true
Expand All @@ -42,7 +45,10 @@ end
function pretty_print(a::Taylor1{HomogeneousPolynomial{T}}) where {T<:Number}
z = zero(a[0])
space = string(" ")
bigO = string("+ 𝒪(t", superscriptify(a.order+1), ")")
# bigO = string("+ 𝒪(t", superscriptify(a.order+1), ")")
bigO = bigOnotation[end] ?
string("+ 𝒪(t", superscriptify(a.order+1), ")") :
string("")
a == zero(a) && return string(space, z, space, bigO)
strout::String = space
ifirst = true
Expand All @@ -64,7 +70,11 @@ end
function pretty_print(a::Taylor1{TaylorN{T}}) where {T<:Number}
z = zero(a[0])
space = string(" ")
bigO = string("+ 𝒪(t", superscriptify(a.order+1), ")")
# bigO = string("+ 𝒪(t", superscriptify(a.order+1), ")")
bigO = bigOnotation[end] ?
string("+ 𝒪(t", superscriptify(a.order+1), ")") :
string("")
a == zero(a) && return string(space, z, space, bigO)
a == zero(a) && return string(space, z, space, bigO)
strout::String = space
ifirst = true
Expand Down Expand Up @@ -94,7 +104,11 @@ end
function pretty_print(a::TaylorN{T}) where {T<:Number}
z = zero(a[0])
space = string("")
bigO::String = string(" + 𝒪(‖x‖", superscriptify(a.order+1), ")")
# bigO::String = string(" + 𝒪(‖x‖", superscriptify(a.order+1), ")")
bigO::String = bigOnotation[end] ?
string(" + 𝒪(‖x‖", superscriptify(a.order+1), ")") :
string("")
a == zero(a) && return string(space, z, space, bigO)
a == zero(a) && return string(space, z, bigO)
strout::String = space#string("")
ifirst = true
Expand Down
6 changes: 6 additions & 0 deletions test/manyvariables.jl
Original file line number Diff line number Diff line change
Expand Up @@ -415,6 +415,12 @@ end
hessian!(hes2,g1(xT+1,yT-1)-g2(xT+1,yT-1))
@test hes1 == hes2

displayBigO(false)
@test string(-xH) == " - 1 x₁"
@test string(xT^2) == " 1 x₁²"
@test string(1im*yT) == " ( 1 im ) x₂"
@test string(xT-im*yT) == " ( 1 ) x₁ - ( 1 im ) x₂"
displayBigO(true)
@test string(-xH) == " - 1 x₁"
@test string(xT^2) == " 1 x₁² + 𝒪(‖x‖¹⁸)"
@test string(1im*yT) == " ( 1 im ) x₂ + 𝒪(‖x‖¹⁸)"
Expand Down
15 changes: 11 additions & 4 deletions test/onevariable.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@
using TaylorSeries
if VERSION < v"0.7.0-DEV.2004"
using Base.Test
eeuler = Base.e
else
using Test
eeuler = Base.MathConstants.e
end

@testset "Tests for Taylor1 expansions" begin
Expand Down Expand Up @@ -177,13 +179,13 @@ end
@test getcoeff(convert(Taylor1{Rational{Int}},cos(t)),8) == 1//factorial(8)
@test abs((tan(t))[7]- 17/315) < tol1
@test abs((tan(t))[13]- 21844/6081075) < tol1
@test evaluate(exp(Taylor1([0,1],17)),1.0) == 1.0*e
@test evaluate(exp(Taylor1([0,1],17)),1.0) == 1.0*eeuler
@test evaluate(exp(Taylor1([0,1],1))) == 1.0
@test evaluate(exp(t),t^2) == exp(t^2)
#Test function-like behavior for Taylor1s
t17 = Taylor1([0,1],17)
myexpfun = exp(t17)
@test myexpfun(1.0) == 1.0*e
@test myexpfun(1.0) == 1.0*eeuler
@test myexpfun() == 1.0
@test myexpfun(t17^2) == exp(t17^2)
@test exp(t17^2)(t17) == exp(t17^2)
Expand Down Expand Up @@ -327,7 +329,7 @@ end

@test norm((inverse(exp(t)-1) - log(1+t)).coeffs) < 2tol1
cfs = [(-n)^(n-1)/gamma(n+1) for n = 1:15]
@test norm(inverse(t*exp(t))[1:end]./cfs-1) < 4tol1
@test norm(inverse(t*exp(t))[1:end]./cfs .- 1) < 4tol1

@test_throws ArgumentError Taylor1([1,2,3], -2)
@test_throws ArgumentError abs(ta(big(0)))
Expand All @@ -345,12 +347,17 @@ end
@test_throws AssertionError derivative(30, exp(ta(1.0pi)))
@test_throws ArgumentError inverse(exp(t))

displayBigO(false)
@test string(ta(-3)) == " - 3 + 1 t "
@test string(ta(0)^3-3) == " - 3 + 1 t³ "
@test TaylorSeries.pretty_print(ta(3im)) == " ( 3 im ) + ( 1 ) t "
@test string(Taylor1([1,2,3,4,5], 2)) == string(Taylor1([1,2,3]))
displayBigO(true)
@test string(ta(-3)) == " - 3 + 1 t + 𝒪(t¹⁶)"
@test string(ta(0)^3-3) == " - 3 + 1 t³ + 𝒪(t¹⁶)"
@test TaylorSeries.pretty_print(ta(3im)) == " ( 3 im ) + ( 1 ) t + 𝒪(t¹⁶)"
@test string(Taylor1([1,2,3,4,5], 2)) == string(Taylor1([1,2,3]))


a = collect(1:12)
t_a = Taylor1(a,15)
t_C = complex(3.0,4.0) * t_a
Expand Down

0 comments on commit 5d9794f

Please sign in to comment.