Re: [R] Call by reference or suggest workaround

2010-06-19 Thread Chidambaram Annamalai
On 6/19/10, Romain Francois  wrote:
>
> Le 19/06/10 16:32, Chidambaram Annamalai a écrit :
>>
>> I have written code to compute multi-indices in R [1] and due to the
>> recursive nature of the computation I need to pass around the *same*
>> matrix object (where each row corresponds to one multi-index). As pass
>> by reference wasn't the default behavior I declared a global matrix
>> (mat) and used the<<- operator to write to the global matrix. So the
>> usage would be to call genMultiIndices(3,2) for side effects to
>> generate all multi-indices of length 3 and sum 2. And then access the
>> global matrix.
>>
>> However, after coding this I can't seem to export the global matrix
>> object (in the NAMESPACE file) and still retain mutability since its
>> binding is "locked" (R throws an error). Can I somehow unlock this?
>>
>> Ideally I would want to pass around the same matrix to the recursive
>> function. Is that possible? If not, could someone please suggest a
>> workaround to use the code in an R package?
>>
>> [1]: http://dpaste.com/209186/
>
> Hi,
>
> You can use lexical scoping and you might like ?Recall as well.
>
> genMultiIndices <- function(N, V) {
>  mat <- matrix(nrow=choose(N + V - 1, V), ncol=N)
>   fillMultiIndices <- function(i, j, n, v) {
>   if (n == 1) {
>   mat[i, j] <<- v
>   }
>   else if (v == 0) {
>   mat[i, j:(j + n - 1)] <<- 0L
>   }
>   else {
>   rowOffset <- 0
>   # the first element of each multi-index can be any of 0, 1, 
> ..., v
>   for (k in v:0) {
>   times <- choose((n - 1) + (v - k) - 1, (v - k))
>   mat[(i + rowOffset):(i + rowOffset + times - 1), j] <<- k
>   Recall(i + rowOffset, j + 1, n - 1, v - k)
>   rowOffset <- rowOffset + times
>   }
>   }
>   }
>  fillMultiIndices(1, 1, N, V)
>  mat
> }

Following David Murdoch's advice that's *exactly* what I did :)

> Also, you can consider writing your code in a language that supports
> references, e.g. C++. Here is a start with inline/Rcpp :
>
> require( inline )
> require( Rcpp )
>
> genMultiIndices_internal <-  local({
> inc <- '
> void fillMultiIndices( Rcpp::IntegerMatrix& mat, int i, int j, int n,
> int v ){
>   if( n == 1 ){
>   mat( (i-1), (j-1) ) = v ;
>   } else if( v == 0 ){
>   for( int k=j; k < j+n; k++){
>   mat( (i-1), (k-1) ) = 0 ;
>   }
>   } else {
>   
>   // using the R function
>   // I leave it to you to use a C implementation
>   Function choose( "choose" ) ;
>   
>   int rowOffset = 0 ;
>   int times ;
>   for( int k=v; k>=0; k--){
>   times = as( choose( (n-1) + (v-k) - 1, (v-k) ) );
>   int start = i + rowOffset ;
>   int end   = i + rowOffset + times ;
>   for( int z = start; z < end; z++ ){
>   mat( z-1 , j-1 ) = k ;
>   }
>   fillMultiIndices( mat, i + rowOffset, j+1, n-1, v-k ) ;
>   rowOffset += times ;
>   }
>   }
> }
> '
> code <- '
>   int N  = as( N_) ;
>   int V  = as( V_) ;
>   int NR = as( NR_) ;
>   Rcpp::IntegerMatrix mat( NR, N ) ;
>   fillMultiIndices( mat, 1, 1, N, V ) ;
>   return mat ;
> '
>   .genMultiIndices <- cxxfunction(
>   signature( N_ = "integer", V_ = "integer", NR_ = "integer" ),
>   code, include = inc, plugin = "Rcpp" )
>   function( N, V){
>   .genMultiIndices( N, V, choose(N + V - 1, V) )
>   }
> } )

Thanks for the C++ version! I will look into this if the symbol lookup
due to <<- is too much of a performance issue.

Chillu

__
R-help@r-project.org mailing list
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.


Re: [R] Call by reference or suggest workaround

2010-06-19 Thread Chidambaram Annamalai
Doh! I never thought about nesting functions.

Thanks a bunch!
Chillu

