This commit is contained in:
jungin.rhee
2023-08-21 11:52:05 +00:00
parent aa9f04b7d4
commit bba6cd9979
25 changed files with 32 additions and 307 deletions

View File

@@ -8,9 +8,7 @@
use std::{collections::HashMap, fmt::Debug};
pub mod semiring;
pub mod square_matrix;
pub mod symbolic_differentiation;
mod semiring_grade;
mod square_matrix_grade;
mod symbolic_differentiation_grade;

View File

@@ -1,88 +0,0 @@
//! Square matrix
/// Square matrix
pub trait SquareMatrix {
/// The type of the submatrix of this square matrix.
/// For example, the submatrix of a 3 x 3 matrix is a 2 x 2 matrix.
/// https://en.wikipedia.org/wiki/Matrix_(mathematics)#Submatrix
type Submatrix;
/// Returns the submatrix obtained by removing the `row`th row and `col`th column
/// from the original matrix.
/// https://en.wikipedia.org/wiki/Matrix_(mathematics)#Submatrix
fn sub_matrix(&self, row: usize, col: usize) -> Self::Submatrix;
/// Returns the determinant of the matrix.
fn det(&self) -> i64;
/// Returns the determinant of ab, where a is self, b is given, and ab is the matrix product of them.
/// Note that the size of a and b are the same.
/// Hint: Use the fact that det(ab) = det(a) * det(b)
/// https://en.wikipedia.org/wiki/Determinant#Multiplicativity_and_matrix_groups
fn det_ab(&self, b: &Self) -> i64 {
todo!()
}
}
/// 2 x 2 matrix
#[derive(Debug, PartialEq)]
pub struct Mat2 {
/// inner is a 2 dimensional array (size: 2 x 2)
pub inner: [[i64; 2]; 2],
}
impl SquareMatrix for Mat2 {
type Submatrix = i64;
fn sub_matrix(&self, row: usize, col: usize) -> Self::Submatrix {
// Hint: The submatrix of a 2 x 2 matrix is simply a single number.
todo!()
}
// Hint: https://en.wikipedia.org/wiki/Determinant
fn det(&self) -> i64 {
todo!()
}
}
/// 3 x 3 matrix
#[derive(Debug, PartialEq)]
pub struct Mat3 {
/// inner is a 2 dimensional array (size: 3 x 3)
pub inner: [[i64; 3]; 3],
}
impl SquareMatrix for Mat3 {
type Submatrix = Mat2;
fn sub_matrix(&self, row: usize, col: usize) -> Self::Submatrix {
todo!()
}
// Hint: Use the determinant of the sub-matrices.
// https://semath.info/src/determinant-three-by-three.html
fn det(&self) -> i64 {
todo!()
}
}
/// 4 x 4 matrix
#[derive(Debug, PartialEq)]
pub struct Mat4 {
/// inner is a 2 dimensional array (size: 4 x 4)
pub inner: [[i64; 4]; 4],
}
impl SquareMatrix for Mat4 {
type Submatrix = Mat3;
fn sub_matrix(&self, row: usize, col: usize) -> Self::Submatrix {
todo!()
}
// Hint: Use the determinant of the sub-matrices.
// https://semath.info/src/determinant-four-by-four.html
fn det(&self) -> i64 {
todo!()
}
}

View File

@@ -1,72 +0,0 @@
#[cfg(test)]
mod test {
use crate::assignments::assignment06::square_matrix::*;
use ntest::assert_about_eq;
#[test]
fn test_mat2() {
let mat = Mat2 {
inner: [[1, 2], [3, 4]],
};
assert_eq!(mat.sub_matrix(1, 1), 4);
assert_eq!(mat.sub_matrix(1, 2), 3);
assert_eq!(mat.sub_matrix(2, 1), 2);
assert_eq!(mat.sub_matrix(2, 2), 1);
assert_eq!(mat.det(), -2);
let mat2 = Mat2 {
inner: [[2, 3], [5, 7]],
};
assert_eq!(mat.det_ab(&mat2), mat.det() * mat2.det());
}
#[test]
fn test_mat3() {
let mat = Mat3 {
inner: [[1, 2, 3], [5, 5, 6], [7, 8, 10]],
};
assert_eq!(
mat.sub_matrix(1, 2),
Mat2 {
inner: [[5, 6], [7, 10]]
}
);
assert_eq!(mat.det(), 1);
let mat2 = Mat3 {
inner: [[2, 3, 5], [7, 10, 11], [12, 14, 20]],
};
assert_eq!(mat2.det(), -42);
assert_eq!(mat.det_ab(&mat2), mat.det() * mat2.det());
}
#[test]
fn test_mat4() {
let mat = Mat4 {
inner: [
[1, 11, 3, 4],
[5, 6, 7, 9],
[25, 10, 11, 20],
[36, 14, 15, 30],
],
};
assert_eq!(
mat.sub_matrix(2, 3),
Mat3 {
inner: [[1, 11, 4], [25, 10, 20], [36, 14, 30]]
}
);
assert_eq!(mat.det(), 2089);
let mat2 = Mat4 {
inner: [
[2, 3, 5, 5],
[7, 10, 11, 20],
[12, 14, 20, 30],
[1, 2, 5, 10],
],
};
assert_eq!(mat2.det(), -340);
assert_eq!(mat.det_ab(&mat2), mat.det() * mat2.det());
}
}

View File

@@ -79,6 +79,7 @@ pub trait Differentiable: Clone {
fn diff(&self) -> Self;
}
/// HINT: Consult <https://en.wikipedia.org/wiki/Differentiation_rules#Constant_term_rule>
impl Differentiable for Rational {
fn diff(&self) -> Self {
todo!()
@@ -115,6 +116,7 @@ impl SingletonPolynomial {
}
impl Differentiable for SingletonPolynomial {
/// HINT: Consult <https://en.wikipedia.org/wiki/Power_rule>
fn diff(&self) -> Self {
todo!()
}
@@ -138,6 +140,7 @@ impl Default for Exp {
}
impl Differentiable for Exp {
/// HINT: Consult <https://en.wikipedia.org/wiki/Differentiation_rules#Derivatives_of_exponential_and_logarithmic_functions>
fn diff(&self) -> Self {
todo!()
}
@@ -173,6 +176,7 @@ impl Trignometric {
}
impl Differentiable for Trignometric {
/// HINT: Consult <https://en.wikipedia.org/wiki/Differentiation_rules#Derivatives_of_trigonometric_functions>
fn diff(&self) -> Self {
todo!()
}
@@ -221,6 +225,7 @@ impl<F: Differentiable> Differentiable for Box<F> {
}
impl<F: Differentiable> Differentiable for ComplexFuncs<F> {
/// HINT: Consult <https://en.wikipedia.org/wiki/Differentiation_rules#Elementary_rules_of_differentiation>
fn diff(&self) -> Self {
todo!()
}