Matrix Multiplication | Recursive

3

Given two matrices A and B. The task is to multiply matrix A and matrix B recursively. If matrix A and matrix B are not multiplicative compatible, then generate output “Not Possible”.

Examples:

Input: A = 12 56
           45 78
       B = 2 6
           5 8
Output: 304 520
        480 894

Input: A = 1 2 3
           4 5 6
           7 8 9
       B = 1 2 3
           4 5 6
           7 8 9

Output: 30  36  42  
        66  81  96  
       102 126 150  

It is recommended to first refer Iterative Matrix Multiplication.

First check if multiplication between matrices is possible or not. For this, check if number of columns of first matrix is equal to number of rows of second matrix or not. If both are equal than proceed further otherwise generate output “Not Possible”.

In Recursive Matrix Multiplication, we implement three loops of Iteration through recursive calls. The inner most Recursive call of multiplyMatrix() is to iterate k (col1 or row2). The second recursive call of multiplyMatrix() is to change the columns and the outermost recursive call is to change rows.

Below is Recursive Matrix Multiplication code.

C/C++

// Recursive code for Matrix Multiplication
#include <stdio.h>

const int MAX = 100;

void multiplyMatrixRec(int row1, int col1, int A[][MAX],
                       int row2, int col2, int B[][MAX],
                       int C[][MAX])
{
    // Note that below variables are static
    // i and j are used to know current cell of
    // result matrix C[][]. k is used to know
    // current column number of A[][] and row
    // number of B[][] to be multiplied
    static int i = 0, j = 0, k = 0;

    // If all rows traversed.
    if (i >= row1)
        return;

    // If i < row1
    if (j < col2)
    {
      if (k < col1)
      {
         C[i][j] += A[i][k] * B[k][j];
         k++;

         multiplyMatrixRec(row1, col1, A, row2, col2,
                                               B, C);
      }

      k = 0;
      j++;
      multiplyMatrixRec(row1, col1, A, row2, col2, B, C);
    }

    j = 0;
    i++;
    multiplyMatrixRec(row1, col1, A, row2, col2, B, C);
}

// Function to multiply two matrices A[][] and B[][]
void multiplyMatrix(int row1, int col1, int A[][MAX],
                    int row2, int col2, int B[][MAX])
{
    if (row2 != col1)
    {
        printf("Not Possible\n");
        return;
    }

    int C[MAX][MAX] = {0};

    multiplyMatrixRec(row1, col1, A, row2, col2, B, C);

    // Print the result
    for (int i = 0; i < row1; i++)
    {
        for (int j = 0; j < col2; j++)
            printf("%d  ", C[i][j]);

        printf("\n");
    }
}

// Driven Program
int main()
{
    int A[][MAX] = { {1, 2, 3},
                    {4, 5, 6},
                    {7, 8, 9}};

    int B[][MAX] = { {1, 2, 3},
                    {4, 5, 6},
                    {7, 8, 9} };

    int row1 = 3, col1 = 3, row2 = 3, col2 = 3;
    multiplyMatrix(row1, col1, A, row2, col2, B);

    return 0;
}

Java

// Java recursive code for Matrix Multiplication

class GFG 
{
    public static int MAX = 100;
    
    // Note that below variables are static
    // i and j are used to know current cell of
    // result matrix C[][]. k is used to know
    // current column number of A[][] and row
    // number of B[][] to be multiplied
    public static int i = 0, j = 0, k = 0;
    
    static void multiplyMatrixRec(int row1, int col1, int A[][],
                       int row2, int col2, int B[][],
                       int C[][])
    {
        // If all rows traversed
        if (i >= row1)
            return;
 
        // If i < row1
        if (j < col2)
        {
            if (k < col1)
            {
                C[i][j] += A[i][k] * B[k][j];
                k++;
 
                multiplyMatrixRec(row1, col1, A, row2, col2, B, C);
            }
 
            k = 0;
            j++;
            multiplyMatrixRec(row1, col1, A, row2, col2, B, C);
        }
 
        j = 0;
        i++;
        multiplyMatrixRec(row1, col1, A, row2, col2, B, C);
    }
 
    // Function to multiply two matrices A[][] and B[][]
    static void multiplyMatrix(int row1, int col1, int A[][],
                    int row2, int col2, int B[][])
    {
        if (row2 != col1)
        {
            System.out.println("Not Possible\n");
            return;
        }
 
        int[][] C = new int[MAX][MAX];
 
        multiplyMatrixRec(row1, col1, A, row2, col2, B, C);
 
        // Print the result
        for (int i = 0; i < row1; i++)
        {
            for (int j = 0; j < col2; j++)
                System.out.print(C[i][j]+" ");
 
            System.out.println();
        }
    }
    
    // driver program
	public static void main (String[] args) 
	{
	    int row1 = 3, col1 = 3, row2 = 3, col2 = 3;
		int A[][] = { {1, 2, 3},
                      {4, 5, 6},
                      {7, 8, 9}};
 
        int B[][] = { {1, 2, 3},
                      {4, 5, 6},
                      {7, 8, 9} };
 
        multiplyMatrix(row1, col1, A, row2, col2, B);
	}
}

// Contributed by Pramod Kumar


Output:

30  36  42  
66  81  96  
102  126  150  

This article is contributed by Anuj Chauhan. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

GATE CS Corner    Company Wise Coding Practice

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.

Recommended Posts:



3 Average Difficulty : 3/5.0
Based on 4 vote(s)










Writing code in comment? Please use ide.geeksforgeeks.org, generate link and share the link here.