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

Transpose of expression crashes?

Tags: None
(comma "," separated)
fjafjan
Registered Member
Posts
2
Karma
0

Transpose of expression crashes?

Wed Oct 28, 2015 11:53 am
Hi, I am new to using Eigen, so I apologize if this is an obvious question, but I was unable to find any answer through google or this forum

I have written the following

Code: Select all
   ArrayXf theta = ArrayXf::LinSpaced(n_elements, 0, 2 * 3.141592);
   ArrayXf X = theta.cos();
   ArrayXf Y = theta.sin();
   ArrayXf Z = ArrayXf::Zero(n_elements);
   ArrayXXf x_world(X.rows(), X.cols() + Y.cols() + Z.cols() );
   x_world << X, Y, Z;
   Matrix3f rotation = Matrix3f::Identity();
   MatrixXf x_image = (rotation * (x_world.matrix().transpose())).transpose();


and the code crashes with an error message
Code: Select all
Unhandled Exception: System.AccessViolationException: Attempted to read or write
 protected memory. This is often an indication that other memory is corrupt.
.

If I change it to

Code: Select all
 MatrixXf x_image = (rotation * (x_world.matrix().transpose()));
x_image.transposeInPlace();


i.e. I remove the transpose at the end, the code runs fine and I get what I expect. So why does the final transpose cause the code to crash, is it simply not possible to transpose
at the end of an expression like that or is there something else I am missing?

Grateful for any answers.
User avatar
ggael
Moderator
Posts
3447
Karma
19
OS

Re: Transpose of expression crashes?

Wed Oct 28, 2015 12:48 pm
I cannot reproduce using either 3.1, 3.2, 3.3 versions of Eigen, and gcc or clang, with and without compiler optimizations. Test program:
Code: Select all
#include <Eigen/Dense>
#include <iostream>
using namespace Eigen;
int main() {
  int n_elements = 100;
  ArrayXf theta = ArrayXf::LinSpaced(n_elements, 0, 2 * 3.141592);
  ArrayXf X = theta.cos();
  ArrayXf Y = theta.sin();
  ArrayXf Z = ArrayXf::Zero(n_elements);
  ArrayXXf x_world(X.rows(), X.cols() + Y.cols() + Z.cols() );
  x_world << X, Y, Z;
  Matrix3f rotation = Matrix3f::Identity();
  MatrixXf x_image = (rotation * (x_world.matrix().transpose())).transpose();
  std::cout << x_image << "\n";
}


As usual with memory issue, the core of the problem is not where the problem occurs. Running your program within a memory debugger tool (like valgrind) should help you to spot the true issue. Of course, if you can reproduce your issue with a self-contained exemple (as the one above), then please provide eigen version, compiler version, and all compiler flags so that we can reproduce.
fjafjan
Registered Member
Posts
2
Karma
0
Hmm, that's peculiar.

I am running eigen 3.1.3 (in Windows/Visual Studio, without optimization) but my code barely does anything else than what I posted. The full file is

Code: Select all
#include <Eigen/Dense>
#include "stdafx.h"
#include <iostream>
#include <math.h>
#include "UnitTest.h"

using namespace Eigen;
using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{

   /// Our parameters
   const int n_elements = 20;
   const double radius = 1;

   const double angle_x = 0.2;
   const double angle_y = 0.1;
   const double angle_z = 0;

    const double Tx0 = 0;
    const double Ty0 = 0;
    const double board_distance = 20;

   cout << "N elements is " << n_elements << endl;

   /// This creates a circle which is easy to see how it has been distorted.
   ArrayXf theta = ArrayXf::LinSpaced(n_elements, 0, 2 * 3.141592);
   ArrayXf X = theta.cos();
   ArrayXf Y = theta.sin();
   ArrayXf Z = ArrayXf::Zero(n_elements);

   cout << "X becomes" << endl << X << endl;
   cout << "Z becomes" << endl << Z << endl;

   cout << "X rows " << X.rows() << " and total cols " << X.cols() + Y.cols() + Z.cols();
   ArrayXXf x_world(X.rows(), X.cols() + Y.cols() + Z.cols() );
   x_world << X, Y, Z;

   cout << "x_world becomes " << endl << x_world << endl;
   Matrix3f rotation = Matrix3f::Identity();

   Matrix3f rotX, rotY, rotZ;
   rotX << cos(angle_x), -sin(angle_x) , 0,
           sin(angle_x), cos(angle_x)  , 0,
         0           , 0             , 1;

    rotY << cos(angle_y), 0             , -sin(angle_y),
            0           , 1             , 0,
            sin(angle_y), 0             , cos(angle_y);

    rotZ << 1           , 0             , 0,
            0           , cos(angle_z), -sin(angle_z),
            0           , sin(angle_z), cos(angle_z);

    rotation = (rotZ * rotY)*rotX;
   cout << "Our x rotation matrix is "<< endl << rotation << endl;
    /// Compare with python output, it should be the same.

    MatrixXf x_image = (rotation * (x_world.matrix().transpose())).transpose();
 }




I also tried copying your code and replacing the entire mainloop with it and I got the same error as before. So perhaps it is somehow a platform issue?

EDIT: I suppose all compiler flags would be

Code: Select all
/Yu"stdafx.h" /GS /analyze- /W3 /Zc:wchar_t /I"C:\ivcommon\include" /Zi /Od /Fd"Debug\vc120.pdb" /fp:precise /D "WIN32" /D "_DEBUG" /D "_UNICODE" /D "UNICODE" /errorReport:prompt /WX- /Zc:forScope /Oy- /clr /FU"c:\Program Files (x86)\Microsoft Visual Studio 12.0\Common7\IDE\PublicAssemblies\Microsoft.VisualStudio.QualityTools.UnitTestFramework.dll" /FU"C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.0\mscorlib.dll" /FU"C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.0\System.Data.dll" /FU"C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.0\System.dll" /FU"C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.0\System.Xml.dll" /MD /Fa"Debug\" /EHa /nologo /Fo"Debug\" /Fp"Debug\DefaultTest.pch"


Which is mostly just **** that is auto-included in Visual Studio...
tienhung
Registered Member
Posts
29
Karma
0
I can run your last code without problem (VS2013, eigen 3.2.6). Is there anything to check in the header "UnitTest.h" ?
User avatar
ggael
Moderator
Posts
3447
Karma
19
OS
Your are using a pretty old version of Eigen, please try first with the latest stable release (3.2.6).


Bookmarks



Who is online

Registered users: bartoloni, Bing [Bot], Evergrowing, Google [Bot], q.ignora