2010-06-19 5 views
1

J'essaye de construire une classe qui facilite l'utilisation de la boost boost :: numeric :: ublas :: matrix. Ainsi, j'ai:Accéder à un sous-secteur d'une matrice boost dans une classe d'assistance

using namespace boost::numeric::ublas; 
typedef matrix<double> matrix_t; 

class Tensor : public matrix_t { 

    public: 
    Tensor (const int M, const int N) : matrix_t(M, N) { } 
    virtual ~Tensor() { } 

    Tensor SubMatrix (const int start1, const int size1, const int start2, const int size2) const; 

    void Print() const; 

}; // tensor 

Et je définir plus précisément les SubMatrix() comme suit:

Tensor Tensor::SubMatrix (const int start1, const int size1, const int start2, const int size2) const { 

    matrix_slice<matrix_t> s (this, slice(start1, 1, size1), slice(start2, 1, size2)); 
    Tensor t (matrix_expression<matrix_t> (s)); 

    return t; 
} 

Je voudrais être en mesure de créer facilement de nouvelles Tensor s en saisissant des sous-matrices à partir d'un Tensor. Le compilateur se plaint de ce qui suit:

g++ -ftemplate-depth-100 -Drestrict= -Wall -Wno-deprecated -g3 -ggdb -Wall -D_GLIBCXX_DEBUG -I/home/eshamay/md/src -I/home/eshamay/share/include -I/usr/include -I/usr/local/include -I/home/eshamay/src/boost-1_43_0 -L/home/eshamay/share/lib -L/home/eshamay/src/lapack-3.2.1 -lconfig++ -c -o morita2002.o morita2002.cpp 
morita2002.cpp: In member function ‘morita::math::Tensor morita::math::Tensor::SubMatrix(int, int, int, int) const’: 
morita2002.cpp:109: error: no matching function for call to ‘boost::numeric::ublas::matrix_slice<boost::numeric::ublas::matrix<double, boost::numeric::ublas::   basic_row_major<long unsigned int, long int>, boost::numeric::ublas::unbounded_array<double, std::allocator<double> > > >::matrix_slice(const morita::math::Tensor* const, boost::numeric::ublas::slice, boost::numeric::ublas::slice)’ 
/usr/include/boost/numeric/ublas/matrix_proxy.hpp:3192: note: candidates are: boost::numeric::ublas::matrix_slice<E>::matrix_slice(const typename boost::mpl::   if_<boost::is_const<T>, typename M::const_closure_type, typename M::closure_type>::type&, const boost::numeric::ublas::basic_slice<typename A::size_type, typename A::  difference_type>&, const boost::numeric::ublas::basic_slice<typename A::size_type, typename A::difference_type>&, int) [with M = boost::numeric::ublas::matrix<double,  boost::numeric::ublas::basic_row_major<long unsigned int, long int>, boost::numeric::ublas::unbounded_array<double, std::allocator<double> > >] 
/usr/include/boost/numeric/ublas/matrix_proxy.hpp:3183: note:     boost::numeric::ublas::matrix_slice<E>::matrix_slice(M&, const boost::numeric::ublas:: basic_slice<typename A::size_type, typename A::difference_type>&, const boost::numeric::ublas::basic_slice<typename A::size_type, typename A::difference_type>&) [with M = boost::numeric::ublas::matrix<double, boost::numeric::ublas::basic_row_major<long unsigned int, long int>, boost::numeric::ublas::unbounded_array<double, std::    allocator<double> > >] 
/usr/include/boost/numeric/ublas/matrix_proxy.hpp:3155: note:     boost::numeric::ublas::matrix_slice<boost::numeric::ublas::matrix<double, boost::numeric::ublas::basic_row_major<long unsigned int, long int>, boost::numeric::ublas::unbounded_array<double, std::allocator<double> > > >::matrix_slice(const boost::numeric::ublas::matrix_slice<boost::numeric::ublas::matrix<double, boost::numeric::ublas::basic_row_major<long unsigned int, long int>, boost::numeric::ublas::unbounded_array<double, std::allocator<double> > > >&) 
morita2002.cpp:112: error: conversion from ‘morita::math::Tensor (*)(boost::numeric::ublas::matrix_expression<boost::numeric::ublas::matrix<double, boost::numeric:: ublas::basic_row_major<long unsigned int, long int>, boost::numeric::ublas::unbounded_array<double, std::allocator<double> > > >)’ to non-scalar type ‘morita::math::  Tensor’ requested 

Après avoir essayé un certain nombre de variations pour apaiser le compilateur, je suis perplexe. Quel est le problème, et pourquoi ne puis-je pas créer le matrix_slice comme il est écrit?

+0

Votre classe 'Tenseur 'semble incomplète. Est-ce le seul constructeur que vous avez? – Inverse

+0

oui, devrais-je ajouter un constructeur de copie? – Shamster

Répondre

1

matrix_slice requiert une référence comme premier argument, alors que vous lui donnez un pointeur (this). Essayez plutôt d'utiliser *this.

En outre, puisque vous êtes SubMatrix méthode a un qualificateur de const, le type de * ce serait matrix_t const&, vous devez donc utiliser matrix_slice<matrix_t const>.

Questions connexes