From tpapp at Princeton.EDU  Wed Dec 13 23:56:32 2006
From: tpapp at Princeton.EDU (Tamas K Papp)
Date: Wed, 13 Dec 2006 17:56:32 -0500
Subject: [Rd] caching frequently used values
In-Reply-To: <45807694.9060707@fhcrc.org>
References: <20061213211729.GA9737@pu100877.student.princeton.edu>
	<45807694.9060707@fhcrc.org>
Message-ID: <20061213225632.GA12440@pu100877.student.princeton.edu>

Hi Robert,

Thanks for your answer.  I would create and environment with
new.env(), but how can I assign and retrieve values based on a
numerical index (the derivative)?  The example of the help page of
assign explicitly shows that assign("a[1]") does not work for this
purpose.

Thanks,

Tamas

On Wed, Dec 13, 2006 at 01:54:28PM -0800, Robert Gentleman wrote:

> the idea you are considering is also, at times, referred to as 
> memoizing. I would not use a list, but rather an environment, and 
> basically you implement something that first looks to see if there is a 
> value, and if not, compute and store. It can speed things up a lot in 
> some examples (and slow them down a lot in others).
> 
> Wikipedia amongst other sources:
>  http://en.wikipedia.org/wiki/Memoization
> 
> Environments have advantages over lists here (if there are lots of 
> matrices the lookup can be faster - make sure you use hash=TRUE), and 
> reference semantics, which you probably want.
> 
> Tamas K Papp 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 at r-project.org mailing list
> >https://stat.ethz.ch/mailman/listinfo/r-devel
> >
> 
> -- 
> Robert Gentleman, PhD
> Program in Computational Biology
> Division of Public Health Sciences
> Fred Hutchinson Cancer Research Center
> 1100 Fairview Ave. N, M2-B876
> PO Box 19024
> Seattle, Washington 98109-1024
> 206-667-7700
> rgentlem at fhcrc.org



[Rd] caching frequently used values
Tamas K Papp