C,使用动态内存分配的矩阵转置乘法

时间:2018-05-11 23:27:11

标签: c matrix-multiplication transpose

基本上,我试图转置并乘以一些矩阵 我得到2个矩阵,matrixAmatrixB要执行的操作是trace(transpose(matrixA)*matrixB)

我设法让这个适用于nxn矩阵,但我无法使用mxn其中的地方(n>mm>n)。

我在网上寻找解决方案,但我无法将解决方案应用到我的解决方案中。

如果您更喜欢整个代码I linked it here.

,我删除了几乎所有代码以简化阅读

如果您确实要运行整个代码,要重新创建问题,请使用以下命令:

zeroes matrixA 2 3
zeroes matrixB 2 3
set matrixA
1 2 3 4 5 6
set matrixB
6 5 4 3 2 1
frob matrixA matrixB

以上命令应返回Sum 56,但我得到Sum 18

int* matrixATransposed = (int*) malloc(matrixARowLenght * matrixAColLenght * sizeof(int));
for (int i = 0; i < matrixARowLenght; i++)
{
    for (int j = 0; j < matrixAColLenght; j++)
    {
        *(matrixATransposed + i * matrixAColLenght + j) = *(matrixA + j * matrixAColLenght + i);
    }
}
// Multiply
int* mulRes = (int*)malloc(matrixARowLenght * matrixAColLenght * sizeof(int));
for (int i = 0; i < matrixAColLenght; i++) {
    for (int j = 0; j < matrixBColLenght; j++) {
        *(mulRes + i * matrixARowLenght + j) = 0;
        for (int k = 0; k < matrixARowLenght; k++)
            *(mulRes + i * matrixAColLenght + j) += *(matrixATransposed + i * matrixAColLenght + k) * *(matrixB + k * matrixBColLenght + j);
    }
}
// Sum the trace
int trace = 0;
for (int i = 0; i < matrixARowLenght; i++) {
    for (int j = 0; j < matrixAColLenght; j++) {
        if (i == j) {
            trace += *(mulRes + i * matrixAColLenght + j);
        }
    }
}
printf_s("Sum: %d\n", trace);

1 个答案:

答案 0 :(得分:0)

用于计算转置,乘法和跟踪的数组索引似乎不正确。我在以下程序中纠正了它们:

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

int main(int argc, char **argv) {
    int matrixARowLenght = 2;
    int matrixAColLenght = 3;
    int matrixA[] = {1,2,3,4,5,6};

    int matrixBRowLenght = 2;
    int matrixBColLenght = 3; 
    int matrixB[] = {6,5,4,3,2,1};

    // Transpose
    int* matrixATransposed = (int *) malloc(matrixARowLenght * matrixAColLenght * sizeof(int));
    for (int i = 0; i < matrixAColLenght; i++) {
        for (int j = 0; j < matrixARowLenght; j++) {
            *(matrixATransposed + i * matrixARowLenght + j) = *(matrixA + j * matrixAColLenght + i);
        }
    }

    // Multiply
    int *mulRes = (int *) malloc(matrixARowLenght * matrixAColLenght * sizeof(int));
    for (int i = 0; i < matrixAColLenght; ++i) {
        for (int j = 0; j < matrixAColLenght; ++j) {
            *(mulRes + (i * matrixAColLenght) + j) = 0;
            for (int k = 0; k < matrixARowLenght; ++k) {
                *(mulRes + (i * matrixAColLenght) + j) += *(matrixATransposed + (i * matrixARowLenght) + k) * *(matrixB + (k * matrixAColLenght) + j);
            } 
        }
    }
    free(matrixATransposed);

    // Sum the trace
    int trace = 0;
    for (int i = 0; i < matrixAColLenght; i++) {
        for (int j = 0; j < matrixAColLenght; j++) {
            if (i == j) {
                trace += *(mulRes + i * matrixAColLenght + j);
            }
        }
    }
    printf("Sum: %d\n", trace);
    free(mulRes);

    return 0;
}

上述程序将输出您的预期值:

Sum: 56

<小时/> ** 更新 **
正如MFisherKDX所指出的,如果结果矩阵不是方阵,则上述代码将不起作用。以下代码修复了此问题:

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

int main(int argc, char **argv) {
    int matrixARowLenght = 2;
    int matrixAColLenght = 3;
    int matrixA[] = {1,2,3,4,5,6};

    int matrixBRowLenght = 2;
    int matrixBColLenght = 4; 
    int matrixB[] = {8,7,6,5,4,3,2,1};

    // Transpose
    int* matrixATransposed = (int *) malloc(matrixARowLenght * matrixAColLenght * sizeof(int));
    for (int i = 0; i < matrixAColLenght; i++) {
        for (int j = 0; j < matrixARowLenght; j++) {
            *(matrixATransposed + i * matrixARowLenght + j) = *(matrixA + j * matrixAColLenght + i);
        }
    }

    // Multiply
    int *mulRes = (int *) malloc(matrixAColLenght * matrixBColLenght * sizeof(int));
    for (int i = 0; i < matrixAColLenght; ++i) {
        for (int j = 0; j < matrixBColLenght; ++j) {
            *(mulRes + (i * matrixBColLenght) + j) = 0;
            for (int k = 0; k < matrixARowLenght; ++k) {
                *(mulRes + (i * matrixBColLenght) + j) += *(matrixATransposed + (i * matrixARowLenght) + k) * *(matrixB + (k * matrixBColLenght) + j);
            } 
        }
    }
    free(matrixATransposed);

    // Sum the trace
    int trace = 0;
    for (int i = 0; i < matrixAColLenght; i++) {
        for (int j = 0; j < matrixBColLenght; j++) {
            if (i == j) {
                trace += *(mulRes + i * matrixBColLenght + j);
            }
        }
    }
    printf("Sum: %d\n", trace);
    free(mulRes);

    return 0;
}

此代码将按预期输出以下内容:

Sum: 83