This commit is contained in:
Janggun Lee
2024-07-30 15:26:33 +09:00
parent d34f586e4d
commit af889722bc
64 changed files with 452 additions and 384 deletions

View File

@@ -2,8 +2,8 @@
//!
//! The primary goal of this assignment is to understand generics, traits, and lifetimes.
//!
//! You should fill out the `todo!()` placeholders in such a way that `/scripts/grade.sh 6` works fine.
//! See `assignment06/*_grade.rs` and `/scripts/grade.sh 6` for the test script.
//! You should fill out the `todo!()` placeholders in such a way that `/scripts/grade.sh 6` works
//! fine. See `assignment06/*_grade.rs` and `/scripts/grade.sh 6` for the test script.
//!
//! To submit, run
//! ```bash

View File

@@ -1,6 +1,9 @@
//! Semiring
use std::{collections::HashMap, fmt::Debug};
use std::collections::HashMap;
use std::fmt::Debug;
use itertools::Itertools;
/// Semiring.
///
@@ -147,10 +150,8 @@ impl<C: Semiring> From<C> for Polynomial<C> {
///
/// Assumptions:
/// - Each term is separated by ` + `.
/// - Each term is one of the following form:
/// `a`, `x`, `ax`, `x^n`, and `ax^n`,
/// where `a` is a `usize` number and `n` is a `u64` number.
/// This `a` should then be converted to a `C` type.
/// - Each term is one of the following form: `a`, `x`, `ax`, `x^n`, and `ax^n`, where `a` is a
/// `usize` number and `n` is a `u64` number. This `a` should then be converted to a `C` type.
/// - In `a`, it is guaranteed that `a >= 1`.
/// - In `ax` and `ax^n`, it is guaranteed that `a >= 2`.
/// - In `x^n` and `ax^n`, it is guaranteed that `n >= 2`.

View File

@@ -1,8 +1,9 @@
#[cfg(test)]
mod test {
use crate::assignments::assignment06::semiring::*;
use ntest::assert_about_eq;
use crate::assignments::assignment06::semiring::*;
fn test_from_str(s: &str, f: impl Fn(i64) -> i64) {
let poly = s.parse::<Polynomial<i64>>().unwrap();
for i in 0..10 {

View File

@@ -5,10 +5,12 @@ use std::ops::*;
/// Rational number represented by two isize, numerator and denominator.
///
/// Each Rational number should be normalized so that `demoninator` is nonnegative and `numerator` and `demoninator` are coprime.
/// See `normalize` for examples. As a corner case, 0 is represented by Rational { numerator: 0, demoninator: 0 }.
/// Each Rational number should be normalized so that `demoninator` is nonnegative and `numerator`
/// and `demoninator` are coprime. See `normalize` for examples. As a corner case, 0 is represented
/// by `Rational { numerator: 0, demoninator: 0 }`.
///
/// For "natural use", Rational also overloads standard arithmetic operations, i.e, `+`, `-`, `*`, `/`.
/// For "natural use", it also overloads standard arithmetic operations, i.e, `+`, `-`, `*`, and
/// `/`.
///
/// See [here](https://doc.rust-lang.org/core/ops/index.html) for details.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
@@ -96,9 +98,9 @@ pub enum SingletonPolynomial {
Const(Rational),
/// Non-const polynomial.
Polynomial {
/// coefficent of polynomial. Must be non-zero.
/// Coefficent of polynomial. Must be non-zero.
coeff: Rational,
/// power of polynomial. Must be non-zero.
/// Power of polynomial. Must be non-zero.
power: Rational,
},
}
@@ -156,7 +158,7 @@ pub enum Trignometric {
/// Coefficent
coeff: Rational,
},
/// Sine function.
/// Cosine function.
Cosine {
/// Coefficent
coeff: Rational,

View File

@@ -1,8 +1,9 @@
#[cfg(test)]
mod test {
use crate::assignments::assignment06::symbolic_differentiation::*;
use ntest::assert_about_eq;
use crate::assignments::assignment06::symbolic_differentiation::*;
// Constant rationals to use
const TWO: Rational = Rational::new(2, 1);
const FOUR: Rational = Rational::new(4, 1);
@@ -172,7 +173,7 @@ mod test {
// Mult
//
// d/dx (2x^4 * cos(x) * exp(x)) =
// 8x^3 * cos(x) * exp(x) - 2x^4 * sin(x) * exp(x) + 2x^4 * cos(x) * exp(x)
// 8x^2 * cos(x) * exp(x) - 2x^4 * sin(x) * exp(x) + 2x^4 * cos(x) * exp(x)
let f1 = SingletonPolynomial::new_poly(TWO, FOUR);
let f2 = Trignometric::new_cosine(ONE);
let f3 = Exp::new();