From 4f2afb1ca7063206411475bce89669238e41cb2b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juan=20Manuel=20Tom=C3=A1s?= Date: Thu, 14 Jan 2021 07:03:01 -0300 Subject: Implement derivative of polynomials --- src/poly.rs | 14 ++++++++++++++ 1 file changed, 14 insertions(+) (limited to 'src') diff --git a/src/poly.rs b/src/poly.rs index 90b7b78..cb84d64 100644 --- a/src/poly.rs +++ b/src/poly.rs @@ -117,10 +117,24 @@ pub fn gcd(a: &Poly, b: &Poly) -> Poly { } } +pub fn derivative(p: &Poly) -> Poly { + let mut v = vec![0.0; p.degree()]; + for i in 0..v.len() { + v[i] = p.data[i + 1] * (i + 1) as Number; + } + Poly::new(v) +} + #[cfg(test)] mod tests { use super::*; + #[test] + fn derivative_test() { + let p = Poly::new(vec![1.0, 2.0, 3.0, 4.0, 5.0]); + assert_eq!(derivative(&p), Poly::new(vec![2.0, 6.0, 12.0, 20.0])); + } + #[test] fn div_equal() { let a = Poly::new(vec![6.0, 7.0, 1.0]); -- cgit v1.2.3