#include // forward declaration class dtrMatrix ; namespace Rcpp{ namespace traits { template <> class is_convertible : public true_type{} ; } } #include class dtrMatrix { protected: Rcpp::NumericVector d_dat; Rcpp::List d_dimnames; int d_size; // must be square bool d_upper; // true if upper triangular bool d_unit; // true if unit diagonal public: dtrMatrix(Rcpp::S4&) throw (std::runtime_error); operator SEXP() const ; }; using namespace Rcpp ; dtrMatrix::dtrMatrix(Rcpp::S4& xp) throw (std::runtime_error) : d_dat(xp.slot("x")), d_dimnames(xp.slot("Dimnames")) { if (!xp.inherits("dtrMatrix")) throw std::runtime_error("constructor needs S4 class dtrMatrix"); IntegerVector dd = xp.slot("Dim"); if (dd.size() != 2 || dd[0] != dd[1]) throw std::runtime_error("Matrix must be square"); d_size = dd[0]; std::string uplo = as(xp.slot("uplo")); d_upper = (uplo == "U") || (uplo == "u"); std::string diag = as(xp.slot("diag")); d_unit = (diag == "U") || (diag == "u"); } dtrMatrix::operator SEXP() const { Rcpp::S4 ans("dtrMatrix"); ans.slot("x") = clone(d_dat); ans.slot("Dimnames") = clone(d_dimnames); IntegerVector dd(2); dd[0] = dd[1] = d_size; ans.slot("Dim") = dd; ans.slot("uplo") = d_upper ? "U" : "L"; ans.slot("diag") = d_unit ? "U" : "N"; return ans; } namespace Rcpp { template <> SEXP wrap( const dtrMatrix& m ){ return m ; } }