template meta programming - Implementing the A(:,k)=b; Matlab-like syntax in a C++ matrix library -
i have developed expression templates-based c++ matrix class of own. have overloaded ()
operator can read or write element matrices as, example,
cout << a(i,j) << endl;
and
a(i,j)=b;
respectively. have implemented range
class enable matlab-like reads as
cout << a(range(3,5),range(0,10)) << endl;
the template matrix
class exemplified as
template <typename outtype> class matrix { private: int rows_; //number of rows int columns_; //number of columns outtype *data_; //row-major order allocation public: // --- access operators inline outtype & operator()(const int i, const int j) { return data_[idx2r(i,j,getcolumns())]; } inline outtype operator()(const int i, const int j) const { return data_[idx2r(i,j,getcolumns())]; } // --- subexpressions - range range inline expr<submatrixexpr<const outtype*,outtype>,outtype> operator()(range range1, range range2) { typedef submatrixexpr<const outtype*,outtype> sexpr; return expr<sexpr,outtype>(sexpr(data_,rows_,columns_,range1.numelements_,range2.numelements_,range1.start_,range1.step_,range2.start_,range2.step_), range1.numelements_,range2.numelements_); } }
i enable matlab-like assignments as
a(range(3,5),range(0,10))=b;
where b
appropriate matrix.
i think that, achieve matlab-like syntax above, 2 possibilities be
- overloading
()
operator, returns array of pointers, , overloading=
operator latter act between array of pointers ,matrix
; - exploit performed overload of
()
operator indicated above , overloading=
operator latter act between expression ,matrix
.
maybe first option not convenient, especilly large matrices. correct? there other more efficient/effective possibilities using perhaps more sophisticated c++ features (e.g., move semantics)?
thank help.
i think best bet have non-const version of operator()(range, range)
return proxy object has overloaded assignment operator knows how assign range (back original matrix example).
Comments
Post a Comment