简体   繁体   English

矩阵和向量乘法

[英]matrix and vector multiplication

Suppose we have 3*3 matrix like this:假设我们有这样的 3*3 矩阵:

1 3  4
2 6 8
9 0 12

And some vector like this:还有一些像这样的向量:

1   2   3 

My question is: how to implement it so that I could multiply one by another?我的问题是:如何实现它以便我可以乘以另一个? I have example code:我有示例代码:

#include <cstdlib>
#include <math.h>
#include <iostream>

using namespace std;

int main(int argc, char *argv[])
{
    int a[3][3]={{ 2,4,3},{1,5,7},{0,2,3}};
    int b[]={2,5,6};
    int c[3];

    for (int i=0;i<3;i++){
         c[i]=0;
    }

    for (int i=0;i<3;i++){
        for (int j=0;j<3;j++){
            c[i]+=( a[i][j]*b[j]);
        }
    }

    for (int i=0;i<3;i++){
        cout<<a[i]<<"  "<<endl;
    }

    system("PAUSE");
    return EXIT_SUCCESS;
}

The result I got is:我得到的结果是:

0x22ff10
0x22ff1c
0x22ff28

Change:改变:

 for (int i=0;i<3;i++){
      cout<<a[i]<<"  "<<endl;

to:到:

 for (int i=0;i<3;i++){
      cout<<c[i]<<"  "<<endl;

我认为您想在最后一个循环中打印c[i] ,而不是a[i]

Design an object?设计一个对象? Here's some pseudo code to get you started:这里有一些伪代码可以帮助您入门:

// matrix of ints, floats, doubles, whatever numeric type you want
template<typename T>
class Matrix
{
public:
   Matrix(int rows, int cols)
   {
      // init m_values to appropriate rows and cols
   }

   Matrix<T> operator+(const Matrix<T>& rhs)
   {
       // add this matrix to the rhs matrix
   }

   Matrix<T> operator*(const Matrix<T>& rhs)
   {
       // verify both matrices have same dimensions (3x3 etc)
       // multiple this matrix by rhs by indexing into m_values
   }

   // etc

private:
   // two dimensional dynamic array of type T values
   std::vector<std::vector<T>> m_values; 
};

You could also make non-member template function to perform operations.您还可以使用非成员模板函数来执行操作。 If you want it to be fancy I would make a class representing a Row that has values, equality, row operation, etc. Then make a Matrix class in terms of a vector of rows.如果你希望它是看上我会做代表的一类Row有价值观,平等,行操作等,然后做一个Matrix类中的行向量的条款。

#include<iostream>
#include<math.h>
using namespace std;
int main()
{
    int a[3][3]={{ 1,2,3},{4,5,6},{7,8,9}};
    int b[]={1,2,3};
    int c[3];

    for (int i=0;i<3;i++)
    {
         c[i]=0;
    }

    for (int i=0;i<3;i++)
    {
        for (int j=0;j<3;j++)
        {
            c[i]+=(a[i][j]*b[j]);
        }
    }

    for (int i=0;i<3;i++){
        cout<<c[i]<<"  "<<endl;
    }
    system ("pause>0");

}

anther formula in parallelism:平行的花药公式:

 #include <iostream>
 #include<vector>
 #include<ctime>
 #include<omp.h>

    using namespace std;

const int matSize = 3;
int mat[matSize][matSize];

int main()
{
    clock_t t1, t2;
    double wt1, wt2;

    vector<int> vec;

    for (int i = 0; i < matSize; i++)
    {
        for (int j = 0; j < matSize; j++)
        {
            mat[i][j] = i + j;
        }
    }

    for (int i = 0; i < matSize; i++)
    {
        vec.push_back(i);
    }

    long sum;
    vector<int> res;

    t1 = clock();
    wt1 = omp_get_wtime();
    #pragma omp parallel for reduction(+:sum)
    {
        for (int i = 0; i < matSize; i++)
        {
            sum = 0;
            for (int j = 0; j < matSize; j++)
            {
                sum += mat[i][j] * vec[j];
            }
            res.push_back(sum);
        }
    }
    t2 = clock();
    wt2 = omp_get_wtime();


    cout << "the matrix " << endl;
    for (int i = 0; i < matSize; i++)
    {
        for (int j = 0; j < matSize; j++)
        {
            cout << mat[i][j] << "  ";
        }
        cout << endl;
    }

    cout << endl << "the vector " << endl;
    for (int i = 0; i < vec.size(); i++)
    {
        cout << vec[i] << endl;
    }

    cout << endl << "the result " << endl;
    for (int i = 0; i < res.size(); i++)
    {
        cout << res[i] << endl;
    }

    cout << "CPU time " << double(t2 - t1) / CLOCKS_PER_SEC << endl;
    cout << "wTime " << wt2 - wt1 << endl;

    return 0;
}

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

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