2017-06-14 173 views
-1

我想使用清除函数与随机生成的值做一个矩阵乘法。因此,我希望使用函数(mat_def)来生成矩阵,而另一个函数(mat_mul)在矩阵作为参数发送时将它们相乘。矩阵 - 返回并传递参数C++

#include <iostream> 
#include <stdio.h> 
#include <stdlib.h> 
#include <time.h> 

using namespace std; 

double mat_def(int n) //how to return the matrix 
{ 
    double a[n][n]; 

    double f; 

    for(int i=0; i<n; i++) 
    { 
     for(int j=0; j<n; j++) 
     { 
      f= rand(); 
      cout<<f ; 
      a[i][j]=f; 
     } 

    } 

    return 0; 
} 


double mat_mul(int n, double a[n][n], double b[n][n]) //how to send matrix as parameter 
{ 
    return 0; 
} 


int main() 
{ 
    /* initialize random seed: */ 
    srand (time(NULL)); 
    mat_def(10); 
} 
+0

你的问题是什么?你想知道如何增加两个矩阵或其他东西? –

+0

在C++中实现Matrix类有很多种方法。通常,查找重载':: operator []'并返回一个内部类的引用,该类也重载':: operator []',这样你就可以拥有真正的Matrix语法。 – jiveturkey

+1

问题:在编译时已知矩阵的大小吗? 矩阵是否仅仅是方矩阵? –

回答

2

这是一个很好的标准C++矩阵模板。

Matrix.h

#include <vector> 

class Matrix 
{ 
    class InnerM 
    { 
     private: 
      int ydim; 
      double* values; 

     public: 
      InnerM(int y) : ydim(y) 
      { 
       values = new double[y]; 
      } 

      double& operator[](int y) 
      { 
       return values[y]; 
      } 
    }; 

    private: 
     int xdim; 
     int ydim; 

     std::vector<InnerM> inner; 

    public: 

     Matrix(int x, int y) : xdim(x), ydim(y), inner(xdim, InnerM(ydim)) 
     { 
     } 

     InnerM& operator[](int x) 
     { 
      return inner[x]; 
     } 
}; 

所有的内存泄漏在那里为你,但你的想法。从这里你可以在矩阵类中通过过滤::operator*()来处理乘法。

+1

班是正确的路,但有一些改进的余地。 'InnerM'需要析构函数。现在它泄漏。一旦解决了问题,您可能会注意到它不符合[三项规则](https://stackoverflow.com/questions/4172722/what-is-the-rule-of-ree),并且可能导致喜剧之后的错误。到目前为止,这两个问题的最佳解决方案是另一个“std :: vector”。最后一项改进是性能调整问题。通过使用包含动态分配的“vector”,您将失去存储的连续性,并轻松缓存友好性。 – user4581301

+0

绝对如我所说,我只是吐出一些东西。 – jiveturkey

1

我假设你的问题是定义二维数组,然后将它传递给mat_mul函数来乘以矩阵。其余的将会非常简单。

定义2-d阵列(考虑存储器的需要在运行时知道):

int rows,cols; 
    cin >> rows; 
    cin >> cols; 

    int **arr = new int*[rows];   // rows X cols 2D-array 
    for(int i = 0; i < rows; ++i) { 
     arr[i] = new int[cols]; 
    } 

可以定义另一个2-d阵列完全相同的方式与所需的行和列。

现在,跑过2-d阵列起作用:

void mat_mul(int **arr1, int **arr2, int m, int n, int p, int q){ 
    //define a 2-D array to store the result 
    //do the multiplication operation 
    //you could store the result in one of the two arrays 
    //so that you don't have to return it 
    //or else the return type should be modified to return the 2-D array 
} 

例如:

void display(int **arr, int row, int col){ 
    for (int i=0; i<row; i++){ 
     for(int j=0;j<col; j++){ 
      cout << arr[i][j] << '\t'; 
     } 
     cout << endl; 
    } 
} 

删除存储如果不是用下面的语法不再需要:

for(int i=0; i<rows; i++){ 
    delete[] array[i]; 
} 
delete[] array; 

希望这将足以完成您的工作!

关于如何返回SO上的二维数组已经有了答案。检查下面的链接。

https://stackoverflow.com/a/8618617/8038009

+0

不要对矩阵使用数组。它很慢,很烦人,并且会丢弃程序员的所有内存管理。至少在课堂上包装它。 – user4581301

2

返回原始分配是一个吸盘的赌注。您需要管理自己分配的所有内存,并使用矩阵大小参数传递它。

为什么要受苦?采用了矩阵类

template<class Type> 
class Matrix{ 
    int rows; 
    int cols; 
    std::vector<type> data; 
public: 
    Matrix(int row, int col):rows(row), cols(col), data(rows*cols) 
    { 
     // does nothing. All of the heavy lifting was in the initializer 
    } 
    // std::vector eliminates the need for destructor, assignment operators, and copy 
    //and move constructors. 
    //add a convenience method for easy access to the vector 
    type & operator()(size_t row, size_t col) 
    { 
     return data[row*cols+col]; 
    } 
    type operator()(size_t row, size_t col) const 
    { 
     return data[row*cols+col]; 
    } 
}; 

用法是

Matrix<double> mat_mul(const Matrix<double> &a, const Matrix<double> &b) 
{ 
    Matrix<double> result; 
    // do multiplication 

    return result; 
} 

int main() 
{ 
    /* initialize random seed: */ 
    srand (time(NULL)); 
    Matrix<double> matA(10, 10); 
    matA(0,0) = 3.14; // sample assignment 
    matA(9,9) = 2.78; 
    double x = matA(0,0) * matA(9,9) 
    Matrix<double> matB(10, 10); 

    Matrix<double> matC = mat_mul(matA, matB) ; 
} 

更多的功能,如从initializer list建设,可被添加到类,让您的生活更轻松。如果您选择,您还可以指定operator *过载Matrix并使用该过载代替mat_mul。阅读Operator overloading以了解更多信息。