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

WIP: Nested Taylors #126

Open
wants to merge 34 commits into
base: master
Choose a base branch
from
Open

WIP: Nested Taylors #126

wants to merge 34 commits into from

Conversation

blas-ko
Copy link
Contributor

@blas-ko blas-ko commented Sep 23, 2017

This is WIP for Nested Taylors, which I would like to benchmark eventually with TaylorIntegration's Jet Transport and compare it with TaylorN performance for a number of variables. Currently, all basic operations from arithmetic.jl are working correctly, and do as a natural extension of Taylor1 operations, but calculus.jl and other_functions.jl need to be handled differently.
To define N independent variables:

using TaylorSeries

num_vars, ord_ = (3,6)

@time x,y,z = set_nested_variables(num_vars,ord_) #coeffs type can also be specified as a third argument
  0.000035 seconds (273 allocations: 12.703 KiB)
  3-element Array{Number,1}:
    1.0 t + 𝒪(t⁷) + 𝒪(t⁷) + 𝒪(t⁷)
    1.0 + 𝒪(t⁷) t + 𝒪(t⁷) + 𝒪(t⁷)
    1.0 + 𝒪(t⁷) + 𝒪(t⁷) t + 𝒪(t⁷)

@time X,Y,Z = set_variables("x",numvars=3)
  0.000243 seconds (335 allocations: 25.219 KiB)
  3-element Array{TaylorSeries.TaylorN{Float64},1}:
    1.0 x₁ + 𝒪(‖x‖⁷)
    1.0 x₂ + 𝒪(‖x‖⁷)
    1.0 x₃ + 𝒪(‖x‖⁷)

Output is nothing near pretty for now, but one can check that basic operations work

@time 2x+3y^2-4z+5x*y-3z^3*x+10x
   0.001323 seconds (47.58 k allocations: 3.110 MiB)
   12.0 t + 𝒪(t⁷) +  5.0 t + 𝒪(t⁷) t +  3.0 + 𝒪(t⁷) t² + 𝒪(t⁷) +   - 4.0 + 𝒪(t⁷) + 𝒪(t⁷) t +   - 3.0 t + 𝒪(t⁷) + 𝒪(t⁷) t³ + 𝒪(t⁷)

@time 2X+3Y^2-4Z+5X*Y-3Z^3*X+10X
  0.000120 seconds (1.04 k allocations: 85.328 KiB)
  12.0 x₁ - 4.0 x₃ + 5.0 x₁ x₂ + 3.0 x₂² - 3.0 x₁ x₃³ + 𝒪(‖x‖⁷)

@time exp(y)
  0.000234 seconds (9.07 k allocations: 610.672 KiB)
   1.0 + 𝒪(t⁷) +  1.0 + 𝒪(t⁷) t +  0.5 + 𝒪(t⁷) t² +  0.16666666666666666 + 𝒪(t⁷) t³ +  
  0.041666666666666664 + 𝒪(t⁷) t⁴ +  0.008333333333333333 + 𝒪(t⁷) t⁵ +
  0.0013888888888888887 + 𝒪(t⁷) t⁶ + 𝒪(t⁷) + 𝒪(t⁷)

@time exp(Y)
  0.000018 seconds (108 allocations: 8.828 KiB)
 1.0 + 1.0 x₂ + 0.5 x₂² + 0.16666666666666666 x₂³ + 0.041666666666666664 x₂⁴ + 
  0.008333333333333333 x₂⁵ + 0.0013888888888888887 x₂⁶ + 𝒪(‖x‖⁷)

Jet Transport for Nested Taylors can't be done yet as there isn't a well defined method for norm(x,p) where x is a Nested Taylor.

All feedback is more than welcomed, @lbenet @PerezHz @dpsanders . No tests were added yet.

EDIT: I forgot to reference the corresponding discussion of #123

@coveralls
Copy link

coveralls commented Sep 23, 2017

Coverage Status

Coverage increased (+2.4%) to 96.566% when pulling 43cb5f0 on blas-ko:nested_taylor into 347fce1 on JuliaDiff:master.