On 6/19/10, Duncan Murdoch  wrote:
> On 19/06/2010 10:32 AM, Chidambaram Annamalai wrote:
>> I have written code to compute multi-indices in R [1] and due to the
>> recursive nature of the computation I need to pass around the *same*
>> matrix object (where each row corresponds to one multi-index). As pass
>> by reference wasn't the default behavior I declared a global matrix
>> (mat) and used the <<- operator to write to the global matrix. So the
>> usage would be to call genMultiIndices(3,2) for side effects to
>> generate all multi-indices of length 3 and sum 2. And then access the
>> global matrix.
>>
>> However, after coding this I can't seem to export the global matrix
>> object (in the NAMESPACE file) and still retain mutability since its
>> binding is "locked" (R throws an error). Can I somehow unlock this?
>>
>> Ideally I would want to pass around the same matrix to the recursive
>> function. Is that possible? If not, could someone please suggest a
>> workaround to use the code in an R package?
>
>
> If you pass the object to multiple functions it will only create a new
> copy when necessary due to modifying it, so the cost of passing it down
> to every recursive call is not so large as you seem to think.  But it's
> not zero cost, passing arguments to functions costs a little bit.
>
> You can avoid this using nested functions and <<- as you had before.
> That is, instead of making your recursive function a top level function
> so that <<- assigns into the namespace environment (which is what's
> causing the error), make it a nested function within another, and do
> your assignments to the outer frame.  For example,
>
> outer <- function(args) {
>
>   mat <- matrix(NA, 3,3)
>
>   recursive <- function() {
> mat <<- newvalue  # does the assignment into outer's frame
> if (!stop) recursive()
>   }
>
> }
>
> Duncan Murdoch
>

__
R-help@r-project.org mailing list
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.


[R] Call by reference or suggest workaround

2010-06-19 Thread Chidambaram Annamalai
I have written code to compute multi-indices in R [1] and due to the
recursive nature of the computation I need to pass around the *same*
matrix object (where each row corresponds to one multi-index). As pass
by reference wasn't the default behavior I declared a global matrix
(mat) and used the <<- operator to write to the global matrix. So the
usage would be to call genMultiIndices(3,2) for side effects to
generate all multi-indices of length 3 and sum 2. And then access the
global matrix.

However, after coding this I can't seem to export the global matrix
object (in the NAMESPACE file) and still retain mutability since its
binding is "locked" (R throws an error). Can I somehow unlock this?

Ideally I would want to pass around the same matrix to the recursive
function. Is that possible? If not, could someone please suggest a
workaround to use the code in an R package?

[1]: http://dpaste.com/209186/

__
R-help@r-project.org mailing list
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.


Re: [R] Derivative of a smooth function

2010-04-03 Thread Chidambaram Annamalai
While this doesn't answer your question, I want to let you know that there
is a proposal for a related improvement within R that will let users compute
(numerically) the derivatives, of any order, of a given function inside of
R. In your case, this means that you will write the smooth spline function,
symbolically f(x), that will interpolate between the points. Using Automatic
Differentiation, the proposed solution, will automatically let you find
f'(x), f''(x), etc.. by using the same function but overloading the meaning
of the arithmetic operators and mathematical functions to act upon a special
data type.

The initial idea
came
from Prof. John Nash who suggested bringing the ability of Automatic
Differentiation to R. We both have, since, collaborated to bring out
adetailed 
proposaloutlining
the various features to be implemented.

Note that, this is being planned to be implemented as part of Google's
Summer of Code program for this year. So, should our proposal be selected,
much more than simple second derivative computation can be accomplished from
within R.

Regards,
Chillu

On Fri, Apr 2, 2010 at 2:06 PM, FMH  wrote:

