Skip to content

Commit

Permalink
fix: roots_for_poly_repr
Browse files Browse the repository at this point in the history
  • Loading branch information
supragya committed Mar 6, 2024
1 parent 23d662a commit b42326a
Showing 1 changed file with 27 additions and 4 deletions.
31 changes: 27 additions & 4 deletions polynomial/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::ops::Mul;
use std::{fmt::Debug, ops::{Mul, Sub}};

use num_traits::Zero;
use num_traits::{One, Zero};

pub enum PolynomialRepr<T> {
Points(Vec<(T, T)>),
Expand All @@ -27,9 +27,18 @@ where
repr: PolynomialRepr::Coeff(coeffs.to_vec()),
}
}

/// Generate a polynomial from its roots `r_0`, `r_1`, ... `r_n`
/// when polynomial looks as follows:
/// `(x - r_0)(x - r_1)(x - r_2)...(x - r_n)`
pub fn new_from_roots(roots: &[T]) -> Self {
Self {
repr: PolynomialRepr::Roots(roots.to_vec()),
}
}
}

impl<T: Zero + Mul<Output = T> + Clone> Polynomial<T> {
impl<T: Zero + One + Mul<Output = T> + Sub<Output = T> + Clone + Debug> Polynomial<T> {
/// Evaluates the polynomial at a point.
///
/// # Examples
Expand All @@ -45,7 +54,13 @@ impl<T: Zero + Mul<Output = T> + Clone> Polynomial<T> {
pub fn eval(&self, x: T) -> T {
match &self.repr {
PolynomialRepr::Points(_points) => unimplemented!(),
PolynomialRepr::Roots(_roots) => unimplemented!(),
PolynomialRepr::Roots(roots) => {
let mut result: T = One::one();
for n in roots.iter() {
result = (x.clone() - n.clone()) * result;
}
result
},
PolynomialRepr::Coeff(coeffs) => {
let mut result: T = Zero::zero();
for n in coeffs.iter().rev() {
Expand All @@ -68,4 +83,12 @@ mod tests {
assert_eq!(polynomial.eval(1), 6);
assert_eq!(polynomial.eval(10), 123);
}

#[test]
fn eval_for_roots_polynomial_repr() {
let polynomial = Polynomial::<i32>::new_from_roots(&[1, 2, 3]);
assert_eq!(polynomial.eval(1), 0);
assert_eq!(polynomial.eval(2), 0);
assert_eq!(polynomial.eval(3), 0);
}
}

0 comments on commit b42326a

Please sign in to comment.