-
Notifications
You must be signed in to change notification settings - Fork 42
Factor linear/Lotka-Voltera ODE analyses through PolynomialSystem
#1007
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
base: main
Are you sure you want to change the base?
Changes from all commits
10fcf6c
77a436d
64cec93
122e0f9
ac9d9de
3002eb0
f2ae653
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -14,7 +14,7 @@ use tsify::Tsify; | |
|
|
||
| use super::{ODEAnalysis, SignedCoefficientBuilder}; | ||
| use crate::dbl::model::DiscreteDblModel; | ||
| use crate::simulate::ode::{LinearODESystem, ODEProblem}; | ||
| use crate::simulate::ode::{NumericalPolynomialSystem, ODEProblem, linear_polynomial_system}; | ||
| use crate::{one::QualifiedPath, zero::QualifiedName}; | ||
|
|
||
| /// Data defining a linear ODE problem for a model. | ||
|
|
@@ -47,7 +47,7 @@ impl SignedCoefficientBuilder<QualifiedName, QualifiedPath> { | |
| &self, | ||
| model: &DiscreteDblModel, | ||
| data: LinearODEProblemData, | ||
| ) -> ODEAnalysis<LinearODESystem> { | ||
| ) -> ODEAnalysis<NumericalPolynomialSystem<u8>> { | ||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The exponent for linear systems is obviously always 1. So it's tempting to use or define a singleton type, but that might be overkill. Not sure how easy or conventional it is in Rust.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not sure this accounts for constants (zero exponents) though. We'd need a type with 0 and 1
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh yeah, good point 🤦♂️ |
||
| let (matrix, ob_index) = self.build_matrix(model, &data.coefficients); | ||
| let n = ob_index.len(); | ||
|
|
||
|
|
@@ -56,7 +56,7 @@ impl SignedCoefficientBuilder<QualifiedName, QualifiedPath> { | |
| .map(|ob| data.initial_values.get(ob).copied().unwrap_or_default()); | ||
| let x0 = DVector::from_iterator(n, initial_values); | ||
|
|
||
| let system = LinearODESystem::new(matrix); | ||
| let system = linear_polynomial_system(matrix); | ||
| let problem = ODEProblem::new(system, x0).end_time(data.duration); | ||
| ODEAnalysis::new(problem, ob_index) | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,9 +1,9 @@ | ||
| //! Commutative algebra and polynomials. | ||
|
|
||
| use num_traits::{One, Pow, Zero}; | ||
| use num_traits::{One, Pow, Zero, one, zero}; | ||
| use std::collections::BTreeMap; | ||
| use std::fmt::Display; | ||
| use std::iter::{Product, Sum}; | ||
| use std::iter::{self, Product, Sum}; | ||
| use std::ops::{Add, AddAssign, Mul, Neg}; | ||
|
|
||
| use derivative::Derivative; | ||
|
|
@@ -35,7 +35,7 @@ pub trait CommAlg: CommRing + Module<Ring = Self::R> { | |
| /// In abstract terms, polynomials with coefficients valued in a [commutative | ||
| /// ring](super::rig::CommRing) *R* are the free [commutative algebra](CommAlg) | ||
| /// over *R*. | ||
| #[derive(Clone, PartialEq, Eq, Derivative)] | ||
| #[derive(Clone, PartialEq, Eq, Derivative, Debug)] | ||
| #[derivative(Default(bound = ""))] | ||
| pub struct Polynomial<Var, Coef, Exp>(Combination<Monomial<Var, Exp>, Coef>); | ||
|
|
||
|
|
@@ -183,6 +183,29 @@ where | |
| // XXX: Lots of boilerplate to delegate the module structure of `Polynomial` to | ||
| // `Combination` because these traits cannot be derived automatically. | ||
|
|
||
| impl<Var, Coef, Exp> Sum for Polynomial<Var, Coef, Exp> | ||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There's nothing here that's actually specific to Note that
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh, and also, looking at the comment above this, maybe we should actually move the main |
||
| where | ||
| Var: Ord, | ||
| Coef: AdditiveMonoid, | ||
| Exp: Ord, | ||
| { | ||
| fn sum<I: Iterator<Item = Self>>(iter: I) -> Self { | ||
| iter.fold(zero(), |acc, x| acc + x) | ||
| } | ||
| } | ||
|
|
||
| impl<Var, Coef, Exp> Add<Coef> for Polynomial<Var, Coef, Exp> | ||
| where | ||
| Var: Ord, | ||
| Coef: Add<Output = Coef>, | ||
| Exp: Ord + Zero, | ||
| { | ||
| type Output = Polynomial<Var, Coef, Exp>; | ||
| fn add(self, a: Coef) -> Self::Output { | ||
| Polynomial(self.0 + iter::once((a, one::<Monomial<_, _>>())).collect()) | ||
| } | ||
| } | ||
|
|
||
| impl<Var, Coef, Exp> AddAssign<(Coef, Monomial<Var, Exp>)> for Polynomial<Var, Coef, Exp> | ||
| where | ||
| Var: Ord, | ||
|
|
@@ -210,7 +233,7 @@ where | |
| impl<Var, Coef, Exp> Zero for Polynomial<Var, Coef, Exp> | ||
| where | ||
| Var: Ord, | ||
| Coef: Add<Output = Coef> + Zero, | ||
| Coef: Zero, | ||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This slight generalisation was useful during debugging. I'm not sure if it was necessary in the end, but there's no harm. Coming from Haskell, I am a bit surprised that the compiler doesn't warn about redundant constraints.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, I've found myself wishing for such warnings but AFAIK, the Rust compiler doesn't warn about either redundant constraints or unnecessary constraints. |
||
| Exp: Ord, | ||
| { | ||
| fn zero() -> Self { | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To avoid triggering the linter in CI, you can run
cargo clippylocally before pushing.