简体   繁体   English

C++ 中的二维矩阵乘法

[英]2D Matrix Multiplication in C++

I want to write a function which allows us to do multiplication of 2 2D matrices.我想编写一个函数,它允许我们对 2 维矩阵进行乘法运算。 This function has the following parameters as inputs: list1 and list2 are 2D arrays.此函数具有以下参数作为输入:list1 和 list2 是二维数组。 They are transferred to the function as pointers.它们作为指针传递给函数。 Row1, col1, row2, col2 are int values for the size of list1 and list2. Row1、col1、row2、col2 是 list1 和 list2 大小的 int 值。

When I have square matrices for the inputs I get correct results.当我有输入的方阵时,我会得到正确的结果。 For example;例如; list1[2][2] = {1,2,3,4} and list2[2][2] = {1,2,3,4} gives the result {7,10,15,22}. list1[2][2] = {1,2,3,4} 和 list2[2][2] = {1,2,3,4} 给出结果 {7,10,15,22}。

When I don't use square matrices I don't get correct results.当我不使用方阵时,我不会得到正确的结果。 For example;例如; list1[2][1] = {1,2} and list2[1][2] = {1,2} gives the result {1,4,4,0} which is wrong and list1[2][1] = {1,2} and list2[1][2] = {1,2} gives a garbage value instead of a scalar number. list1[2][1] = {1,2} 和 list2[1][2] = {1,2} 给出了错误的结果 {1,4,4,0} 和 list1[2][1] = {1,2} 和 list2[1][2] = {1,2} 给出垃圾值而不是标量。

EDIT : Fixed the returning pointer problem but I still don't get the correct result编辑:修复了返回指针问题,但我仍然没有得到正确的结果

#include <iostream>

using namespace std;

void printArray2D(int* list, int row, int col) {
    for (int i = 0; i < row; i++) {
        for (int j = 0; j < col; j++) {
            cout << list[i * col + j] << " ";
        }
        cout << "" << endl;
    }
}

int* matrixMultiplication(int* list1, int* list2, int row1, int col1, int row2, int col2) {
    if (col1 != row2) {
        cout << "Array sizes don't match";
        return NULL;
    }
    else {
        int* newList = new int[5];
        newList[0] = 0;
        newList[1] = 0;
        newList[2] = 0;
        newList[3] = 0;
        newList[4] = 0;
        for (int i = 0; i < row1; i++) {
            for (int j = 0; j < col2; j++) {
                for (int k = 0; k < col1; k++) {
                    newList[i * col1 + j] = newList[i * col1 + j] + list1[i * col1 + k] * list2[k * col1 + j];
                }
            }
        }
        return newList;
    }
}

int main()
{
    int list1[2][1] = {1,2};

    int list2[1][2] = {1,2};
    

    int row1 = sizeof(list1) / sizeof(list1[0]);
    
    int col1 = sizeof(list1[0]) / sizeof(list1[0][0]);

    int row2 = sizeof(list2) / sizeof(list2[0]);

    int col2 = sizeof(list2[0]) / sizeof(list2[0][0]);
   

    int* result = matrixMultiplication((int*)list1, (int*)list2, row1, col1, row2, col2);

    printArray2D((int*)result, row1, col2);

    return 0;
}

I wouldn't mix in one function two entirely different matters, a memory allocation and a matrix-matrix multiplication!我不会在一个函数中混合两个完全不同的问题,内存分配和矩阵-矩阵乘法!

I would do C = A * B like this:我会这样做C = A * B像这样:

bool matrixMultiplication(int* C, int* A, int * B, int rowA, int colA, int rowB, int colB) {

    if (colA != rowB) return true;

    for (int i = 0; i < rowA; i++)
        for (int j = 0; j < colB; j++)
            for (int k = 0; k < colA; k++)
                C[i*colB + j] += A[i*colA + k] * B[k*colB + j];

    return false;
}

so, in the main script have something like this:所以,在主脚本中有这样的东西:

bool error = matrixMultiplication( /* parameters */ );
if ( error ) {
    std::cout << "invalid matrix dimensions" << std::endl;
    return 0;
}

It is generally a VERY BAD idea to hide memory allocations under routines in this way!以这种方式在例程下隐藏内存分配通常是一个非常糟糕的主意! Design the code better by separation of concerns.通过关注点分离更好地设计代码。 The scope of the routine is to multiply two matrices A and B , and store results to C .该例程的范围是将两个矩阵AB相乘,并将结果存储到C

Note:笔记:

  1. Make sure that all values under C are set to zero, in advance!提前确保C下的所有值都设置为零!

  2. We have a disagreement in our choices about this line: C[i*colB + j] += A[i*colA + k] * B[k*colB + j];我们对这一行的选择存在分歧: C[i*colB + j] += A[i*colA + k] * B[k*colB + j];

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

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