簡體   English   中英

C ++犰狳稀疏矩陣類型轉換

[英]C++ armadillo sparse matrix type conversion

我想添加兩個帶有operator+的任意(不同)類型的稀疏犰狳矩陣,例如

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

// ..fill both matrices

cout<<M1 + M2<<endl;

編譯后,編譯器會抱怨沒有為那些類型定義operator+

當對DENSE矩陣執行相同操作時,犰狳自動將雙精度矩陣提升為復雜的矩陣,執行加法並打印復雜的結果矩陣。

在include_dir中的operator_plus.hpp中有一個與此操作符相對應的模板,用於添加兩個稀疏對象,它們可能具有不同的類型(至少模板定義建議這樣做),但它似乎僅在兩個操作數屬於相同類型時才起作用。 上面代碼的實際編譯器消息如下

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> >’

有任何想法嗎? 此功能可能尚未實現嗎? 謝謝!

似乎尚未實現兩個不同類型的稀疏矩陣的加法運算(使用當前最新版本6.400.3)。 我認為下面也回答了這個問題

Armadillo使用SFINAE技術將功能模板從不適合的operator+ (以及許多其他功能/運算符)的重載候選列表中排除,在評估模板參數時僅保留所需候選。

我們在這里想要的候選者是將兩個稀疏矩陣相加的候選者。 它具有以下簽名

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是在strictors.hpp中定義的模板結構,其工作方式與std :: enable_if大致相同:如果enable_if的第一個模板參數(在上述聲明中為長布爾表達式)求值為true,則enable_if具有與第二個模板參數的類型相同(在本例中為SpGlue<T1,T2,spglue_plus> )。

這意味着,如果布爾表達式的計算結果為true,則此候選者才有效,否則將從可能的候選者列表中將其丟棄。 如您所見,布爾表達式還包含該部分

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

如果T1T2不相同,則哪個值的計算結果當然為false,因此enable_if2沒有成員enable_if2::result並且該功能已從候選列表中刪除。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM