Skip to content

Commit 3d20090

Browse files
committed
Add solution for matrix multiplication
1 parent 354a12f commit 3d20090

File tree

4 files changed

+54
-14
lines changed

4 files changed

+54
-14
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ Contains solutions to homework assignments. Click [here](https://dhole.github.io
4242
<td>
4343
<ul>
4444
<li><a href="hw01/src/problem1.rs">Vector & Slice Manipulation</a> → :green_apple: </li>
45-
<li>Matrix Multiplication → :lemon:</li>
45+
<li>Matrix Multiplication → :green_apple:</li>
4646
<li>Sieve of Eratosthenes → :tomato: </li>
4747
<li>Towers of Hanoi → :tomato: </li>
4848
</ul>

hw01/src/problem2.rs

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,20 @@ pub type Matrix = Vec<Vec<f32>>;
33

44
/// Computes the product of the inputs `mat1` and `mat2`.
55
pub fn mat_mult(mat1: &Matrix, mat2: &Matrix) -> Matrix {
6-
// TODO
7-
unimplemented!();
8-
}
6+
assert_eq!(mat1[0].len(), mat2.len());
7+
8+
let rows = mat1.len();
9+
let cols = mat2[0].len();
10+
let mid = mat1[0].len();
11+
12+
let mut result_matrix = vec![vec![0.; cols]; rows];
13+
14+
for i in 0..rows {
15+
for j in 0..cols {
16+
for k in 0..mid {
17+
result_matrix[i][j] += mat1[i][k] * mat2[k][j]
18+
}
19+
}
20+
}
21+
result_matrix
22+
}

hw01/tests/tests_provided.rs

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
#![cfg(test)]
22

3-
4-
use hw01::problem1::{sum, dedup, filter};
3+
use hw01::problem1::{dedup, filter, sum};
54
use hw01::problem2::mat_mult;
65
use hw01::problem3::sieve;
76
use hw01::problem4::{hanoi, Peg};
@@ -14,16 +13,16 @@ use hw01::problem4::{hanoi, Peg};
1413

1514
#[test]
1615
fn test_sum_small() {
17-
let array = [1,2,3,4,5];
16+
let array = [1, 2, 3, 4, 5];
1817
assert_eq!(sum(&array), 15);
1918
}
2019

2120
// Part 2
2221

2322
#[test]
2423
fn test_dedup_small() {
25-
let vs = vec![1,2,2,3,4,1];
26-
assert_eq!(dedup(&vs), vec![1,2,3,4]);
24+
let vs = vec![1, 2, 2, 3, 4, 1];
25+
assert_eq!(dedup(&vs), vec![1, 2, 3, 4]);
2726
}
2827

2928
// Part 3
@@ -34,8 +33,8 @@ fn even_predicate(x: i32) -> bool {
3433

3534
#[test]
3635
fn test_filter_small() {
37-
let vs = vec![1,2,3,4,5];
38-
assert_eq!(filter(&vs, &even_predicate), vec![2,4]);
36+
let vs = vec![1, 2, 3, 4, 5];
37+
assert_eq!(filter(&vs, &even_predicate), vec![2, 4]);
3938
}
4039

4140
//
@@ -44,11 +43,11 @@ fn test_filter_small() {
4443

4544
#[test]
4645
fn test_mat_mult_identity() {
47-
let mut mat1 = vec![vec![0.;3]; 3];
46+
let mut mat1 = vec![vec![0.; 3]; 3];
4847
for i in 0..mat1.len() {
4948
mat1[i][i] = 1.;
5049
}
51-
let mat2 = vec![vec![5.;3]; 3];
50+
let mat2 = vec![vec![5.; 3]; 3];
5251
let result = mat_mult(&mat1, &mat2);
5352
for i in 0..result.len() {
5453
for j in 0..result[i].len() {
@@ -63,7 +62,7 @@ fn test_mat_mult_identity() {
6362

6463
#[test]
6564
fn test_sieve_basic() {
66-
assert_eq!(vec![2,3,5,7,11], sieve(12));
65+
assert_eq!(vec![2, 3, 5, 7, 11], sieve(12));
6766
}
6867

6968
//

hw01/tests/tests_student.rs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#![cfg(test)]
2+
3+
use hw01::problem2::mat_mult;
4+
5+
//
6+
// Problem 2
7+
//
8+
9+
#[test]
10+
fn test_mat_mult() {
11+
let mat1 = vec![vec![12., 7., 3.], vec![4., 5., 6.], vec![7., 8., 9.]];
12+
let mat2 = vec![
13+
vec![5., 8., 1., 2.],
14+
vec![6., 7., 3., 0.],
15+
vec![4., 5., 9., 1.],
16+
];
17+
18+
let result = mat_mult(&mat1, &mat2);
19+
assert_eq!(
20+
result,
21+
vec![
22+
vec![114., 160., 60., 27.],
23+
vec![74., 97., 73., 14.],
24+
vec![119., 157., 112., 23.]
25+
]
26+
);
27+
}

0 commit comments

Comments
 (0)