1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
use std::ops::{Add, Div, Mul, Neg, Sub}; pub trait State: Clone { type PositionDerivative: StateDerivative; type MomentumDerivative: StateDerivative; fn shift_position(&self, dir: &Self::PositionDerivative, amount: f64) -> Self { let mut result = self.clone(); result.shift_position_in_place(dir, amount); result } fn shift_position_in_place(&mut self, dir: &Self::PositionDerivative, amount: f64); fn shift_momentum(&self, dir: &Self::MomentumDerivative, amount: f64) -> Self { let mut result = self.clone(); result.shift_momentum_in_place(dir, amount); result } fn shift_momentum_in_place(&mut self, dir: &Self::MomentumDerivative, amount: f64); } pub trait StateDerivative: Clone + Sized + Add<Self, Output = Self> + Sub<Self, Output = Self> + Mul<f64, Output = Self> + Div<f64, Output = Self> + Neg<Output = Self> { } #[cfg(feature = "nalgebra")] mod nalgebra_impl { use std::ops::{Add, Div, Mul, Neg, Sub}; use super::StateDerivative; use nalgebra::{storage::Storage, Dim, Vector}; impl<D: Dim, S> StateDerivative for Vector<f64, D, S> where Self: Clone + Add<Self, Output = Self> + Sub<Self, Output = Self> + Mul<f64, Output = Self> + Div<f64, Output = Self> + Neg<Output = Self>, S: Storage<f64, D>, { fn abs(&self) -> f64 { self.dot(self).sqrt() } } }