summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/poly.rs14
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]);