C编程:对两个相邻的以行为主的数组执行矩阵乘法

wfsdck30  于 2023-01-20  发布在  其他
关注(0)|答案(1)|浏览(138)

我试图写一个函数,对两个相邻的以行为主的数组执行简单的矩阵乘法。但是当我试图在末尾打印每个值时,我得到了垃圾。我猜这是因为我混淆了正确的迭代和跳转行/列所需的缩放。有人有什么建议吗?
完整的代码如下所示:

#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;
}
3qpi33ja

3qpi33ja1#

主要问题是打印内部for循环中的一个拼写错误。

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

但您需要递增j,而不是i

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

下面是我的代码,它有一个独立的矩阵打印函数,适用于您正在使用的数组:

/* 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;
}

我也已经初始化了变量,代码应该检查内存是否被分配了,但是没有。
当我在没有打印的情况下运行这段代码时,我得到了正确的结果,所以我仔细看了一下,发现了问题。

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

相关问题