DasBetterR
bachmeier
no at spam.net
Tue Aug 1 14:00:30 UTC 2023
On Tuesday, 1 August 2023 at 12:37:58 UTC, jmh530 wrote:
> You mention creating a better interface, but recall for rstan
> to work you need to put the data into an R list with names that
> match what is in your program (your stan program could be all
> kinds of complicated with all kinds of different naming
> conventions). For it to be seamless, you would need to be able
> to pass a named D tuple to R easily. rstanarm might be easier
> to create initially, but I don't use that much.
That should easy using a list, [as demonstrated
here](https://github.com/bachmeil/betterr/blob/main/testing/testlist.d).
> Anyway, it looks like you have done the hard work in terms of
> figuring out the code needed to pass a pointer to double[] from
> D to R. Is that something that you will incorporate into the
> broader project?
I'm not sure. There are a couple of issues. First is that you
have to allocate an extra 80 bits at the beginning of the array
so R can use it, and that might be sufficiently inconvenient that
nobody would want to use it. Second, it currently only works for
vectors. I've checked the R source code and creating a new matrix
or array always allocates a new vector for storage, then copies
existing data into it. Data frames would be fine (they're lists
holding vectors of the same length) but the matrix and array
types are also important.
That means I'd have to add my own matrix and array allocation
functions that use custom allocators for this to be practical.
Here's the function that allocates a matrix:
https://github.com/wch/r-source/blob/713cbe56df52e75f7711e176749c1458e0b2bb05/src/main/array.c#L212 A matrix/array is just a wrapper around a vector plus metadata to interpret the elements. The full code that allocates the data in a matrix is `PROTECT(s = allocVector(mode, n));`. In principle, I can just change that to `PROTECT(s = allocVector3(mode, n, &customAllocator));`. If it's that simple, then yes, I'll do more with this.
More information about the Digitalmars-d-announce
mailing list