This forum has been archived. All content is frozen. Please use KDE Discuss instead.

Reference to transpose

Tags: None
(comma "," separated)
tflynn
Registered Member
Posts
3
Karma
0

Reference to transpose

Mon Mar 24, 2014 7:49 am
Hey

I need to do a matrix-vector multiply, using the matrix or it's transpose depending on some condition, and I wanted to hide this test so I came up with: (where my_c is an instance of class c containing some matrices, and v is a vector)
Code: Select all
x = my_c.get_m()*v


the implementation of c::get_m():
Code: Select all
const MatrixXf & c::get_m(){
  if (cond)
    return m;
  else
    return m.transpose();
}


This works OK. My first question is if this code will be good and do some kind of implicit transposition. The next thing is, I also want to sometimes modify either the matrix or its transpose depending on a condition i.e. do something like
Code: Select all
my_c.get_m() += /* matrix expression */


But I found that when I alter get_m to instead return a regular, non-const, reference I get an error
Code: Select all
error: initial value of reference to non-const must be an lvalue


Is there some way I can return a non-constant reference to the transpose?

Thanks in advance for any insights
User avatar
ggael
Moderator
Posts
3447
Karma
19
OS

Re: Reference to transpose

Mon Mar 24, 2014 8:01 am
MatrixXf is a column major matrix, so when you do return m.transpose();, m.transpose() will be copied into a temporary. If "cond" can be known at compile time, then you can use template meta programming to return a const MatrixXf& or a Transpose<const MatrixXf> depending on "cond".
tflynn
Registered Member
Posts
3
Karma
0

Re: Reference to transpose

Tue Apr 01, 2014 4:21 am
OK Thanks for the help


Bookmarks



Who is online

Registered users: Baidu [Spider], Bing [Bot], Google [Bot]