ASKSAGE: Sage Q&A Forum - RSS feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Tue, 06 Jan 2015 05:06:23 +0100Design of classes for dynamical systemshttps://ask.sagemath.org/question/25403/design-of-classes-for-dynamical-systems/ I have an ongoing project using Sage to process some dynamical systems (ODEs, that is), and along the way I've been developing some classes to generalize the process of storing an ODE system and working with it. I'd be interested in contributing the work to Sage if it's helpful, and in understanding what's a "sage-like" way to do it.
So here I'm wondering about what would be worth trying to contribute to Sage, and asking whether people would do the design differently.
I have a central class called ODEsystem, which is built around a dictionary encoding the flow function, e.g. if the system is
$$dx/dt = -ax-by$$
$$dy/dt = -cx-dy$$
the flow is a dictionary `{ x:-ax-by, y:-cx-dy }` whose keys are variables and values are symbolic expressions. The object has methods to write itself out in LaTeX form, to integrate the dynamics from initial conditions, to find its equilibria and their stability, to make bifurcation diagrams under certain conditions, and a few other things. My intention is to allow room for a class hierarchy of models to develop, including difference equations, stochastic dynamics, PDEs, etc. There's also room to create subclasses and functions that derive specific models from general ones, simple models as limit cases of complex models, etc.
Another class that may be of use to others is Bindings, which binds variables to specific values in various expressions. For example, if I write
var('a, b')
generic_system = ODESystem( { x:a-bx }, vars=[x] )
specific_bindings = Bindings( a=1, b=2 )
latex( generic_system.bind( specific_bindings ) )
I'll get $dx/dt = 1-2x$. This is a wrapper for `substitute()` and `substitute_function()`, of course, but adds convenience and flexibility. (Note ODEsystem uses 'vars' to define the order of state variables. It's not logically necessary but it's useful in multiple dimensions.)
Code for those two classes is at https://github.com/worden-lee/SageDynamics/blob/master/SageDynamics/dynamicalsystems.py.step, and there's some more stuff in other files there. Example output is at http://lalashan.mcmaster.ca/theobio/worden/index.php/SageDynamics/Demo.
Questions:
* Could something like this become a useful part of Sage
* Would people want it to be designed differently
* Different situations call for different choices of integration routines. One strategy is to provide different ODEsystem classes that use different numerical integrators, and another one is to uncouple the integration from the class that represents the system. I'm not sure what would be better.
* Would it be good to represent the ODE as an element of a ring of vector fields on an appropriate manifold, with some extra methods added? It seems like that would provide some power, but I don't know the system well enough to go there right now.
* Should I send this to sage-devel or something instead of posting hereMon, 05 Jan 2015 21:45:15 +0100https://ask.sagemath.org/question/25403/design-of-classes-for-dynamical-systems/Comment by Nathann for <p>I have an ongoing project using Sage to process some dynamical systems (ODEs, that is), and along the way I've been developing some classes to generalize the process of storing an ODE system and working with it. I'd be interested in contributing the work to Sage if it's helpful, and in understanding what's a "sage-like" way to do it.</p>
<p>So here I'm wondering about what would be worth trying to contribute to Sage, and asking whether people would do the design differently.</p>
<p>I have a central class called ODEsystem, which is built around a dictionary encoding the flow function, e.g. if the system is</p>
<p>$$dx/dt = -ax-by$$
$$dy/dt = -cx-dy$$</p>
<p>the flow is a dictionary <code>{ x:-ax-by, y:-cx-dy }</code> whose keys are variables and values are symbolic expressions. The object has methods to write itself out in LaTeX form, to integrate the dynamics from initial conditions, to find its equilibria and their stability, to make bifurcation diagrams under certain conditions, and a few other things. My intention is to allow room for a class hierarchy of models to develop, including difference equations, stochastic dynamics, PDEs, etc. There's also room to create subclasses and functions that derive specific models from general ones, simple models as limit cases of complex models, etc.</p>
<p>Another class that may be of use to others is Bindings, which binds variables to specific values in various expressions. For example, if I write</p>
<pre><code>var('a, b')
generic_system = ODESystem( { x:a-bx }, vars=[x] )
specific_bindings = Bindings( a=1, b=2 )
latex( generic_system.bind( specific_bindings ) )
</code></pre>
<p>I'll get $dx/dt = 1-2x$. This is a wrapper for <code>substitute()</code> and <code>substitute_function()</code>, of course, but adds convenience and flexibility. (Note ODEsystem uses 'vars' to define the order of state variables. It's not logically necessary but it's useful in multiple dimensions.)</p>
<p>Code for those two classes is at <a href="https://github.com/worden-lee/SageDynamics/blob/master/SageDynamics/dynamicalsystems.py.step">https://github.com/worden-lee/SageDyn...</a>, and there's some more stuff in other files there. Example output is at <a href="http://lalashan.mcmaster.ca/theobio/worden/index.php/SageDynamics/Demo">http://lalashan.mcmaster.ca/theobio/w...</a>.</p>
<p>Questions:</p>
<ul>
<li>Could something like this become a useful part of Sage</li>
<li>Would people want it to be designed differently</li>
<li>Different situations call for different choices of integration routines. One strategy is to provide different ODEsystem classes that use different numerical integrators, and another one is to uncouple the integration from the class that represents the system. I'm not sure what would be better.</li>
<li>Would it be good to represent the ODE as an element of a ring of vector fields on an appropriate manifold, with some extra methods added? It seems like that would provide some power, but I don't know the system well enough to go there right now.</li>
<li>Should I send this to sage-devel or something instead of posting here</li>
</ul>
https://ask.sagemath.org/question/25403/design-of-classes-for-dynamical-systems/?comment=25409#post-id-25409Yep, that's probably a good question for sage-devel !Tue, 06 Jan 2015 05:06:23 +0100https://ask.sagemath.org/question/25403/design-of-classes-for-dynamical-systems/?comment=25409#post-id-25409Answer by wonder for <p>I have an ongoing project using Sage to process some dynamical systems (ODEs, that is), and along the way I've been developing some classes to generalize the process of storing an ODE system and working with it. I'd be interested in contributing the work to Sage if it's helpful, and in understanding what's a "sage-like" way to do it.</p>
<p>So here I'm wondering about what would be worth trying to contribute to Sage, and asking whether people would do the design differently.</p>
<p>I have a central class called ODEsystem, which is built around a dictionary encoding the flow function, e.g. if the system is</p>
<p>$$dx/dt = -ax-by$$
$$dy/dt = -cx-dy$$</p>
<p>the flow is a dictionary <code>{ x:-ax-by, y:-cx-dy }</code> whose keys are variables and values are symbolic expressions. The object has methods to write itself out in LaTeX form, to integrate the dynamics from initial conditions, to find its equilibria and their stability, to make bifurcation diagrams under certain conditions, and a few other things. My intention is to allow room for a class hierarchy of models to develop, including difference equations, stochastic dynamics, PDEs, etc. There's also room to create subclasses and functions that derive specific models from general ones, simple models as limit cases of complex models, etc.</p>
<p>Another class that may be of use to others is Bindings, which binds variables to specific values in various expressions. For example, if I write</p>
<pre><code>var('a, b')
generic_system = ODESystem( { x:a-bx }, vars=[x] )
specific_bindings = Bindings( a=1, b=2 )
latex( generic_system.bind( specific_bindings ) )
</code></pre>
<p>I'll get $dx/dt = 1-2x$. This is a wrapper for <code>substitute()</code> and <code>substitute_function()</code>, of course, but adds convenience and flexibility. (Note ODEsystem uses 'vars' to define the order of state variables. It's not logically necessary but it's useful in multiple dimensions.)</p>
<p>Code for those two classes is at <a href="https://github.com/worden-lee/SageDynamics/blob/master/SageDynamics/dynamicalsystems.py.step">https://github.com/worden-lee/SageDyn...</a>, and there's some more stuff in other files there. Example output is at <a href="http://lalashan.mcmaster.ca/theobio/worden/index.php/SageDynamics/Demo">http://lalashan.mcmaster.ca/theobio/w...</a>.</p>
<p>Questions:</p>
<ul>
<li>Could something like this become a useful part of Sage</li>
<li>Would people want it to be designed differently</li>
<li>Different situations call for different choices of integration routines. One strategy is to provide different ODEsystem classes that use different numerical integrators, and another one is to uncouple the integration from the class that represents the system. I'm not sure what would be better.</li>
<li>Would it be good to represent the ODE as an element of a ring of vector fields on an appropriate manifold, with some extra methods added? It seems like that would provide some power, but I don't know the system well enough to go there right now.</li>
<li>Should I send this to sage-devel or something instead of posting here</li>
</ul>
https://ask.sagemath.org/question/25403/design-of-classes-for-dynamical-systems/?answer=25404#post-id-25404I guess a Bindings is a morphism from SR to itself, that can be coerced more or less trivially to operate on vectors, vector fields, dynamical systems, etc. I don't know whether it would be useful to represent it that way somehow.Mon, 05 Jan 2015 23:13:45 +0100https://ask.sagemath.org/question/25403/design-of-classes-for-dynamical-systems/?answer=25404#post-id-25404