# Matrix Multiplication

Condition for the multiplication of 2 matrices

• The count of columns in the first matrix should match the count of rows in the second matrix.

Program for the multiplication of 2 matrices using the straight-forward (naive) technique.
Time complexity : O ( n 3 )

``````def Multiplication (matrix_1, matrix_2, result) :

rows_1 = len(matrix_1)
cols_1 = len(matrix_1[0])
cols_2 = len(matrix_2[0])

for r in range(rows_1) :
for c in range(cols_2) :
for p in range(cols_1) :
result [r][c] += matrix_1[r][p] * matrix_2[p][c]

def main () :

print("Dimensions of Matrix 1")
rows_1 = int(input("Enter row count : "))
cols_1 = int(input("Enter column count : "))

matrix_1 = [0] * rows_1
for r in range(rows_1) :
matrix_1[r] = [0] * cols_1

for r in range(rows_1) :
for c in range(cols_1) :
matrix_1[r][c] = int(input("Enter [" + str(r) + "]" + "[" + str(c) + "] : "))

print("\nDimensions of Matrix 2")
print("Rows of Matrix 2 = " + str(cols_1) + " (matching the column count of matrix 1)")
rows_2 = cols_1

cols_2 = int(input("Enter column count : "))

matrix_2 = [0] * rows_2
for r in range(rows_2) :
matrix_2[r] = [0] * cols_2

for r in range(rows_2) :
for c in range(cols_2) :
matrix_2[r][c] = int(input("Enter [" + str(r) + "]" + "[" + str(c) + "] : "))

result = [0] * rows_1
for r in range(rows_1) :
result[r] = [0] * cols_2

Multiplication (matrix_1, matrix_2, result)

print("\nResult Matrix")
for r in range(rows_1) :
for c in range(cols_2) :
print("[" + str(r) + "]" + "[" + str(c) + "] : " + str(result[r][c]), end = ' ')
print("")

if __name__ == "__main__" :
main()
``````

Output

``````Dimensions of Matrix 1
Enter row count : 2
Enter column count : 3
Enter [0][0] : 1
Enter [0][1] : 2
Enter [0][2] : 3
Enter [1][0] : 4
Enter [1][1] : 5
Enter [1][2] : 6

Dimensions of Matrix 2
Rows of Matrix 2 = 3 (matching the column count of matrix 1)
Enter column count : 2
Enter [0][0] : 6
Enter [0][1] : 5
Enter [1][0] : 4
Enter [1][1] : 3
Enter [2][0] : 2
Enter [2][1] : 1

Result Matrix
[0][0] : 20 [0][1] : 14
[1][0] : 56 [1][1] : 41
``````
``````#include<iostream>

void Matrix_Multiplication ( int ** matrix_1, int rows_1, int cols_1,
int ** matrix_2, int rows_2, int cols_2,
int ** result ) {

for (int r = 0; r < rows_1; r ++) {
for (int c = 0; c < cols_2; c++) {
for ( int p = 0; p < cols_1; p++) {
result [r][c] += matrix_1[r][p] * matrix_2[p][c];
}
}
}
}

int main() {

int rows_1, cols_1, rows_2, cols_2;
std :: cout << "Dimensions of Matrix 1" << std :: endl;
std :: cout << "Enter row count : ";
std :: cin >> rows_1;
std :: cout << "Enter column count : ";
std :: cin >> cols_1;

int ** matrix_1 = new int * [rows_1];
for (int r = 0; r < rows_1; r++) {
matrix_1[r] = new int [cols_1];
}

for (int r = 0; r < rows_1; r++) {
for (int c = 0; c < cols_1; c++) {
std :: cout << "Enter [" << r << "]" << "[" << c << "] : ";
std :: cin >> matrix_1[r][c];
}
}

std :: cout << "\nDimensions of Matrix 2" << std :: endl;
std :: cout << "Rows Count : " << cols_1 << " ( matching the column count of matrix 1 )" << std :: endl;
std :: cout << "Enter column count : ";
std :: cin >> cols_2;
rows_2 = cols_1;

int ** matrix_2 = new int * [rows_2];
for (int r = 0; r < rows_2; r++) {
matrix_2[r] = new int [cols_2];
}

for (int r = 0; r < rows_2; r++) {
for (int c = 0; c < cols_2; c++) {
std :: cout << "Enter [" << r << "]" << "[" << c << "] : ";
std :: cin >> matrix_2[r][c];
}
}

int ** result = new int * [rows_1];
for (int r = 0; r < rows_1; r++) {
result [r] = new int [cols_2];
}

Matrix_Multiplication (matrix_1, rows_1, cols_1, matrix_2, rows_2, cols_2, result);

std :: cout << "\nResult Matrix" << std :: endl;
for (int r = 0; r < rows_1; r++) {
for (int c = 0; c < cols_2; c++) {
std :: cout << "[" << r << "]" << "[" << c << "] : " << result[r][c] << "  ";
} std :: cout << std :: endl;
}

// Deallocate the memory of matrix 1
for (int r=0; r < rows_1; r++) {
delete [] matrix_1[r];
}
delete [] matrix_1;

// Deallocate the memory of matrix 2
for (int r=0; r < rows_2; r++) {
delete [] matrix_2[r];
}
delete [] matrix_2;

// Deallocate the memory of result matrix
for (int r=0; r < rows_1; r++) {
delete [] result[r];
}
delete [] result;

return 0;
}
``````

