Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/150.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ Eigen:按列或行顺序将SparseMatrix传递给同一个函数_C++_Eigen_Eigen3 - Fatal编程技术网

C++ Eigen:按列或行顺序将SparseMatrix传递给同一个函数

C++ Eigen:按列或行顺序将SparseMatrix传递给同一个函数,c++,eigen,eigen3,C++,Eigen,Eigen3,假设我写以下内容: func(Eigen::SparseMatrixBase<double> & A){ for(int i = 0; i < A.outerSize(); i++) for(Eigen::SparseMatrixBase<double>::InnerIterator it(A,i);it;++it) // do something } 当我编译时: error: cannot convert

假设我写以下内容:

func(Eigen::SparseMatrixBase<double> & A){
    for(int i = 0; i < A.outerSize(); i++)
        for(Eigen::SparseMatrixBase<double>::InnerIterator it(A,i);it;++it)
            // do something
}
当我编译时:

error: cannot convert Eigen::SparseMatrix<double,RowMajor> & 
to Eigen::SparseMatrix<double,0,int> &
错误:无法转换Eigen::SparseMatrix&
到本征::SparseMatrix&
然后,我更改类型:

func<Eigen::SparseMatrix<double,0,int>,ditto::InnerIterator>(Arowmajor)
func(Arowmajor)
错误呢?与上一个相反:

error: cannot convert Eigen::SparseMatrix<double,0,int> & 
to Eigen::SparseMatrix<double,RowMajor> & 
错误:无法转换Eigen::SparseMatrix&
到本征::SparseMatrix&



使用特征类处理迭代和模板化的正确方法是什么?

不必显式使用两个模板参数,您可以使用一个模板参数来表示
SparseMatrix
类型,并使用其
内部迭代器

#include <Eigen/SparseCore>
#include <iostream>

using namespace Eigen;

template<class Mat>
void func(Mat & A, double d)
{
    for (int i = 0; i < A.outerSize(); i++)
        for (typename Mat::InnerIterator it(A, i); it; ++it)
            it.valueRef() = d;
}

int main()
{
    SparseMatrix<double> sm(3, 3);
    sm.setIdentity();

    std::cout << sm << "\n\n";

    func(sm, 3.2);

    std::cout << sm << "\n\n";

    return 0;
}
#包括
#包括
使用名称空间特征;
模板
无效功能(材料和材料,双d)
{
对于(int i=0;istd::cout不必显式使用两个模板参数,您可以使用一个模板参数来表示
SparseMatrix
类型,并使用其
InnerIterator
,如下所示:

#include <Eigen/SparseCore>
#include <iostream>

using namespace Eigen;

template<class Mat>
void func(Mat & A, double d)
{
    for (int i = 0; i < A.outerSize(); i++)
        for (typename Mat::InnerIterator it(A, i); it; ++it)
            it.valueRef() = d;
}

int main()
{
    SparseMatrix<double> sm(3, 3);
    sm.setIdentity();

    std::cout << sm << "\n\n";

    func(sm, 3.2);

    std::cout << sm << "\n\n";

    return 0;
}
#包括
#包括
使用名称空间特征;
模板
无效功能(材料和材料,双d)
{
对于(int i=0;istd::我可以添加一个编辑吗?在
Mat::InnerIterator
之前,我必须添加
typename Mat::InnerIterator
才能使其正常工作。铿锵的错误消息并不清楚,但当我用G++编译时,立即发现了问题。也就是说,我不明白为什么有必要添加
typename
——这似乎是真的It’这就像是为了避免编译器逻辑的装饰,而不是必要的。@bordeo是的,请便。。我的坏习惯是通过使用Visual Studio来支持的。我可以添加一个编辑吗?在
Mat::InnerIterator
之前,我必须添加
typename Mat::InnerIterator
来实现这一点。叮当我用G编译时,发出的错误消息并不清楚++,立即发现了问题。尽管如此,我不明白为什么有必要添加
typename
——这似乎是为了避免编译器逻辑而进行的修饰,而不是必要的。@bordeo是的,请便。。我的坏习惯是由Visual Studio支持的。