>
> Dear All,
>
> I've been searching for appropriate codes to compute the rate of change and
> the curvature of  nonparametric regression model whish was denoted by a
> smooth function but unfortunately don't manage to do it. I presume that such
> characteristics from a smooth curve can be determined by the first and
> second derivative operators.
>
> The following are the example of fitting a nonparametric regression model
> via smoothing spline function from the Help file in R.
>
> ###
> attach(cars)
> plot(speed, dist, main = "data(cars)  &  smoothing splines")
> cars.spl <- smooth.spline(speed, dist)
> lines(cars.spl, col = "blue")
> lines(smooth.spline(speed, dist, df=10), lty=2, col = "red")
> legend(5,120,c(paste("default [C.V.] => df =",round(cars.spl$df,1)),"s( * ,
> df = 10)"), col = c("blue","red"), lty = 1:2, bg='bisque')
> detach()
>
> ###
>
>
> Could someone please advice me the appropriate way to determine such
> derivatives on the curves which were fitted by the function above and would
> like to thank you in advance.
>
> Cheers
> Fir
>
>
>
>
>
> __
> R-help@r-project.org mailing list
> 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.
>

[[alternative HTML version deleted]]

__
R-help@r-project.org mailing list
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.


Re: [R] Operator overloading for custom classes

2010-03-23 Thread Chidambaram Annamalai
Thanks!


On Wed, Mar 24, 2010 at 1:01 AM, Sharpie  wrote:

>
>
> Chidambaram Annamalai wrote:
> >
> > Hi,
> >
> > I need some help to get some of the object orientation, specifically the
> > methods that overload the basic arithmetic operations, from sample C++
> > code to R. I don't have experience with such advanced language features
> > inside of R. So I was wondering if some of you could help me out in this
> > regard.
> >
> > {snip}
> >
> >
>
> For S3 objects:
>
>  ?base::Ops
>
>  http://n4.nabble.com/Operator-overloading-td854452.html#a854452
>  http://n4.nabble.com/Overloading-td850827.html#a850828
>
> For S4 objects:
>
>  ?methods::Ops
>
>  http://n4.nabble.com/How-to-define-new-operators-td974842.html#a974842
>
>
> A good resource for reading material on S4 classes, which are more formally
> defined than S3 classes, can be found in the R wiki:
>
>  http://rwiki.sciviews.org/doku.php?id=tips:classes-s4
>
>
> Hope this can get you started!
>
> -Charlie
>
> -
> Charlie Sharpsteen
> Undergraduate-- Environmental Resources Engineering
> Humboldt State University
> --
> View this message in context:
> http://n4.nabble.com/Operator-overloading-for-custom-classes-tp1679467p1679573.html
> Sent from the R help mailing list archive at Nabble.com.
>
> __
> R-help@r-project.org mailing list
> 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.
>

[[alternative HTML version deleted]]

__
R-help@r-project.org mailing list
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.


[R] Operator overloading for custom classes

2010-03-23 Thread Chidambaram Annamalai
Hi,

I need some help to get some of the object orientation, specifically the
methods that overload the basic arithmetic operations, from sample C++
code to R. I don't have experience with such advanced language features
inside of R. So I was wondering if some of you could help me out in this
regard.

I have written a simple demonstration of a forward mode automatic
differentiator in C++ and it is currently hosted on github:
http://github.com/quantumelixir/ad-demo/blob/master/simple.cpp. It uses
simple operator overloading techniques to modify the meaning of the
basic arithmetic operations (+, -, *, /) for the "derivative" type Dual
number class that I have defined. Could you show me how this could be
equivalently done in R? I want to know how to define custom classes and
define the meaning of arithmetic for them.

I had checked for operator overloading in R but could only find the
equivalence of a + b and '+'(a, b) in the R language definition. Could
you show how I could extend the simple object oriented-ness in the C++
code neatly to R?

Thanks a bunch!
Chillu

PS: Sorry that I had mistakenly sent the email to the r-devel list earlier,
instead of the intended r-help list. This is a copy of the same email.

[[alternative HTML version deleted]]

__
R-help@r-project.org mailing list
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.


Re: [R] Reading data file with both fixed and tab-delimited fields

2010-03-02 Thread Chidambaram Annamalai
I tried to shoehorn the read.* functions and match both the fixed width and
the variable width fields
in the data but it doesn't seem evident to me. (read.fwf reads fixed width
data properly but the rest
of the fields must be processed separately -- maybe insert NULL stubs in the
remaining fields and
fill them in later?)

One way is to sidestep the entire issue and convert the structured data you
have into a csv
file using sed (usually available on  most *nix systems) with something like
so:

cat data | sed -r 's/^(..)(.)(..)(.{6})(..)[ \t]*([^ \t]*)[ \t]*([^ \t]*)[
\t]*([^ \t]*)[ \t]*([^ \t]*)[ \t]*([^ \t]*)/\1,\2,\3,\4,\5,\6,\7,\8,\9/' |
less

and see if the output is alright and use the resulting .csv file directly in
R using read.csv

If that does not satisfy you maybe the R Wizards on the list might be able
to point you to a
native R way of doing this possibly using scan? I'm not sure though.

Hope this helps,
Chillu

On Tue, Mar 2, 2010 at 9:42 PM, Marshall Feldman  wrote:

> Hello R wizards,
>
> What is the best way to read a data file containing both fixed-width and
> tab-delimited files? (More detail follows.)
>
> _*Details:*_
> The U.S. Bureau of Labor Statistics provides local area unemployment
> statistics at ftp://ftp.bls.gov/pub/time.series/la/, and the data are
> documented in the file la.txt
> . Each data file has five
> tab-delimited fields:
>
>* series_id
>* year
>* period (codes for things like quarter or month of year)
>* value
>* footnote_codes
>
> The series_id consists of five fixed-width subfields (length in
> parentheses):
>
>* survey abbreviation (2)
>* seasonal code (1)
>* area type code (2)
>* area code (6)
>* measure code (2)
>
> So an example record might be:
>
> LASPS36040003   1990M01 8.8 L
>
> I want to read in the data in one pass and convert them to a data frame
> with the following columns (actual name, class in parentheses):
>
>Survey abbreviation (survey, character)
>Seasonal (seasonal, logical seasonal=T)
>Area type (area_type_code, factor)
>Area (area_code, factor)
>Measure (measure_code, factor)
>Year (year, Date)
>Period (period, factor)
>Value (value, numeric)
>Footnote (footnote_codes, character but see note)
>
> (Regarding the Footnote, I have to look at the data more. If there's
> just one code per record, this will be a factor; if there are multiple,
> it will either be character or a list. For not I'm making it only
> character.)
>
> Currently I can read the data just fine using read.table, but this makes
> series_id the first variable. I want to break out the subfields as
> separate columns.
>
> Any suggestions?
>
> Thanks.
> Marsh Feldman
>
>
>
>
>[[alternative HTML version deleted]]
>
> __
> R-help@r-project.org mailing list
> 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.
>

[[alternative HTML version deleted]]

__
R-help@r-project.org mailing list
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.