Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to shift a matrix in Eigen?

Tags:

c++

matrix

eigen

I'm trying to make a simple shift of Eigen's Matrix<int,200,200>, but I can't get Eigen::Translation to work. Since I'm rather new to C++, Eigen`s official documentation isn't of much use to me. I can't extract any useful information from it. I've tried to declare my translation as:

Translation<int,2> t(1,0);

hoping for a simple one row shift, but I can't get it to do anything with my matrix. Actually I'm not even sure if that's what this method is for... if not, could you please recommend some other, preferably fast, way of doing matrix translation on a torus? I'm looking for an equivalent to MATLab's circshift.

like image 943
Maksim Gorkiy Avatar asked Aug 12 '26 15:08

Maksim Gorkiy


1 Answers

The Translation class template is from the Geometry module and represents a translation transformation. It has nothing to do with shifting values in an array/matrix.

According to this discussion, the shifting feature wasn't implemented yet as of 2010 and was of low priority back then. I don't see any indication in the documentation that things are any different now, 4 years later.

So, you need to do it yourself. For example:

/// Shifts a matrix/vector row-wise.
/// A negative \a down value is taken to mean shifting up.
/// When passed zero for \a down, the input matrix is returned unchanged.
/// The type \a M can be either a fixed- or dynamically-sized matrix.
template <typename M> M shiftedByRows(const M & in, int down)
{
  if (!down) return in;
  M out(in.rows(), in.cols());
  if (down > 0) down = down % in.rows();
  else down = in.rows() - (-down % in.rows());
  // We avoid the implementation-defined sign of modulus with negative arg. 
  int rest = in.rows() - down;
  out.topRows(down) = in.bottomRows(down);
  out.bottomRows(rest) = in.topRows(rest);
  return out;
}
like image 198
Kuba hasn't forgotten Monica Avatar answered Aug 15 '26 05:08

Kuba hasn't forgotten Monica