Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Function to perform matrix multiplication
- void performMatrixMultiplication(SparseMatrixWithArray<T>& mat2) {
- if (num_cols != mat2.num_rows) {
- std::cerr << "Matrix multiplication not possible. Number of columns in the first matrix must be equal to the number of rows in the second matrix." << std::endl;
- return;
- }
- // Perform matrix multiplication and print the result
- for (int i = 0; i < num_rows; i++) {
- for (int j = 0; j < mat2.num_cols; j++) {
- T sum = 0;
- for (int k = 0; k < num_cols; k++) {
- for (const Element<T>& element1 : elements) {
- if (element1.row == i && element1.col == k) {
- for (const Element<T>& element2 : mat2.elements) {
- if (element2.row == k && element2.col == j) {
- sum += element1.value * element2.value;
- break;
- }
- }
- }
- }
- }
- std::cout << sum << " ";
- }
- std::cout << std::endl;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement