Giter VIP home page Giter VIP logo

sciml / rootedtrees.jl Goto Github PK

View Code? Open in Web Editor NEW
36.0 4.0 11.0 1.34 MB

A collection of functionality around rooted trees to generate order conditions for Runge-Kutta methods in Julia for differential equations and scientific machine learning (SciML)

Home Page: https://sciml.github.io/RootedTrees.jl

License: MIT License

Julia 100.00%
rooted-trees runge-kutta julia differential-equations sciml scientific-machine-learning hacktoberfest

rootedtrees.jl's Introduction

RootedTrees

Docs-stable Docs-dev Build Status Coverage Status codecov License: MIT DOI

A collection of functionality around rooted trees to generate order conditions for Runge-Kutta methods in Julia. This package also provides basic functionality for BSeries.jl.

API Documentation

The API of RootedTrees.jl is documented in the following. Additional information on each function is available in their docstrings and in the online documentation.

Construction

RootedTrees are represented using level sequences, i.e., AbstractVectors containing the distances of the nodes from the root, see

  • Beyer, Terry, and Sandra Mitchell Hedetniemi. "Constant time generation of rooted trees". SIAM Journal on Computing 9.4 (1980): 706-712. DOI: 10.1137/0209055

RootedTrees can be constructed from their level sequence using

julia> t = rootedtree([1, 2, 3, 2])
RootedTree{Int64}: [1, 2, 3, 2]

In the notation of Butcher (Numerical Methods for ODEs, 2016), this tree can be written as [[τ] τ] or (τ ∘ τ) ∘ (τ ∘ τ), where is the non-associative Butcher product of RootedTrees, which is also implemented.

To get the representation of a RootedTree introduced by Butcher, use butcher_representation:

julia> t = rootedtree([1, 2, 3, 4, 3, 3, 2, 2, 2, 2, 2])
RootedTree{Int64}: [1, 2, 3, 4, 3, 3, 2, 2, 2, 2, 2]

julia> butcher_representation(t)
"[[[τ]τ²]τ⁵]"

There are also some simple plot recipes for Plots.jl. Thus, you can visualize a rooted tree t using plot(t) when using Plots.

Additionally, there is an un-exported function RootedTrees.latexify that can generate LaTeX code for a rooted tree t based on the LaTeX package forest. The relevant code that needs to be included in the preamble can be obtained from the docstring of RootedTrees.latexify (type ? and RootedTrees.latexify in the Julia REPL). The same format is used when you are using Latexify and their function latexify, see Latexify.jl.

Iteration over RootedTrees

A RootedTreeIterator(order::Integer) can be used to iterate efficiently over all RootedTrees of a given order.

Be careful that the iterator is stateful for efficiency reasons, so you might need to use copy appropriately, e.g.,

julia> map(identity, RootedTreeIterator(4))
4-element Array{RootedTrees.RootedTree{Int64,Array{Int64,1}},1}:
 RootedTree{Int64}: [1, 2, 2, 2]
 RootedTree{Int64}: [1, 2, 2, 2]
 RootedTree{Int64}: [1, 2, 2, 2]
 RootedTree{Int64}: [1, 2, 2, 2]

julia> map(copy, RootedTreeIterator(4))
4-element Array{RootedTrees.RootedTree{Int64,Array{Int64,1}},1}:
 RootedTree{Int64}: [1, 2, 3, 4]
 RootedTree{Int64}: [1, 2, 3, 3]
 RootedTree{Int64}: [1, 2, 3, 2]
 RootedTree{Int64}: [1, 2, 2, 2]

Functions on Trees

The usual functions on RootedTrees are implemented, cf. Butcher (Numerical Methods for ODEs, 2016).

  • order(t::RootedTree): The order of a RootedTree, i.e., the length of its level sequence.
  • σ(t::RootedTree) or symmetry(t): The symmetry σ of a rooted tree, i.e., the order of the group of automorphisms on a particular labelling (of the vertices) of t.
  • γ(t::RootedTree) or density(t): The density γ(t) of a rooted tree, i.e., the product over all vertices of t of the order of the subtree rooted at that vertex.
  • α(t::RootedTree): The number of monotonic labelings of t not equivalent under the symmetry group.
  • β(t::RootedTree): The total number of labelings of t not equivalent under the symmetry group.

