简体   繁体   English

使用向量的矩阵乘法

[英]Matrix multiplication using vector

I'm trying to multiply matrices. 我正在尝试将矩阵相乘。 This line A[i][j].emplace_back(i*j); 这行A[i][j].emplace_back(i*j); cause this error: 导致此错误:

IntelliSense: expression must have class type IntelliSense:表达式必须具有类类型

What's wrong with this code? 此代码有什么问题? I also try push_back() function. 我也尝试push_back()函数。

#include "stdafx.h"
#include <iostream>
#include <vector>

using namespace std;
using matrix = vector<vector<double>>;

matrix multiple(matrix A, matrix B);
void initialize(matrix& A, matrix& B);


int _tmain(int argc, _TCHAR* argv[])
{
    matrix A;
    matrix B;
    initialize(A, B);
    matrix C = multiple(A, B);
    return 0;
}

matrix multiple(matrix A, matrix B)
{
    matrix C;
    for (int i = 0; i < A.size(); ){
        for (int j = 0; j < B.size(); ){
            for (int k = 0; k < A.size();k++)
                C[i][j] += A[i][k] * B[k][j];
        }
    }
    return C;
}

void initialize(matrix& A, matrix& B)
{
    for (int i = 0; i < A.size(); i++){
        for (int j = 0; j < B.size(); j++){
            A[i][j].emplace_back(i*j);
            B[i][j].emplace_back(i*j);
        }
    }
}

A[i][j] is actually just a double, so it doesn't have any member function called emplace_back this is why you get the error you get. A[i][j]实际上只是一个double,因此它没有任何名为emplace_back成员函数,这就是为什么您得到错误的原因。

Because you already have the index already you can just place the element in directly: 因为您已经有了索引,所以可以直接将元素放入:

void initialize(matrix& A, matrix& B)
{
    for (int i = 0; i < A.size(); i++){
        for (int j = 0; j < B.size(); j++){
            A[i][j] = i*j;
            B[i][j] = i*j;
        }
    }
}

However there's more to it than this, this code won't compile, so always try to compile first and post all errors. 但是,除此之外,此代码将无法编译,因此请始终先编译并发布所有错误。 A and B are actually empty when they get passed to initialize here so A.size() and B.size() are both zero and hence no initialization actually occurs. A和B在传递给此处进行初始化时实际上为空,因此A.size()B.size()均为零,因此实际上没有初始化发生。

This is a fairly good example of a situation where you want to define your own datastructure for a matrix that contains the dimensional data. 这是一个很好的例子,说明了您要为包含维数据的矩阵定义自己的数据结构的情况。 This way you won't run into these problems that occur when the size of the dimensions gets lost. 这样,您就不会遇到尺寸丢失时出现的这些问题。

Perhaps something along the lines of: 也许是这样的:

struct Matrix{
Matrix(unsigned int rows, unsigned int cols):
    m_rows(rows),
    m_cols(cols)
{ 
    for(unsigned int i = 0: i < m_cols; ++i){
        m_data.push_back(vector<double>(m_rows));
    }
}

unsigned int m_rows;
unsigned int m_cols;
vector<vector<double>> m_data;
};

Alternatively you can use nested std::arrays along with a template containing the dimensions. 或者,您可以使用嵌套的std::arrays以及包含维度的模板。

Here is a template class so vector can be int or double 这是一个模板类,因此vector可以是int或double

template <class T>
std::vector <std::vector<T>> Multiply(std::vector <std::vector<T>> &a, std::vector <std::vector<T>> &b)
{
    const int n = a.size();     // a rows
    const int m = a[0].size();  // a cols
    const int p = b[0].size();  // b cols

    std::vector <std::vector<T>> c(n, std::vector<T>(p, 0));
    for (auto j = 0; j < p; ++j)
    {
        for (auto k = 0; k < m; ++k)
        {
            for (auto i = 0; i < n; ++i)
            {
                c[i][j] += a[i][k] * b[k][j];
            }
        }
    }
    return c;
}



std::vector <std::vector<double>> a (2, std::vector<double>(2));
a[0][0] = 1;
a[0][1] = 2;
a[1][0] = 3;
a[1][1] = 4;

std::vector <std::vector<double>> b(2, std::vector<double>(2));
b[0][0] = 5;
b[0][1] = 6;
b[1][0] = 7;
b[1][1] = 8;

auto c = Multiply(a, b);

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

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