简体   繁体   English

创建具有结构元素类型的特征稀疏矩阵

[英]Create an Eigen sparse matrix with elements type of a struct

I would like to know if there is any way to define a sparse matrix as Eigen::SparseMatrix< StructElem >, which means each element of the matrix is an struct.我想知道是否有任何方法可以将稀疏矩阵定义为 Eigen::SparseMatrix< StructElem >,这意味着矩阵的每个元素都是一个结构。

I tried the following code, but I got the error of "no suitable constructor exists to convert from int to StructElem".我尝试了以下代码,但出现“不存在合适的构造函数来从 int 转换为 StructElem”的错误。

// the structure of element:
    struct StructElem
    {
       unsigned int mInd;
       bool mIsValid;
       double mVec[ 4 ];
    };

// define the matrix:    
    Eigen::SparseMatrix< StructElem > lA( m, n);

// work with the matrix
    for( unsigned int i = 0; i < m; i++)
    {
       StructElem lElem;
       lElem.mInd = i;
       lElem.mIsValid = true;
       lElem.mVec= {0.0, 0.1, 0.2, 0.4};

       lA.coeffRef(i, i) = lElem; // got the error here!
    }

I was wondering if you would have any sort of ideas to solve this error?我想知道您是否有解决此错误的任何想法?

You can't store classes or structs as coefficients of an Eigen::SparseMatrix .您不能将类或结构存储为Eigen::SparseMatrix的系数。 The constructor of the Eigen::SparseMatrix is of the type Eigen::SparseMatrix 的构造函数的类型是

Eigen::SparseMatrix< _Scalar, _Options, _StorageIndex >::SparseMatrix ( )   

This means that an Eigen::SparseMatrix can only be used to store scalar coefficients.这意味着Eigen::SparseMatrix只能用于存储标量系数。 The scalar types that can be used are any numeric type, such as float , double , int or std::complex<float> , etc., as described here .可以使用的标量类型是任何数字类型,例如floatdoubleintstd::complex<float>等,如此所述。

It is not clear why you would want to store anything else than a scalar type as coefficients of a SparseMatrix, since sparse matrices are used for arithmetic operations.目前尚不清楚为什么要将标量类型以外的任何内容存储为 SparseMatrix 的系数,因为稀疏矩阵用于算术运算。 But if you really want to address the structs via a SparseMatrix, you could instead prepare an array of structs of the type StructElem and store the indices of this array into an Eigen::SparseMatrix<int> .但是,如果您真的想通过 SparseMatrix 处理结构,则可以改为准备一个StructElem类型的结构数组并将该数组的索引存储到Eigen::SparseMatrix<int>中。

As @RHertel noticed, Eigen::SparseMatrix is intended to be used for types which behave like Scalar types.正如@RHertel 所注意到的, Eigen::SparseMatrix旨在用于行为类似于标量类型的类型。 Eg, the type should be constructable from 0 , and it should be add-able and multiply-able (the latter is only required if you do actual linear algebra with it).例如,该类型应该可以从0构造,并且应该是可加和可乘的(只有在使用它进行实际线性代数时才需要后者)。

You can fool Eigen to handle your custom type, by adding a constructor which takes an int (but ignores it):您可以通过添加一个采用int的构造函数(但忽略它)来欺骗 Eigen 来处理您的自定义类型:

struct StructElem
{
   unsigned int mInd;
   bool mIsValid;
   std::array<double,4> mVec;
   explicit StructElem(int) {}
   StructElem() = default;
};

Full demo: https://godbolt.org/z/7iPz5U完整演示: https://godbolt.org/z/7iPz5U

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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