Additionally, functions on trees connected to Runge-Kutta methods are implemented.

  • elementary_weight(t, A, b, c): Compute the elementary weight Φ(t) of t::RootedTree for the Butcher coefficients A, b, c of a Runge-Kutta method.
  • derivative_weight(t, A, b, c): Compute the derivative weight (ΦᵢD)(t) of t for the Butcher coefficients A, b, c of a Runge-Kutta method.
  • residual_order_condition(t, A, b, c): The residual of the order condition (Φ(t) - 1/γ(t)) / σ(t) with elementary weight Φ(t), density γ(t), and symmetry σ(t) of the rooted tree t for the Runge-Kutta method with Butcher coefficients A, b, c.

Brief Changelog

  • v2.16: The LaTeX printing of rooted trees changed to allow representing colored rooted trees. Please update your LaTeX preamble as described in the docstring of RootedTrees.latexify.
  • v2.0: Rooted trees are considered up to isomorphisms introduced by shifting each coefficient of their level sequence by the same number.

Referencing

If you use RootedTrees.jl for your research, please cite the paper

@article{ketcheson2023computing,
  title={Computing with {B}-series},
  author={Ketcheson, David I and Ranocha, Hendrik},
  journal={ACM Transactions on Mathematical Software},
  volume={49},
  number={2},
  year={2023},
  month={06},
  doi={10.1145/3573384},
  eprint={2111.11680},
  eprinttype={arXiv},
  eprintclass={math.NA}
}

In addition, you can also refer to RootedTrees.jl directly as

