[R] getting a vector of unown size
Jeff Newmiller
jdnewmil at dcn.davis.CA.us
Fri Sep 4 20:51:06 CEST 2015
If you had read the Posting Guide as every posting admonishes you in the footer, you would know that this question about C belongs on R-devel, not R-help. I think a lot of C questions are addressed in the Writing R Extensions document also, so be sure to convey how the documentation failed to communicate an answer to you when you do post again.
If this question were about how to handle returning vectors of unknown size in an R function, I would point out that one end of the spectrum is your over allocation strategy, and the other end is building an intermediate list of atomic data values (rows of data on a file?) and in between those would be a list of small blocks of memory that you allocate as each "current" chunk is filled up. Such a list can be measured after all data are in memory so a block of exactly the right size can be allocated and the data transferred and the list can then be discarded/freed. Which direction to go depends strongly on the variability/size of your particular data and how much time you have to optimize it.
---------------------------------------------------------------------------
Jeff Newmiller The ..... ..... Go Live...
DCN:<jdnewmil at dcn.davis.ca.us> Basics: ##.#. ##.#. Live Go...
Live: OO#.. Dead: OO#.. Playing
Research Engineer (Solar/Batteries O.O#. #.O#. with
/Software/Embedded Controllers) .OO#. .OO#. rocks...1k
---------------------------------------------------------------------------
Sent from my phone. Please excuse my brevity.
On September 4, 2015 4:51:02 AM PDT, Marcelo Kittlein <kittlein at mdp.edu.ar> wrote:
>Hi all!
>
>I have written some code in C for simulating the fate of population
>using a dll loaded in R. During this simulation the population may go
>extinct such that the length of its trajectory is unknown beforehand. I
>
>wonder there is a way to define the size of the result in C and make it
>
>known to R to get the vector appropriately.
>
>I have defined the output size very much larger than necessary to hold
>the result and then shrink the vector to the size where meaningful
>values occur, but this waste much resources and time unnecessarily.
>
>The C code
>
>
>#include <stdlib.h>
>#include <math.h>
>
>
>double min(double a, double b)
>{
>if(a>b)
>return b;
>else
>return a;
>}
>
>double unirand()
>{
>return (rand()+1.0)/(RAND_MAX+1.0);
>}
>
>void RndSBDdemography(double *b, double *d, double *a, double *c,
>double
>*No, double *tmax, double *tiempo, double *Nind)
>{
>double tb, td, n;
>int bc=0;
>tiempo[0]=1.0;
>Nind[0] = *No;
>
>// This loop generates output of variable and a priori unknown size
>
>while (Nind[bc] >= 2 && tiempo[bc] <= *tmax)
>{
>bc = bc + 1;
>tb = -(1 / (*b - *a * Nind[bc-1])) * log(unirand());
>td = -(1 / (*d + *c * Nind[bc-1])) * log(unirand());
>
>tiempo[bc]= tiempo[bc-1] + min(tb, td)/ Nind[bc-1];
>
>if (tb < td) n=1.0 ; else n= -1.0;
>
>Nind[bc] = Nind[bc-1] + n;
>
>}
>
>}
>
>
>The definition of the function call in R
>
>
>RndSBDdemography = function(b=b,d=d,a=a,c=c, No=No, tmax=tmax){
>tiempo=numeric(100000);
>Nind=numeric(100000);
>out=.C("RndSBDdemography",
>b=as.double(b),
>d=as.double(d),
>a=as.double(a),
>c=as.double(c),
>No=as.double(No),
>tmax=as.double(tmax),
>tiempo=as.double(tiempo),
>Nind=as.double(Nind)
>)
>
># trim the output to a size where meaningful values occur
>indi=which(out$tiempo!=0)
>return(data.frame(tiempo=out$tiempo[indi], Nind=out$Nind[indi]))
>}
>
>Hope someone can give ahint on this.
>
>Best Regards
>
>Marcelo Kittlein
>
>______________________________________________
>R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
>https://stat.ethz.ch/mailman/listinfo/r-help
>PLEASE do read the posting guide
>http://www.R-project.org/posting-guide.html
>and provide commented, minimal, self-contained, reproducible code.
More information about the R-help
mailing list