[Rcpp-devel] seq(from, to, by) for Rcpp::sugar

Masaki Tsuda teuder at gmail.com
Wed Aug 24 15:51:16 CEST 2016


Dear Rcpp developers,

I wrote seq(from, to, by) function for Rcpp::sugar.
It worked for me to generate numerical sequence in ascending/descending order and absolute value of the common difference is smaller than 1

It is great pleasure if you consider the function is correct and it could be integrated into Rcpp package.

Best regards,

Masaki Tsuda


========

namespace Rcpp{
namespace sugar{

class Seq : public VectorBase< REALSXP,false,Seq > {
public:
    Seq( double from_, double to_ , double by_ = 1.0) : from(from_), by(by_), len(((to_-from_)/by_)+1) {
        if(from_ <= to_){
            if(by_ < 0.0) stop("'by' shuld be positive when 'to' is greater than 'from'.");
        } else {
            if(by_ == 1.0) by = -1.0;
            else if(by_ > 0.0) stop("'by' shuld be negative when 'from' is greater than 'to'.");
            len = (((to_-from_)/by_)+1);
        }
    }
    inline double operator[]( R_xlen_t i ) const {
        return from + i*by ;
    }
    inline R_xlen_t size() const { return len ; }
    
private:
    double from ;
    double by ;
    R_xlen_t len ;
} ;

} // sugar

inline sugar::Seq seq( const double from, const double to , const double by){
    return sugar::Seq( from, to, by ) ;
}

} // Rcpp

========









\




-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.r-forge.r-project.org/pipermail/rcpp-devel/attachments/20160824/8562596a/attachment.html>


More information about the Rcpp-devel mailing list