diff options
author | Juan Manuel Tomás <jtomas1815@gmail.com> | 2021-01-14 07:03:01 -0300 |
---|---|---|
committer | Juan Manuel Tomás <jtomas1815@gmail.com> | 2021-01-14 07:03:24 -0300 |
commit | 4f2afb1ca7063206411475bce89669238e41cb2b (patch) | |
tree | 31e205508ae13555b7209b22fe4e284fdd9914dc | |
parent | ed7f00f4d3833877c1e3336ba2bb85d7e3733112 (diff) | |
download | bezier-4f2afb1ca7063206411475bce89669238e41cb2b.tar.gz bezier-4f2afb1ca7063206411475bce89669238e41cb2b.zip |
Implement derivative of polynomials
-rw-r--r-- | src/poly.rs | 14 |
1 files changed, 14 insertions, 0 deletions
diff --git a/src/poly.rs b/src/poly.rs index 90b7b78..cb84d64 100644 --- a/src/poly.rs +++ b/src/poly.rs @@ -117,11 +117,25 @@ 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]); let b = Poly::new(vec![-6.0, -5.0, 1.0]); |