Skip to content

Commit

Permalink
Fix multiply_vector() implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
cm-jones committed Jun 29, 2024
1 parent 783901d commit 136f4dc
Showing 1 changed file with 12 additions and 12 deletions.
24 changes: 12 additions & 12 deletions src/matrix.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -280,20 +280,20 @@ Matrix<T> Matrix<T>::operator*(const Matrix<T>& other) const {
}

template <typename T>
Vector<T> multiply_vector(const Vector<T>& vec) const {
if (this->get_cols() != vec.size()) {
throw std::invalid_argument("Matrix and vector dimensions do not match for multiplication");
}
Vector<T> result(this->get_rows());
for (size_t row = 0; row < this->get_rows(); ++row) {
T sum = T();
for (size_t col = 0; col < this->get_cols(); ++col) {
sum += (*this)(row, col) * vec[col];
}
result[row] = sum;
Vector<T> Matrix<T>::multiply_vector(const Vector<T>& vec) const {
if (this->get_cols() != vec.size()) {
throw std::invalid_argument("Matrix and vector dimensions do not match for multiplication");
}
Vector<T> result(this->get_rows());
for (size_t row = 0; row < this->get_rows(); ++row) {
T sum = T();
for (size_t col = 0; col < this->get_cols(); ++col) {
sum += (*this)(row, col) * vec[col];
}
return result;
result[row] = sum;
}
return result;
}

// Matrix properties

Expand Down

0 comments on commit 136f4dc

Please sign in to comment.