一维数组中的矩阵相乘

Bri*_*ehn 0 c arrays matrix matrix-multiplication

void multiply(int a[], int row1, int col1, int b[], int row2, int col2) {
    int d[size];
    for (int i = 0; i < row1; i++) {
        for (int j = 0; j < col2; j++) {
            int sum = 0.0;
            for (int k = 0; k < col2; k++)
                sum = sum + a[i * col1 + k] * b[k * col2 + j];
            d[i * col2 + j] = sum;
        }
    }
    for (int i = 0; i < size; i++) {
        if (i % col2 == 0) {
            printf("\n");
        }
        printf("%d ", d[i]);
    }
}
Run Code Online (Sandbox Code Playgroud)

我将此作为多个应该是矩阵的两个一维数组的函数。我正在使用在线编译器,当我运行代码时它什么也没给我。

编辑:

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

int size = 8;

void multiply(int a[], int row1, int col1, int b[], int row2, int col2) {
    int d[size];
    for (int i = 0; i < row1; i++) {
        for (int j = 0; j < col2; j++) {
            int sum = 0.0;
            for (int k = 0; k < col2; k++) {
            sum = sum + a[i * col1 + k] * b[k * col2 + j];
            }
            d[i * col2 + j] = sum;
        }
    }
    for (int i = 0; i < size; i++) {
    if (i % col2 == 0) {
      printf("\n");
    }
    printf("%d ", d[i]);
  }
}

int main() {
    int a[size] = {
    1,  2,  3,  4, // 0  1  2  3
    5,  6,  7,  8  // 4  5  6  7
    };
    int c[size] = {
    1,  4,  // 0  1
    2,  3,  // 2  3
    3,  2,  // 4  5
    4,  1   // 6  7
    };
    printf("Multipying Matrices\n");
    multiply(a, 2, 4, c, 4, 2);
}
Run Code Online (Sandbox Code Playgroud)

这是我运行以测试 Costantino Grana 的乘法函数的完整代码,但对其进行了编辑,因为我还没有进行内存分配。

这段代码的结果是:

5 10
17 38
2 4
6 8
Run Code Online (Sandbox Code Playgroud)

这已经是错误的,因为两个矩阵的相乘应该产生一个 2x2 的矩阵。

Cos*_*ana 5

这是一个带有最小、完整和可验证示例的固定版本 ,您应该已经发布了。

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

void multiply(int *a, int row1, int col1, int *b, int row2, int col2) 
{
    assert(col1 == row2);

    int size = row1*col2;
#ifdef _MSC_VER
    int *d = malloc(size * sizeof *d);
#else
    int d[size];
#endif
    for (int i = 0; i < row1; i++) {
        for (int j = 0; j < col2; j++) {
            int sum = 0;
            for (int k = 0; k < col1; k++)
                sum = sum + a[i * col1 + k] * b[k * col2 + j];
            d[i * col2 + j] = sum;
        }
    }

    for (int i = 0; i < size; i++) {
        if (i % col2 == 0) {
            printf("\n");
        }
        printf("%d ", d[i]);
    }

#ifdef _MSC_VER
    free(d);
#endif
}

int main(void)
{
    int a[] = {
        1, 2, 3,
        4, 5, 6,
    };
    int b[] = {
        7, 10,
        8, 11,
        9, 12,
    };

    multiply(a, 2, 3, b, 3, 2);
}
Run Code Online (Sandbox Code Playgroud)

除了有避免 VLA 的解决方案外,错误在于应该运行到 col1 或 row2 而不是 col2 的内循环。

By the way: I'd avoid passing col1 and row2. These should be the same. I substituted a[] and b[] with *a and *b, which is closer to the real thing (my opinion). Moreover you should avoid mixing computation and printing. Have your function return a new matrix and another function to print matrices. Finally, use a struct to keep the data pointer, rows and cols together.