You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

C++中dealii库eigenvectors函数返回值的类型识别与访问问题

Understanding Your 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:

  • .first holds the eigenvalue (a double in your case)
  • .second holds the eigenvector (a 3-dimensional Tensor<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 eigenpair
    • eigs[2] → Third eigenpair

That should resolve your error and help you access the eigenvalues and eigenvectors correctly!

内容的提问来源于stack exchange,提问作者lorenzo bennati

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.29 15:49:08