|Re: [eigen] generic argument declaration taking a matrix, vector or expression|
[ Thread Index |
| More lists.tuxfamily.org/eigen Archives
Christoph, thanks a lot for your hint. That solved the problem.
I agree with you that with the new declaration we lost type safety.
Effectively A, b and x now can be of a different scalar type each.
While it's fine in our case that the function is templated over
different scalar types, it should be required that all arguments are of
the same type. Also it would be nice if that shows up in the interface.
As of now, I guess this is not possible?
On 01/10/2012 10:55 PM, Christoph Hertzberg wrote:
On 11.01.2012 07:19, Bernhard Zeisl wrote:
If we understand the tutorial correctly, foo should look like
template <typename TypeT>
void foo (const Eigen::MatrixBase<TypeT> &mat);
where a const cast needs to be used to write data back to mat.
Yes, the const_cast is indeed not very nice. The problem is that you
can't pass a temporary object (such as A.col(i)) as a non-const
reference (unless you use C++11's RValue references).
Of course this bears the problem that you can also pass a const
Matrix<...> to foo and modify it -- which actually should be prohibited.
Another problem in my opinion is that you can't easily achieve type
safety of foo, i.e. if you only want to allow Matrices of a certain
size or scalar type (but you can achieve this by static assertions).
I attached a an example, which should make more clear what we are trying
You need to give every matrix a different template parameter, e.g.:
template <typename Type_A, typename Type_b, typename Type_x> void
solveLeastSquares (const Eigen::MatrixBase<Type_A> &A, const
Eigen::MatrixBase<Type_b> &b, Eigen::MatrixBase<Type_x> const &x_,
LEAST_SQUARES_METHOD method = SVD)
Maybe the documentation could point that out more explicitly.
Especially in the quoted tutorial parameters x and y should get
different template arguments (in the last two examples)