Skip to content

Commit

Permalink
Use new syntax of Julia 0.6 (#115)
Browse files Browse the repository at this point in the history
* Change the syntax according to new Julia 0.6 directives

This includes `struct`, inner constructors and `where` syntax.

* Delete use of Compat.jl for compatibility with Julia 0.5.

* Drop testing with Julia 0.5

* Delet 0.5 badge in README.md, corrections in the docs

* Use {...} after where

* Solve inconsistencies introduced in where

* Some  corrections, mainly in the docs.
  • Loading branch information
lbenet authored Oct 9, 2017
1 parent 909689b commit d2eedd7
Show file tree
Hide file tree
Showing 19 changed files with 414 additions and 327 deletions.
1 change: 0 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ os:
- osx

julia:
- 0.5
- 0.6
- nightly

Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
A [Julia](http://julialang.org) package for Taylor expansions in one or more
independent variables.

[![TaylorSeries](http://pkg.julialang.org/badges/TaylorSeries_0.5.svg)](http://pkg.julialang.org/?pkg=TaylorSeries)
[![TaylorSeries](http://pkg.julialang.org/badges/TaylorSeries_0.6.svg)](http://pkg.julialang.org/?pkg=TaylorSeries)
[![TaylorSeries](http://pkg.julialang.org/badges/TaylorSeries_0.7.svg)](http://pkg.julialang.org/?pkg=TaylorSeries)
[![Coverage Status](https://coveralls.io/repos/JuliaDiff/TaylorSeries.jl/badge.svg?branch=master)](https://coveralls.io/github/JuliaDiff/TaylorSeries.jl?branch=master)

[![](https://img.shields.io/badge/docs-stable-blue.svg)](http://www.juliadiff.org/TaylorSeries.jl/stable)
Expand Down
3 changes: 1 addition & 2 deletions REQUIRE
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
julia 0.5
Compat 0.17.0
julia 0.6
2 changes: 1 addition & 1 deletion docs/make.jl
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ makedocs(
deploydocs(
repo = "github.com/JuliaDiff/TaylorSeries.jl.git",
target = "build",
julia = "0.5",
julia = "0.6",
osname = "linux",
deps = nothing,
make = nothing
Expand Down
4 changes: 4 additions & 0 deletions docs/src/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ show_params_TaylorN
get_coeff
evaluate
evaluate!
taylor_expand
update!
derivative
integrate
gradient
Expand All @@ -41,6 +43,8 @@ hessian!
inverse
abs
norm
isapprox
isfinite
```

## Internals
Expand Down
5 changes: 3 additions & 2 deletions docs/src/examples.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ The first example shows that the four-square identity holds:
\end{eqnarray}
```
which was originally proved by Euler. The code can also be found in
[this test](../../test/identities_Euler.jl) of the package.
[this test](https://github.com/JuliaDiff/TaylorSeries.jl/blob/master/test/identities_Euler.jl) of the package.

First, we reset the maximum degree of the polynomial to 4, since the RHS
of the equation has *a priori* terms of fourth order, and define the 8
Expand Down Expand Up @@ -127,7 +127,8 @@ monomials in 4 variables.
### Bechmarks

The functions described above have been compared against Mathematica v11.1.
The relevant files used for benchmarking can be found [here](../../perf/).
The relevant files used for benchmarking can be found
[here](https://github.com/JuliaDiff/TaylorSeries.jl/tree/master/perf).
Running on a MacPro with Intel-Xeon processors 2.7GHz, we obtain that
Mathematica requires on average (5 runs) 3.075957 seconds for the computation,
while for `fateman1` and `fateman2` above we obtain 2.811391 and 1.490256,
Expand Down
83 changes: 53 additions & 30 deletions docs/src/userguide.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,13 @@ monomial, so that
etc. This is a dense representation of the polynomial.
The order of the polynomial can be
omitted in the constructor, which is then fixed by the length of the
vector of coefficients; otherwise, it defines the maximum
degree of the polynomial, and may be used to truncate it.
vector of coefficients. If the length of the vector does not correspond with
the `order`, `order` is used, which effectively truncates polynomial to degree `order`.

```@repl userguide
Taylor1([1, 2, 3],4) # Polynomial of order 4 with coefficients 1, 2, 3
Taylor1([0.0, 1im]) # Also works with complex numbers
Taylor1(ones(8), 2) # Polynomial of order 2
Taylor1(ones(8), 2) # Polynomial truncated to order 2
shift_taylor(a) = a + Taylor1(typeof(a),5) ## a + taylor-polynomial of order 5
t = shift_taylor(0.0) # Independent variable `t`
```
Expand All @@ -67,7 +67,7 @@ t*(t^2-4)/(t+2)
tI = im*t
(1-t)^3.2
(1+t)^t
t^6 # order is 5
t^6 # t is of order 5
```

If no valid Taylor expansion can be computed, an error is thrown, for instance
Expand Down Expand Up @@ -123,7 +123,7 @@ integrate(exp(t))
integrate( exp(t), 1.0)
integrate( derivative( exp(-t)), 1.0 ) == exp(-t)
derivative(1, exp(shift_taylor(1.0))) == exp(1.0)
derivative(5, exp(shift_taylor(1.0))) == exp(1.0) # Fifth derivative of `exp(1+t)`
derivative(5, exp(shift_taylor(1.0))) == exp(1.0) # 5-th derivative of `exp(1+t)`
```

To evaluate a Taylor series at a given point, Horner's rule is used via the
Expand All @@ -142,22 +142,34 @@ eBig = evaluate( exp(tBig), one(BigFloat) )
e - eBig
```

Another way to obtain the value of a `Taylor1` polynomial `p` at a given value `x`, is to call `p` as if it were a function:
Another way to obtain the value of a `Taylor1` polynomial `p` at a given value `x`, is to call `p` as if it were a function, i.e., `p(x)`:

```@repl userguide
t = Taylor1(15)
p = sin(t)
evaluate(p, pi/2) #get value of p at pi/2 using `evaluate`
p(pi/2) #get value of p at pi/2 by calling p as a function
evaluate(p, pi/2) # value of p at pi/2 using `evaluate`
p(pi/2) # value of p at pi/2 by evaluating p as a function
p(pi/2) == evaluate(p, pi/2)
p(0.0) #get value of `p` at 0.0 by calling p as a function
evaluate(p) #get p 0-th order coefficient using `evaluate`
p() #a shortcut to get 0-th order coefficient of `p`
p() == evaluate(p)
p() == p(0.0)
p(0.0)
p() == p(0.0) # p() is a shortcut to obtain the 0-th order coefficient of `p`
```

Note that the syntax `p(x)` is equivalent to `evaluate(p, x)`, whereas `p()` is
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
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.

```@repl userguide
p = taylor_expand( x -> sin(x), pi/2, order=16) # 16-th order expansion of sin(t) around pi/2
update!(p, 0.025) # updates the expansion given by p, by shifting it further by 0.025
p
```

Note that the syntax `p(x)` is equivalent to `evaluate(p,x)`, whereas `p()` is 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.

## Many variables

Expand All @@ -172,10 +184,10 @@ was discussed [here](https://groups.google.com/forum/#!msg/julia-users/AkK_UdST
The structure [`TaylorN`](@ref) is constructed as a vector of parameterized
homogeneous polynomials
defined by the type [`HomogeneousPolynomial`](@ref), which in turn is a vector of
coefficients of given order (degree). This implementation imposes that the user
has to specify the (maximum) order considered and the number of independent
variables, which is done using the [`set_variables`](@ref) function.
A vector of the resulting Taylor variables is returned:
coefficients of given order (degree). This implementation imposes the user
to specify the (maximum) order considered and the number of independent
variables at the beginning, which can be conveniently done using
[`set_variables`](@ref). A vector of the resulting Taylor variables is returned:

```@repl userguide
x, y = set_variables("x y")
Expand All @@ -185,13 +197,14 @@ x.coeffs
```

As shown, the resulting objects are of `TaylorN{Float64}` type.
There is an optional `order` keyword argument in [`set_variables`](@ref):
There is an optional `order` keyword argument in [`set_variables`](@ref),
used to specify the maximum order of the `TaylorN` polynomials.

```@repl userguide
set_variables("x y", order=10)
```

Numbered variables are also available by specifying a single
Similarly, numbered variables are also available by specifying a single
variable name and the optional keyword argument `numvars`:

```@repl userguide
Expand All @@ -205,12 +218,6 @@ parameters, in an info block.
show_params_TaylorN()
```

julia> show_params_TaylorN()
INFO: Parameters for `TaylorN` and `HomogeneousPolynomial`:
Maximum order = 10
Number of variables = 3
Variable names = UTF8String["α₁","α₂","α₃"]

Internally, changing the parameters (maximum order and number of variables)
redefines the hash-tables that
translate the index of the coefficients of a [`HomogeneousPolynomial`](@ref)
Expand Down Expand Up @@ -307,7 +314,18 @@ exy([.1,.02])
exy([.1,.02]) == e^0.12
```

Again, note that the syntax `p(x)` for `p::TaylorN` is equivalent to `evaluate(p,x)`, whereas `p()` is 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.
Again, the syntax `p(x)` for `p::TaylorN` is equivalent to `evaluate(p,x)`, and
`p()` is equivalent to `evaluate(p)`.

The functions `taylor_expand` and `update!` work as well for `TaylorN`.

```@repl userguide
xysq = x^2 + y^2
update!(xysq, [1.0, -2.0]) # expand around (1,-2)
xysq
update!(xysq, [-1.0, 2.0]) # shift-back
xysq == x^2 + y^2
```

Functions to compute the gradient, Jacobian and
Hessian have also been implemented. Using the
Expand All @@ -326,7 +344,8 @@ jacobian([p,q], [2,1])
hessian(r, [1.0,1.0])
```

Other specific applications are described in the next [section](examples).
Other specific applications are described in the
[Examples](@ref).

## Mixtures

Expand All @@ -338,15 +357,15 @@ somewhat special.

```@repl userguide
x, y = set_variables("x y", order=3)
t = Taylor1([zero(x), one(x)], 5)
t1N = Taylor1([zero(x), one(x)], 5)
```

The last line defines a `Taylor1{TaylorN{Float64}}` variable, which is of order
5 in `t` and order 3 in `x` and `y`. Then, we can evaluate functions involving
such polynomials:

```@repl userguide
cos(2.1+x+t)
cos(2.1+x+t1N)
```

This kind of expansions are of interest when studying the dependence of
Expand All @@ -355,3 +374,7 @@ the dependence of the solution of a differential equation on the initial conditi
around a given solution. In this case, `x` and `y` represent small variations
around a given value of the parameters, or around some specific initial condition.
Such constructions are exploited in the package [`TaylorIntegration.jl`](https://github.com/PerezHz/TaylorIntegration.jl).

```@meta
CurrentModule = nothing
```
11 changes: 1 addition & 10 deletions src/TaylorSeries.jl
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,9 @@ see also [`HomogeneousPolynomial`](@ref).
"""
module TaylorSeries

using Compat

if !isdefined(Base, :iszero)
import Compat: iszero
export iszero
else
import Base: iszero
end

import Base: ==, +, -, *, /, ^

import Base: zero, one, zeros, ones, isinf, isnan,
import Base: zero, one, zeros, ones, isinf, isnan, iszero,
convert, promote_rule, promote, eltype, length, show,
real, imag, conj, ctranspose,
rem, mod, mod2pi, abs, gradient,
Expand Down
Loading

0 comments on commit d2eedd7

Please sign in to comment.