How to resolve the algorithm Matrix multiplication step by step in the C programming language

Published on 7 June 2024 03:52 AM
#C

How to resolve the algorithm Matrix multiplication step by step in the C programming language

Table of Contents

Problem Statement

Multiply two matrices together. They can be of any dimensions, so long as the number of columns of the first matrix is equal to the number of rows of the second matrix.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Matrix multiplication step by step in the C programming language

This C code solves the problem of matrix multiplication. It takes as input two matrices a and b and computes their product c.

The code defines a macro MAT_ELEM(rows,cols,r,c) that computes the matrix element at position (r,c) of a matrix with rows rows and cols columns. It also uses a typedef to create alias MAT_IN_t and MAT_OUT_t that are used to represent the input and output matrices.

The function mat_mult computes the product of two matrices a and b. It does so by iterating over the rows and columns of the output matrix c and computing the dot product of the corresponding row of a and column of b for each element of c.

The function mat_show prints the contents of a matrix to the standard output.

The main function creates two matrices a and b and computes their product c using the mat_mult function. It then prints the contents of c to the standard output using the mat_show function.

Here is an example of the output of the program:

       7.000      2.000     -2.333
      35.000     11.000    -7.000
     107.000     33.000   -21.000
     255.000     79.000   -55.000

Source code in the c programming language

#include <stdio.h>

#define MAT_ELEM(rows,cols,r,c) (r*cols+c)

//Improve performance by assuming output matrices do not overlap with
//input matrices. If this is C++, use the __restrict extension instead
#ifdef __cplusplus
    typedef double * const __restrict MAT_OUT_t;
#else
    typedef double * const restrict MAT_OUT_t;
#endif
typedef const double * const MAT_IN_t;

static inline void mat_mult(
    const int m,
    const int n,
    const int p, 
    MAT_IN_t a,
    MAT_IN_t b,
    MAT_OUT_t c)
{
    for (int row=0; row<m; row++) {
        for (int col=0; col<p; col++) {
            c[MAT_ELEM(m,p,row,col)] = 0;
            for (int i=0; i<n; i++) {
                c[MAT_ELEM(m,p,row,col)] += a[MAT_ELEM(m,n,row,i)]*b[MAT_ELEM(n,p,i,col)];
            }
        }
    }
}

static inline void mat_show(
    const int m,
    const int p,
    MAT_IN_t a)
{
    for (int row=0; row<m;row++) {
        for (int col=0; col<p;col++) {
            printf("\t%7.3f", a[MAT_ELEM(m,p,row,col)]);
        }
        putchar('\n');
    }
}

int main(void)
{
    double a[4*4] = {1, 1,   1,   1,
                     2, 4,   8,  16,
                     3, 9,  27,  81,
                     4, 16, 64, 256};

    double b[4*3] = {    4.0,   -3.0,  4.0/3,
                     -13.0/3, 19.0/4, -7.0/3,
                       3.0/2,   -2.0,  7.0/6,
                      -1.0/6,  1.0/4, -1.0/6};

    double c[4*3] = {0};
    
    mat_mult(4,4,3,a,b,c);
    mat_show(4,3,c);
    return 0;
}


  

You may also check:How to resolve the algorithm Left factorials step by step in the PowerShell programming language
You may also check:How to resolve the algorithm Biorhythms step by step in the R programming language
You may also check:How to resolve the algorithm Blum integer step by step in the Maxima programming language
You may also check:How to resolve the algorithm Draw a sphere step by step in the PicoLisp programming language
You may also check:How to resolve the algorithm Set step by step in the Ol programming language