简体   繁体   English

C ++犰狳稀疏矩阵类型转换

[英]C++ armadillo sparse matrix type conversion

I want to add two sparse armadillo matrices of arbitrary (different) type with operator+ , eg 我想添加两个带有operator+的任意(不同)类型的稀疏犰狳矩阵,例如

SpMat<double> M1(2,2);
SpMat<cx_double> M2(2,2);

// ..fill both matrices

cout<<M1 + M2<<endl;

Upon compiling, the compiler complains that operator+ is not defined for those types. 编译后,编译器会抱怨没有为那些类型定义operator+

When doing the same with DENSE matrices, armadillo automatically promotes the double matrix to a complex one, performs the addition and prints a complex result matrix. 当对DENSE矩阵执行相同操作时,犰狳自动将双精度矩阵提升为复杂的矩阵,执行加法并打印复杂的结果矩阵。

There is a corresponding template for this operator in operator_plus.hpp in the include dir for adding two sparse objects, possibly of different type (at least the template definition suggests that), but it seems to only work when both operands are of the same type. 在include_dir中的operator_plus.hpp中有一个与此操作符相对应的模板,用于添加两个稀疏对象,它们可能具有不同的类型(至少模板定义建议这样做),但它似乎仅在两个操作数属于相同类型时才起作用。 The actual compiler message for the above code is the following 上面代码的实际编译器消息如下

operator_plus.hpp:164:1: note: template<class T1, class T2> typename arma::enable_if2<((arma::is_arma_sparse_type<T1>::value && arma::is_arma_sparse_type<T2>::value) && arma::is_same_type<typename T1::elem_type, typename T2::elem_type>::value), arma::SpGlue<T1, T2, arma::spglue_plus> >::result arma::operator+(const T1&, const T2&)
operator_plus.hpp:164:1: note:   template argument deduction/substitution failed:
operator_plus.hpp:164:1: error: no type named ‘result’ in ‘struct arma::enable_if2<false, arma::SpGlue<arma::SpMat<double>, arma::SpMat<std::complex<double> >, arma::spglue_plus> >’

Any ideas? 有任何想法吗? Is it possible that this feature is just not implemented yet? 此功能可能尚未实现吗? Thanks! 谢谢!

It seems the addition of two sparse matrices of different types has not been implemented yet (using current newest release 6.400.3). 似乎尚未实现两个不同类型的稀疏矩阵的加法运算(使用当前最新版本6.400.3)。 I reckon the below also answers this question . 我认为下面也回答了这个问题

Armadillo uses the SFINAE technique to exclude function templates from the possible list of overloaded candidates for operator+ (and many other functions/operators too) that don't fit, to be left with only the desired candidate upon evaluating the template parameters. Armadillo使用SFINAE技术将功能模板从不适合的operator+ (以及许多其他功能/运算符)的重载候选列表中排除,在评估模板参数时仅保留所需候选。

The candidate we want here is the one adding two sparse matrices. 我们在这里想要的候选者是将两个稀疏矩阵相加的候选者。 It has the following signature 它具有以下签名

template<typename T1, typename T2>
inline arma_hot
typename enable_if2
<
(is_arma_sparse_type<T1>::value && is_arma_sparse_type<T2>::value && is_same_type<typename T1::elem_type, typename T2::elem_type>::value),
 SpGlue<T1,T2,spglue_plus>
>::result
operator+(const T1& x,const T2& y)

enable_if2 is a template struct defined in restrictors.hpp and works very much the same way as std::enable_if : if the first template parameter of enable_if (in the above declaration the long boolean expression) evaluates to true, enable_if has a member of the same type as the second template parameter (in this case SpGlue<T1,T2,spglue_plus> ). enable_if2是在strictors.hpp中定义的模板结构,其工作方式与std :: enable_if大致相同:如果enable_if的第一个模板参数(在上述声明中为长布尔表达式)求值为true,则enable_if具有与第二个模板参数的类型相同(在本例中为SpGlue<T1,T2,spglue_plus> )。

That means that this candidate is only valid, if the boolean expression evaluates to true, otherwise it is also discarded from the list of possible candidates. 这意味着,如果布尔表达式的计算结果为true,则此候选者才有效,否则将从可能的候选者列表中将其丢弃。 As you can see, the boolean expression also contains the part 如您所见,布尔表达式还包含该部分

is_same_type<typename T1::elem_type, typename T2::elem_type>::value

which of course evaluates to false if T1 and T2 are not the same, enable_if2 thus has no member enable_if2::result and the function is removed from the candidate list. 如果T1T2不相同,则哪个值的计算结果当然为false,因此enable_if2没有成员enable_if2::result并且该功能已从候选列表中删除。

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

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