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

comma-initializer with Vector3d::asDiagonal()

Tags: None
(comma "," separated)
eudoxos
Registered Member
Posts
36
Karma
0
Hi there, I was badly bitten (and it took LOOONG before I got the idea to check it) by this code -- it creates elastic stiffness matrix, which is 6x6 with zeros everywhere except for the 3rd rank major submatrix (upper left corner) and the main diagonal.

I initialized with comma initializer, giving 4 3x3 blocks:

Code: Select all
Real E=1e6, nu=0; // for instance
Matrix6r C; // the "r" stands for Real, which is usually typedef for double
C<<
  (Matrix3r()<<1,-nu,-nu,-nu,1,-nu,-nu,-nu,1).finished(),
  Matrix3r::Zero(),
  Matrix3r::Zero(),
  Vector3r(2*(1+nu),2*(1+nu),2*(1+nu)).asDiagonal();
C*=E/((1+nu)*(1-2*nu));

with this result:
Code: Select all
     1388.89     -277.778     -277.778            0            0            0
    -277.778      1388.89     -277.778            0            0            0
    -277.778     -277.778      1388.89            0            0            0
           0            0            0 3.54736e-310            0 3.54752e-310
           0            0            0  4.08345e-79 7.39258e-181 2.11972e+229
           0            0            0 3.50662e-310 1.68824e-310 1.33285e-303

hinting at wrong interpretation of the .asDiagonal() expression; I changed the last block initializer to
Code: Select all
  Matrix3r(Vector3r(2*(1+nu),2*(1+nu),2*(1+nu)).asDiagonal());

getting then the correct result
Code: Select all
1388.89 -277.778 -277.778        0        0        0
-277.778  1388.89 -277.778        0        0        0
-277.778 -277.778  1388.89        0        0        0
       0        0        0  3333.33        0        0
       0        0        0        0  3333.33        0
       0        0        0        0        0  3333.33


I would think it is a bug, but having been badly hurt, I think I would not be impartial now. Using Eigen 3.0.1.
User avatar
ggael
Moderator
Posts
3447
Karma
19
OS
well, the bug is that the bad version should not compile. Nevertheless, here it does fail at runtime with the following assertion:

Eigen/src/Core/CommaInitializer.h:106: Eigen::CommaInitializer<MatrixType>::~CommaInitializer() [with XprType = Eigen::Matrix<double, 6, 6>]: Assertion `(m_row+m_currentBlockRows) == m_xpr.rows() && m_col == m_xpr.cols() && "Too few coefficients passed to comma initializer (operator<<)"' failed.

assuming you don't define NDEBUG


Bookmarks



Who is online

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