<span style="color:rgb(80,0,80);font-family:arial,sans-serif;font-size:13px">How about clip():</span><br style="color:rgb(80,0,80);font-family:arial,sans-serif;font-size:13px"><span style="color:rgb(80,0,80);font-family:arial,sans-serif;font-size:13px">    p = clip(p, 0.0, 1.0);</span><br>
<div><span style="color:rgb(80,0,80);font-family:arial,sans-serif;font-size:13px"><br></span></div>Great idea!!!<div class="gmail_extra"><br><br><div class="gmail_quote">On Wed, Nov 21, 2012 at 2:00 PM, Romain Francois <span dir="ltr"><<a href="mailto:romain@r-enthusiasts.com" target="_blank">romain@r-enthusiasts.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Le 21/11/12 13:22, Darren Cook a écrit :<div class="im"><br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
I wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">

P.S. I don't think the sugar versions can be made any quicker, because<br>
they have to allocate intermediate vectors, and do more memory copies.<br>
</blockquote></blockquote></blockquote>
<br>
By "sugar versions" I meant vacc4() vs. vacc3()<br>
(<a href="https://gist.github.com/4111256" target="_blank">https://gist.github.com/<u></u>4111256</a>) not pmin() and friends. The vacc4()<br>
code looks like:<br>
<br>
   NumericVector p(age.size());<br>
   p = 0.25 + 0.3 * 1 / (1 - exp(0.04 * age)) + 0.1 * ily;<br>
   p = p * ifelse(female, 1.25, 0.75);<br>
   p = pmax(0,p);<br>
   p = pmin(1,p);<br>
<br>
Each operation copies the whole NumericVector each time, each of which<br>
needs a memory allocation.<br>
</blockquote>
<br></div>
Nope. The NumericVector::operator=( sugar expression ) is used and memory for p is allocated just once, when p is constructed with this lne:<br>
<br>
 NumericVector p(age.size());<br>
<br>
None of the "p = ..." lines allocate memory for p.<br>
<br>
The differences in timing don't come from additional memory allocations.<br>
<br>
Although this thread allows me to identify something that can be improved in the operator=.<div class="im"><br>
<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
vacc3a() does the same pipeline of operations on a single double, which<br>
is therefore likely to be a single CPU register, and the whole of<br>
vacc3a() will be inlined in vacc3().<br>
<br>
<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
Also, I'm probably goinf to add a function that does the equivalent of :<br>
<br>
pmax( b, pmin( a, x ) )<br>
<br>
I'm thinking of naming it pminmax. Any better idea ?<br>
</blockquote>
<br>
I'd expect a function called minmax() to return two values, the min and<br>
max function. The "p" prefix is too subtle for me :-)<br>
<br>
How about clip():<br>
    p = clip(p, 0.0, 1.0);<br>
</blockquote>
<br>
<br></div>
I like "clip".<div class="im HOEnZb"><br>
<br>
<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
This is such a common operation that I'm surprised R doesn't already<br>
have a function like this!<br>
<br>
Darren<br>
<br>
<br>
</blockquote>
<br>
<br>
-- <br></div><div class="im HOEnZb">
Romain Francois<br>
Professional R Enthusiast<br>
<a href="tel:%2B33%280%29%206%2028%2091%2030%2030" value="+33628913030" target="_blank">+33(0) 6 28 91 30 30</a><br>
<br>
R Graph Gallery: <a href="http://gallery.r-enthusiasts.com" target="_blank">http://gallery.r-enthusiasts.<u></u>com</a><br>
`- <a href="http://bit.ly/SweN1Z" target="_blank">http://bit.ly/SweN1Z</a> : SuperStorm Sandy<br>
<br>
blog:            <a href="http://romainfrancois.blog.free.fr" target="_blank">http://romainfrancois.blog.<u></u>free.fr</a><br>
|- <a href="http://bit.ly/RE6sYH" target="_blank">http://bit.ly/RE6sYH</a> : OOP with Rcpp modules<br>
`- <a href="http://bit.ly/Thw7IK" target="_blank">http://bit.ly/Thw7IK</a> : Rcpp modules more flexible<br>
<br></div><div class="HOEnZb"><div class="h5">
______________________________<u></u>_________________<br>
Rcpp-devel mailing list<br>
<a href="mailto:Rcpp-devel@lists.r-forge.r-project.org" target="_blank">Rcpp-devel@lists.r-forge.r-<u></u>project.org</a><br>
<a href="https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel" target="_blank">https://lists.r-forge.r-<u></u>project.org/cgi-bin/mailman/<u></u>listinfo/rcpp-devel</a><br>
</div></div></blockquote></div><br></div>