Re: [eigen] Documenting functions which return an Matrix |

[ Thread Index | Date Index | More lists.tuxfamily.org/eigen Archives ]

*To*: eigen@xxxxxxxxxxxxxxxxxxx*Subject*: Re: [eigen] Documenting functions which return an Matrix*From*: 何震邦 <jdh8@xxxxxxxxxxxxxx>*Date*: Thu, 14 Feb 2013 13:16:28 +0800

You can also use a functor. If you are trying to manipulate different sizes of matrices, I suggest you use MatrixBase, the base class of dense matrix expression. We use CRTP to implement static polymorphism. For example, MatrixXd derives from MatrixBase<MatrixXd>. template<typename ResultType> struct my_functor { static ResultType run(const MatrixBase<Derived> &_A) const { const Derived &A = _A; // play with A; // return something; } } Later, you can use it like: MatrixXd A, B; A = my_functor<Matrix<double, 3, 6, RowMajor> >::run(B); 在 2013 二月 13 週三 12:52:35，Dale Lukas Peterson 寫道: > I have some functions which need to return Matrices of specific sizes > and I would like clients of the class to see at a glance what size > those matrices are. I have been doing things like: > > Matrix<double, 3, 6, RowMajor> my_func(); > > then in client code: > > a = my_func(); > > The reason I am declaring the function this way is primarily so users > who see it know what size matrix they are getting back. Is this the > preferred way to do this? > > Some of my matrices are small (3x3), and some are bigger (20x20). > Inside of my_func(), I typically declare a matrix on the heap to do > the necessary calculations and populate the matrix, and then return > it. I am fine with all things being on the heap, but it would be nice > to know that I'm not copying things unnecessarily because of the way > I've declared the function. Passing const references is the best way to send invariant matrices into a function. :) > I realize I can require clients to pass a const reference to a Matrix > of their choosing (Dynamic or Fixed) as a function parameter, rather > than using return, but the syntax of operator= is nicer and I don't > really want to give up that syntax. > > 1) How do people document their functions that return matrices of > known size, even if those matrices are allocated on the heap? > > 2) I see four cases that I'm not 100% clear on what happens when I do > something like a = my_func(): > > a) a is a dynamic sized Matrix and my_func returns a fixed size Matrix. > b) a is a dynamic sized Matrix and my_func returns a dynamic size Matrix. > c) a is a fixed sized Matrix and my_func returns a fixed size Matrix. > d) a is a fixed sized Matrix and my_func returns a dynamic size Matrix. It is quite safe becuase my_func() evaluates into a temporary object. > Can anybody clarify this for me and give me recommendations on how to > make it clear to users what the size of the matrices are without > shooting myself in the foot? > > Thanks, > Luke I hope this helps you. ;) Regards, Chen-Pang

**References**:**[eigen] Documenting functions which return an Matrix***From:*Dale Lukas Peterson

**Messages sorted by:**[ date | thread ]- Prev by Date:
**[eigen] Documenting functions which return an Matrix** - Next by Date:
**Re: [eigen] Using Eigen in CUDA kernels** - Previous by thread:
**[eigen] Documenting functions which return an Matrix** - Next by thread:
**[eigen] Pass POD by reference or by pointer?**

Mail converted by MHonArc 2.6.19+ | http://listengine.tuxfamily.org/ |