@misc{ranocha2019rootedtrees,
  title={{RootedTrees.jl}: {A} collection of functionality around rooted trees
         to generate order conditions for {R}unge-{K}utta methods in {J}ulia
         for differential equations and scientific machine learning ({SciM}L)},
  author={Ranocha, Hendrik and contributors},
  year={2019},
  month={05},
  howpublished={\url{https://github.com/SciML/RootedTrees.jl}},
  doi={10.5281/zenodo.5534590}
}

rootedtrees.jl's People

Contributors

arnostrouwen avatar chrisrackauckas avatar christopher-dg avatar dependabot[bot] avatar erikqqy avatar github-actions[bot] avatar juliatagbot avatar mkg33 avatar pw0lf avatar ranocha avatar thazhemadam avatar yingboma avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

rootedtrees.jl's Issues

Colored tree improvements

The first version is implemented in #54. Left to do:

  • General ColoredRootedTreeIterator
  • LaTeX output of (bi-) colored rooted trees
  • filter function to filter out some trees for the iterators, e.g., for Nyström methods
  • Possible performance improvements are marked with # TODO: ColoredRootedTree (#71)
  • partitions (#69) (required for substitute)
  • SplittingIterator (required for compose)

See also #28 and ranocha/BSeries.jl#8

Detect invalid trees

Currently

RootedTree([1,1])

results in a valid rooted tree. If one uses the butcher printing style, the result is actually shown as the [1,2] tree.

Screen Shot 2023-02-20 at 4 34 02 AM

We get the same thing from

RootedTree([1,3])

and

RootedTree([1,0])

Draft a release

Could we publish a first release v0.0.1, please? This would make it easier for me to rely on a specific set of exports of this package.

Hook into Latexify

It could be an option to allow pretty printing of rooted tree in LaTeX via something like

% Butcher trees, cf. https://tex.stackexchange.com/questions/283343/butcher-trees-in-tikz
\usepackage{forest}
\forestset{
  */.style={
    delay+={append={[]},}
  },
  rooted tree/.style={
    for tree={
      grow'=90,
      parent anchor=center,
      child anchor=center,
      s sep=2.5pt,
      if level=0{
        baseline
      }{},
      delay={
        if content={*}{
          content=,
          append={[]}
        }{}
      }
    },
    before typesetting nodes={
      for tree={
        circle,
        fill,
        minimum width=3pt,
        inner sep=0pt,
        child anchor=center,
      },
    },
    before computing xy={
      for tree={
        l=5pt,
      }
    }
  }
}
\DeclareDocumentCommand\rootedtree{o}{\Forest{rooted tree [#1]}}

and \rootedtree[[],[]], \rootedtree[[[]]] etc.

TagBot trigger issue

This issue is used to trigger TagBot; feel free to unsubscribe.

If you haven't already, you should update your TagBot.yml to include issue comment triggers.
Please see this post on Discourse for instructions and more details.

If you'd like for me to do this for you, comment TagBot fix on this issue.
I'll open a PR within a few hours, please be patient!

`BicoloredRootedTreeIterator` gives some non-canonical trees

There is an issue with the iteration over colored rooted trees. The BicoloredRootedTreeIterator yields all 2^order(t) color sequences. However, not all of them are in canonical representation. For example, we get

julia> for t_iterator in BicoloredRootedTreeIterator(3)
           t_canonicl = RootedTrees.canonical_representation(t_iterator)
           if t_iterator != t_canonicl
               @warn "shouldn't be here" t_iterator t_canonicl
           end
       end
┌ Warning: shouldn't be here
│   t_iterator = ColoredRootedTree{Int64}: ([1, 2, 2], Bool[0, 0, 1])
│   t_canonicl = ColoredRootedTree{Int64}: ([1, 2, 2], Bool[0, 1, 0])
└ @ Main REPL[107]:4
┌ Warning: shouldn't be here
│   t_iterator = ColoredRootedTree{Int64}: ([1, 2, 2], Bool[1, 0, 1])
│   t_canonicl = ColoredRootedTree{Int64}: ([1, 2, 2], Bool[1, 1, 0])
└ @ Main REPL[107]:4

Xref ranocha/BSeries.jl#51

New Release

I've added some additional functionality in the last days but do not plan to add more in the next few days. Shall/can we release a new version?
How shall we generally proceed with releasing a new version?

More AD

Document and improve AD functionality. MWE

(@v1.7) pkg> activate --temp

(jl_SQuc5M) pkg> add RootedTrees ForwardDiff Zygote

julia> using ForwardDiff, Zygote, RootedTrees

julia> A = [0 0 0 0; 1//2 0 0 0; 0 1//2 0 0; 0 0 1 0]; b = [1//6, 1//3, 1//3, 1//6]; # classical RK4

julia> coeffs = vcat(vec(A), vec(b)); # one vector of all parameters

julia> function foo(t, coeffs)
           # for an RK method with four stages
           A = reshape(view(coeffs, 1:16), 4, 4)
           b = view(coeffs, 17:20)
           residual_order_condition(t, RungeKuttaMethod(A, b))
       end

julia> t = rootedtree([1, 2, 3, 4, 3])
RootedTree{Int64}: [1, 2, 3, 4, 3]

julia> foo(t, coeffs) # RK4 is not fifth-order accurate
-1//240

julia> ForwardDiff.gradient(coeffs -> foo(t, coeffs), coeffs)
20-element Vector{Rational{Int64}}:
 1//24
 1//24
 1//24
 0//1
 1//24
 1//12
 1//12
 0//1
 1//16
 1//8
 1//8
 1//48
 1//8
 7//24
 7//24
 1//12
 0//1
 0//1
 0//1
 1//8

julia> Zygote.gradient(coeffs -> foo(t, coeffs), coeffs)
(Rational{Int64}[1//24, 1//24, 1//24, 0//1, 1//24, 1//12, 1//12, 0//1, 1//16, 1//8, 1//8, 1//48, 1//8, 7//24, 7//24, 1//12, 0//1, 0//1, 0//1, 1//8],)

Make `collect(::RootedTreeIterator)` more convenient

A RootedTreeIterator iterates over view to mutable internal caches for performance (as documented). It can be convenient to specialize collect such that copies are made automatically, i.e., basically collect(::RootedTreeIterator) = map(copy, ::RootedTreeIterator).

Transfer to JuliaDiffEq?

@YingboMa is interested in taking this all the way to automated optimization kernel generation. We were trying this with ModelingToolkit.jl and it's working pretty well, so we were wondering if you'd like to transfer it over to JuliaDiffEq so we could start hacking away at it.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.