forked from root/public
Augusto
4 years ago
4 changed files with 139 additions and 0 deletions
@ -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", |
||||||
|
] |
@ -0,0 +1,10 @@ |
|||||||
|
[package] |
||||||
|
name = "matrix_test" |
||||||
|
version = "0.1.0" |
||||||
|
authors = ["Augusto <aug.ornelas@gmail.com>"] |
||||||
|
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"} |
@ -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<u32> = Matrix(vec![vec![3, 6], vec![8, 0]]); |
||||||
|
println!("{:?}", matrix.col(0)); |
||||||
|
println!("{:?}", matrix.row(1)); |
||||||
|
|
||||||
|
let matrix_1: Matrix<u32> = Matrix(vec![vec![0, 1], vec![0, 0]]); |
||||||
|
let matrix_2: Matrix<u32> = 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<u32> = 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<u32> = 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<u32> = Matrix(vec![vec![0, 1], vec![0, 0]]); |
||||||
|
let matrix_2: Matrix<u32> = Matrix(vec![vec![0, 0], vec![1, 0]]); |
||||||
|
let expected: Matrix<u32> = Matrix(vec![vec![1, 0], vec![0, 0]]); |
||||||
|
assert_eq!(matrix_1 * matrix_2, Some(expected)); |
||||||
|
|
||||||
|
let matrix_1: Matrix<u32> = Matrix(vec![vec![0, 1], vec![0, 0]]); |
||||||
|
let matrix_2: Matrix<u32> = Matrix(vec![vec![0, 0, 0], vec![1, 0, 0], vec![1, 1, 1]]); |
||||||
|
assert_eq!(matrix_1 * matrix_2, None); |
||||||
|
} |
@ -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<T> { |
||||||
|
pub fn number_of_cols(&self) -> usize { |
||||||
|
} |
||||||
|
|
||||||
|
pub fn rows(&self) -> usize { |
||||||
|
} |
||||||
|
|
||||||
|
pub fn number_of_row(&self, n: usize) -> Vec<T> { |
||||||
|
} |
||||||
|
|
||||||
|
pub fn col(&self, n: usize) -> Vec<T> { |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
impl Mul for Matrix<T> { |
||||||
|
} |
||||||
|
``` |
||||||
|
|
||||||
|
### Usage |
||||||
|
|
||||||
|
Here is a program to test your function. |
||||||
|
|
||||||
|
```rust |
||||||
|
fn main() { |
||||||
|
let matrix: Matrix<u32> = Matrix(vec![vec![3, 6], vec![8, 0]]); |
||||||
|
println!("{:?}", matrix.col(0)); |
||||||
|
println!("{:?}", matrix.row(1)); |
||||||
|
|
||||||
|
let matrix_1: Matrix<u32> = Matrix(vec![vec![0, 1], vec![0, 0]]); |
||||||
|
let matrix_2: Matrix<u32> = 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$ |
||||||
|
``` |
Loading…
Reference in new issue