<html xmlns:v="urn:schemas-microsoft-com:vml" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:w="urn:schemas-microsoft-com:office:word" xmlns:m="http://schemas.microsoft.com/office/2004/12/omml" xmlns="http://www.w3.org/TR/REC-html40">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta name="Generator" content="Microsoft Word 15 (filtered medium)">
<!--[if !mso]><style>v\:* {behavior:url(#default#VML);}
o\:* {behavior:url(#default#VML);}
w\:* {behavior:url(#default#VML);}
.shape {behavior:url(#default#VML);}
</style><![endif]--><style><!--
/* Font Definitions */
@font-face
        {font-family:"Cambria Math";
        panose-1:2 4 5 3 5 4 6 3 2 4;}
@font-face
        {font-family:Calibri;
        panose-1:2 15 5 2 2 2 4 3 2 4;}
/* Style Definitions */
p.MsoNormal, li.MsoNormal, div.MsoNormal
        {margin:0in;
        font-size:11.0pt;
        font-family:"Calibri",sans-serif;}
a:link, span.MsoHyperlink
        {mso-style-priority:99;
        color:blue;
        text-decoration:underline;}
span.EmailStyle18
        {mso-style-type:personal-reply;
        font-family:"Calibri",sans-serif;
        color:windowtext;}
.MsoChpDefault
        {mso-style-type:export-only;
        font-size:10.0pt;}
@page WordSection1
        {size:8.5in 11.0in;
        margin:1.0in 1.0in 1.0in 1.0in;}
div.WordSection1
        {page:WordSection1;}
--></style><!--[if gte mso 9]><xml>
<o:shapedefaults v:ext="edit" spidmax="1026" />
</xml><![endif]--><!--[if gte mso 9]><xml>
<o:shapelayout v:ext="edit">
<o:idmap v:ext="edit" data="1" />
</o:shapelayout></xml><![endif]-->
</head>
<body lang="EN-US" link="blue" vlink="purple" style="word-wrap:break-word">
<div class="WordSection1">
<p class="MsoNormal">Francisco,<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">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:<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal"><a href="https://github.com/coatless/r-to-armadillo/blob/master/src/seq.cpp">https://github.com/coatless/r-to-armadillo/blob/master/src/seq.cpp</a>
<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">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:<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">  // Initialization:<o:p></o:p></p>
<p class="MsoNormal">  Rcpp::NumericVector my_vec = Rcpp::NumericVector::create(1, 2, 3, 4, 5, 6);<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">  // Subset and Assign to 5:<o:p></o:p></p>
<p class="MsoNormal">  Rcpp::IntegerVector subset_idx = Rcpp::Range(0, 3);<o:p></o:p></p>
<p class="MsoNormal">  my_vec[subset_idx] = 20;<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">  // Subset Positions:<o:p></o:p></p>
<p class="MsoNormal"> Rcpp::NumericVector subset_result = my_vec[Rcpp::Range(4, 5)];<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">Hope it helps.<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">Best,<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">JJB<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<div style="border:none;border-top:solid #B5C4DF 1.0pt;padding:3.0pt 0in 0in 0in">
<p class="MsoNormal"><b><span style="font-size:12.0pt;color:black">From: </span></b><span style="font-size:12.0pt;color:black">Rcpp-devel <rcpp-devel-bounces@lists.r-forge.r-project.org> on behalf of Francisco Bischoff <fbischoff@med.up.pt><br>
<b>Date: </b>Friday, January 15, 2021 at 8:05 PM<br>
<b>To: </b>Dirk Eddelbuettel <edd@debian.org><br>
<b>Cc: </b>"rcpp-devel@lists.r-forge.r-project.org" <rcpp-devel@lists.r-forge.r-project.org><br>
<b>Subject: </b>Re: [Rcpp-devel] Ranges and Casting<o:p></o:p></span></p>
</div>
<div>
<p class="MsoNormal"><o:p> </o:p></p>
</div>
<div>
<div>
<p class="MsoNormal">Thanks for the tips.<o:p></o:p></p>
<div>
<p class="MsoNormal"><o:p> </o:p></p>
</div>
<div>
<p class="MsoNormal">Still, <span style="font-family:"Courier New"">Range() </span>
is the correct way to write:<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"><o:p> </o:p></p>
</div>
<div>
<p class="MsoNormal"><span style="font-family:"Courier New"">NumericVector my_vec(200);</span><o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"><o:p> </o:p></p>
</div>
<div>
<p class="MsoNormal"><span style="font-family:"Courier New"">my_vec(Range(0,10)) = 10;</span><o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"><o:p> </o:p></p>
</div>
<div>
<p class="MsoNormal">?<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"><o:p> </o:p></p>
</div>
<div>
<p class="MsoNormal">Thanks in advance<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"><o:p> </o:p></p>
</div>
<div>
<p class="MsoNormal"><o:p> </o:p></p>
</div>
<div>
<p class="MsoNormal"><o:p> </o:p></p>
</div>
<div>
<p class="MsoNormal"><br clear="all">
<o:p></o:p></p>
<div>
<div>
<div>
<div>
<div>
<div>
<div>
<div>
<div>
<div>
<div>
<div>
<div>
<div>
<p class="MsoNormal"><span style="font-size:9.5pt">--<br>
</span><span style="font-size:18.0pt">Francisco Bischoff, MD, MSc</span><o:p></o:p></p>
</div>
<div>
<p class="MsoNormal">Faculty of Medicine of the University of Porto, Portugal<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"><o:p> </o:p></p>
</div>
<div>
<p class="MsoNormal">- Master of Medical Informatics | topic: time series<br>
- Research Associate | <a href="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$" target="_blank">
artificial intelligence for health</a> @ <a href="https://urldefense.com/v3/__https:/mailtrack.io/trace/link/89b15c4212d5d6c8d4ba354ec92ab002ba9a96ef?url=http*3A*2F*2Fcintesis.eu&userId=3785237&signature=5fa97b31373fa664__;JSUl!!DZ3fjg!oa0yWHu4WaDeqycuBEcPRuw3vEvJrYARPPIAzJNsnxdet6loFCJ7O95nLR4B0QFIUzg$" target="_blank">
cintesis.eu</a><br>
- Teaching Assistant | department of community medicine, information and health decision sciences @
<a href="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$" target="_blank">
med.up.pt</a><br>
<br>
ORCID: 0000-0002-5301-8672 | Mendeley: francisco-bischoff | Google: tCeA0uUAAAAJ | ResearcherID: H-8261-2016 | ResearchGate: Francisco_Bischoff | CiênciaID B413-E0A0-DE8D | LinkedIn: franzbischoff<o:p></o:p></p>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
<p class="MsoNormal"><o:p> </o:p></p>
</div>
</div>
<p class="MsoNormal"><o:p> </o:p></p>
<div>
<div>
<p class="MsoNormal">On Sat, Jan 16, 2021 at 1:54 AM Dirk Eddelbuettel <<a href="mailto:edd@debian.org">edd@debian.org</a>> wrote:<o:p></o:p></p>
</div>
<blockquote style="border:none;border-left:solid #CCCCCC 1.0pt;padding:0in 0in 0in 6.0pt;margin-left:4.8pt;margin-right:0in">
<p class="MsoNormal"><br>
On 16 January 2021 at 01:35, Francisco Bischoff wrote:<br>
| About the Range() function, I think it should handle decreasing ranges<br>
| too...<br>
| But, idk if using Matlabs approach or R approach:<br>
| <br>
| R's:<br>
| <br>
| a <- 1<br>
| b <- 10<br>
| <br>
| print(a:b)<br>
| 1 2 3 4 5 6 7 8 9 10<br>
| print(b:a)<br>
| 10 9 8 7 6 5 4 3 2 1<br>
| <br>
| Matlab's<br>
| a = 1;<br>
| b = 10;<br>
| disp(a:b);<br>
| 1 2 3 4 5 6 7 8 9 10<br>
| disp(b:a);<br>
| numeric(0)<br>
| disp(b:-1:a)<br>
| 10 9 8 7 6 5 4 3 2 1<br>
| <br>
| I think that we are in the R domain, so we should use Range(b, a) normally<br>
| (my humble opinion).<br>
<br>
Hm but I think the Range class in Rcpp is not the same as R's seq(). It is<br>
used internally in a few places and may not generalize well to the 'b:-1:a'<br>
use.<br>
<br>
I had a quick look, and Armadillo may not have anything directly relevant<br>
either. linspace() is close but not quite the same.<br>
<br>
I would probably just write myself a little helper function.<br>
<br>
| About Casting, this is a good example:<br>
| <br>
| Rcout << Range(0, 10) << std::endl;<br>
| <br>
| This does not compute...<br>
| <br>
| What should I do?<br>
| <br>
| Rcout << as<IntegerVector>(Range(0, 10)) << std::endl;<br>
| or<br>
| Rcout << (IntegerVector)(Range(0, 10)) << std::endl;<br>
<br>
That can happen as template expressions can get in the way. Alternatives are<br>
maybe using Armadillo types for, again, just using a quick loop and printing<br>
elements.<br>
<br>
Rcpp has a lot of functions. But it doesn't have "all of R's functions" and<br>
should not be seen as aiming for that.<br>
<br>
Dirk<br>
<br>
-- <br>
<a href="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$" target="_blank">https://dirk.eddelbuettel.com</a>
 | @eddelbuettel | <a href="mailto:edd@debian.org" target="_blank">edd@debian.org</a><o:p></o:p></p>
</blockquote>
</div>
<p class="MsoNormal"><span style="border:solid windowtext 1.0pt;padding:0in"><img border="0" width="32" height="32" style="width:.3333in;height:.3333in" id="_x0000_i1025" src="cid:~WRD0000.jpg" alt="Image removed by sender."></span><o:p></o:p></p>
</div>
</div>
</body>
</html>