From 8eb53b5bfd6c8292b8cf299d062f8d8304706ba8 Mon Sep 17 00:00:00 2001 From: Augusto Date: Sun, 27 Dec 2020 21:54:52 +0000 Subject: [PATCH] Add test and subject of exercise `matrix_mult` --- rust/tests/matrix_mult_test/Cargo.lock | 19 +++++++ rust/tests/matrix_mult_test/Cargo.toml | 10 ++++ rust/tests/matrix_mult_test/src/main.rs | 43 ++++++++++++++++ subjects/matrix_mult/README.md | 67 +++++++++++++++++++++++++ 4 files changed, 139 insertions(+) create mode 100644 rust/tests/matrix_mult_test/Cargo.lock create mode 100644 rust/tests/matrix_mult_test/Cargo.toml create mode 100644 rust/tests/matrix_mult_test/src/main.rs create mode 100644 subjects/matrix_mult/README.md diff --git a/rust/tests/matrix_mult_test/Cargo.lock b/rust/tests/matrix_mult_test/Cargo.lock new file mode 100644 index 00000000..7fb91365 --- /dev/null +++ b/rust/tests/matrix_mult_test/Cargo.lock @@ -0,0 +1,19 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "lalgebra_scalar" +version = "0.1.0" + +[[package]] +name = "matrix" +version = "0.1.0" +dependencies = [ + "lalgebra_scalar", +] + +[[package]] +name = "matrix_test" +version = "0.1.0" +dependencies = [ + "matrix", +] diff --git a/rust/tests/matrix_mult_test/Cargo.toml b/rust/tests/matrix_mult_test/Cargo.toml new file mode 100644 index 00000000..9aba827c --- /dev/null +++ b/rust/tests/matrix_mult_test/Cargo.toml @@ -0,0 +1,10 @@ +[package] +name = "matrix_test" +version = "0.1.0" +authors = ["Augusto "] +edition = "2018" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +matrix = { path = "../../../../rust-piscine-solutions/matrix"} diff --git a/rust/tests/matrix_mult_test/src/main.rs b/rust/tests/matrix_mult_test/src/main.rs new file mode 100644 index 00000000..a643a2e5 --- /dev/null +++ b/rust/tests/matrix_mult_test/src/main.rs @@ -0,0 +1,43 @@ +// Now define the matrix multiplication by implementing the +// std::ops::Mul for the type matrix + +use matrix::Matrix; + +fn main() { + let matrix: Matrix = Matrix(vec![vec![3, 6], vec![8, 0]]); + println!("{:?}", matrix.col(0)); + println!("{:?}", matrix.row(1)); + + let matrix_1: Matrix = Matrix(vec![vec![0, 1], vec![0, 0]]); + let matrix_2: Matrix = Matrix(vec![vec![0, 0], vec![1, 0]]); + let mult = matrix_1.clone() * matrix_2.clone(); + println!("{:?}", mult); + println!("{:?}", matrix_1.number_of_cols()); + println!("{:?}", matrix_2.number_of_rows()); +} + +#[test] +fn get_row() { + let matrix: Matrix = Matrix(vec![vec![3, 6], vec![8, 0]]); + assert_eq!(vec![3u32, 6], matrix.row(0)); + assert_eq!(vec![8u32, 0], matrix.row(1)); +} + +#[test] +fn get_col() { + let matrix: Matrix = Matrix(vec![vec![3, 6], vec![8, 0]]); + assert_eq!(matrix.col(0), vec![3u32, 8]); + assert_eq!(vec![6u32, 0], matrix.col(1)); +} + +#[test] +fn matrix_multiplication() { + let matrix_1: Matrix = Matrix(vec![vec![0, 1], vec![0, 0]]); + let matrix_2: Matrix = Matrix(vec![vec![0, 0], vec![1, 0]]); + let expected: Matrix = Matrix(vec![vec![1, 0], vec![0, 0]]); + assert_eq!(matrix_1 * matrix_2, Some(expected)); + + let matrix_1: Matrix = Matrix(vec![vec![0, 1], vec![0, 0]]); + let matrix_2: Matrix = Matrix(vec![vec![0, 0, 0], vec![1, 0, 0], vec![1, 1, 1]]); + assert_eq!(matrix_1 * matrix_2, None); +} diff --git a/subjects/matrix_mult/README.md b/subjects/matrix_mult/README.md new file mode 100644 index 00000000..a566d4f0 --- /dev/null +++ b/subjects/matrix_mult/README.md @@ -0,0 +1,67 @@ +## matrix_mult + +### Instructions + +Implement the methods: + +- `number_of_cols` which returns the number of columns of the matrix. + +- `number_of_rows` which returns the number of rows of the matrix. + +- `row(n)` which returns the `n`th row in the matrix. + +- `col(n)` which returns the `n`th + +Define the matrix multiplication by implementing the std::ops::Mul for the type matrix + +### Expected Functions + +```rust +impl Matrix { + pub fn number_of_cols(&self) -> usize { + } + + pub fn rows(&self) -> usize { + } + + pub fn number_of_row(&self, n: usize) -> Vec { + } + + pub fn col(&self, n: usize) -> Vec { + } +} + +impl Mul for Matrix { +} +``` + +### Usage + +Here is a program to test your function. + +```rust +fn main() { + let matrix: Matrix = Matrix(vec![vec![3, 6], vec![8, 0]]); + println!("{:?}", matrix.col(0)); + println!("{:?}", matrix.row(1)); + + let matrix_1: Matrix = Matrix(vec![vec![0, 1], vec![0, 0]]); + let matrix_2: Matrix = Matrix(vec![vec![0, 0], vec![1, 0]]); + let mult = matrix_1.clone() * matrix_2.clone(); + println!("{:?}", mult); + println!("{:?}", matrix_1.number_of_cols()); + println!("{:?}", matrix_2.number_of_rows()); +} +``` + +And its output + +```console +student@ubuntu:~/[[ROOT]]/test$ cargo run +[3, 8] +[8, 0] +Some(Matrix([[1, 0], [0, 0]])) +2 +2 +student@ubuntu:~/[[ROOT]]/test$ +```