446,194 Members | 859 Online
Need help? Post your question and get tips & solutions from a community of 446,194 IT Pros & Developers. It's quick & easy.

# matrix copy problem

 P: n/a I have a problem to copy (assign) a matrix to another matrix. Curreny, I know copy the number using loops, while it will take some time, I wonder if there have faster method. The following code explain my situation detailed. double ** matrixa, **matrixb; int nrow = 10, mcol = 10; matrixa = initmatrix(nrow, mcol); // allocate memory a matrixb = initmatrix(nrow, mcol); // allocate memory b // copy a => b for (int i=0;i b without loop all the data. Thanks Jul 23 '05 #1
4 Replies

 P: n/a "Yudan Yi" wrote...I have a problem to copy (assign) a matrix to another matrix. Curreny, Iknow copy the number using loops, while it will take some time, I wonder ifthere have faster method. The following code explain my situation detailed. double ** matrixa, **matrixb; int nrow = 10, mcol = 10; matrixa = initmatrix(nrow, mcol); // allocate memory a matrixb = initmatrix(nrow, mcol); // allocate memory b // copy a => b for (int i=0;i b without loop all the data. See above. HTH V Jul 23 '05 #2

 P: n/a Yudan Yi wrote: I have a problem to copy (assign) a matrix to another matrix. Curreny, I know copy the number using loops, while it will take some time, I wonder if there have faster method. The following code explain my situation detailed. double ** matrixa, **matrixb; int nrow = 10, mcol = 10; matrixa = initmatrix(nrow, mcol); // allocate memory a matrixb = initmatrix(nrow, mcol); // allocate memory b // copy a => b for (int i=0;i b without loop all the data. Thanks For matrices, you are better off by far using a single array and addressing it as matrix[ncols * i + j]. You get better data locality in the cache, and you don't need a loop of malloc/new every time you want to make a copy. With modern compilers & processors, the cost of the multiply is usually either optimized away completely or irrelevant (a multiply is a cycle or so, a memory lookup is at least a few, more likely a few dozen, cycles). If you use something like BLAS (http://www.netlib.org/blas) to do the underlying arithmetic, then even this cost disappears. Versus using nested arrays requires an additional indirection which may be quite expensive. Try wrapping this in a class as well, eg class Matrix { public: Matrix() : nrows_(0), ncols_(0), Data_(0) {} Matrix(int nrows, int ncols) : nrows_(nrows), ncols_(ncols), Data_(new double[nrows_ * ncols_]) {} Matrix(Matrix const& Other) // fill in boring details Matrix& operator=(Matrix const& Other); // fill in boring details ~Matrix() { delete[] Data_; } double operator()(int i, int j) { return Data_[i * ncols + j]; } // plus some functions to get the size, etc private: int nrows_, ncols_; double* Data_; }; With a bit of thought, you can do operator[] too, so you can use the 'old' notation. Hint: return a pointer to the start of the row of the matrix. But the operator() solution is easier, especially becuase it is trivial to add some debug assert() statements to get bounds checking. That can potentially save you an unbelievable amount of time, as can wrapping all of the ugly memory management stuff inside a class. By the way, for builtin types like double, you can use memcpy() to implement the copy constructor and copy-assignment. Another advantage of the single-array approach: you only need a single memcpy, versus a loop of them for the multi-array approach. But don't do this for arrays of user-defined types! HTH, Ian McCulloch Jul 23 '05 #3

 P: n/a Yudan Yi wrote: I have a problem to copy (assign) a matrix to another matrix. Curreny, I know copy the number using loops, while it will take some time, I wonder if there have faster method. The following code explain my situation detailed. double ** matrixa, **matrixb; int nrow = 10, mcol = 10; matrixa = initmatrix(nrow, mcol); // allocate memory a matrixb = initmatrix(nrow, mcol); // allocate memory b // copy a => b for (int i=0;i b without loop all the data. Thanks Use a matrix class ... (I posted the one below about a year ago) #include template class matrix { public: typedef int t_Size; t_Size m_columns; t_Size m_rows; std::vector m_data; matrix( t_Size i_columns = 0, t_Size i_rows = 0 ) : m_columns( i_columns ), m_rows( i_rows ), m_data( i_columns * i_rows ) { } w_elem_type * operator[]( t_Size i_index ) { return & ( m_data[ i_index * m_rows ] ); } template matrix( const w_Type (&i_array)[w_columns][w_rows] ) : m_columns( w_columns ), m_rows( w_rows ), m_data( & (i_array[0][0]), & (i_array[w_columns-1][w_rows]) ) { } }; #include double array[3][4] = { { 1.0, 2.0, 3.3, 4.4 }, { 1.0, 2.0, 3.3, 4.4 }, { 1.0, 2.0, 3.3, 4.5 }, }; int main() { matrix mat1( 3, 4 ); matrix mat2; matrix mat3( array ); mat2 = mat3; std::cout << mat2[2][3] << "\n"; } Jul 23 '05 #4

 P: n/a Yudan Yi wrote: I have a problem to copy (assign) a matrix to another matrix. Curreny, I know copy the number using loops, while it will take some time, I wonder if there have faster method. The following code explain my situation detailed. double ** matrixa, **matrixb; int nrow = 10, mcol = 10; matrixa = initmatrix(nrow, mcol); // allocate memory a matrixb = initmatrix(nrow, mcol); // allocate memory b // copy a => b for (int i=0;i b without loop all the data. Take a look at The C++ Scalar, Vector, Matrix and Tensor class Library http://www.netwood.net/~edwin/svmtl/ The Object Oriented Numerics Page http://www.oonumerics.org/oon/ and Bjarne Stroustrup, "The C++ Programming Language: Third Edition", Chapter 22 Numerics, Section 4 Vector Arithmetic, Subsection 6 Slice_array, pages 671-5 Jul 23 '05 #5

### This discussion thread is closed

Replies have been disabled for this discussion.