use crseo::{cu::CuType, cu::Single, Cu}; fn test_algebra() { let x: Cu = vec![1f32, 2f32, 3f32].into(); let a_mat: Cu = vec![vec![1f32, 2f32]; 3].into(); // let mut y = a_mat.mv(&x); let mut y = (&a_mat) * (&x); println!("y: {:?}", Vec::::from(&mut y)); let mut yscale = (&y) * 2f32; println!("yscale: {:?}", Vec::::from(&mut yscale)); yscale -= (&a_mat) * (&x); println!("yscale: {:?}", Vec::::from(&mut yscale)); } struct StateSpace { a_mat: Cu, b_mat: Cu, c_mat: Cu, d_mat: Option>, x: Cu, x_next: Cu, pub y: Cu, } impl StateSpace { pub fn new( a_mat: Vec>, b_mat: Vec>, c_mat: Vec>, d_mat: Option>>, ) -> Self { let n_x = a_mat[0].len(); let n_y = c_mat[0].len(); Self { a_mat: a_mat.into(), b_mat: b_mat.into(), c_mat: c_mat.into(), d_mat: d_mat.map(|x| x.into()), x: vec![0.0; n_x].into(), x_next: vec![0.0; n_x].into(), y: vec![0.0; n_y].into(), } } pub fn update(&mut self, u: &Cu) -> &mut Cu { self.y = &self.c_mat * &self.x; if let Some(ref d_mat) = self.d_mat { self.y = d_mat * u; } self.x_next = &self.a_mat * &self.x; self.x_next += &self.b_mat * u; self.x = self.x_next.clone(); &mut self.y } } fn main() { test_algebra(); let mut ss: StateSpace = StateSpace::new( vec![vec![1f32]], vec![vec![1f32]], vec![vec![-0.5f32]], None, ); (0..50).for_each(|k| { let mut u = Cu::::from(vec![1f32]); u += ss.y.clone(); println!("{:2}: {:.3}", k, Vec::::from(ss.update(&u))[0]); }); /* let mut ss: StateSpace = StateSpace::new( vec![vec![0f32,1f32],vec![1f32,0f32]], vec![vec![1f32,0f32]], vec![vec![0f32],vec![-0.2f32]], None, ); (0..50).for_each(|k| { let mut u = Cu::::from(vec![1f32]); u += ss.y.clone(); println!("{:2}: {:.?}", k, Vec::::from(ss.update(&u))); });*/ }