mirror of
https://github.com/kmc7468/cs220.git
synced 2025-12-16 23:18:47 +00:00
Assignment 6 Done
This commit is contained in:
@@ -33,55 +33,55 @@ pub fn from_usize<T: Semiring>(value: usize) -> T {
|
||||
|
||||
impl Semiring for u64 {
|
||||
fn zero() -> Self {
|
||||
todo!()
|
||||
0
|
||||
}
|
||||
|
||||
fn one() -> Self {
|
||||
todo!()
|
||||
1
|
||||
}
|
||||
|
||||
fn add(&self, rhs: &Self) -> Self {
|
||||
todo!()
|
||||
self + rhs
|
||||
}
|
||||
|
||||
fn mul(&self, rhs: &Self) -> Self {
|
||||
todo!()
|
||||
self * rhs
|
||||
}
|
||||
}
|
||||
|
||||
impl Semiring for i64 {
|
||||
fn zero() -> Self {
|
||||
todo!()
|
||||
0
|
||||
}
|
||||
|
||||
fn one() -> Self {
|
||||
todo!()
|
||||
1
|
||||
}
|
||||
|
||||
fn add(&self, rhs: &Self) -> Self {
|
||||
todo!()
|
||||
self + rhs
|
||||
}
|
||||
|
||||
fn mul(&self, rhs: &Self) -> Self {
|
||||
todo!()
|
||||
self * rhs
|
||||
}
|
||||
}
|
||||
|
||||
impl Semiring for f64 {
|
||||
fn zero() -> Self {
|
||||
todo!()
|
||||
0.0
|
||||
}
|
||||
|
||||
fn one() -> Self {
|
||||
todo!()
|
||||
1.0
|
||||
}
|
||||
|
||||
fn add(&self, rhs: &Self) -> Self {
|
||||
todo!()
|
||||
self + rhs
|
||||
}
|
||||
|
||||
fn mul(&self, rhs: &Self) -> Self {
|
||||
todo!()
|
||||
self * rhs
|
||||
}
|
||||
}
|
||||
|
||||
@@ -105,42 +105,91 @@ pub struct Polynomial<C: Semiring> {
|
||||
|
||||
impl<C: Semiring> Semiring for Polynomial<C> {
|
||||
fn zero() -> Self {
|
||||
todo!()
|
||||
Self {
|
||||
coefficients: HashMap::new(),
|
||||
}
|
||||
}
|
||||
|
||||
fn one() -> Self {
|
||||
todo!()
|
||||
Self {
|
||||
coefficients: HashMap::from([(0, C::one())]),
|
||||
}
|
||||
}
|
||||
|
||||
fn add(&self, rhs: &Self) -> Self {
|
||||
todo!()
|
||||
let mut coefficients = self.coefficients.clone();
|
||||
|
||||
for (deg, coef) in &rhs.coefficients {
|
||||
_ = coefficients
|
||||
.entry(*deg)
|
||||
.and_modify(|value| *value = value.add(coef))
|
||||
.or_insert(coef.clone());
|
||||
}
|
||||
|
||||
coefficients.retain(|_, coef| *coef != C::zero());
|
||||
|
||||
Self { coefficients }
|
||||
}
|
||||
|
||||
fn mul(&self, rhs: &Self) -> Self {
|
||||
todo!()
|
||||
let mut coefficients: HashMap<u64, C> = HashMap::new();
|
||||
|
||||
for (ldeg, lcoef) in &self.coefficients {
|
||||
for (rdeg, rcoef) in &rhs.coefficients {
|
||||
let coef = lcoef.mul(rcoef);
|
||||
_ = coefficients
|
||||
.entry(ldeg + rdeg)
|
||||
.and_modify(|value| *value = value.add(&coef))
|
||||
.or_insert(coef);
|
||||
}
|
||||
}
|
||||
|
||||
coefficients.retain(|_, coef| *coef != C::zero());
|
||||
|
||||
Self { coefficients }
|
||||
}
|
||||
}
|
||||
|
||||
impl<C: Semiring> Polynomial<C> {
|
||||
/// Constructs polynomial `x`.
|
||||
pub fn x() -> Self {
|
||||
todo!()
|
||||
Self {
|
||||
coefficients: HashMap::from([(1, C::one())]),
|
||||
}
|
||||
}
|
||||
|
||||
/// Evaluates the polynomial with the given value.
|
||||
pub fn eval(&self, value: C) -> C {
|
||||
todo!()
|
||||
let mut result = C::zero();
|
||||
|
||||
for (deg, coef) in &self.coefficients {
|
||||
let mut xn = C::one();
|
||||
let mut n = 0;
|
||||
|
||||
while n < *deg {
|
||||
xn = xn.mul(&value);
|
||||
n += 1;
|
||||
}
|
||||
|
||||
result = result.add(&coef.mul(&xn));
|
||||
}
|
||||
|
||||
result
|
||||
}
|
||||
|
||||
/// Constructs polynomial `ax^n`.
|
||||
pub fn term(a: C, n: u64) -> Self {
|
||||
todo!()
|
||||
Self {
|
||||
coefficients: HashMap::from([(n, a)]),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<C: Semiring> From<C> for Polynomial<C> {
|
||||
fn from(value: C) -> Self {
|
||||
todo!()
|
||||
Self {
|
||||
coefficients: HashMap::from([(0, value)]),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -164,6 +213,30 @@ impl<C: Semiring> std::str::FromStr for Polynomial<C> {
|
||||
type Err = (); // Ignore this for now...
|
||||
|
||||
fn from_str(s: &str) -> Result<Self, Self::Err> {
|
||||
todo!()
|
||||
let mut coefficients: HashMap<u64, C> = HashMap::new();
|
||||
|
||||
for term in s.split(" + ") {
|
||||
let has_x: bool = term.contains('x');
|
||||
let has_power = term.contains('^');
|
||||
|
||||
let deg: u64 = if has_power {
|
||||
term.split("^").nth(1).unwrap().parse().unwrap()
|
||||
} else if has_x {
|
||||
1
|
||||
} else {
|
||||
0
|
||||
};
|
||||
let coef: usize = if has_x && term.find("x").unwrap() == 0 {
|
||||
1
|
||||
} else if has_x {
|
||||
term.split("x").nth(0).unwrap().parse().unwrap()
|
||||
} else {
|
||||
term.parse().unwrap()
|
||||
};
|
||||
|
||||
let _unused = coefficients.insert(deg, from_usize(coef));
|
||||
}
|
||||
|
||||
Ok(Self { coefficients })
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user