sword_smith

coefficients-reversed

May 26th, 2021 (edited)
496
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Rust 0.80 KB | None | 0 0
  1. // calculate a reversed representation of the coefficients of
  2. // prod_{i=0}^{N}((x- q_i))    
  3.     pub fn prod_helper(input: &[i128]) -> Vec<i128> {
  4.         if let Some((q_j, elements)) = input.split_first() {
  5.             match elements {
  6.                 [] => vec![1, -q_j],
  7.                 _ => {
  8.                     // The recursive call calculates (x-q_j)*rec = x*rec - q_j*rec
  9.                     let mut rec = Polynomial::prod_helper(elements);
  10.                     rec.push(0);
  11.                     let mut i = rec.len() - 1;
  12.                     while i > 0 {
  13.                         rec[i] -= q_j * rec[i - 1];
  14.                         i -= 1;
  15.                     }
  16.                     rec
  17.                 }
  18.             }
  19.         } else {
  20.             panic!("Empty array received");
  21.         }
  22.     }
Add Comment
Please, Sign In to add comment