@@ -1148,36 +1148,6 @@ pub fn sparse_threshold(w: &[Vec<(u16, I32F32)>], threshold: I32F32) -> Vec<Vec<
11481148 . collect ( )
11491149}
11501150
1151- // /// Calculates the exponential moving average (EMA) for a sparse matrix using dynamic alpha values.
1152- // #[allow(dead_code)]
1153- // pub fn mat_ema_alpha_vec_sparse(
1154- // new: &Vec<Vec<(u16, I32F32)>>,
1155- // old: &Vec<Vec<(u16, I32F32)>>,
1156- // alpha: &Vec<I32F32>,
1157- // ) -> Vec<Vec<(u16, I32F32)>> {
1158- // assert!(new.len() == old.len());
1159- // let n = new.len(); // assume square matrix, rows=cols
1160- // let zero: I32F32 = I32F32::from_num(0.0);
1161- // let mut result: Vec<Vec<(u16, I32F32)>> = vec![vec![]; n];
1162- // for i in 0..new.len() {
1163- // let mut row: Vec<I32F32> = vec![zero; n];
1164- // for (j, value) in new[i].iter() {
1165- // let alpha_val: I32F32 = alpha[*j as usize];
1166- // row[*j as usize] += alpha_val * value;
1167- // }
1168- // for (j, value) in old[i].iter() {
1169- // let one_minus_alpha: I32F32 = I32F32::from_num(1.0) - alpha[*j as usize];
1170- // row[*j as usize] += one_minus_alpha * value;
1171- // }
1172- // for (j, value) in row.iter().enumerate() {
1173- // if *value > zero {
1174- // result[i].push((j as u16, *value))
1175- // }
1176- // }
1177- // }
1178- // result
1179- // }
1180-
11811151/// Calculates the exponential moving average (EMA) for a sparse matrix using dynamic alpha values.
11821152#[ allow( dead_code) ]
11831153pub fn mat_ema_alpha_vec_sparse (
@@ -1192,36 +1162,39 @@ pub fn mat_ema_alpha_vec_sparse(
11921162 let mut result: Vec < Vec < ( u16 , I32F32 ) > > = vec ! [ vec![ ] ; n] ;
11931163
11941164 // Iterate over each row of the matrices.
1195- for i in 0 ..new . len ( ) {
1165+ for ( i , ( new_row , old_row ) ) in new . iter ( ) . zip ( old ) . enumerate ( ) {
11961166 // Initialize a row of zeros for the result matrix.
11971167 let mut row: Vec < I32F32 > = vec ! [ zero; n] ;
11981168
11991169 // Process the new matrix values.
1200- for ( j, value) in new [ i ] . iter ( ) {
1170+ for ( j, value) in new_row . iter ( ) {
12011171 // Retrieve the alpha value for the current column.
1202- let alpha_val: I32F32 = alpha[ * j as usize ] ;
1172+ let alpha_val: I32F32 = alpha. get ( * j as usize ) . copied ( ) . unwrap_or ( zero ) ;
12031173 // Compute the EMA component for the new value using saturating multiplication.
1204- row[ * j as usize ] = alpha_val. saturating_mul ( * value) ;
1174+ if let Some ( row_val) = row. get_mut ( * j as usize ) {
1175+ * row_val = alpha_val. saturating_mul ( * value) ;
1176+ }
12051177 log:: trace!(
12061178 "new[{}][{}] * alpha[{}] = {} * {} = {}" ,
12071179 i,
12081180 j,
12091181 j,
12101182 value,
12111183 alpha_val,
1212- row[ * j as usize ]
1184+ row. get ( * j as usize ) . unwrap_or ( & zero )
12131185 ) ;
12141186 }
12151187
12161188 // Process the old matrix values.
1217- for ( j, value) in old [ i ] . iter ( ) {
1189+ for ( j, value) in old_row . iter ( ) {
12181190 // Retrieve the alpha value for the current column.
1219- let alpha_val: I32F32 = alpha[ * j as usize ] ;
1191+ let alpha_val: I32F32 = alpha. get ( * j as usize ) . copied ( ) . unwrap_or ( zero ) ;
12201192 // Calculate the complement of the alpha value using saturating subtraction.
12211193 let one_minus_alpha: I32F32 = I32F32 :: from_num ( 1.0 ) . saturating_sub ( alpha_val) ;
12221194 // Compute the EMA component for the old value and add it to the row using saturating operations.
1223- row[ * j as usize ] =
1224- row[ * j as usize ] . saturating_add ( one_minus_alpha. saturating_mul ( * value) ) ;
1195+ if let Some ( row_val) = row. get_mut ( * j as usize ) {
1196+ * row_val = row_val. saturating_add ( one_minus_alpha. saturating_mul ( * value) ) ;
1197+ }
12251198 log:: trace!(
12261199 "old[{}][{}] * (1 - alpha[{}]) = {} * {} = {}" ,
12271200 i,
@@ -1236,15 +1209,18 @@ pub fn mat_ema_alpha_vec_sparse(
12361209 // Collect the non-zero values into the result matrix.
12371210 for ( j, value) in row. iter ( ) . enumerate ( ) {
12381211 if * value > zero {
1239- result[ i] . push ( ( j as u16 , * value) ) ;
1240- log:: trace!( "result[{}][{}] = {}" , i, j, value) ;
1212+ if let Some ( result_row) = result. get_mut ( i) {
1213+ result_row. push ( ( j as u16 , * value) ) ;
1214+ log:: trace!( "result[{}][{}] = {}" , i, j, value) ;
1215+ }
12411216 }
12421217 }
12431218 }
12441219
12451220 // Return the computed EMA sparse matrix.
12461221 result
12471222}
1223+
12481224/// Return matrix exponential moving average: `alpha_j * a_ij + one_minus_alpha_j * b_ij`.
12491225/// `alpha_` is the EMA coefficient passed as a vector per column.
12501226#[ allow( dead_code) ]
@@ -1254,13 +1230,13 @@ pub fn mat_ema_alpha_vec(
12541230 alpha : & [ I32F32 ] ,
12551231) -> Vec < Vec < I32F32 > > {
12561232 // Check if the new matrix is empty or its first row is empty.
1257- if new. is_empty ( ) || new[ 0 ] . is_empty ( ) {
1233+ if new. is_empty ( ) || new. first ( ) . map_or ( true , |row| row . is_empty ( ) ) {
12581234 return vec ! [ vec![ ] ; 1 ] ;
12591235 }
12601236
12611237 // Ensure the dimensions of the new and old matrices match.
12621238 assert ! ( new. len( ) == old. len( ) ) ;
1263- assert ! ( new[ 0 ] . len( ) == alpha. len( ) ) ;
1239+ assert ! ( new. first ( ) . map_or ( 0 , |row| row . len( ) ) == alpha. len( ) ) ;
12641240
12651241 // Initialize the result matrix with zeros, having the same dimensions as the new matrix.
12661242 let mut result: Vec < Vec < I32F32 > > =
@@ -1277,9 +1253,15 @@ pub fn mat_ema_alpha_vec(
12771253 let one_minus_alpha = I32F32 :: from_num ( 1.0 ) . saturating_sub ( alpha_val) ;
12781254
12791255 // Compute the EMA for the current element using saturating operations.
1280- result[ i] [ j] = alpha_val
1281- . saturating_mul ( new_row[ j] )
1282- . saturating_add ( one_minus_alpha. saturating_mul ( old_row[ j] ) ) ;
1256+ if let ( Some ( new_val) , Some ( old_val) , Some ( result_val) ) = (
1257+ new_row. get ( j) ,
1258+ old_row. get ( j) ,
1259+ result. get_mut ( i) . and_then ( |row| row. get_mut ( j) ) ,
1260+ ) {
1261+ * result_val = alpha_val
1262+ . saturating_mul ( * new_val)
1263+ . saturating_add ( one_minus_alpha. saturating_mul ( * old_val) ) ;
1264+ }
12831265 }
12841266 }
12851267
0 commit comments