c++ - 以不同的方式将两个矩阵相乘 - 不知道该怎么做

标签 c++ arrays algorithm matrix

作为作业,我有一个听起来像这样的问题:

We have a n*n square matrix. It is called 'subdiagonal'
if all the elements above the main diagonal are null.

a) Copy the useful elements (the ones which are not null, so basically all the elements
 from the main diagonal and below) to an array. (I've done that)

b) Write an algorithm which takes two subdiagonal matrix A, B as an input.
 Those are transformed into arrays V_a and V_b with the algorithm from a),
 then they calculate C = A*B only using only V_a and V_b
例如
Let's say A =
1 0 0 0 0
2 3 0 0 0
4 1 3 0 0
1 9 0 2 0
1 0 1 2 2
B =
2 0 0 0 0
1 1 0 0 0
0 1 2 0 0
1 1 2 3 0
2 0 0 1 2

after this input, V_a = 1,2,3,4,1,3,1,9,0,2,1,0,1,2,2; V_b = 2,1,1,0,1,2,1,1,2,3,2,0,0,1,2

and the product V_c will be 2,7,3,9,4,6,13,11,4,6,8,3,6,8,4

so the matrix will look like 
2   0  0  0  0
7   3  0  0  0
9   4  6  0  0
13  11  4  6  0
8   3  6  8  4
这是我已经工作了一段时间的代码:
#include <iostream>
#include <algorithm>

void read(int& a, int**& matrix)
{
    std::cin >> a;
    matrix = new int*[a];
    for (int i = 0; i < a; i++)
    {
        for (int j = 0; j < a; j++)
        {
            matrix[i] = new int[a];
        }
    }
    for (int i = 0; i < a; i++)
    {
        for (int j = 0; j < a; j++)
        {
            std::cin >> matrix[i][j];
        }
    }
}

void showMatrix(int a, int** matrix)
{
    for (int i = 0; i < a; i++)
    {
        for (int j = 0; j < a; j++)
        {
            std::cout << matrix[i][j] << " ";
        }
        std::cout << std::endl;
    }
}

void showArray(int a, int* array)
{
    for (int i = 0; i < a; i++)
    {
        std::cout << array[i] << " ";
    }
}

void createArray(int a, int& arrayLength, int** matrix, int*& array)
{
    int nrDeElemente = a*a - (a * (a - 1)) / 2;
    array = new int[nrDeElemente+1];
    arrayLength = 0;
    for (int i = 0; i < a; i++)
    {
        for (int j = 0; j < i+1; j++)
        {
            array[arrayLength++] = matrix[i][j];
        }
    }
}

int* multiplyArrays(int a, int arrayLength, int* array1, int* array2)
{
    int* array3 = new int[arrayLength + 1];
    for (int i = 0; i < a; i++)
    {
        array3[i] = 0;
    }
    int t = 1;
    for (int i = 0; i < arrayLength; ++i)
    {
        for (int j = 0; j < t; ++j)
        {
            for (int p = j; p < a; p++)
            {
                array3[i] += array1[j] * array2[p];
            }
        }
        ++t;
    }
    return array3;
}

int main()
{
    int **matrix1, **matrix2;
    int *array1, *array2, *multiplyResult;
    int a, arrayLength;
    read(a, matrix1);
    read(a, matrix2);
    createArray(a, arrayLength, matrix1, array1);
    createArray(a, arrayLength, matrix2, array2);
    multiplyResult = multiplyArrays(a, arrayLength, array1, array2);
    showArray(arrayLength, multiplyResult);
}
我已经做了 a),但我不知道怎么做 b)
我想我在概念上理解了它(经过数小时的试验),但我真的不知道如何实现它。
我需要 3 个 for 循环,例如:
->最外层必须负责我们在新数组上计算的位置
->下一个必须选择第二个数组中的哪些元素将相乘。 (选择乘数)这是其中之一
我不知道如何实现的循环。当行(来自矩阵)结束时,它必须以某种方式停止,并从它停止的位置开始 + 1 个元素。
-> 最内层必须选择乘法的第二项(被乘数)。
我也不知道我应该如何实现这个。它应该选择与乘数一样多的元素,而且循环很奇怪(因为我每次都需要从同一行中选择所有元素)。
有人可以帮我解决b点并解释他们的想法吗?
我挣扎了很多,我真的觉得我需要帮助。
顺便说一句,来自multiplyArrays的3个for循环没有意义,你可以写其他东西而不是它们。这 3 个 for 循环基本上是我的程序需要的唯一东西(我认为)。
谢谢 :)

最佳答案

矩阵乘法C = A*BC(i,j) = sum_k A(i,k)*B(k,j) 定义.矩阵A具有结构非零值,其中 i >= k , 和 B ,其中 k >= j .因此迭代就足够了

  • (外循环)i来自 0n-1
  • (中)j来自 0i
  • (内)k来自 ji .

  • 另一块是车削坐标(i,j)相对于一维存储格式的偏移量。第一个 i 中结构非零的数量行由 i 给出第 3 个三角形数,(i+1)*i/2 .因此 j此行中的第 th 个元素位于(从零开始的)索引 (i+1)*i/2 + j .您或您的编译器可以强度减少乘法。

    关于c++ - 以不同的方式将两个矩阵相乘 - 不知道该怎么做,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66454003/

    相关文章:

    c++ - CUDA 条件线程同步

    PHP/C++ : shm_open() error when sharing memory

    java - 从用户输入中找到最常见的词

    javascript - 需要 React 帮助 - 索引增加时组件不更新

    algorithm - 最长重复子串问题

    c++ - 为什么 ofstream 将我未包含的值写入我的文件?

    c++ - 为什么我不能使外部类型看起来像 C++ 中的内部类型?

    javascript - Node.JS 递归函数错误变量

    algorithm - 汇编程序通过问题

    php - 交友网站算法