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

RowVector / MatrixX3

Tags: None
(comma "," separated)
miker
Registered Member
Posts
5
Karma
0

RowVector / MatrixX3

Wed Mar 12, 2014 2:26 pm
Hi

I'm writing some software that uses MatrixX3d and RowVector3d to represent Nx3 data where sometimes I want to constraint the data to only be 1xN.

My problem is that functions like this:

Code: Select all
void ConvYUV2RGB(const MatrixX3d &YUV, MatrixX3d &RGB) const;


allow the first argument to be either a RowVector3d or a MatrixX3d, but the second argument won't compile with a RowVector3d argument passed in.
I'm doing a lot of this:
Code: Select all
RowVector3d singleRow;
MatrixX3d singleRowM;
ConvYUV2RGB(YUV,singleRowM);
singleRow= singleRowM.row(0);

which is really annoying.

My functions always just perform per row operations or Nx3 \times 3xM matrices so will all work in the case of N==1


What's the easiest way to allow my function to compile with both types without copying. I've started templating the code. but i have to put a whole load of includes in my headers because the whole implementation has to be avaliable and I don't wnat to do that. I could use MatrixX3d as my only type, but sometimes the data really has to be just one row and Id like that enforced at compile time. Can I cast my rowvectors to Matrices of size 1,3 without triggering a copy?

Ideas are welcome.
User avatar
ggael
Moderator
Posts
3447
Karma
19
OS

Re: RowVector / MatrixX3  Topic is solved

Wed Mar 12, 2014 8:28 pm
To get high performance in the single-row case you could have to overload that would call a generic templated version (only needed in the cpp file). Or, if you can lost some performance you should be able to use a Ref<MatrixX3d> for the RBG parameter.
miker
Registered Member
Posts
5
Karma
0

Re: RowVector / MatrixX3

Thu Mar 13, 2014 12:19 pm
Oh yeah, templated functions in the cpp - I didn't think of that.
miker
Registered Member
Posts
5
Karma
0

Re: RowVector / MatrixX3

Thu Mar 13, 2014 12:20 pm
The Ref<MatrixX3d> method deosn't work BTW - thats one I tried already. It won't compile when you pass RowVector3d into the functions.
User avatar
ggael
Moderator
Posts
3447
Karma
19
OS

Re: RowVector / MatrixX3

Thu Mar 13, 2014 2:40 pm
oh, I see. When storage does not match, Ref<> cannot work without any copy. However, you are in a degenerate case that could work. We'll fix it.
User avatar
ggael
Moderator
Posts
3447
Karma
19
OS

Re: RowVector / MatrixX3

Thu Mar 13, 2014 5:06 pm
Fixed:

https://bitbucket.org/eigen/eigen/commits/81b83819b1b8/
Changeset: 81b83819b1b8
User: ggael
Date: 2014-03-13 18:04:19
Summary: Relax Ref such that Ref<MatrixXf> accepts a RowVectorXf which can be seen as a degenerate MatrixXf(1,N)


https://bitbucket.org/eigen/eigen/commits/1f063cc98b8c/
Changeset: 1f063cc98b8c
Branch: 3.2
User: ggael
Date: 2014-03-13 18:04:19
Summary: Relax Ref such that Ref<MatrixXf> accepts a RowVectorXf which can be seen as a degenerate MatrixXf(1,N)
(grafted from 81b83819b1b8c4a92d52115d9f22bbb76c34e4a3)
miker
Registered Member
Posts
5
Karma
0

Re: RowVector / MatrixX3

Mon Mar 17, 2014 9:03 pm
Fantastic - Thanks.

One last question - I'm using your suggestion of templated functions in the cpp file like this:

Code: Select all
  template<typename X3Type>
  void ConvRGB2Lab(const Eigen::MatrixBase<X3Type> &RGB, RGBSpec RGB_spec, Eigen::MatrixBase<X3Type> &Lab)
  {
    Eigen::MatrixBase<X3Type> xyz;
    ConvRGB2XYZ(RGB,RGB_spec,xyz);
    ConvXYZ2Lab(xyz,Lab);
  }


But this doesn't compile due to the Eigen::MatrixBase<X3Type> temporary. I'm not sure how to declare a variable with the same type as the templated MatrixBase. Can you help me with the syntax? THe only docs I can find are here, but I can't work out the exact syntax i need.

http://eigen.tuxfamily.org/dox/TopicFunctionTakingEigenTypes.html

Code: Select all
template <typename Derived, typename OtherDerived>
void cov(const MatrixBase<Derived>& x, const MatrixBase<Derived>& y, MatrixBase<OtherDerived> const & C)
{
[b]typedef typename Derived::Scalar Scalar;
typedef typename internal::plain_row_type<Derived>::type RowVectorType;[/b]
const Scalar num_observations = static_cast<Scalar>(x.rows());
const RowVectorType x_mean = x.colwise().sum() / num_observations;
const RowVectorType y_mean = y.colwise().sum() / num_observations;
const_cast< MatrixBase<OtherDerived>& >(C) =
(x.rowwise() - x_mean).transpose() * (y.rowwise() - y_mean) / num_observations;
}
User avatar
ggael
Moderator
Posts
3447
Karma
19
OS

Re: RowVector / MatrixX3

Mon Mar 17, 2014 9:48 pm
You can use typename Derived::PlainObject


Bookmarks



Who is online

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