#number of variables in a nested polynomial
function get_nested_numvars{T<:Number}(a::Taylor1{T})
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There may be some type instability in this function, since a_aux may change of type. I suggest to do the same but recursively (so call again get_nested_numvars), which may require to define another method, where a is not an AbstractSeries.

end


function nest{T<:Number}(a::Taylor1{T},num_vars::Integer,dim_a::Integer)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Again here, a is changing type, which may affect the performance.

end

#Shortcut to define an array of independent variables of nested taylors
function set_nested_variables(numvars::Integer,order::Integer,T::Type=Float64)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would suggest to have this as set_nested_variables(T::Type, numvars::Integer, order::Integer) which follows the convention we have, for example, to define Taylor1, and then define a specific method without it, which corresponds to the default Float64.

Though it is not urgent at the moment, I think it would be neat to have the possibility of different orders for the different variables, which in this case could be done by passing a Tuple of Int's of the correct length (numvars).

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As above, I think there may be type-stability issues here.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A possibility, to reduce temporary allocations (and maybe type instability problems too) is to exploit things like Taylor(T, order). In the loop in the function, T would be updated (but it would always be a DataType!) to get the nesting of the types.

PerezHz and others added 22 commits September 29, 2017 15:06
JuliaDiff#118)

* Add function-like behavior for Taylor1

* Relocate new code

* Add function-like behavior for TaylorN

* Fix TaylorN functor methods

* Add tests for Taylor1

* Add more Taylor1 tests

* Add TaylorN tests; more Taylor1 tests; add missing evaluate methods

* Add function-like behavior for HomogeneousPolynomial and corresponding tests

* Add missing tests for HomogeneousPolynomial

* Add another test for Taylor1s

* Fix test

* Add extra evaluate method for Taylor1 (suggested by @blas-ko)

* Add an evaluate test for mixtures (more to come)

* Add tests for mixtures; add/fix evaluate methods

* A small fix

* Add missing evaluate methods for mixtures and tests

* Update docs

* Add evaluate method and tests

* Fix new method

* Update docstrings

* Changes suggested by @lbenet 's review
* Added norm methods to get a non-negative Number from an AbstractSeries.

Suggestions from @lbenet and @PerezHz

* Added some tests for norm.

* Add isapprox and auxiliary methods

* Add one variable tests for isapprox

* Fix isfinite

* Add many variable tests

* Add NaN test for many variables isfinite

* Add isapprox tests for mixtures; fix `real` method for types

* Small fix on tests

* Fix mixtures tests

* Test isapprox for mixtures with BigFloats

* Add docstrings

* Add docstrings and a couple tests with ≉

* Added more mixture tests for norm.

* fixed some norm tests.

* Added documentation to abs and norm.

* silly fix for abs docs.

* Travis fix as proposed by @lbenet.

* Fix printing as suggested by @Keno; fix tests

* Add one more fix

* trigger travis build
* Added function and macro `taylor_expand` for expanding arbitraty functions.

* Deleted macros and set order as keyword argument so methods doesnt clash

`taylor_expand(f,x0::Int64)` and `taylor_expand(f,order)` clashed.

* Added some tests for `taylor_expand`.

* un-exported taylor_expand macros.

* Updated taylor_expand tests.

* Added taylor_expand method for TaylorN and warning if number of variables is changed.

* Fix travis issue for taylor_expand (hopefully).

* Added taylor_expand! and tests for Taylor1.

Followed suggestion from @lbenet.

