[Rcpp-devel] Ranges and Casting

Francisco Bischoff fbischoff at med.up.pt
Sat Jan 16 02:35:47 CET 2021


Hello,

About the Range() function, I think it should handle decreasing ranges
too...
But, idk if using Matlabs approach or R approach:

R's:

a <- 1
b <- 10

print(a:b)
1 2 3 4 5 6 7 8 9 10
print(b:a)
10 9 8 7 6 5 4 3 2 1

Matlab's
a = 1;
b = 10;
disp(a:b);
1 2 3 4 5 6 7 8 9 10
disp(b:a);
numeric(0)
disp(b:-1:a)
10 9 8 7 6 5 4 3 2 1

I think that we are in the R domain, so we should use Range(b, a) normally
(my humble opinion).

About Casting, this is a good example:

Rcout << Range(0, 10) << std::endl;

This does not compute...

What should I do?

Rcout << as<IntegerVector>(Range(0, 10)) << std::endl;
or
Rcout << (IntegerVector)(Range(0, 10)) << std::endl;

--
Francisco Bischoff, MD, MSc
Faculty of Medicine of the University of Porto, Portugal

- Master of Medical Informatics | topic: time series
- Research Associate | artificial intelligence for health
<https://mailtrack.io/trace/link/261f22c687548623a329c468d9ee8e2177eb0450?url=http%3A%2F%2Fcintesis.eu%2Fai4health%2F&userId=3785237&signature=3e0be0b7841b95c4>
@ cintesis.eu
<https://mailtrack.io/trace/link/37900be19872e834994af798ab1d53480613e66c?url=http%3A%2F%2Fcintesis.eu&userId=3785237&signature=91ac56f3d356e8bd>
- Teaching Assistant | department of community medicine, information and
health decision sciences @ med.up.pt
<https://mailtrack.io/trace/link/6951087469cd0e54d4bc365eedae9d9ad24d5970?url=http%3A%2F%2Fmed.up.pt&userId=3785237&signature=ac6fffd4ce8944ca>

ORCID: 0000-0002-5301-8672 | Mendeley: francisco-bischoff | Google:
tCeA0uUAAAAJ | ResearcherID: H-8261-2016 | ResearchGate: Francisco_Bischoff
| CiênciaID B413-E0A0-DE8D | LinkedIn: franzbischoff


On Wed, Jan 6, 2021 at 6:11 AM Dirk Eddelbuettel <edd at debian.org> wrote:

>
> Francisco,
>
> On 5 January 2021 at 13:58, Francisco Bischoff wrote:
> | First of all, let me thank all involved with the Rcpp package!
> |
> | I just have a (maybe silly) question, but I struggle with this frequently
> | in my codes:
> |
> | Range() vs IntegerVector
> |
> | One example is when I need a reverse index, like R's 10:1, so I had to
> | write my own function that returns an IntegerVector. But, to work I have
> to
> | do this ugly code:
> |
> |           IntegerVector dot_rev_idxs1 = ::seq((i - 1), beginidx);
> |           IntegerVector dot_rev_idxs2 = ::seq((idx_nn - 1), (beginidx +
> | idx_diff));
> |           IntegerVector dot_rev_idxs3 = ::seq((i - 1 + window_size), (
> | beginidx + window_size));
> |           IntegerVector dot_rev_idxs4 = ::seq((idx_nn - 1 +
> window_size), (
> | beginidx + idx_diff + window_size));
> |           dotproduct[dot_rev_idxs1] =
> (NumericVector)((NumericVector)cumsum(
> | data[dot_rev_idxs1] * data[dot_rev_idxs2] -
> |
> | data[dot_rev_idxs3] * data[dot_rev_idxs4]) +
> |                                                       dotproduct[i]);
> |
> | I frequently have to cast the results with (NumericVector)...
> |
> | This also happens using Range(). But there is a difference if I use
> Range()
> | this way: my_vector[Range(0,10)]; or Range range = Range(0,
> | 10); my_vector[range];
> |
> | How is the proper way to deal with it?
>
> I am not sure I fully understand what it is that you try to and what you
> are
> struggling with.
>
> Could you construct a small self-contained example?
>
> | Other question: as<NumericVector() doesn't 'cast' right? Does it try to
> | 'convert' formats?
> |
> | And finally, I see that Matlab has a unique way to deal with ranges:
> |
> | Ex.:
> |
> | 1) (1:10) --> 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
> | 2) (10:1) --> returns a vector with size zero
> | 3) (10:-1:1) --> 10, 9, 8, 7, 6, 5, 4, 3, 2, 1
> |
> | Range() shouldn't at least reproduce the R's counterpart and accept
> Range(10,1)
> | == 10:1?
>
> We could probably generalize seq() to count down from 'high' to 'low'. Pull
> requests welcome...
>
> Dirk
>
> --
> https://dirk.eddelbuettel.com
> <https://mailtrack.io/trace/link/3dc4aa6120269dd41ea957abba6a53feaf80a120?url=https%3A%2F%2Fdirk.eddelbuettel.com&userId=3785237&signature=a0cad3f7fa0cc627>
> | @eddelbuettel | edd at debian.org
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.r-forge.r-project.org/pipermail/rcpp-devel/attachments/20210116/fb5c3744/attachment.html>


More information about the Rcpp-devel mailing list