Output

``````Dimensions of Matrix 1
Enter row count : 2
Enter column count : 3
Enter [0][0] : 1
Enter [0][1] : 2
Enter [0][2] : 3
Enter [1][0] : 4
Enter [1][1] : 5
Enter [1][2] : 6

Dimensions of Matrix 2
Rows Count : 3 ( matching the column count of matrix 1 )
Enter column count : 2
Enter [0][0] : 6
Enter [0][1] : 5
Enter [1][0] : 4
Enter [1][1] : 3
Enter [2][0] : 2
Enter [2][1] : 1

Result Matrix
[0][0] : 20  [0][1] : 14
[1][0] : 56  [1][1] : 41
``````
``````import java.util.Scanner;

class Matrix {

void Multiplication (int [][] matrix_1, int[][] matrix_2, int result[][]) {

int rows_1 = matrix_1.length;
int cols_1 = matrix_1[0].length;
int cols_2 = matrix_2[0].length;

for (int r = 0; r < rows_1; r ++) {
for (int c = 0; c < cols_2; c++) {
for ( int p = 0; p < cols_1; p++) {
result [r][c] += matrix_1[r][p] * matrix_2[p][c];
}
}
}
}

public static void main (String[] args) {

int rows_1, cols_1, rows_2, cols_2;

Scanner scan = new Scanner(System.in);

System.out.println("Dimensions of Matrix 1");

System.out.print("Enter row count : ");
rows_1 = scan.nextInt();

System.out.print("Enter column count : ");
cols_1 = scan.nextInt();

int[][] matrix_1 = new int[rows_1][cols_1];

for (int r = 0; r < rows_1; r++) {
for (int c = 0; c < cols_1; c++) {
System.out.print("Enter [" + r + "]" + "[" + c + "] : ");
matrix_1[r][c] = scan.nextInt();
}
}

System.out.println("\nDimensions of Matrix 2");
System.out.println("Rows of Matrix 2 = " + cols_1 + " (matching the column count of matrix 1)");
rows_2 = cols_1;

System.out.print("Enter column count : ");
cols_2 = scan.nextInt();

int[][] matrix_2 = new int[rows_2][cols_2];

for (int r = 0; r < rows_2; r++) {
for (int c = 0; c < cols_2; c++) {
System.out.print("Enter [" + r + "]" + "[" + c + "] : ");
matrix_2[r][c] = scan.nextInt();
}
}

int[][] result = new int[rows_1][cols_2];

Matrix m = new Matrix();
m.Multiplication (matrix_1, matrix_2, result);

System.out.println("\nResult Matrix");
for (int r = 0; r < rows_1; r++) {
for (int c = 0; c < cols_2; c++) {
System.out.print("[" + r + "]" + "[" + c + "] : " + result[r][c] + "  ");
} System.out.println();
}
}
}
``````

Output

``````Dimensions of Matrix 1
Enter row count : 2
Enter column count : 3
Enter [0][0] : 1
Enter [0][1] : 2
Enter [0][2] : 3
Enter [1][0] : 4
Enter [1][1] : 5
Enter [1][2] : 6

Dimensions of Matrix 2
Rows of Matrix 2 = 3 (matching the column count of matrix 1)
Enter column count : 2
Enter [0][0] : 6
Enter [0][1] : 5
Enter [1][0] : 4
Enter [1][1] : 3
Enter [2][0] : 2
Enter [2][1] : 1

Result Matrix
[0][0] : 20  [0][1] : 14
[1][0] : 56  [1][1] : 41
``````