Matrix Multiplication

Matrix Multiplication in C++ (Multiply Two Matrix)

C++Intermediate
C++
#include <iostream>
using namespace std;

int main() {
    int rows1 = 2, cols1 = 3;
    int rows2 = 3, cols2 = 2;
    
    int matrix1[2][3] = {{1, 2, 3}, {4, 5, 6}};
    int matrix2[3][2] = {{7, 8}, {9, 10}, {11, 12}};
    int result[2][2] = {{0, 0}, {0, 0}};
    
    // Matrix multiplication
    for (int i = 0; i < rows1; i++) {
        for (int j = 0; j < cols2; j++) {
            for (int k = 0; k < cols1; k++) {
                result[i][j] += matrix1[i][k] * matrix2[k][j];
            }
        }
    }
    
    cout << "Result matrix:" << endl;
    for (int i = 0; i < rows1; i++) {
        for (int j = 0; j < cols2; j++) {
            cout << result[i][j] << " ";
        }
        cout << endl;
    }
    
    return 0;
}

Output

Result matrix:
58 64
139 154

This program teaches you how to multiply two matrices in C++. Matrix multiplication is a fundamental operation in linear algebra, used extensively in mathematics, computer graphics, machine learning, and scientific computing. Unlike element-wise multiplication, matrix multiplication follows specific rules and produces a result matrix with dimensions determined by the input matrices.


1. What This Program Does

The program multiplies two matrices and displays the result. For example:

  • Matrix 1 (2x3): 1 2 3 4 5 6

  • Matrix 2 (3x2): 7 8 9 10 11 12

  • Result (2x2): 58 64 139 154

Matrix multiplication is not commutative (A×B ≠ B×A in general).


2. Header File Used

#include <iostream>

This header provides:

  • cout for displaying output
  • cin for taking input from the user

3. Understanding Matrix Multiplication Rules

Dimension Requirement:

For matrices A (m×n) and B (p×q) to be multipliable:

  • Number of columns in A must equal number of rows in B
  • That is: n = p

Result Dimensions:

  • Result matrix will be m x q
  • Example: (2 x 3) x (3 x 2) = (2 x 2)

Multiplication Process:

  • Element at result[i][j] = sum of (A[i][k] * B[k][j]) for all k
  • This is the dot product of row i of A and column j of B

4. Declaring Variables

The program declares: int rows1 = 2, cols1 = 3; int rows2 = 3, cols2 = 2; int matrix1[2][3] = {{1, 2, 3}, {4, 5, 6}}; int matrix2[3][2] = {{7, 8}, {9, 10}, {11, 12}}; int result[2][2] = {{0, 0}, {0, 0}};

  • rows1, cols1: dimensions of first matrix (2×3)
  • rows2, cols2: dimensions of second matrix (3×2)
  • matrix1: first matrix
  • matrix2: second matrix
  • result: stores the multiplication result (2×2)

5. The Matrix Multiplication Algorithm

The core algorithm uses three nested loops:

for (int i = 0; i < rows1; i++) { for (int j = 0; j < cols2; j++) { for (int k = 0; k < cols1; k++) { result[i][j] += matrix1[i][k] * matrix2[k][j]; } } }

How it works:

  • Outer loop (i): iterates through rows of result matrix (rows1)
  • Middle loop (j): iterates through columns of result matrix (cols2)
  • Inner loop (k): iterates through common dimension (cols1 = rows2)
  • result[i][j] accumulates: matrix1[i][k] × matrix2[k][j]

Step-by-step calculation (for result[0][0]):

k = 0: result[0][0] += matrix1[0][0] * matrix2[0][0] = 1 * 7 = 7

k = 1: result[0][0] += matrix1[0][1] * matrix2[1][0] = 2 * 9 = 18

k = 2: result[0][0] += matrix1[0][2] * matrix2[2][0] = 3 * 11 = 33

Final: result[0][0] = 7 + 18 + 33 = 58

Visual Representation:

result[0][0] = (17) + (29) + (311) = 7 + 18 + 33 = 58 result[0][1] = (18) + (210) + (312) = 8 + 20 + 36 = 64 result[1][0] = (47) + (59) + (611) = 28 + 45 + 66 = 139 result[1][1] = (48) + (510) + (612) = 32 + 50 + 72 = 154


6. Understanding the Dot Product

Dot Product Concept:

Each element in the result is the dot product of:

  • A row from the first matrix
  • A column from the second matrix

Example (result[0][0]):

  • Row 0 of matrix1: [1, 2, 3]
  • Column 0 of matrix2: [7, 9, 11]
  • Dot product: (17) + (29) + (3*11) = 58

7. Displaying the Result

The program displays the result matrix:

for (int i = 0; i < rows1; i++) { for (int j = 0; j < cols2; j++) { cout << result[i][j] << " "; } cout << endl; }

Output:

58 64 139 154


8. Important Matrix Multiplication Properties

Not Commutative:

  • A * B != B * A (in general)
  • Order matters!

Associative:

  • (A * B) * C = A * (B * C)
  • You can group multiplications

Distributive:

  • A * (B + C) = A * B + A * C
  • Multiplication distributes over addition

Identity Matrix:

  • I * A = A * I = A
  • Multiplying by identity matrix gives original matrix

9. When to Use Matrix Multiplication

Linear Algebra:

  • Solving systems of linear equations
  • Matrix transformations
  • Eigenvalue calculations

Computer Graphics:

  • 3D transformations (rotation, scaling, translation)
  • Coordinate system conversions
  • Projection matrices

Machine Learning:

  • Neural network computations
  • Feature transformations
  • Data processing pipelines

Scientific Computing:

  • Numerical methods
  • Signal processing
  • Image processing

10. Important Considerations

Dimension Compatibility:

  • Always check: cols1 == rows2
  • If not compatible, multiplication is undefined

Initialization:

  • Initialize result matrix to zeros
  • Important for accumulation: result[i][j] += ...

Time Complexity:

  • O(rows1 * cols2 * cols1) for standard algorithm
  • For n x n matrices: O(n^3)
  • Can be optimized using algorithms like Strassen's

Memory Requirements:

  • Result matrix requires rows1 × cols2 space
  • For large matrices, consider memory constraints

11. Common Mistakes

Wrong Loop Order:

  • Must be: i (rows1), j (cols2), k (cols1)
  • Wrong order produces incorrect results

Not Initializing Result:

  • Result matrix must be initialized to zeros
  • Otherwise, contains garbage values

Dimension Mismatch:

  • Not checking if cols1 == rows2
  • Leads to undefined behavior

12. return 0;

This ends the program successfully.


Summary

  • Matrix multiplication requires cols1 = rows2 for compatibility.
  • Result dimensions: (rows1 x cols1) x (rows2 x cols2) = (rows1 x cols2).
  • Algorithm uses three nested loops: i (result rows), j (result cols), k (common dimension).
  • Each result element is dot product of row from first matrix and column from second.
  • Matrix multiplication is not commutative but is associative and distributive.
  • Understanding matrix multiplication is essential for linear algebra, graphics, and ML.
  • Always initialize result matrix and verify dimension compatibility.

This program is fundamental for beginners learning matrix operations, understanding nested loops, and preparing for advanced topics in linear algebra, computer graphics, machine learning, and scientific computing in C++ programs.