Re: [eigen] generic argument declaration taking a matrix, vector or expression |
[ Thread Index |
Date Index
| More lists.tuxfamily.org/eigen Archives
]
- To: eigen@xxxxxxxxxxxxxxxxxxx
- Subject: Re: [eigen] generic argument declaration taking a matrix, vector or expression
- From: Benoit Jacob <jacob.benoit.1@xxxxxxxxx>
- Date: Wed, 11 Jan 2012 19:24:30 -0500
- Dkim-signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :content-type; bh=S/hUIufjwTJ4rhBREba1E6bROO8NYOqRJ3w/moL42Gs=; b=vLmpVDh8B4CIl7dAHTw7fOzleoE+gb1Ulg5vuq8MbbUpYmjDFppO8c1AAoJkgE2duL BjKEzBOTnV/tCIVdF9zpo1uy+Y3tJjhVOwJrvXR7m4RiFgPbuPuzAW8uW9+zV+QGSU1B CNBhwFq8G1RyolkEFzupTckejLsFecgO1OvPI=
2012/1/11 Bernhard Zeisl <bzeisl@xxxxxxxxxxxxxxxx>:
> 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?
As Christoph said, the only way to enforce that is by a static
assertion, and that won't show up in the interface (if by interface
you mean prototype). "Concepts" were supposed to make that possible
but they got dropped from c++11, sadly.
Benoit
>
> Best, Bernhard
>
>
>
> 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
>>> to achieve.
>>
>>
>> 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)
>>
>> HTH
>> Christoph
>>
>>
>>
>
>