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