pub fn div(self, other: Poly) -> Result<(Self, Self), &'static str> {
        let d_self = self.deg();
        let d_other = other.deg();
        if other.is_zero() {
            return Err("Cannot divide by a 0 polynomial");
        } else if d_self < d_other {
            return Ok((poly![], Poly::new(self.vec)));
        }
        let lc_other = other.lc();
        let mut rem_poly = self;
        let mut d_rem = d_self;
        let mut quo = poly![];
        loop {
            let lc_rem = rem_poly.lc();
            // Currently, this only supports integer division.
            // TODO: Expand this to handle fractions.
            if lc_rem % lc_other != 0 {
                // The current iteration won't divide evenly, so we're done.
                // TODO: above
                break;
            }
            let cur_term_coeff = lc_rem / lc_other;
            quo = quo.add_term(cur_term_coeff, d_rem - d_other);

            rem_poly = rem_poly.sub(
                // Subtract (current term coeff * rhs) from the rest of polynomial we need to
                // divide.
                other.clone().mul_term(cur_term_coeff, d_rem - d_other),
            );

            let d_rem_old = d_rem;
            d_rem = rem_poly.deg();
            if d_rem < d_other {
                break;
            } else if d_rem >= d_rem_old {
                return Err("Unexpected state: remainder degreee not lower after division");
            }
        }
        Ok((quo, rem_poly))
    }
/// Divides one polynomial by another, returning a tuple of (quotient, remainder) or an error
/// if division failed.
///
/// # Examples:
///
/// ```ignore
/// // (x^2 - 4) / (x + 2) -> ((x - 2), 0)
/// assert_eq!(poly![-4, 0, 1].div(poly![2, 1]), Ok((poly![-2, 1], poly![])));
///
/// // (x^2 - 2x) / (x + 1) -> ((x - 3), 3)
/// assert_eq!(poly![-2, 0, 1].div(poly![1, 1]), Ok((poly![-3, 1], poly![3])));
/// ```
Divides one polynomial by another, returning a tuple of (quotient, remainder) or an error
if division failed.
Divides one polynomial by another returning a tuple of ( quotient remainder ) or an error if division failed .
pub fn div(self, other: Poly) -> Result<(Self, Self), &'static str> {
        let d_self = self.deg();
        let d_other = other.deg();
        if other.is_zero() {
            return Err("Cannot divide by a 0 polynomial");
        } else if d_self < d_other {
            return Ok((poly![], Poly::new(self.vec)));
        }
        let lc_other = other.lc();
        let mut rem_poly = self;
        let mut d_rem = d_self;
        let mut quo = poly![];
        loop {
            let lc_rem = rem_poly.lc();
            // Currently, this only supports integer division.
            // TODO: Expand this to handle fractions.
            if lc_rem % lc_other != 0 {
                // The current iteration won't divide evenly, so we're done.
                // TODO: above
                break;
            }
            let cur_term_coeff = lc_rem / lc_other;
            quo = quo.add_term(cur_term_coeff, d_rem - d_other);
            rem_poly = rem_poly.sub(
                // Subtract (current term coeff * rhs) from the rest of polynomial we need to
                // divide.
                other.clone().mul_term(cur_term_coeff, d_rem - d_other),
            );
            let d_rem_old = d_rem;
            d_rem = rem_poly.deg();
            if d_rem < d_other {
                break;
            } else if d_rem >= d_rem_old {
                return Err("Unexpected state: remainder degreee not lower after division");
            }
        }
        Ok((quo, rem_poly))
    }
pub fn div ( self , other : Poly ) -> Result < ( Self , Self ) , & ' static str > { let d_self = self . deg ( ) ; let d_other = other . deg ( ) ; if other . is_zero ( ) { return Err ( " " ) ; } else if d_self < d_other { return Ok ( ( poly ! [ ] , Poly :: new ( self . vec ) ) ) ; } let lc_other = other . lc ( ) ; let mut rem_poly = self ; let mut d_rem = d_self ; let mut quo = poly ! [ ] ; loop { let lc_rem = rem_poly . lc ( ) ; // Currently, this only supports integer division. // TODO: Expand this to handle fractions. if lc_rem % lc_other != 0 { // The current iteration won't divide evenly, so we're done. // TODO: above break ; } let cur_term_coeff = lc_rem / lc_other ; quo = quo . add_term ( cur_term_coeff , d_rem - d_other ) ; rem_poly = rem_poly . sub ( // Subtract (current term coeff * rhs) from the rest of polynomial we need to // divide. other . clone ( ) . mul_term ( cur_term_coeff , d_rem - d_other ) , ) ; let d_rem_old = d_rem ; d_rem = rem_poly . deg ( ) ; if d_rem < d_other { break ; } else if d_rem >= d_rem_old { return Err ( " " ) ; } } Ok ( ( quo , rem_poly ) ) }
Divides one polynomial by another, returning a tuple of (quotient, remainder) or an error
if division failed.
Divides one polynomial by another returning a tuple of ( quotient remainder ) or an error if division failed .
self
other
Poly
self
other
Poly
examples:
None