[Rd] .Call in R

Joris Meys jorismeys at gmail.com
Fri Nov 18 16:45:41 CET 2011

Because if you calculate the probability and then make uniform values,
nothing guarantees that the sum of those uniform values actually is
larger than 50,000. You only have 50% chance it is, in fact...
Cheers
Joris

On Fri, Nov 18, 2011 at 4:08 PM, Karl Forner <karl.forner at gmail.com> wrote:
> Hi,
>
> A probably very naive remark, but I believe that the probability of sum(
> runif(10000) ) >= 50000 is exactly 0.5. So why not just test that, and
> generate the uniform values only if needed ?
>
>
> Karl Forner
>
> On Thu, Nov 17, 2011 at 6:09 PM, Raymond <gwgc5 at mail.missouri.edu> wrote:
>
>> Hi R developers,
>>
>>    I am new to this forum and hope someone can help me with .Call in R.
>> Greatly appreciate any help!
>>
>>    Say, I have a vector called "vecA" of length 10000, I generate a vector
>> called "vecR" with elements randomly generated from Uniform[0,1]. Both vecA
>> and vecR are of double type. I want to replace elements vecA by elements in
>> vecR only if sum of elements in vecR is greater than or equal to 5000.
>> Otherwise, vecR remain unchanged. This is easy to do in R, which reads
>>    vecA<-something;
>>    vecR<-runif(10000);
>>    if (sum(vecR)>=5000)){
>>       vecA<-vecR;
>>    }
>>
>>
>>    Now my question is, if I am going to do the same thing in R using .Call.
>> How can I achieve it in a more efficient way (i.e. less computation time
>> compared with pure R code above.).  My c code (called "change_vecA.c")
>> using
>> .Call is like this:
>>
>>    SEXP change_vecA(SEXP vecA){
>>         int i,vecA_len;
>>         double sum,*res_ptr,*vecR_ptr,*vecA_ptr;
>>
>>         vecA_ptr=REAL(vecA);
>>         vecA_len=length(vecA);
>>         SEXP res_vec,vecR;
>>
>>         PROTECT(res_vec=allocVector(REALSXP, vec_len));
>>         PROTECT(vecR=allocVector(REALSXP, vec_len));
>>         res_ptr=REAL(res_vec);
>>         vecR_ptr=REAL(vecR);
>>         GetRNGstate();
>>         sum=0.0;
>>         for (i=0;i<vecA_len;i++){
>>              vecR_ptr[i]=runif(0,1);
>>              sum+=vecR_ptr[i];
>>         }
>>         if (sum>=5000){
>>            /*copy vecR to the vector to be returned*/
>>            for (i=0;i<vecA_len;i++){
>>                  res_ptr[i]=vecR_ptr[i];
>>            }
>>         }
>>         else{
>>                /*copy vecA to the vector to be returned*/
>>                for (i=0;i<vecA_len;i++){
>>                      res_ptr[i]=vecA_ptr[i];
>>                }
>>         }
>>
>>         PutRNGstate();
>>         UNPROTECT(2);
>>         resturn(res);
>> }
>> My R wrapper function is
>>        change_vecA<-function(vecA){
>>              .Call("change_vecA",vecA);
>>        }
>>
>>         Now my question is, due to two loops (one generates the random
>> vector and one determines the vector to be returned), can .Call still be
>> faster than pure R code (only one loop to copy vecR to vecA given condition
>> is met)? Or, how can I improve my c code to avoid redundant loops if any.
>> My
>> concern is if vecA is large (say of length 1000000 or even bigger), loops
>> in
>> C code can slow things down.  Thanks for any help!
>>
>>
>>
>>
>>
>> --
>> View this message in context:
>> http://r.789695.n4.nabble.com/Call-in-R-tp4080721p4080721.html
>> Sent from the R devel mailing list archive at Nabble.com.
>>
>> ______________________________________________
>> R-devel at r-project.org mailing list
>> https://stat.ethz.ch/mailman/listinfo/r-devel
>>
>
>        [[alternative HTML version deleted]]
>
> ______________________________________________
> R-devel at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel
>

--
Joris Meys
Statistical consultant

Ghent University
Faculty of Bioscience Engineering
Department of Mathematical Modelling, Statistics and Bio-Informatics

tel : +32 9 264 59 87
Joris.Meys at Ugent.be
-------------------------------
Disclaimer : http://helpdesk.ugent.be/e-maildisclaimer.php
{"ft_lang_label":"__label__en","ft_lang_prob":0.56851697,"math_prob":0.6457206,"size":3707,"snap":"2022-05-2022-21","text_gpt3_token_len":1088,"char_repetition_ratio":0.124223605,"word_repetition_ratio":0.03786575,"special_character_ratio":0.36984083,"punctuation_ratio":0.18321678,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9698781,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2022-01-21T22:38:07Z\",\"WARC-Record-ID\":\"<urn:uuid:e25df578-f921-4d4b-aea9-2377e6b19821>\",\"Content-Length\":\"9521\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:cc4a06cc-3d0f-4ed4-9257-3fc0d120e942>\",\"WARC-Concurrent-To\":\"<urn:uuid:4242238c-6833-4af1-8871-511106e6eeb3>\",\"WARC-IP-Address\":\"129.132.119.195\",\"WARC-Target-URI\":\"https://stat.ethz.ch/pipermail/r-devel/2011-November/062593.html\",\"WARC-Payload-Digest\":\"sha1:QBC3Q7F3TCD4XTGCY3VK5T2BDGDO4AWP\",\"WARC-Block-Digest\":\"sha1:27G6KSV6MKO67X2SQ7LVJ3T427UXUBEE\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2022/CC-MAIN-2022-05/CC-MAIN-2022-05_segments_1642320303717.35_warc_CC-MAIN-20220121222643-20220122012643-00655.warc.gz\"}"}