Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement Matrix:Matrix multiplication #85

Open
wants to merge 1 commit into
base: matrix
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 63 additions & 0 deletions src/types/matrix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -386,6 +386,21 @@ where

rows_collection
}

// Returns the real number dot product of self Matrix row vector at row index `row_index` and
// right hand side Matrix column vector at column index `column_index`.
fn impl_dot_row_column_real(&self, row_index: usize, rhs: &Matrix<T>, col_index: usize) -> T
where
T: Num + Copy + Sync + Send + Default + std::ops::AddAssign,
{
let mut dot_product = T::zero();
for (i, row_value) in self.rows[row_index].iter().enumerate() {
dot_product += *row_value * rhs.rows[i][col_index];
}
dot_product
}

// TODO: impl dot product for complex numbers
}

// ================================
Expand Down Expand Up @@ -1089,6 +1104,54 @@ mod tests {
assert_relative_eq!(m.additive_inverse()[1][1].im, rows_expected_neg[1][1].im);
}

// ================================
//
// Private methods
//
// ================================

#[test]
fn matrix_dot_row_column_real_i32() {
let m1 = crate::matrix!([3_i32, 4], [-1, 2], [0, 4]);
let m2 = crate::matrix!([5, 1], [3, 1]);

let x00 = m1.impl_dot_row_column_real(0, &m2, 0);
let x10 = m1.impl_dot_row_column_real(1, &m2, 0);
let x20 = m1.impl_dot_row_column_real(2, &m2, 0);
let x01 = m1.impl_dot_row_column_real(0, &m2, 1);
let x11 = m1.impl_dot_row_column_real(1, &m2, 1);
let x21 = m1.impl_dot_row_column_real(2, &m2, 1);

assert_eq!(x00, 27);
assert_eq!(x10, 1);
assert_eq!(x20, 12);

assert_eq!(x01, 7);
assert_eq!(x11, 1);
assert_eq!(x21, 4);
}

#[test]
fn matrix_dot_row_column_real_f64() {
let m1 = crate::matrix!([3.0_f64, 4.0], [-1.0, 2.0], [0.0, 4.0]);
let m2 = crate::matrix!([5.0_f64, 1.0], [3.0, 1.0]);

let x00 = m1.impl_dot_row_column_real(0, &m2, 0);
let x10 = m1.impl_dot_row_column_real(1, &m2, 0);
let x20 = m1.impl_dot_row_column_real(2, &m2, 0);
let x01 = m1.impl_dot_row_column_real(0, &m2, 1);
let x11 = m1.impl_dot_row_column_real(1, &m2, 1);
let x21 = m1.impl_dot_row_column_real(2, &m2, 1);

assert_relative_eq!(x00, 27.0);
assert_relative_eq!(x10, 1.0);
assert_relative_eq!(x20, 12.0);

assert_relative_eq!(x01, 7.0);
assert_relative_eq!(x11, 1.0);
assert_relative_eq!(x21, 4.0);
}

// ================================
//
// Index and IndexMut trait tests
Expand Down