一维数组中的矩阵相乘 [英] Multiplying Matrices in One-Dimensional Arrays

查看:114
本文介绍了一维数组中的矩阵相乘的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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]);
    }
}

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

I have this as a function to multiple two one-dimensional arrays that are supposed to be matrices. I'm using an online compiler and it just gives me nothing when I run the code.

#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);
}

这是我运行的全部代码,用于测试Costantino Grana的乘法功能,但是由于我没有参与内存分配,因此对其进行了编辑.

This is the entire code that I run to test out the multiplication function from Costantino Grana but edited it because I haven't gone into memory allocation.

此代码的结果是:

5 10
17 38
2 4
6 8

这已经是错误的,因为两个矩阵的相乘将导致2x2的矩阵.

This is already wrong because the multiplication of the two matrices should result in a 2x2 matrix.

推荐答案

此处是带有最小,完整和可验证示例的固定版本,您应该已经发布了.

Here is a fixed version with a Minimal, Complete, and Verifiable example, which you should have posted.

#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);
}

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

Apart having a solution for avoiding VLAs, the mistake was in the inner loop which should run up to col1 or row2, not col2.

顺便说一句:我避免通过col1 row2.这些应该是相同的.我用*a*b替换了a[]b[],这与真实情况更接近(我认为).此外,您应该避免混合计算和打印.让您的函数返回一个新的矩阵,并让另一个函数打印矩阵. 最后,使用结构将数据指针,行和列保持在一起.

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.

这篇关于一维数组中的矩阵相乘的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