[Rcpp-devel] Ranges and Casting
Balamuta, James Joseph
balamut2 at illinois.edu
Sat Jan 16 20:26:32 CET 2021
Francisco,
Based on the opening post, I’d probably push you more toward Armadillo as Dirk was in the previous message. Using linspace<>(), may be a drop-in replacement for seq() with the appropriate helper functions. Long ago (~4+ years now) I wrote a few, see:
https://github.com/coatless/r-to-armadillo/blob/master/src/seq.cpp
That said, Range is definitely preferred inside of subsets operations into Rcpp-like data structures. However, the subset and assign operation is a bit problematic with template expansion. So, the given example needs to have an intermediary:
// Initialization:
Rcpp::NumericVector my_vec = Rcpp::NumericVector::create(1, 2, 3, 4, 5, 6);
// Subset and Assign to 5:
Rcpp::IntegerVector subset_idx = Rcpp::Range(0, 3);
my_vec[subset_idx] = 20;
// Subset Positions:
Rcpp::NumericVector subset_result = my_vec[Rcpp::Range(4, 5)];
Hope it helps.
Best,
JJB
From: Rcpp-devel <rcpp-devel-bounces at lists.r-forge.r-project.org> on behalf of Francisco Bischoff <fbischoff at med.up.pt>
Date: Friday, January 15, 2021 at 8:05 PM
To: Dirk Eddelbuettel <edd at debian.org>
Cc: "rcpp-devel at lists.r-forge.r-project.org" <rcpp-devel at lists.r-forge.r-project.org>
Subject: Re: [Rcpp-devel] Ranges and Casting
Thanks for the tips.
Still, Range() is the correct way to write:
NumericVector my_vec(200);
my_vec(Range(0,10)) = 10;
?
Thanks in advance
--
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://urldefense.com/v3/__https:/mailtrack.io/trace/link/1765b288bbce44d233e851a8d862ec594378aa04?url=http*3A*2F*2Fcintesis.eu*2Fai4health*2F&userId=3785237&signature=1b62d7a180130981__;JSUlJSU!!DZ3fjg!oa0yWHu4WaDeqycuBEcPRuw3vEvJrYARPPIAzJNsnxdet6loFCJ7O95nLR4ByfJyNzg$> @ cintesis.eu<https://urldefense.com/v3/__https:/mailtrack.io/trace/link/89b15c4212d5d6c8d4ba354ec92ab002ba9a96ef?url=http*3A*2F*2Fcintesis.eu&userId=3785237&signature=5fa97b31373fa664__;JSUl!!DZ3fjg!oa0yWHu4WaDeqycuBEcPRuw3vEvJrYARPPIAzJNsnxdet6loFCJ7O95nLR4B0QFIUzg$>
- Teaching Assistant | department of community medicine, information and health decision sciences @ med.up.pt<https://urldefense.com/v3/__https:/mailtrack.io/trace/link/32f5f446b7bc0d597241524764b3636b5a75d466?url=http*3A*2F*2Fmed.up.pt&userId=3785237&signature=cfbca25f5fe210b3__;JSUl!!DZ3fjg!oa0yWHu4WaDeqycuBEcPRuw3vEvJrYARPPIAzJNsnxdet6loFCJ7O95nLR4BoT_L--M$>
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 Sat, Jan 16, 2021 at 1:54 AM Dirk Eddelbuettel <edd at debian.org<mailto:edd at debian.org>> wrote:
On 16 January 2021 at 01:35, Francisco Bischoff wrote:
| 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).
Hm but I think the Range class in Rcpp is not the same as R's seq(). It is
used internally in a few places and may not generalize well to the 'b:-1:a'
use.
I had a quick look, and Armadillo may not have anything directly relevant
either. linspace() is close but not quite the same.
I would probably just write myself a little helper function.
| 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;
That can happen as template expressions can get in the way. Alternatives are
maybe using Armadillo types for, again, just using a quick loop and printing
elements.
Rcpp has a lot of functions. But it doesn't have "all of R's functions" and
should not be seen as aiming for that.
Dirk
--
https://dirk.eddelbuettel.com<https://urldefense.com/v3/__https:/mailtrack.io/trace/link/ec16eaacf00108f6b925d8cd55d469ea736d3313?url=https*3A*2F*2Fdirk.eddelbuettel.com&userId=3785237&signature=5ff5161e5ff16475__;JSUl!!DZ3fjg!oa0yWHu4WaDeqycuBEcPRuw3vEvJrYARPPIAzJNsnxdet6loFCJ7O95nLR4BxG2v_1M$> | @eddelbuettel | edd at debian.org<mailto:edd at debian.org>
[Image removed by sender.]
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.r-forge.r-project.org/pipermail/rcpp-devel/attachments/20210116/ed12186e/attachment.html>
More information about the Rcpp-devel
mailing list