It always frightens us when we hear the word “Matrix” and the complex computations involved in Multiplying two Prime Numbers.

Lets take a look at the serial code , Brute force approach

**Brute force**

- Input: matrices A and B
- Let C be a new matrix of the appropriate size
- Pick a tile size
*T*= Θ(√*M*) - For I from 1 to n in steps of T:
- For J from 1 to p in steps of T:
- For K from 1 to m in steps of T:
- Multiply
*A*_{I:I+T, K:K+T}and*B*_{K:K+T, J:J+T}into*C*_{I:I+T, J:J+T}, that is: - For i from I to min(
*I*+*T*,*n*):- For j from J to min(
*J*+*T*,*p*):- Let sum = 0
- For k from K to min(
*K*+*T*,*m*):- Set sum ← sum +
*A*×_{ik}*B*_{kj}

- Set sum ← sum +
- Set
*C*←_{ij}*C*+ sum_{ij}

- For j from J to min(

- Multiply

- For K from 1 to m in steps of T:

- For J from 1 to p in steps of T:
- Return C

Is the Algorithm for Brute force is optimal for smaller sizes

**Divide and Conquer **

Divide and Conquer Serial

- Inputs: matrices A of size
*n*×*m*, B of size*m*×*p*. - Base case: if max(
*n*,*m*,*p*) is below some threshold, use an unrolled version of t - If max(
*n*,*m*,*p*) =*n*, split A horizontally:

**Now Serial Divide and Conquer- D & C (Serial)**

**Now Strassen Matrix Multiplication Strassen Serial**

Source : WIKIPEDIA for algo

Advertisements