Home > Enterprise >  C Programming: doing matrix multiplication of two contiguous, row-major arrays
C Programming: doing matrix multiplication of two contiguous, row-major arrays

Time:01-20

I'm trying to write a function that does naive matrix multiplication of two contiguous, row-major arrays. But when I attempt to print each value at the end I get garbage. I'm guessing it's because I've mixed up the proper iterations and scaling needed to jump rows/columns. Does anyone have any advice?

Full code necessary is below:

#include <stdio.h>
#include <stdlib.h>

void dmatmul(double *a, double *b, double *c, int astride, int bstride, int cdim_0, int cdim_1) {
    int i, j, p;

    for (i = 0; i < cdim_0; i  ) {
        for (j = 0; j < cdim_1; j  ) {
            c[i * cdim_1   j] = 0.0;
            for (p = 0; p < (astride); p  ) {
                c[i * cdim_1   j]  = a[i * (astride)   p] * b[p * (bstride)   j];
            }
        }
    }
}

int main(void) {
    double *x, *y, *z;
    int xdim_0, xdim_1, ydim_0, ydim_1, zdim_0, zdim_1, i, j;

    xdim_0 = 2;
    xdim_1 = 4;
    ydim_0 = 4;
    ydim_1 = 2;
    zdim_0 = 2;
    zdim_1 = 2;

    x = (double *) malloc (xdim_0 * xdim_1 * sizeof(double));
    y = (double *) malloc (ydim_0 * ydim_1 * sizeof(double));
    z = (double *) malloc (zdim_0 * zdim_1 * sizeof(double));

    for (i = 0; i < xdim_0 * xdim_1; i  ) {
        x[i] = i   1;
        y[i] = 2 * (i   1);
    }

    dmatmul(x, y, z, xdim_1, ydim_1, zdim_0, zdim_1);
    printf("\nMatrix product of X and Y dimensions: (%d, %d)\n", zdim_0, zdim_1);
    printf("Matrix product of X and Y values:");
    for (i = 0; i < zdim_0; i  ) {
        printf("\n");
        for (j = 0; j < zdim_1; i  ) {
            printf("\t%f", z[i * zdim_1   j]);
        }
    }

    return 0;
}

CodePudding user response:

The primary problem is a typo in the inner for loop doing the printing. You have:

for (j = 0; j < zdim_1; i  )

but you ned to increment j, not i:

for (j = 0; j < zdim_1; j  )

Here's my code, which has an independent matrix printing function appropriate for the arrays you're using:

/* SO 7516-7451 */
#include <stdio.h>
#include <stdlib.h>

static void dmatmul(double *a, double *b, double *c, int astride, int bstride, int cdim_0, int cdim_1)
{
    int i, j, p;

    for (i = 0; i < cdim_0; i  )
    {
        for (j = 0; j < cdim_1; j  )
        {
            c[i * cdim_1   j] = 0.0;
            for (p = 0; p < (astride); p  )
            {
                c[i * cdim_1   j]  = a[i * (astride)   p] * b[p * (bstride)   j];
            }
        }
    }
}

static void mat_print(const char *tag, int rows, int cols, double *matrix)
{
    printf("%s (%dx%d):\n", tag, rows, cols);
    for (int i = 0; i < rows; i  )
    {
        for (int j = 0; j < cols; j  )
            printf("%4.0f", matrix[i * cols   j]);
        putchar('\n');
    }
}

int main(void)
{
    int xdim_0 = 2;
    int xdim_1 = 4;
    int ydim_0 = 4;
    int ydim_1 = 2;
    int zdim_0 = 2;
    int zdim_1 = 2;
    double *x = (double *)malloc(xdim_0 * xdim_1 * sizeof(double));
    double *y = (double *)malloc(ydim_0 * ydim_1 * sizeof(double));
    double *z = (double *)malloc(zdim_0 * zdim_1 * sizeof(double));

    for (int i = 0; i < xdim_0 * xdim_1; i  )
    {
        x[i] = i   1;
        y[i] = 2 * (i   1);
    }

    mat_print("X", xdim_0, xdim_1, x);
    mat_print("Y", ydim_0, ydim_1, y);

    dmatmul(x, y, z, xdim_1, ydim_1, zdim_0, zdim_1);

    mat_print("Z", zdim_0, zdim_1, z);

    printf("\nMatrix product of X and Y dimensions: (%d, %d)\n", zdim_0, zdim_1);
    printf("Matrix product of X and Y values:\n");
    for (int i = 0; i < zdim_0; i  )
    {
        for (int j = 0; j < zdim_1; j  )
            printf("\t%f", z[i * zdim_1   j]);
        printf("\n");
    }

    return 0;
}

I've also initialized the variables as I declared them. The code should, but does not, check that the memory was allocated.

When I ran this code without your printing, I got the correct result, so then I took a good look at that and saw the problem.

X (2x4):
   1   2   3   4
   5   6   7   8
Y (4x2):
   2   4
   6   8
  10  12
  14  16
Z (2x2):
 100 120
 228 280

Matrix product of X and Y dimensions: (2, 2)
Matrix product of X and Y values:
    100.000000  120.000000
    228.000000  280.000000
  • Related