矩阵和向量乘法

时间:2011-09-16 17:42:04

标签: c++ multidimensional-array

假设我们有这样的3 * 3矩阵:

1 3  4
2 6 8
9 0 12

还有一些这样的矢量:

1   2   3 

我的问题是:如何实现它以便我可以相互繁殖?我有示例代码:

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

我得到的结果是:

0x22ff10
0x22ff1c
0x22ff28

5 个答案:

答案 0 :(得分:5)

变化:

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

为:

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

答案 1 :(得分:3)

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

答案 2 :(得分:2)

设计一个对象?这里有一些伪代码可以帮助您入门:

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

您还可以使用非成员模板功能来执行操作。如果你想要它是花哨的,我会创建一个代表Row的类,它具有值,相等,行操作等。然后根据行向量创建一个Matrix类。

答案 3 :(得分:1)

#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");

}

答案 4 :(得分:0)

并行的另一个公式:

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