mirror of https://github.com/01-edu/public.git
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
68 lines
1.3 KiB
68 lines
1.3 KiB
![]()
4 years ago
|
## 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$
|
||
|
```
|