Advertisement
HjHimansh

Untitled

Sep 17th, 2023
22
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.15 KB | None | 0 0
  1. // Function to perform matrix multiplication
  2. void performMatrixMultiplication(SparseMatrixWithArray<T>& mat2) {
  3. if (num_cols != mat2.num_rows) {
  4. 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;
  5. return;
  6. }
  7.  
  8. // Perform matrix multiplication and print the result
  9. for (int i = 0; i < num_rows; i++) {
  10. for (int j = 0; j < mat2.num_cols; j++) {
  11. T sum = 0;
  12. for (int k = 0; k < num_cols; k++) {
  13. for (const Element<T>& element1 : elements) {
  14. if (element1.row == i && element1.col == k) {
  15. for (const Element<T>& element2 : mat2.elements) {
  16. if (element2.row == k && element2.col == j) {
  17. sum += element1.value * element2.value;
  18. break;
  19. }
  20. }
  21. }
  22. }
  23. }
  24. std::cout << sum << " ";
  25. }
  26. std::cout << std::endl;
  27. }
  28. }
  29.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement