ブーストの uBlas マトリックスをバックエンドとして使用して、3 次元テンソルを実装しようとしています。関数の 1 つは、スライスへの参照を取得することで、マトリックスの簡単な割り当てを可能にします。
以下は、テンソル クラスのフラグメントです。
template<class L, class M>
class tensor {
public:
typedef L layout_type;
typedef std::size_t size_type;
typedef M array_type;
private:
size_type size1_;
size_type size2_;
size_type size3_;
array_type data_;
public:
/**
* Return a constant reference to the internal storage of a dense tensor, i.e. the raw data
* It's type depends on the type used by the tensor to store its data
*/
BOOST_UBLAS_INLINE
const array_type &data() const {
return data_;
}
/**
* Return a reference to the internal storage of a dense tensor, i.e. the raw data
* It's type depends on the type used by the tensor to store its data
*/
BOOST_UBLAS_INLINE
array_type &data() {
return data_;
}
/**@}*/
/**
* @name Slices' access
* Accessors for slices across each dimension
*/
/**@{*/
BOOST_UBLAS_INLINE
typename layout_type::template slice<1>::matrix_slice_type
at_dim1_slice(uint32_t i) {
return ublas::trans(ublas::project(data(), layout_type::template slice<1>::coord1(i, size1_, size2_, size3_),
layout_type::template slice<1>::coord2(i, size1_, size2_, size3_)));
}
}
layout_type は次のようになります。
template<class M>
struct basic_dim2_major {
typedef M matrix_type;
template<int DIM, class DUMMY = void>
struct slice {
};
template<class DUMMY> struct slice<1, DUMMY> {
struct trans {
template<class T>
auto operator()(T &x) ->decltype(ublas::trans(x))
{
return ublas::trans(x);
}
};
typedef ublas::matrix_slice<matrix_type> matrix_slice_type;
typedef typename std::result_of<trans(matrix_slice_type&)>::type Type;
static BOOST_UBLAS_INLINE
Type
orient(matrix_slice_type &data){
return ublas::trans(data);
}
static BOOST_UBLAS_INLINE
ublas::slice coord1(size_type i, size_type size_i, size_type size_j, size_type size_k) {
return ublas::slice(i, size_i, size_k);
}
static BOOST_UBLAS_INLINE
ublas::slice coord2(size_type i, size_type size_i, size_type size_j, size_type size_k) {
return ublas::slice(0, 1, size_j);
}
}
}
そして、ユースケースは次のとおりです。
ublas::matrix slice1(3,4);
tensor<> t(2,3,4);
t.at_dim1_slice(0) = slice1;
問題は次の行にあります。
return ublas::trans(ublas::project(data(), layout_type::template slice<1>::coord1(i, size1_, size2_, size3_),
layout_type::template slice<1>::coord2(i, size1_, size2_, size3_)));
trans 関数と project 関数を一緒に使用すると、コンパイラは project と trans の const オーバーロードを選択し、上記のような代入を行うことができません。ただし、プロジェクトのみを残すと、非 const メソッドが使用され、すべてが機能します。残念ながら、設計されたストレージ レイアウト (2 次元マトリックスへのマッピング) のために、スライスの転置が必要です。
const matrix_slice<const M> project (const M &data, const typename matrix_slice<M>::slice_type &s1, const typename matrix_slice<M>::slice_type &s2);
matrix_slice<M> project (matrix_slice<M> &data, const typename matrix_slice<M>::slice_type &s1, const typename matrix_slice<M>::slice_type &s2);
正しい関数のオーバーロードを示す解決策はありますか? それとも、どこかでエラーをしましたか?