* Add function-like behavior for Taylor1, TaylorN, HomogeneousPolynomial (JuliaDiff#118)

* Add function-like behavior for Taylor1

* Relocate new code

* Add function-like behavior for TaylorN

* Fix TaylorN functor methods

* Add tests for Taylor1

* Add more Taylor1 tests

* Add TaylorN tests; more Taylor1 tests; add missing evaluate methods

* Add function-like behavior for HomogeneousPolynomial and corresponding tests

* Add missing tests for HomogeneousPolynomial

* Add another test for Taylor1s

* Fix test

* Add extra evaluate method for Taylor1 (suggested by @blas-ko)

* Add an evaluate test for mixtures (more to come)

* Add tests for mixtures; add/fix evaluate methods

* A small fix

* Add missing evaluate methods for mixtures and tests

* Update docs

* Add evaluate method and tests

* Fix new method

* Update docstrings

* Changes suggested by @lbenet 's review

* Added method for evaluating a TaylorN with an array of TaylorNs.

* Added taylor_expand! method for TaylorN

* Added some test for taylor_expand!

* Documentation for taylor_expand and taylor_expand!

* Added function and macro `taylor_expand` for expanding arbitraty functions.

* Deleted macros and set order as keyword argument so methods doesnt clash

`taylor_expand(f,x0::Int64)` and `taylor_expand(f,order)` clashed.

* Added some tests for `taylor_expand`.

* un-exported taylor_expand macros.

* Updated taylor_expand tests.

* Added taylor_expand method for TaylorN and warning if number of variables is changed.

* Fix travis issue for taylor_expand (hopefully).

* Added taylor_expand! and tests for Taylor1.

Followed suggestion from @lbenet.

* Add function-like behavior for Taylor1, TaylorN, HomogeneousPolynomial (JuliaDiff#118)

* Add function-like behavior for Taylor1

* Relocate new code

* Add function-like behavior for TaylorN

* Fix TaylorN functor methods

* Add tests for Taylor1

* Add more Taylor1 tests

* Add TaylorN tests; more Taylor1 tests; add missing evaluate methods

* Add function-like behavior for HomogeneousPolynomial and corresponding tests

* Add missing tests for HomogeneousPolynomial

* Add another test for Taylor1s

* Fix test

* Add extra evaluate method for Taylor1 (suggested by @blas-ko)

* Add an evaluate test for mixtures (more to come)

* Add tests for mixtures; add/fix evaluate methods

* A small fix

* Add missing evaluate methods for mixtures and tests

* Update docs

* Add evaluate method and tests

* Fix new method

* Update docstrings

* Changes suggested by @lbenet 's review

* Added method for evaluating a TaylorN with an array of TaylorNs.

* Added taylor_expand! method for TaylorN

* Added some test for taylor_expand!

* Documentation for taylor_expand and taylor_expand!

* Corrected silly mistake from rebasing.

* Rearanged taylor_expand tests to another place.

They use set_variables internally.

* called coeff_table directly so it doesn't make a copy.

* changed docs for taylor_expand

* Changed taylor_expand for TaylorN.

* It doesn't use set_variables() anymore.
* typeof(x0) is preserved if possible.

* Changed taylor_expand! for update!

* Added 1 more test...

* Little performance and compatibility fix for taylor_expand
* 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.
* Use explicit recursion for acos in acos! ...

...instead of using asin!

* Add internalmutfunct.jl

* Add unary add! subst! and other methods, and ammend dictionaries

* Small corrections

* Add mul! and div! involving numeric coefficients

* Rename internalmutfunct.jl to dictmutfunct.jl and export constant_term

* Add identity! and another method of constant_term

* Add zero! and one! methods

* Include in _internalmutfunc_call TayloSeries before the function

* Revise the docstrings

* Add specific cases depending on the power to `pow!`

* Add missing methods for add! and subst! (JuliaDiff#117)

* Add missing in-place methods for unary +,-

* Add tests for new add!, subst! methods

* Add `abs!`

* Add `abs2(a)` as `a^2`, and the corresponding `abs2!`

* Correct `abs!` so it works for Taylor1{TaylorN{T}}

* Import abs2

* Add tests for some mutating functions and dictionary calls

* Fix some deprecations warning
* Declared new method for get_variables.

* Changed get_variables method so it takes order as a keyword argument.

* Added docs & tests for get_variables.

* Addition of get_variables to userguide.
* Start indexing in 0 for Taylor1

* Start indexing for TaylorN at 0

* Fix mixtures

* Fix mutatingfuncts tests

* Small fix for fateman40 test

* Disallow setindex! methods for UnitRange/Colon with HomogeneousPolynomials

... to avoid inconsistencies.

* Add docs

* Add show_monomials

* Export show_monomials and docs

* Add start, next, done

* Fix issues in sqrt! and derivative, and a test

* Correct a typo in userguide.md

[ci skip]

* Fixed another typo [ci skip]

* Fixes after review.
* Implement integrate for TaylorN variables with tests

* Add docs for integrate, fix one case, and a test
* Add derivative method
derivative(n,a) returns a Taylor1 variable; renamed existing derivative(n,a)
method as derivativeval

* Export derivativeval; add tests

* Fix & add derivative tests

* derivative(n,a) returns value; derivative(a,n) returns polynomial

* Improve performance with in-place operations with suggestions by @lbenet

* Fix docstrings

* Fix tests for julia v0.7-DEV

* Remove comment; add derivative test; add jacobian test for mixtures
* Add displayBigO to set/unset printing the bigO notation

This commit also addresses some deprecation warnings for Julia 0.7.

* Add docs [ci skip]
)

* Fix a problem with iterating over Taylor1/TaylorNs

* Revert defining start, next and done for Taylor1/TaylorN

Iterations should be handled as `a[i0:i1]` or `a[:]`

* Do not import Base: start, next, done
* Add rad2deg, deg2rad

* Add tests

* Add rad2deg!, deg2rad!, and tests
* Avoid irrelevant checks in getindex and setindex!

* Simplify code and remove unused `zero_korder` and `order_posTb`
* Added matrix evaluation for Taylor1 and TaylorN.

* Added tests for matrix evaluation.

* Extended evaluation methods to include SubArrays.

* Deleted redundant functions.

* Updated Matrix evaluation tests.

* extended norm and isapprox for AbstractSeries vectors.

* Fixed problematic test
* Correct a truncation issue when factorization is possible

* Small corrections and tests

* Correct a problem with the bounds in sqrt!

* Allow failures of nightly in travis
* Variable symbols

* Fix symbols

* Fix tests, export new functions, and extend set_variable_names (JuliaDiff#152)
* Add derivative and integrate methods with symbols

* Add evaluate for a TaylorN with a symbol and a value

* Include symbols usage into the docs

* Add two tests for Homogeneous polynomials

* Add lookupvar and evaluate methods for Pair with tests
* Use `@doc doc"..."`

* Use Compat, and update syntax for uninitialized arrays

* Add packages of stdlib, and fix some tests

* Replace Complex128, and load stdlib packages for tests

* Use lastindex, update findfirst and lookupvar, and fix broadcasting

* Instances related to Compat and broadcasting

* Solve an issue in 0.6 related to endof

* Fix endof for HomogeneousPolynomials

* Further depraction warnings solved

* Solve zero/one, rtoldefault and findfirst  deprecations

* Solve deprecations using convert methods as constructors
* Add methods of evaluate to handle NTuples

* Extend evaluate with varargs.

* Use zero./one. (instead of ugly fill!)

* Fix a problem with evaluate and Pairs
lbenet and others added 9 commits March 12, 2018 05:33
* Fix promotion and conversion involving irrationals

* Fix deprecations related to .=

* Workaround Irrational/ AbstractIrrational

* Add more tests
* Fix a deprecation (uninitialized -> undef) and an error in lookupvar

* Fix findfirst predicate in lookupvar
* Type-stability fixes in ^

* Use iszero

* Parameterize mutating functions

* Fixes to few arithmetic mutable functions

* Add more tests

* Use sqr! in abs2!

* Add more tests

* Add findlast for Taylor1, with tests.

* Fixes in a^n for a::Taylor1 and integer powers

* Avoid computations in evaluate if coeff is zero

* Add REQUIRE for documentation

* Fixes in docs
* Add arbitrary partial derivatives of TaylorN

* Add tests and docs

* Add a new method of derivative, and use same convention as Taylor1's derivative

* Add methods for getcoeff involving tuples
* Add methods of ^, that distinguish Real from AbstractFloat

* Add one new test

* Add tests involving intervals

* Add test/REQUIRE and modify runtests.jl

* Use ca^2 instead of ca*ca; important for Intervals

* Fix tests

* Fix (again) a broken test
@blas-ko
Copy link
Contributor Author

blas-ko commented May 14, 2018

Just rebased. I'll continue with this work.

@lbenet
Copy link
Member

lbenet commented May 14, 2018

Did you rebased to current master? Some commits that are within this PR seem to me already merged...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants