简体   繁体   English

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

[英]C, matrix transposed multiplication using dynamic memory allocation

I'm trying to transpose and multiply some matrices, basically I get 2 matrices, matrixA and matrixB the action to be performed is trace(transpose(matrixA)*matrixB) . 我试图对某些矩阵进行转置和乘法,基本上我得到2个矩阵, matrixAmatrixB ,要执行的操作是trace(transpose(matrixA)*matrixB)

I managed to get this working for nxn matrices but I can't get it to work with mxn where ( n>m or m>n ). 我设法使它适用于nxn矩阵,但是我无法使其与mxn ,其中( n>mm>n )。

I've looked online for solutions but I can't implement theirs solution into mine. 我在网上寻找解决方案,但无法将其解决方案实施到我的解决方案中。

I removed almost all the code to simplify reading, if you prefer the entire code I linked it here. 如果您喜欢此处链接的整个代码,则删除了几乎所有代码以简化阅读

If you do want to run the entire code, to recreate the problem use the following commands: 如果确实要运行整个代码,请使用以下命令来重新创建问题:

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

The above commands should return Sum 56 but instead I get Sum 18 上面的命令应该返回总和56但我得到总和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);

Your array indices for calculating the transpose, multiplication, and the trace seem to be incorrect. 您用于计算转置,乘法和轨迹的数组索引似乎不正确。 I've corrected them in the following program: 我已在以下程序中更正了它们:

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

The above program will output your expected value: 上面的程序将输出您的期望值:

Sum: 56


** UPDATE ** ** 更新 **
As pointed by MFisherKDX, the above code will not work if the result matrix is not a square matrix. 如MFisherKDX所指出的,如果结果矩阵不是方矩阵,则上面的代码将不起作用。 The following code fixes this issue: 以下代码解决了此问题:

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

This code will output the following as expected: 该代码将按预期输出以下内容:

 Sum: 83 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM