Re: [Rd] caching frequently used values

From: Gabor Grothendieck <ggrothendieck_at_gmail.com>
Date: Sun 17 Dec 2006 - 00:13:35 GMT

See:

http://finzi.psych.upenn.edu/R/Rhelp02a/archive/83547.html

On 12/13/06, Tamas K Papp <tpapp@princeton.edu> wrote:
> Hi,
>
> I am trying to find an elegant way to compute and store some
> frequently used matrices "on demand". The Matrix package already uses
> something like this for storing decompositions, but I don't know how
> to do it.
>
> The actual context is the following:
>
> A list has information about a basis of a B-spline space (nodes,
> order) and gridpoints at which the basis functions would be evaluated
> (not necessarily the nodes). Something like this:
>
> bsplinegrid <- list(nodes=1:8,order=4,grid=seq(2,5,by=.2))
>
> I need the design matrix (computed by splineDesign) for various
> derivatives (not necessarily known in advance), to be calculated by
> the function
>
> bsplinematrix <- function(bsplinegrid, deriv=0) {
> x <- bsplinegrid$grid
> Matrix(splineDesign(bslinegrid$knots, x, ord=basis$order,
> derivs = rep(deriv,length(x))))
> }
>
> However, I don't want to call splineDesign all the time. A smart way
> would be storing the calculated matrices in a list inside bsplinegrid.
> Pseudocode would look like this:
>
> bsplinematrix <- function(bsplinegrid, deriv=0) {
> if (is.null(bsplinegrid$matrices[[deriv+1]])) {
> ## compute the matrix and put it in the list bsplinegrid$matrices,
> ## but not of the local copy
> }
> bsplinegrid$matrices[[deriv+1]]
> }
>
> My problem is that I don't know how to modify bsplinegrid$matrices
> outside the function -- assignment inside would only modify the local
> copy.
>
> Any help would be appreciated -- I wanted to learn how Matrix does it,
> but don't know how to display the source with s3 methods (getAnywhere
> doesn't work).
>
> Tamas
>
> ______________________________________________
> R-devel@r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel
>



R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel Received on Sun Dec 17 12:29:30 2006

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.1.8, at Sun 17 Dec 2006 - 02:31:00 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-devel. Please read the posting guide before posting to the list.