Skip to content

Commit

Permalink
feat: derivative
Browse files Browse the repository at this point in the history
  • Loading branch information
0xWOLAND committed Oct 20, 2023
1 parent e6520a8 commit 41f53b5
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions src/polynomial.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,19 @@ pub struct Polynomial {
pub coef: Vec<i64>,
}

impl Polynomial {
pub fn diff(mut self) -> Self {
let N = self.coef.len();
for n in (1..N).rev() {
self.coef[n] = self.coef[n - 1] * ((N - n) as i64);
}
self.coef[0] = 0;
self.coef = self.coef[1..].to_vec();

self
}
}

impl Add<Polynomial> for Polynomial {
type Output = Polynomial;

Expand Down Expand Up @@ -139,4 +152,13 @@ mod tests {
};
println!("{:?}", a * b);
}

#[test]
fn diff() {
let a = Polynomial {
coef: vec![3, 2, 1],
};
let da = a.diff();
println!("{:?}", da);
}
}

0 comments on commit 41f53b5

Please sign in to comment.