本征三元运算符中的类型错误 [英] Type error in ternary operator in Eigen

查看:112
本文介绍了本征三元运算符中的类型错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用c ++编写一门课程,以概括两个稀疏矩阵求解器(SparseLU和Sparse Cholesky)。当我尝试使用三元运算符时,它表示操作数类型不兼容,但是如果使用If语句,则代码会编译。

I'm doing a class in c++ to generalize two sparse matrix solvers (SparseLU and Sparse Cholesky). When I try to use the ternary operator it says that the operand types are incompatible, but if I use the If statement, the code compiles.


错误2错误:操作数类型不兼容( const Eigen :: Solve< Eigen :: SimplicialLDLT< Eigen :: SparseMatrix< double,0,int>,1,Eigen :: AMDOrdering< int>>,,Eigen :: Matrix< double,-1,1,0,-1,1>>和 const Eigen :: Solve< Eigen :: SparseLU< Eigen :: SparseMatrix< double,0,int>,Eigen :: COLAMDOrdering< int>>,Eigen :: Matrix< double,-1,1,0,-1,1>>)

Error 2 error: operand types are incompatible ("const Eigen::Solve < Eigen::SimplicialLDLT < Eigen::SparseMatrix < double, 0, int > , 1, Eigen::AMDOrdering < int > > , Eigen::Matrix < double, -1, 1, 0, -1, 1 > > " and "const Eigen::Solve < Eigen::SparseLU < Eigen::SparseMatrix < double, 0, int > , Eigen::COLAMDOrdering < int > > , Eigen::Matrix < double, -1, 1, 0, -1, 1 > > ")



eigen::VectorXd solve(eigen::VectorXd &b) {
    return is_cholesky ? cholesky.solve(b) : lu.solve(b); // ERROR
}

X

eigen::VectorXd solve(eigen::VectorXd &b) {
    if (is_cholesky) {
        return cholesky.solve(b);
    }
    else {
        return lu.solve(b);
    }
}

整个代码:

#pragma once  

#ifndef SOLVER_H
#define SOLVER_H

#include <Eigen/Core>
#include <Eigen/Sparse>
#include <Eigen/SparseLU>
#include <Eigen/SparseCholesky>

#define eigen Eigen

class Solver {
private:
    bool is_cholesky;
    eigen::SimplicialLDLT<eigen::SparseMatrix<double>> cholesky;
    eigen::SparseLU<eigen::SparseMatrix<double>> lu;

public:
    Solver(bool is_choleski) {
        this->is_cholesky = is_choleski;
    }

    eigen::ComputationInfo info() {
        return is_cholesky ? cholesky.info() : lu.info();
    }

    void compute(eigen::SparseMatrix<double> &B) {
        is_cholesky ? cholesky.compute(B) : lu.compute(B);
    }

    eigen::VectorXd solve(eigen::VectorXd &b) {
        return is_cholesky ? cholesky.solve(b) : lu.solve(b); // ERROR HERE
    }
};

#endif // SOLVER_H


推荐答案

a类型的规则? b:c 表达式要求它是 b 表达式的类型还是 c 表达式。

The rules for the type of a a ? b : c expression require that it is either the type of the b expression or the type of the c expression.

此处 cholesky.solve(b)的类型不同于 lu.solve(b),并且两个都没有隐式转换。结果表达式将需要转换为 eigen :: VectorXd 。因此,操作数类型不兼容错误。

Here cholesky.solve(b) has a type different to lu.solve(b) and neither has an implicit conversion to the other. That the resulting expression will need a conversion to eigen::VectorXd is ignored. Thus the "operand types are incompatible" error.

return is_cholesky ? cholesky.solve(b) : lu.solve(b); // ERROR

这里每个表达式都必须直接转换为 eigen: :VectorXd (存在。)

Here each expression directly has to have a conversion to eigen::VectorXd, which exists.

if (is_cholesky) {
    return cholesky.solve(b);
}
else {
    return lu.solve(b);
}

您可以强制的类型? / code>表达式到 eigen :: VectorXd 的明确提及,例如

You can force the type of the ?: expression to eigen::VectorXd by mentioning it explicitly, e.g.

return is_cholesky ? eigen::VectorXd{ cholesky.solve(b) } : lu.solve(b);

这篇关于本征三元运算符中的类型错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