Re: [eigen] Assigning row vector expression to VectorXd |
[ Thread Index |
Date Index
| More lists.tuxfamily.org/eigen Archives
]
- To: eigen@xxxxxxxxxxxxxxxxxxx
- Subject: Re: [eigen] Assigning row vector expression to VectorXd
- From: "Jean-Claude Monnin" <jc_monnin@xxxxxxxxxxxxx>
- Date: Wed, 14 Aug 2019 15:24:04 +0200
- Dkim-signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=emailplus.org; h=mime-version:message-id:in-reply-to:references:date:from:to :subject:content-type:content-transfer-encoding; s=fm1; bh=WB3pK vJtDdZyyeS+cF2Ueqw+5A8yW1PqlfHvuI0OYog=; b=pBRlA0JXnW0bzo+BOA3si TzV9g1SuE/P7zjDlCXmbcDqeztbVVCiND/rfR8ktLVsMFfJLSj/+E4Hy18mCohtI UkAdzs6wSKKqrJ54u6VqwfebUMcD4ZPRfIHST4y2pFRyxo689QpenBkXGVIFyFBB 1RvyE3HaXinQZjas6fwvMsJxepnuFu4s555+TwQHhFFXsSbk0WTbR4XqO9WwfctO o3gwATGY+D6tdB32loZ2bEmRmZYk/l+DCfNaMdYVTTd3k0KfbYkOHFabEehGi/YN +qPKQnOh1xbB48KToxnG39FYCMWm3DvK33Hz5ir9U74O8EWB+43+bUNzOKI/8ANa Q==
- Dkim-signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=content-transfer-encoding:content-type :date:from:in-reply-to:message-id:mime-version:references :subject:to:x-me-proxy:x-me-proxy:x-me-sender:x-me-sender :x-sasl-enc; s=fm3; bh=WB3pKvJtDdZyyeS+cF2Ueqw+5A8yW1PqlfHvuI0OY og=; b=Fm5/OT+1bBMEtEwT3xs3xLiea8eMDvjy6aJTr6BbmEnVA5gttl63hCo/+ Be1/XK08n8YTzlnc2bISyunUv7rioOh8lBtdrNAsjPbgytugOAVp9RtwyDVXjsh5 BbwibjRthBPv3OIHw3ncSOXd085VoRg6gpr/N764hsG3szZ0CbXOOhi7g+k+aKnJ ajivxdOrDKws0GezUDtJk3juNNkIc+cZUsguhVS4QTMzh9WFQ9OPq/4Hk9QU05FX 6iDm7E+rJCf6hZW4bnqYL6X5up4Jsk/XcOXbcasLQngOwzWYu/UaDtcUtq+IjnC1 GJsw4q2y3zSICJeo/4IoEVYenr6aA==
Hi Christoph,
Thanks for your reply.
I didn't realize the size mismatch was on the right hand side of the expression with q being a column vector. I wrongly assumed the problem was on the assignment. Therefore, following expression also works:
VectorXd v4 = (m.row(i) - m.row(j)).cwiseQuotient(q.transpose());
As you say, the assignments work in a consistent way, which makes much more sense.
Cheers,
Jean-Claude
> On 14 Aug 2019, at 14:04, Christoph Hertzberg <chtz@xxxxxxxxxxxxxxxxxxxxxxxx> wrote:
>
> Hi!
>
> This is not a problem of implicitly converting a row-vector to a column vector, but that you are mixing a column and a row-vector in the same expression. I.e., expressions like
>
> row.cwiseQuotient(col); row + col;
>
> much more likely are user-errors, so they are prohibited (this has nothing to do with assigning them). Also, for `row+col` what should be the dimensions of the result (should it result in a row or column)?
>
> If one of the dimensions is Dynamic, it is only possible to check this at run-time, since Dynamic could be 1 at runtime. And runtime checks are disabled when compiled with `-DNDEBUG` (or `-DEIGEN_NO_DEBUG`).
>
> Allowing to assign row-expressions to VectorXd was an early design-decision which can't be changed without breaking a lot of existing code. I would generally suggest to always make sure that dimensions match exactly. Adding a `.transpose()` does not add any runtime overhead (when compiled with optimizations enabled).
>
> I can't find the exact point where (or if) this behavior (the implicit transposition) is documented.
> Related: http://eigen.tuxfamily.org/bz/show_bug.cgi?id=1218
>
>
> Cheers,
> Christoph
>
>
> On 14/08/2019 11.30, Jean-Claude Monnin wrote:
>> Hi,
>> When assigning a row vector into VectorXd (eg. a column vector), it does an implicit transpose in most cases (like in the line `VectorXd tmp = m.row(i) - m.row(j);` below).
>> I'm wondering why it doesn't behave the same way in line `VectorXd v3 = (m.row(i) - m.row(j)).cwiseQuotient(q);`. This gives the the wrong result in release mode (but calls`abort` in debug mode due to size mismatch).
>> Why does the assignment of `v3` behaves differently than the assignment of `tmp` in the example below? Is this documented behavior?
>> Cheers,
>> Jean-Claude
>> {
>> using namespace Eigen;
>> MatrixXd m = MatrixXd::Random(10, 4);
>> VectorXd q = VectorXd::Random(m.cols());
>> Index i = 3;
>> Index j = 8;
>> VectorXd tmp = m.row(i) - m.row(j);
>> VectorXd v1 = tmp.cwiseQuotient(q);
>> VectorXd v2 = (m.row(i) - m.row(j)).transpose().cwiseQuotient(q);
>> VectorXd v3 = (m.row(i) - m.row(j)).cwiseQuotient(q);
>> std::cout << "v1 rows:" << v1.rows() << ", cols:" << v1.cols() << std::endl;
>> std::cout << "v2 rows:" << v2.rows() << ", cols:" << v2.cols() << std::endl;
>> std::cout << "v3 rows:" << v3.rows() << ", cols:" << v3.cols() << std::endl;
>> std::cout << "v1 = " << v1.transpose() << std::endl;
>> std::cout << "v2 = " << v2.transpose() << std::endl;
>> std::cout << "v3 = " << v3.transpose() << std::endl;
>> }
>> Output in release mode:
>> v1 rows:4, cols:1
>> v2 rows:4, cols:1
>> v3 rows:1, cols:1
>> v1 = -0.0375015 1.61562 -1.3097 -0.394064
>> v2 = -0.0375015 1.61562 -1.3097 -0.394064
>> v3 = -0.0375015
>
> --
> Dr.-Ing. Christoph Hertzberg
>
> Besuchsadresse der Nebengeschäftsstelle:
> DFKI GmbH
> Robotics Innovation Center
> Robert-Hooke-Straße 5
> 28359 Bremen, Germany
>
> Postadresse der Hauptgeschäftsstelle Standort Bremen:
> DFKI GmbH
> Robotics Innovation Center
> Robert-Hooke-Straße 1
> 28359 Bremen, Germany
>
> Tel.: +49 421 178 45-4021
> Zentrale: +49 421 178 45-0
> E-Mail: christoph.hertzberg@xxxxxxx
>
> Weitere Informationen: http://www.dfki.de/robotik
> -------------------------------------------------------------
> Deutsches Forschungszentrum für Künstliche Intelligenz GmbH
> Trippstadter Strasse 122, D-67663 Kaiserslautern, Germany
>
> Geschäftsführung:
> Prof. Dr. Jana Koehler (Vorsitzende)
> Dr. Walter Olthoff
>
> Vorsitzender des Aufsichtsrats:
> Prof. Dr. h.c. Hans A. Aukes
> Amtsgericht Kaiserslautern, HRB 2313
> -------------------------------------------------------------
>
>