/** * Converts an Octave numeric NDArray into a double R array. * * Currently only 3D arrays are supported. */ inline SEXP wrap(const NDArray& x){ int nd = x.ndims(); VERBOSE_LOG("(%iD-NDArray)", nd); if( nd > 3 ){ std::ostringstream err; err << "<NDArray> - Could not convert NDArray[" << x.ndims() << "]: only up to 3 dimensions are supported"; WRAP_ERROR(err.str().c_str()); }else if( nd == 2 ){// safe-guard in case of a 2D-NDArray (i.e. a Matrix object) VERBOSE_LOG(" = "); return wrap(x.as_matrix()) ; } // copy values from the outer to inner dimensions int n = x.dim1(); int p = x.dim2(); int q = x.dim3(); VERBOSE_LOG("[%i x %i x %i]", n, p, q); Rcpp::NumericVector res( Rcpp::Dimension(n, p, q) ); Rcpp::NumericVector::iterator z = res.begin(); for(int k=0; k<q; k++){ for(int j=0; j<p; j++){ for(int i=0; i<n; i++){ *z = x.elem(i,j,k); z++; } } } return res; }
/** * Converts an Octave numeric NDArray into a double R array. * * Currently only 3D arrays are supported. */ inline SEXP wrap(const NDArray& x){ VERBOSE_LOG("(NDArray) -> Array"); if( x.ndims() > 3 ){ std::ostringstream err; err << "<NDArray> - Could not convert NDArray[" << x.ndims() << "]: only up to 3 dimensions are supported"; WRAP_ERROR(err.str().c_str()); } // copy values from the outer to inner dimensions int n = x.dim1(); int p = x.dim2(); int q = x.dim3(); Rcpp::NumericVector res( Rcpp::Dimension(n, p, q) ); Rcpp::NumericVector::iterator z = res.begin(); for(int k=0; k<q; k++){ for(int j=0; j<p; j++){ for(int i=0; i<n; i++){ *z = x.elem(i,j,k); z++; } } } return res; }