C++中dealii库eigenvectors函数返回值的类型识别与访问问题
eigs Variable and Fixing the Access Error Hey Lorenzo, let's clear up the confusion here and fix that error you're seeing!
First, let's break down what eigs actually is: it's neither a matrix nor a simple vector—it's a std::array where each element is a std::pair. Each pair stores one of your matrix's eigenvalues and its corresponding eigenvector. Since you're working in 3 dimensions (dim=3), the array has exactly 3 such pairs (one for each eigenpair of your 3x3 symmetric matrix).
Why eigs[0][0] throws an error
When you write eigs[0], you're accessing the first element of the array—which is a std::pair<double, dealii::Tensor<1, 3>>. Unlike arrays or vectors, std::pair doesn't support the operator[] to access its elements. Instead, pairs use .first and .second to get their two values:
.firstholds the eigenvalue (adoublein your case).secondholds the eigenvector (a 3-dimensionalTensor<1,3,double>from deal.II)
Correct way to access the values
Here's how you should retrieve the data you need:
- To get the first eigenvalue:
eigs[0].first - To get the first component of the corresponding eigenvector:
eigs[0].second[0]
For example, if you want to print these values, your code should look like this:
#include <iostream> // ... other necessary includes // After calling eigenvectors(G) std::cout << "First eigenvalue: " << eigs[0].first << std::endl; std::cout << "First component of its eigenvector: " << eigs[0].second[0] << std::endl;
Quick recap of the structure
Let's visualize the hierarchy to make it even clearer:
eigs→std::array(size 3)eigs[0]→std::pair(eigenvalue + eigenvector)eigs[0].first→ double (eigenvalue)eigs[0].second→Tensor<1,3,double>(eigenvector, use[0],[1],[2]to access components)
eigs[1]→ Second eigenpaireigs[2]→ Third eigenpair
That should resolve your error and help you access the eigenvalues and eigenvectors correctly!
内容的提问来源于stack exchange,提问作者lorenzo bennati

