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

Assigning scalar to ColXpr.

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

Assigning scalar to ColXpr.

Wed Jun 11, 2014 3:11 pm
Hi, can you please have a look at the following code snippet? The problem manifests in test2 where I try to assign a scalar value to a ColXpr.

Thanks,
Martin

Code: Select all
#include <iostream>

#include <Eigen/Core>

void test1 ()
{
  Eigen::Matrix3Xi mat;
  mat.resize (Eigen::NoChange, 4);

  for (int c = 0; c < mat.cols (); ++c)
  {
    // This is just to show that it works with a vector.
    mat.col (c) = Eigen::Vector3i (10 * c + 0, 10 * c + 1, 10 * c + 2);
  }

  std::cout << mat << "\n\n";
}

void test2 ()
{
  Eigen::RowVectorXi vec;
  vec.resize (Eigen::NoChange, 4);

  for (int c = 0; c < vec.cols (); ++c)
  {
    // This one does not compile.
    // vec.col (c) = c;

    // These compile but don't assign the value to vec!
    // vec.col (c) = Eigen::Matrix <int, 1, 1> (c);
    vec.col (c) = Eigen::Array <int, 1, 1> (c);

    // This would work:
    // vec (c) = c;
  }

  std::cout << vec << "\n\n";
}

int main ()
{
  std::cout << "test 1\n"; test1 ();
  std::cout << "test 2\n"; test2 ();

  return (0);
}


Output:
Code: Select all
test 1
 0 10 20 30
 1 11 21 31
 2 12 22 32

test 2
0 0 0 0

Last edited by martin_ on Tue Jun 17, 2014 7:01 am, edited 1 time in total.
User avatar
ggael
Moderator
Posts
3447
Karma
19
OS

Re: Assigning scalar to ColXpr.

Sat Jun 14, 2014 7:43 pm
You can uses arrays or the comma initializer (http://eigen.tuxfamily.org/dox-devel/gr ... ation.html). e.g., vec.col(c) << c;
martin_
Registered Member
Posts
5
Karma
0

Re: Assigning scalar to ColXpr.

Tue Jun 17, 2014 7:22 am
Thanks a lot for the answer. Using the comma initializer assigns the value correctly.
Code: Select all
vec.col (c) << c;


However my problem is that this code compiles as well but does not assign the value correctly (I have edited my original post to make this more clear).
Code: Select all
vec.col (c) = Eigen::Matrix <int, 1, 1> (c);
// or
vec.col (c) = Eigen::Array <int, 1, 1> (c);


Maybe here is a better example to express my problem. I am using the same syntax for the vec and mat. The assignment to the matrix works but the one to the vector doesn't:
Code: Select all
int main ()
{
  Eigen::Matrix <int, 1, Eigen::Dynamic> vec;
  Eigen::Matrix <int, 2, Eigen::Dynamic> mat;

  vec.resize (Eigen::NoChange, 4);
  mat.resize (Eigen::NoChange, 4);

  for (int c = 0; c < 4; ++c)
  {
    vec.col (c) = Eigen::Matrix <int, 1, 1> (c);
    mat.col (c) = Eigen::Matrix <int, 2, 1> (c, 10 * c);
  }

  std::cout << "vec:\n" << vec << "\n";
  std::cout << "mat:\n" << mat << "\n";

  return (0);
}


Output
Code: Select all
vec:
0 0 0 0
mat:
 0  1  2  3
 0 10 20 30
User avatar
ggael
Moderator
Posts
3447
Karma
19
OS

Re: Assigning scalar to ColXpr.

Tue Jun 17, 2014 7:54 am
This is because Matrix <int, 1, 1> (c) calls that constructor: http://eigen.tuxfamily.org/dox/classEig ... 7aaa077c4b.

If you omit -DNDEBUG, then you should get an assert. I admit that's confusing. We have the same issue for 2D vectors Eigen::Matrix <int, 2, 1>(rows,cols) versus Eigen::Matrix <int, 2, 1>(x,y) for which we have a workaround to get the expected behavior. Somehow, we did not though about the extremely rare 1x1 case.
martin_
Registered Member
Posts
5
Karma
0

Re: Assigning scalar to ColXpr.

Tue Jun 17, 2014 12:38 pm
You are right, I was compiling in Release mode. In debug it asserts.

Thanks for the explanation. Now I understand the problem.


Bookmarks



Who is online

Registered users: Bing [Bot], claydoh, Google [Bot], rblackwell, Yahoo [Bot]