From a1106576781b31336b10bbf8cf4c0cd07a79efc3 Mon Sep 17 00:00:00 2001 From: Augusto Date: Sun, 27 Dec 2020 21:02:47 +0000 Subject: [PATCH] Add the subject and test of the exercise `matrix` --- rust/tests/matrix_test/Cargo.lock | 19 ++++++++++ rust/tests/matrix_test/Cargo.toml | 10 ++++++ rust/tests/matrix_test/src/main.rs | 55 ++++++++++++++++++++++++++++ subjects/matrix/README.md | 57 ++++++++++++++++++++++++++++++ 4 files changed, 141 insertions(+) create mode 100644 rust/tests/matrix_test/Cargo.lock create mode 100644 rust/tests/matrix_test/Cargo.toml create mode 100644 rust/tests/matrix_test/src/main.rs create mode 100644 subjects/matrix/README.md diff --git a/rust/tests/matrix_test/Cargo.lock b/rust/tests/matrix_test/Cargo.lock new file mode 100644 index 00000000..7fb91365 --- /dev/null +++ b/rust/tests/matrix_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_test/Cargo.toml b/rust/tests/matrix_test/Cargo.toml new file mode 100644 index 00000000..9aba827c --- /dev/null +++ b/rust/tests/matrix_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_test/src/main.rs b/rust/tests/matrix_test/src/main.rs new file mode 100644 index 00000000..a8a006af --- /dev/null +++ b/rust/tests/matrix_test/src/main.rs @@ -0,0 +1,55 @@ +// First exercise + +// # Instructions +// Define a data structure to represent a matrix of any size and +// implement the basic operations for this you will need to follow the +// next steps: + +// You can use a 2 dimensional Vec's +// We will consider a matrix as a rectangular arrangements of scalars +// You can use the definition of scalars done in the last exercise: +// `lalgebra_scalar` + +// Then define the associated function `identity` that returns the identity matrix +// of size n +// Ex: +// Matrix::identity(3) == [[1,0,0], [0,1,0], [0,0,1]] + +// And the associated function `zero` that returns a matrix of size +// `row x col` with all the positions filled by zeroes +// Ex: +// Matrix::zero(3, 3) == [[0,0,0],[0,0,0],[0,0,0]] + +// Resources: https://doc.rust-lang.org/book/ch19-03-advanced-traits.html + +use matrix::Matrix; + +#[allow(dead_code)] +fn main() { + let m: Matrix = Matrix(vec![vec![0, 0, 0, 0], vec![0, 0, 0, 0], vec![0, 0, 0, 0]]); + println!("{:?}", m); + println!("{:?}", Matrix::::identity(4)); + println!("{:?}", Matrix::::zero(3, 4)); +} + +#[test] +fn zero_property() { + let matrix: Matrix = Matrix::zero(3, 4); + let expected: Matrix = Matrix(vec![vec![0, 0, 0, 0], vec![0, 0, 0, 0], vec![0, 0, 0, 0]]); + assert_eq!(matrix, expected); + + let matrix: Matrix = Matrix::zero(2, 2); + let expected: Matrix = Matrix(vec![vec![0, 0], vec![0, 0]]); + assert_eq!(matrix, expected); +} + +#[test] +fn identy_matrix() { + let matrix: Matrix = Matrix::identity(2); + let expected: Matrix = Matrix(vec![vec![1, 0], vec![0, 1]]); + assert_eq!(matrix, expected); + + let matrix: Matrix = Matrix::identity(3); + let expected: Matrix = Matrix(vec![vec![1, 0, 0], vec![0, 1, 0], vec![0, 0, 1]]); + assert_eq!(matrix, expected); +} diff --git a/subjects/matrix/README.md b/subjects/matrix/README.md new file mode 100644 index 00000000..16a54465 --- /dev/null +++ b/subjects/matrix/README.md @@ -0,0 +1,57 @@ +## matrix + +### Instructions + +Define a data structure to represent a matrix of any size and implement the basic operations for this you will need to follow the next steps: + +You can use a 2 dimensional Vec's We will consider a matrix as a rectangular arrangements of scalars. + +You have to use the definition of scalars done in the last exercise: `lalgebra_scalar` + +Then define the associated function `identity` that returns the identity matrix of size n + +And the associated function `zero` that returns a matrix of size `row x col` with all the positions filled by zeroes + +### Notions + +[Traits]( https://doc.rust-lang.org/book/ch19-03-advanced-traits.html ) + +### Expected Functions and Structure + +```rust +pub struct Matrix(pub Vec>); + +impl Matrix { + pub fn new() -> Matrix { + } + + pub fn zero(row: usize, col: usize) -> Matrix { + } + + pub fn identity(n: usize) -> Matrix { + } +} +``` + +### Usage + +Here is a program to test your function. + +```rust +fn main() { + let m: Matrix = Matrix(vec![vec![0, 0, 0, 0], vec![0, 0, 0, 0], vec![0, 0, 0, 0]]); + println!("{:?}", m); + println!("{:?}", Matrix::::identity(4)); + println!("{:?}", Matrix::::zero(3, 4)); +} +``` + +And its output + +```console +student@ubuntu:~/[[ROOT]]/test$ cargo run +Matrix([[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]]) +Matrix([[1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 1, 0], [0, 0, 0, 1]]) +Matrix([[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]]) +student@ubuntu:~/[[ROOT]]/test$ +```