简体   繁体   中英

How to access elements of symmetric Eigen matrix

My understanding of a symmetric matrix is that A(i,j) == A(j,i), but in Eigen only one of these is defined. What am I missing?

Here is an example below. I've also tried variations of that and don't seem to see an answer elsewhere. Am I supposed to manually ensure that the indices comply to some internal expectations?

MatrixXf m(4, 4);  // uninitialized 4x4
auto v = m.selfadjointView<Upper>(); // I don't need triangular matrix, but ok by me to store it only once
int i = 1;
int j = 3;
v(i, j) = 3.0f; // either v(i,j) or v(j,i) is undefined. How can I permit both?
std::cout << "\n"
          << v(j, i);

Accessing both halves by (i,j) is essentially not how .selfadjointView<>() is intended to work. Supporting that would require a min / max operation and for complex matrices even some kind of proxy-object, for each access which is quite expensive.

You should only access the upper half by the () operator and afterwards you can use the SelfadjointView object as part of other expressions, such as products, or use special methods like rankUpdate .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM