本文介绍了C ++犰狳稀疏矩阵类型转换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用 operator + 添加任意(不同)类型的两个稀疏犰狳矩阵,例如

I want to add two sparse armadillo matrices of arbitrary (different) type with operator+, e.g.

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

// ..fill both matrices

cout<<M1 + M2<<endl;

编译时会报告 operator +

当使用DENSE矩阵做同样的操作时,armadillo会自动将双重矩阵推广到一个复杂的矩阵,执行相加并打印一个复杂的结果矩阵。

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.

在include dir中的 operator_plus.hpp 中有一个相应的模板用于添加两个稀疏对象,可能是不同类型(至少模板定义建议),但似乎只有当两个操作数是相同类型时才工作。上面代码的实际编译器消息如下

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. 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!

推荐答案

似乎添加了两个不同类型的稀疏矩阵尚未实现最新版本6.400.3)。我估计下面也回答了。

It seems the addition of two sparse matrices of different types has not been implemented yet (using current newest release 6.400.3). I reckon the below also answers this question.

Armadillo使用技术排除功能模板从可能的运算符+ (以及许多其他函数/运算符)的重载候选列表,不匹配,在评估模板参数时只剩下所需的候选。

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.

我们想要的候选是添加两个稀疏矩阵的候选。它具有以下签名

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)


$ b b

enable_if2是在restrictors.hpp中定义的模板结构,其工作方式与:如果enable_if(在上面的声明中为long boolean表达式)的第一个模板参数的计算结果为true,enable_if具有与第二个模板参数相同类型的成员(在这种情况下 SpGlue< T1,T2,spglue_plus> )。

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>).

这意味着这个候选者只有有效,如果布尔表达式为真,否则也从可能的候选列表中被丢弃。如您所见,布尔表达式还包含部分

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. As you can see, the boolean expression also contains the part

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

当然,如果 T1 T2 不一样,enable_if2因此没有成员 enable_if2 :: result 从候选人列表。

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.

这篇关于C ++犰狳稀疏矩阵类型转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 00:11