Re: [R] oddity with method definition
On Tue, 28 Aug 2007, Faheem Mitha wrote: Warning message: in the method signature for function "bar" no definition for class: âÂÂbazâ in: matchSignature(signature, fdef, where) I'm being dense. That just means that the class "baz" has not been defined, presumably. Faheem.__ R-help@stat.math.ethz.ch 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] oddity with method definition
Hi Dr Lumley, Thanks to you and Dr. Murdoch for your helpful explanations. See below. On Mon, 27 Aug 2007, Thomas Lumley wrote: On Mon, 27 Aug 2007, Faheem Mitha wrote: setClass("foo", representation(x="numeric")) bar <- function(object) { return(0) } bar.foo <- function(object) { print([EMAIL PROTECTED]) } setMethod("bar", "foo", bar.foo) bar(f) # bar(f) gives 1. Not for me. It gives bar(f) Error: object "f" not found Error in bar(f) : error in evaluating the argument 'object' in selecting a method for function 'bar' However, if I do f = new("foo", x= 1) first, it gives 1. bar <- function(object) { return(0) } Here you have masked the generic bar() with a new function bar(). Redefining bar() is the problem, not the second setMethod(). bar.foo <- function(object) { print([EMAIL PROTECTED]) } setMethod("bar", "foo", bar.foo) Because there was a generic bar(), even though it is overwritten by the new bar(), setMethod() doesn't automatically create another generic. f = new("foo", x= 1) bar(f) # bar(f) gives 0, not 1. Because bar() isn't a generic function bar function(object) { return(0) } If you had used setGeneric() before setMethod(), as recommended, your example would have done what you expected, but it would still have wiped out any previous methods for bar() -- eg, try setMethod("bar","baz", function(object) print("baz")) before you redefine bar(), and notice that getMethod("bar","baz") no longer finds it. Actually, it does not appear to be wiped. getMethod finds the "baz" version and the "foo" version, but it seems to use the default even for foo, which is of course wrong. Am I missing something? See below. Thanks. I do get this warning message. Don't know what it means, though. Warning message: in the method signature for function "bar" no definition for class: âÂÂbazâ in: matchSignature(signature, fdef, where) Faheem. setClass("foo", representation(x="numeric")) bar <- function(object) { return(0) } setGeneric("bar") bar.foo <- function(object) { print([EMAIL PROTECTED]) } setMethod("bar", "foo", bar.foo) setMethod("bar", "baz", function(object) print("baz")) bar <- function(object) { return(0) } bar.foo <- function(object) { print([EMAIL PROTECTED]) } setMethod("bar", "foo", bar.foo) f = new("foo", x= 1) bar(f) # returns 0 * getMethod("bar", "baz") Method Definition: function (object) print("baz") Signatures: object target "baz" defined "baz" getMethod("bar", "foo") Method Definition: function (object) { print([EMAIL PROTECTED]) } Signatures: object target "foo" defined "foo"__ R-help@stat.math.ethz.ch 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] oddity with method definition
Just wondered about this curious behaviour. I'm trying to learn about classes. Basically setMethod works the first time, but does not seem to work the second time. Faheem. * setClass("foo", representation(x="numeric")) bar <- function(object) { return(0) } bar.foo <- function(object) { print([EMAIL PROTECTED]) } setMethod("bar", "foo", bar.foo) bar(f) # bar(f) gives 1. bar <- function(object) { return(0) } bar.foo <- function(object) { print([EMAIL PROTECTED]) } setMethod("bar", "foo", bar.foo) f = new("foo", x= 1) bar(f) # bar(f) gives 0, not 1. __ R-help@stat.math.ethz.ch 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] binary for Rmath standalone library for Visual C++ 8.0?
On Mon, 19 Jun 2006, Faheem Mitha wrote: > > Hi, > > Does anyone happen to have compiled the Rmath standalone library for Visual > C++ 8.0, and can offer it for me to download? > > I'm not that familiar with Microsoft or Visual C++. I mostly use Linux. > However, my boss needs to use this library on Windows to compile some code I > wrote, and I don't have the time or (probably) the expertise to compile it > myself. > > Help would be much appreciated. Thanks. Forgot to say that this is on Windows XP (Professional x64 Edition). Sorry. Faheem. __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
[R] binary for Rmath standalone library for Visual C++ 8.0?
Hi, Does anyone happen to have compiled the Rmath standalone library for Visual C++ 8.0, and can offer it for me to download? I'm not that familiar with Microsoft or Visual C++. I mostly use Linux. However, my boss needs to use this library on Windows to compile some code I wrote, and I don't have the time or (probably) the expertise to compile it myself. Help would be much appreciated. Thanks. Faheem. __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
[R] comment in src/nmath/dgamma.c
Hi, In src/nmath/dgamma.c the comment at the top says * DESCRIPTION * * Computes the density of the gamma distribution, * * 1/s (x/s)^{a-1} exp(-x/s) *p(x;a,s) = --- *(a-1)! * * where `s' is the scale (= 1/lambda in other parametrizations) * and `a' is the shape parameter ( = alpha in other contexts). Maybe I'm missing something, but shouldn't the denominator be Gamma(a) (since a is not necessarily an integer)? Faheem. __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
[R] type of list elements in .Call
Dear People, Here is something I do not understand. Consider * foo.cc * #include #include #include using std::cout; using std::endl; extern "C" { SEXP printlst(SEXP lst); } SEXP printlst(SEXP lst) { for(int i=0; i dyn.load("foo.so") .Call("printlst", list(c(1,2),c(3,4))) 0 1072693248 0 1074266112 [[1]] [1] 1 2 [[2]] [1] 3 4 If I replace INTEGER by REAL I get dyn.load("foo.so") .Call("printlst", list(c(1,2),c(3,4))) 1 2 3 4 [[1]] [1] 1 2 [[2]] [1] 3 4 as I would expect. I thought that if the vectors in the list could be regarded as integer vectors, they would be, but apparently not. Is there any way I can tell R to regard them as integer vectors? Thanks.Faheem. __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Re: [R] Rinternals.h and iostream don't play nice together'
On Sun, 30 Jan 2005, Paul Roebuck wrote: and if you use the following instead of "Rinternals.h"? extern "C" { #include } Still the same errors. As Dirk pointed out, putting iostream first makes the errors go away in either case. 'Course I'm assuming you read that part in the 'R-exts.pdf' about using C++ iostreams with R being best avoided? If you really need to do so anyway, I'll give you a link to my iostream manipulator source that interfaces with the R console. It handles the scenario described in section 4.6 of above document but requires the GNU C++ compiler (g++) in order to work though. I'm just using iostreams for debugging purposes, not part of the code per se. Since I am trying to write an R wrapper for a prexisting C++ library, I already have print functions using iostreams, which I would prefer to reuse if possible. For my limited needs, iostreams works fine. I also use g++, and would be interested in seeing your code in any case. Thanks. Faheem. __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Re: [R] Rinternals.h and iostream don't play nice together'
On Sun, 30 Jan 2005, Dirk Eddelbuettel wrote: On 30 January 2005 at 18:03, Faheem Mitha wrote: | Consider the following file. | | *** | foo.cc | *** | #include | #include | #include | *** | | R CMD SHLIB foo.cc Two changes are required: You absolutely do need extern "C" ... to get C headers in with C++, and the other is a simple reordering -- not sure where I learned that can help at times ... [EMAIL PROTECTED]:/tmp> cat foo.cc #include extern "C" { #include #include } Ok, but R.h and Rinternals.h are already wrapped in extern "C", as you can check, so this is superfluous. I've verified that putting iostream first fixes this, but this is really a workaround, though still useful for me. Thanks. Faheem. __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
[R] Rinternals.h and iostream don't play nice together'
Hi, Consider the following file. *** foo.cc *** #include #include #include *** R CMD SHLIB foo.cc gives scads of errors. I've use C++ extensively with R before (using C linkage) but not with Rinternals.h. I'm puzzled. The errors make no sense to me. Am I doing something wrong? Is it impossible to use iostream here for some reason? I'm using Debian Sarge with R 2.0.1 and gcc 3.3.5. Please cc me; I'm not subscribed. Thanks in advance. Faheem. R CMD SHLIB foo.cc g++ -I/usr/lib/R/include -fPIC -g -O2 -c foo.cc -o foo.o In file included from /usr/include/c++/3.3/bits/locale_facets.h:528, from /usr/include/c++/3.3/bits/basic_ios.h:44, from /usr/include/c++/3.3/ios:51, from /usr/include/c++/3.3/ostream:45, from /usr/include/c++/3.3/iostream:45, from foo.cc:3: /usr/include/c++/3.3/bits/codecvt.h:110:52: macro "length" passed 4 arguments, but takes just 1 In file included from /usr/include/c++/3.3/bits/locale_facets.h:528, from /usr/include/c++/3.3/bits/basic_ios.h:44, from /usr/include/c++/3.3/ios:51, from /usr/include/c++/3.3/ostream:45, from /usr/include/c++/3.3/iostream:45, from foo.cc:3: /usr/include/c++/3.3/bits/codecvt.h:109: error: parse error before `const' /usr/include/c++/3.3/bits/codecvt.h:113: error: semicolon missing after declaration of `std::__codecvt_abstract_base<_InternT, _ExternT, _StateT>' /usr/include/c++/3.3/bits/codecvt.h:115: error: ISO C++ forbids defining types within return type /usr/include/c++/3.3/bits/codecvt.h:115: error: extraneous `int' ignored /usr/include/c++/3.3/bits/codecvt.h:115: error: non-member function ` std::__codecvt_abstract_base<_InternT, _ExternT, _StateT> std::max_length()' cannot have `const' method qualifier /usr/include/c++/3.3/bits/codecvt.h:115: error: semicolon missing after declaration of `class std::__codecvt_abstract_base<_InternT, _ExternT, _StateT>' /usr/include/c++/3.3/bits/codecvt.h: In function `int std::max_length()': /usr/include/c++/3.3/bits/codecvt.h:115: error: invalid use of `this' in non-member function /usr/include/c++/3.3/bits/codecvt.h: At global scope: /usr/include/c++/3.3/bits/codecvt.h:117: error: parse error before `protected' /usr/include/c++/3.3/bits/codecvt.h:122: error: destructors must be member functions /usr/include/c++/3.3/bits/codecvt.h:122: error: virtual outside class declaration /usr/include/c++/3.3/bits/codecvt.h: In function `void std::__codecvt_abstract_base()': /usr/include/c++/3.3/bits/codecvt.h:122: error: `void std::__codecvt_abstract_base()' redeclared as different kind of symbol /usr/include/c++/3.3/bits/codecvt.h:65: error: previous declaration of ` template class std::__codecvt_abstract_base<_InternT,_ExternT,_StateT>' /usr/include/c++/3.3/bits/codecvt.h:65: error: previous non-function declaration `template class std::__codecvt_abstract_base<_InternT,_ExternT,_StateT>' /usr/include/c++/3.3/bits/codecvt.h:122: error: conflicts with function declaration `void std::__codecvt_abstract_base()' /usr/include/c++/3.3/bits/codecvt.h: At global scope: /usr/include/c++/3.3/bits/codecvt.h:125: error: syntax error before `(' token /usr/include/c++/3.3/bits/codecvt.h:131: error: syntax error before `(' token /usr/include/c++/3.3/bits/codecvt.h:135: error: syntax error before `(' token /usr/include/c++/3.3/bits/codecvt.h:141: error: virtual outside class declaration /usr/include/c++/3.3/bits/codecvt.h:141: error: non-member function `int std::do_encoding()' cannot have `const' method qualifier /usr/include/c++/3.3/bits/codecvt.h:141: error: function `int std::do_encoding()' is initialized like a variable /usr/include/c++/3.3/bits/codecvt.h:144: error: virtual outside class declaration /usr/include/c++/3.3/bits/codecvt.h:144: error: non-member function `bool std::do_always_noconv()' cannot have `const' method qualifier /usr/include/c++/3.3/bits/codecvt.h:144: error: function `bool std::do_always_noconv()' is initialized like a variable /usr/include/c++/3.3/bits/codecvt.h:147: error: parse error before `&' token /usr/include/c++/3.3/bits/codecvt.h:148: error: virtual outside class declaration /usr/include/c++/3.3/bits/codecvt.h:148: error: non-member function `int std::do_length(...)' cannot have `const' method qualifier /usr/include/c++/3.3/bits/codecvt.h:148: error: function `int std::do_length(...)' is initialized like a variable /usr/include/c++/3.3/bits/codecvt.h:151: error: virtual outside class declaration /usr/include/c++/3.3/bits/codecvt.h:151: error: non-member function `int std::do_max_length()' cannot have `const' method qualifier /usr/include/c
Re: [R] converting R objects to C types in .Call
Hi Barry, Thanks for your reply. On Mon, 24 Jan 2005, Barry Rowlingson wrote: Firstly, R is expecting an SEXP as a return value! Ouch... I haven't found anything that says this explicitly, but it looks like .Call expects a SEXP to be returned to R. At any rate, trying to use void instead gives a segfault. And secondly, your SEXP chstr is still a vector - so you need to get an element from it. If there's only one element, then that'll be the zeroth element. Here's my code: #include #include #include SEXP testfn(SEXP chstr) { char * charptr = CHAR(VECTOR_ELT(chstr,0)); printf("%s", charptr); return(chstr); } - I'm just returning the same object back in the return() statement, and using VECTOR_ELT(chstr,0) to get to the 0'th element. SO in R: > foo = .Call("testfn","fnord") should print 'fnord' and return foo as "fnord". > foo = .Call("testfn",c("bar","baz")) will print 'bar' and return foo as c("bar","baz") Thanks for your help. That works fine. Faheem. __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
[R] converting R objects to C types in .Call
Dear People, I'm trying to write an R wrapper for a C++ library, using .Call. I've never used .Call before. I'm currently having some difficulties converting a R character string to a C one. Here is a little test program. #include #include #include SEXP testfn(SEXP chstr) { char * charptr = CHAR(chstr); printf("%s", charptr); } This compiles without problems, but when I try to run this as .Call("testfn", "foo") I get a segmentation fault. I am sure I am making an obvious mistake, but can someone help me to sort it out? Thanks in advance. Please cc me, I'm not subscribed. Faheem. __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Re: [R] help with random numbers and Rmpi
On Mon, 15 Dec 2003, Na Li wrote: > On 15 Dec 2003, Faheem Mitha stated: > > > > So, the question would be how to get the R slaves to load up the library > > and I don't see any obvious way of doing this (using snow functions). > > Say you need boot on each slave, call this on the master, > > clusterEvalQ (cl, library (boot)) > > where cl is the cluster created by make*Cluster () functions from snow. > > This is in the manual. Yes, I see. Sorry, I should have figured this out myself yesterday. Thanks. Faheem. __ [EMAIL PROTECTED] mailing list https://www.stat.math.ethz.ch/mailman/listinfo/r-help
Re: [R] help with random numbers and Rmpi
On Mon, 15 Dec 2003, A.J. Rossini wrote: > Yes, and no. > > Re-read the CPH-statcomp lab, and look at the bootstrap example, which > solves the same problem. > > Look carefully -- it has to initialize the library on each node. > If you are just loading the library manually, just do it on each node; > if you are using libraries, just do that. Ah, I see. I need to use > clusterEvalQ(cl,dyn.load("mg.so")) in my case. I should have realised the slaves were already running, after makeCluster was called. It works now. Excellent. Thanks for your help. Faheem. __ [EMAIL PROTECTED] mailing list https://www.stat.math.ethz.ch/mailman/listinfo/r-help
Re: [R] help with random numbers and Rmpi
On Sun, 14 Dec 2003, A.J. Rossini wrote: > > Did you make sure that you loaded the library that contains the C > code on the remote hosts? No, I only loaded the library on the local node (master). However, I'm not sure how I should do so. If I simply start up R on the remote hosts and load up the library, I presume this would not be any use, since the master spawns its own R slaves? So, the question would be how to get the R slaves to load up the library and I don't see any obvious way of doing this (using snow functions). I must be missing something. Do I need to work at a lower level? Thanks for replying. Faheem. __ [EMAIL PROTECTED] mailing list https://www.stat.math.ethz.ch/mailman/listinfo/r-help
Re: [R] help with random numbers and Rmpi
On Mon, 1 Dec 2003, A.J. Rossini wrote: > Faheem Mitha <[EMAIL PROTECTED]> writes: > > > So, can this (parallelization at the C level) be done without running a > > bunch of C slaves along the lines I had previously written? Any examples > > would be helpful. > > How much heavy lifting happens before you spawn the slaves, and can > that not be moved to R? > > Your best bet is to read the SNOW code for handling SPRNG/RSPRNG, > otherwise. I've tried to use snow as suggested. I have a R function mg.randvec which generates a vector of random variates. This function calls a C routine via the .C call. This works fine if I call it like say... * > mg.randvec(3,2,10,5) $val [1] -1.9967464 -1.8634205 -0.7459255 -1.7591047 -1.7811685 -1.9953316 [7] -1.7932502 -1.9823565 -1.7999789 -1.0501179 -1.9679886 0.1484859 [13] 0.5768898 1.9117889 1.9366872 -1.3847453 -1.5554107 -1.4933195 [19] -1.8508795 -1.6715850 -1.8951212 -1.8900167 -1.1630852 -1.3989748 [25] -1.9400337 -1.6774471 -1.8136065 -1.8685709 -1.9119879 -1.3378416 * However, with snow I get ** > clusterCall(cl,mg.randvec,3,2,10,5) [[1]] [1] "Error in .C(\"rocftp\", as.integer(k), as.integer(len), as.double(theta), : \n\tC/Fortran function name not in load table\n" attr(,"class") [1] "try-error" [[2]] [1] "Error in .C(\"rocftp\", as.integer(k), as.integer(len), as.double(theta), : \n\tC/Fortran function name not in load table\n" attr(,"class") [1] "try-error" [[3]] [1] "Error in .C(\"rocftp\", as.integer(k), as.integer(len), as.double(theta), : \n\tC/Fortran function name not in load table\n" attr(,"class") [1] "try-error" In the cluster case it seems to have difficulty loading up the C routine. I think snow is working Ok, because basic examples like the following work. > clusterCall(cl,runif,3) [[1]] [1] 0.1527429 0.1134621 0.8663094 [[2]] [1] 0.2256776 0.8981241 0.1120226 [[3]] [1] 0.2371450 0.5090693 0.2776081 ** Can anyone tell me what I am doing wrong? All data files is shared across all three machines I am using (AFS space). Thanks in advance. Faheem. __ [EMAIL PROTECTED] mailing list https://www.stat.math.ethz.ch/mailman/listinfo/r-help
Re: [R] compile error with C code and standalone R math C library
On Sat, 13 Dec 2003, Dirk Eddelbuettel wrote: > On Sat, Dec 13, 2003 at 07:44:46PM -0500, Faheem Mitha wrote: > > I just went back to an old piece of C code. On trying to compile it with > > the R math standalone C library I got the following error. Can anyone > > enlighten me what I am doing wrong, if anything? C file (rr-sa.c) follows. > > > > I'm on Debian sarge. I'm running R version 1.8.1. Gcc is version > > 3.3.1. > [...] > > faheem ~/co/rr/trunk>gcc -o rr rr-sa.c -lRmath -lm > > /usr/lib/gcc-lib/i486-linux/3.3.2/../../../libRmath.so: undefined > > reference to `Rlog1p' > > collect2: ld returned 1 exit status > > The linker tells you that it cannot find object code for a function Rlog1p. > So let's check: > > [EMAIL PROTECTED]:~> grep Rlog1p /usr/include/Rmath.h > [EMAIL PROTECTED]:~> grep log1p /usr/include/Rmath.h > double log1p(double); /* = log(1+x) {care for small x} */ > [EMAIL PROTECTED]:~> > > Turns out that there is none defined in Rmath.h, but log1p exists. This may > have gotten renamed since you first wrote your code. Maybe I am being dense, but how is this my fault? I am not using either Rlog1p or log1p in my code (as far as I can see). Thanks for replying. Faheem. __ [EMAIL PROTECTED] mailing list https://www.stat.math.ethz.ch/mailman/listinfo/r-help
[R] compile error with C code and standalone R math C library
Dear People, I just went back to an old piece of C code. On trying to compile it with the R math standalone C library I got the following error. Can anyone enlighten me what I am doing wrong, if anything? C file (rr-sa.c) follows. I'm on Debian sarge. I'm running R version 1.8.1. Gcc is version 3.3.1. Thanks in advance. Faheem. ** faheem ~/co/rr/trunk>gcc -o rr rr-sa.c -lRmath -lm /usr/lib/gcc-lib/i486-linux/3.3.2/../../../libRmath.so: undefined reference to `Rlog1p' collect2: ld returned 1 exit status ** rr-sa.c ** #include #include #define T 3 #define INITVAL 1 #define MATHLIB_STANDALONE #define THETA 2 #include #include void advance(double *node, int *current_pos, double newval); void retreat(double *node, int *current_pos); double new_val(double *node, double theta, int current_pos); double accept_prob(double *node, double theta, int current_pos); double accept_prob_fn(double a, double y); int main() { int currentpos = 0; int i; double newval, node[T+2], theta = THETA, p, u; set_seed(time(NULL), clock()); node[0]=0; for(i=1;i<=T;i++) node[i]= INITVAL;/* for simplicity choose all values the same */ node[T+1]=0; for(i=0; currentpos < T; i++) { u = unif_rand(); newval = new_val(node, theta, currentpos); p = accept_prob(node, theta, currentpos); printf("current position is %u\n",currentpos); printf("uniform random variable determining acceptance/rejection is %g\n",u); printf("value of acceptance probability is %g\n",p); printf("value of proposed new variable is %g\n",newval); if(u < p) advance(node,¤tpos,newval); else retreat(node, ¤tpos); } for(i=1;i <= T; i++) printf("value of node %u is %g\n",i,node[i]); return 0; } /* function that moves chain one step forward in the event of an acceptance */ void advance(double *node, int *current_pos, double newval) { *current_pos = *current_pos + 1; node[*current_pos] = newval; } /* function that moves the chain backwards in the event of a rejection*/ void retreat(double *node, int *current_pos) { /* need special handling for small values of current_pos */ if(*current_pos >= 1) *current_pos = *current_pos - 1; else if(*current_pos == 0) ; /* do nothing, already at beginning */ } /* generate new candidate value with appropriate distribution */ double new_val(double *node, double theta, int current_pos) { double a, u, y; u = unif_rand(); a = node[current_pos] + node[current_pos+2]; if (a != 0) y = (1/a)*log( ( exp(a*theta) - exp(-a*theta) )*u + exp(-a*theta) ); else /* degenerate case (for a=0) is Unif[-theta,theta] */ y = 2*theta*u - theta; return y; } /* generate acceptance probabilities for candidates*/ /* NB: This assumes: (a) That all the initial values of the starting state are set to -1 (b) That theta > 1. */ double accept_prob(double *node, double theta, int current_pos) { double a, p, y, num, denom, leftval, rtval; a = node[current_pos + 1]; y = node[current_pos] + node[current_pos+2]; num = accept_prob_fn(a, y); leftval = accept_prob_fn(a, -theta + node[current_pos+2]); rtval = accept_prob_fn(a, theta + node[current_pos+2]); denom = fmax2(leftval, rtval); /* function in Rmath which returns max of two doubles */ p = num/denom; assert(p>=0 && p <=1); return p; } /* auxilary function for accept_prob */ double accept_prob_fn(double a, double y) { double f; if(y != 0) f = (exp(y) - exp(-y))/(y*exp(a*y)); else f = 2; return f; } __ [EMAIL PROTECTED] mailing list https://www.stat.math.ethz.ch/mailman/listinfo/r-help
Re: [R] help with random numbers and Rmpi
On Mon, 1 Dec 2003, A.J. Rossini wrote: > Faheem Mitha <[EMAIL PROTECTED]> writes: > > > On Mon, 1 Dec 2003, A.J. Rossini wrote: > > > >> > >> use snow. > >> > >> The general approach highlighted in > >> > >> http://www.analytics.washington.edu/~rossini/courses/cph-statcomp > >> > >> in Lab 4 works with Rmpi as well. > > > > Forgive my cluelessness, but are the slaves spawned by snow R slaves or C > > slaves? I need to work low level with C. > > R slaves. > > If you need to work with C slaves, you need to be pretty careful with > SPRNG. > > Is there a reason you can't use C from R code? If so, you get SPRNG > for free, if not, it's a royal pain. It would be great if I could do it that way. I've used the .C interface to R (for example) for ages. However, I need to parallise the code at the C level, which is where I do nearly all of the heavy lifting. More explicitly, I want the C code to do different things on different processors, pass data back and forth and so on, where everything would be controlled from the R level. My impression is that if I was to use C from R, then the parallelization would only be done at the R level. So, can this (parallelization at the C level) be done without running a bunch of C slaves along the lines I had previously written? Any examples would be helpful. > I'd be careful about SPRNG's MPI code, as well -- works with MPICH, > but it's been touchy with LAM-MPI, at least with the versions (LAM, > MPICH) I've worked with. And versions seem to be somewhat important. Yes, the whole SPRNG thing seems rather difficult. Thanks for your help. Faheem. __ [EMAIL PROTECTED] mailing list https://www.stat.math.ethz.ch/mailman/listinfo/r-help
Re: [R] help with random numbers and Rmpi
On Mon, 1 Dec 2003, A.J. Rossini wrote: > > use snow. > > The general approach highlighted in > > http://www.analytics.washington.edu/~rossini/courses/cph-statcomp > > in Lab 4 works with Rmpi as well. Forgive my cluelessness, but are the slaves spawned by snow R slaves or C slaves? I need to work low level with C. I'm looking at (for example) http://www.stat.uiowa.edu/~luke/R/cluster/cluster.html Thanks. Faheem. __ [EMAIL PROTECTED] mailing list https://www.stat.math.ethz.ch/mailman/listinfo/r-help
[R] help with random numbers and Rmpi
Dear People, This may not be the right place to ask a question about Rmpi, but I don't know of a better one. I am trying to get a simple program working using Rmpi with the model of 1 R master and n C slaves. What I am trying to do is have each of the C slaves generate a random number from U[0,1], and then have the master collect all n numbers as a vector and output it. However even doing this is rather over my head. I'm trying to use rsprng and sprng, but I am sure what I am currently doing is wrong. I enclose a first attempt. Any suggestions would be appreciated. Thanks in advance. Faheem. * rand.R * library(Rmpi) rand <- function () { if (mpi.comm.size(1) > 1) stop ("It seems some slaves running on comm 1.") mpi.comm.spawn("./rand") mpi.intercomm.merge(2,0,1) mpi.init.sprng() free.sprng() #does this function exist here? rdata <- double(sum(mpi.comm.size(1))) out <- mpi.gather(0, 2, rdata) ##this isn't right mpi.comm.free() out } * rand.c * #include #include int main(int argc, char **argv) { double rand; double* randarray; MPI_Comm slavecomm, all_processes; /*Initialize MPI*/ MPI_Init(&argc, &argv); MPI_Comm_get_parent(&slavecomm); MPI_Intercomm_merge(slavecomm, 1, &all_processes); /*How many processes are there?*/ MPI_Comm_size(all_processes, &size); /*Which one am I?*/ MPI_Comm_rank(all_processes, &rank); init_sprng() rand = sprng(); free.sprng() randarray = (double *)malloc(sizeof(double)*size); /*Gather random numbers from all C slave processes*/ /* Using randarray doesn't make sense since this should correspond to the rdata vector in the R master process */ MPI_GAther(&rand, 1, 1, MPI_DOUBLE, randarray, 1, MPI_DOUBLE, 0, all_processes); /*All done*/ MPI_Comm_free(&all_processes); MPI_Finalize(); exit(0); } __ [EMAIL PROTECTED] mailing list https://www.stat.math.ethz.ch/mailman/listinfo/r-help
[R] compiling documentation into dvi form
Dear People, Perhaps I am missing something, but I;m trying to get the documentation for the Rmpi package in a printable form. I've tried doing faheem ~>R CMD Rd2dvi /usr/local/lib/R/site-library/Rmpi/ Hmm ... looks like a package Converting Rd files to LaTeX ... /usr/local/lib/R/site-library/Rmpi//man/Rmpi.Rd but the result is basically just one page, which seems to include just one command. What am I doing wrong, if anything? BTW, the Rmpi package does not seem to be in CRAN. It can be found at http://www.stats.uwo.ca/faculty/yu/Rmpi/ Thanks in advance. Faheem. __ [EMAIL PROTECTED] mailing list https://www.stat.math.ethz.ch/mailman/listinfo/r-help
Re: [R] combining mathematical notation and value substitution
On Sun, 22 Jun 2003, Thomas Lumley wrote: > On Sun, 22 Jun 2003, Faheem Mitha wrote: > > If I'm doing this correctly, R does not seem to think it is a call. > > > > > is.call("Monotonic Multigamma run (" * n == len * ", " * theta == t1 > > * ").") > > Error in "Monotonic Multigamma run (" * n : > > non-numeric argument to binary operator > > R is trying to *evaluate* > "Monotonic Multigamma run ("* n==len etc > which doesn't work. Remember, is.call(), like any normal function, will > be passed the *value* of its arguments. Hmm. I'm trying to distinguish in my mind the value of an expression and the expression itself. For some reason it reminds me of the following exchange, from "Through the Looking-Glass". ** "...The name of the song is called 'Haddocks' Eyes.'" "Oh, that's the name of the song, is it?" Alice said, trying to feel interested. "No, you don't understand," the Knight said, looking a little vexed. "That's what the name is called. The name really is 'The Aged, Aged Man.'" "Then I ought to have said 'That's what the song is called'?" Alice corrected herself. "No you oughtn't: that's another thing. The song is called 'Ways and Means' but that's only what it's called, you know!" "Well, what is the song then?" said Alice, who was by this time completely bewildered. "I was coming to that," the Knight said. "The song really is 'A-sitting On a Gate': and the tune's my own invention." ** > You could try > is.call(quote("Monotonic Multigamma run("*n==len)) > which is TRUE. > > > It considers it a valid R expression though. > > > > > (mode(expression("Monotonic Multigamma run (" * n == len * ", " * theta > > == t1 * ")."))) > > [1] "expression" > > > > That's because expression() returns an expression. > > > > > The clearest description I have seen of a call is in S Poetry, where it > > says > > > > "Mode call represents objects that are calls to a function. The first > > component of a call is the name (mode name) of the function being called. > > The rest of the call is the arguments given." > > > > This certainly is how calls are constructed using call(...), but I'm not > > sure how it fits in with an expression like the one above. What is the > > function being called in that case, for example? > > Well, we can find out. It must be either * or ==, but it isn't immediately > obvious which one ends up at the top level > > > thing <- quote("Monotonic Multigamma Run ("*n==len* ", " * theta > ==t1*").") > > mode(thing) > [1] "call" > > length(thing) > [1] 3 > > thing[[1]] > == > > thing[[2]] > "Monotonic Multigamma Run (" * n == len * ", " * theta > > thing[[3]] > t1 * ")." > > mode(thing[[2]]) > [1] "call" > > mode(thing[[3]]) > [1] "call" > > thing[[2]][[1]] > == > > thing[[3]][[1]] > * > > So it is a call to ==, with two arguments, each itself a call. The first > arguemetn is also a call to == and the second is a call to *. And so on in > a tree structure. This is very interesting. I had convinced myself that an expression could not become a call unless created explicitly by call, because it could not know out of all the possible call structures which one to turn the expression into. However, it appears this is not the case. So, naturally, this makes me wonder, what rules are used to make the structure, out of all the various possibilities. For example, the function in the call could have corresponded to one of the *'s, and then the rest of the structure would have been different. And is this rule part of the language definition? Faheem. __ [EMAIL PROTECTED] mailing list https://www.stat.math.ethz.ch/mailman/listinfo/r-help
Re: [R] combining mathematical notation and value substitution
On Sat, 21 Jun 2003, Uwe Ligges wrote: > Faheem Mitha wrote: > > Can you go into a little more detail here about why alpha here is a > > call? > > Thomas already told it and ?expression says as well: "expression returns > a vector of mode "expression" containing its arguments as unevaluated > ``calls''." Yes, but it is just a call by definition then? If you put anything in the place of alpha it would be a call? > > I'm not terribly clear what a call is. If it so similar to an > > expression, what distinguishes it from an expression, and why do we > > need two similar concepts like this? > > Obviously a question for Bill Venables. I admit I've not thought about > it before. In this context the article > W. N. Venables (2002). Programmers Niche, R News, 2(2): 2426, > ISSN 1609-3631, http://CRAN. R-project.org/doc/Rnews/, > and > Venables & Ripley (2000): S Programming, Springer. > are nice references. I've got "S Programming" and spent some of yesterday reading it. It doesn't go into detail into the difference between expressions and calls. It does have a table on pg 66 which gives examples of different modes. Faheem. __ [EMAIL PROTECTED] mailing list https://www.stat.math.ethz.ch/mailman/listinfo/r-help
Re: [R] combining mathematical notation and value substitution
> > "Monotonic Multigamma run (" * n == len * ", " * theta == t1 * > > > > Is this a valid expression? My understanding of an expression is that it > > contains one more more statements. > > That's only part of the expression. This is the full expression > "Monotonic Multigamma run (" * n == len * ", " * theta == t1 * ")." > > Now, this looks very strange, but if it looked like >a* n==len * b * theta==t1 * d > it would be a perfectly reasonable product of five terms, two of which > are logical expressions. I think in fact that it is a call, not an > expression, but in this case it doesn't matter. > > In Uwe's example a, b, and d were strings. The expression is still > lexically valid, but it can't be evaluated any more. That's ok, since it > isn't supposed to be evaluated. So multiplication is lexically valid in R even between logical expression and strings? The * really corresponded to multiplication, then? Hmm. I see the choice of * was so that it would not appear in the final math expression produced by title. Ingenious. > What you can use in the mathematical annotation functions is parsed but > unevaluated R code. You can get this mostly easily as the output of > quote(), expression() or substitute(). > > quote("Parameter " * theta==1) > expression("Parameter " * theta==1) > substitute("Parameter " * theta==t, list(t=1)) > > For the purposes of mathematical annotation these are all equivalent, > though the second returns an `expression' and the other two return a > `call'. > > The second form is occasionally needed, as in > legend(locator(1), lty=1:2, legend=expression(alpha,beta)) > which I don't think you can do any other way. > > You can think of an expression as a vector of calls, so > > expression(alpha,beta)[1] > expression(alpha) This is an expression (according to ?expression). > > expression(alpha,beta)[[1]] > alpha This is a call. Can you go into a little more detail here about why alpha here is a call? I'm not terribly clear what a call is. If it so similar to an expression, what distinguishes it from an expression, and why do we need two similar concepts like this? Also, would it not be more accurate to describe an expression as a list of calls? Since, the [[ ]] applied to a list returns a component of that list, which in this case is apparently a call. > which is what the manual was trying to say (I think). Much of the time > you can ignore the difference between `call' and `expression' objects. Faheem. __ [EMAIL PROTECTED] mailing list https://www.stat.math.ethz.ch/mailman/listinfo/r-help
Re: [R] combining mathematical notation and value substitution
On Fri, 20 Jun 2003, Uwe Ligges wrote: > Faheem Mitha wrote: [snip] > > main=paste("Monotonic Multigamma run (n=", > > deparse(substitute(len)),", ", > > expression(theta),"=", deparse(substitute(theta)),").") > > No, it won't work that way, because you have to specify an S expression > in order to get mathematical annotation. An expression within paste() > will be converted to a character string. > > What you ca do is the other way round: > > > t1 <- theta # you cannot use theta as variable and math. symbol > plot(1:10, main = >substitute("Monotonic Multigamma run (" * n == len * ", " * > theta == t1 * ").", list(len = len, t1 = t1))) > > > See also ?plotmath or that small article in R News: > Ligges (2002): R Help Desk: Automation of Mathematical Annotation in > Plots. R News 2(3), 32-34. Thank you for your help. Your expression works correctly. I've been trying to understand why. I read your article, and I had already looked previously at ?plotmath. However, the syntax of the above is still not clear to me. "Substitute" takes an expression as argument and substitutes the values of any variables given in the second argument, correct? However, I am not sure what it understands by "Monotonic Multigamma run (" * n == len * ", " * theta == t1 * Is this a valid expression? My understanding of an expression is that it contains one more more statements. The R-lang manual says, not entirely intelligibly * "In R one can have objects of type `"expression"'. An _expression_ contains one or more statements. A statement is a syntactically correct collection of tokens. Expression objects are special language objects which they contain parsed, but unevaluated R statements. The main difference is that an expression object can contain several such expressions. Another more subtle difference is that objects of type `"expression"' are only evaluated when explicitly passed to `eval', whereas other language objects may get evaluated in some unexpected cases. An expression object behaves much like a list and its components should be accessed in the same way as the components of a list." * Firstly, I'm having difficulty parsing the sentence "Expression objects are special language objects which they contain parsed, but unevaluated R statements." Should the word "which" be removed? Then, the next sentence says "The main difference is that an expression object can contain several such expressions." The main difference from what? Lastly, if "An expression object behaves much like a list" what are the individual components? Are they expressions, and if so, how are they separated from each other? I apologise for my cluelessness. In any case, an expression object contains several statements, or several expressions (not quite clear on the distinction). So, what are the statements here? You have character strings adjoined by eg. `n == len' which I agree is a sensible statement. I'm guessing that R thinks of each character string as a separate statement. If I do "foo" on the R command line it echoes it back to me, but "foo" "bar" gives an error. Can you explain what the `*' are doing here? Perhaps separating statements? I've looked for use of this in a similar context but was unable to find anything. Thanks again. BTW, does "Introductory Statistics with R" by Peter Dalgaard, contain disscussion of language issues, including "computing on the language" stuff? Faheem. __ [EMAIL PROTECTED] mailing list https://www.stat.math.ethz.ch/mailman/listinfo/r-help
[R] Re: combining mathematical notation and value substitution
On Fri, 20 Jun 2003, Faheem Mitha wrote: > Hmm. Tried this, didn't work either. Inspired by pg 32 of "R for > Beginners". > > main=paste("Monotonic Multigamma run", >as.expression(substitute(n==length,list(length=len))), >as.expression(substitute(theta==th,list(th=theta Sorry, stupid of me. I guess that paste converts things back to character strings, so that won't work. Faheem. __ [EMAIL PROTECTED] mailing list https://www.stat.math.ethz.ch/mailman/listinfo/r-help
[R] Re: combining mathematical notation and value substitution
On Fri, 20 Jun 2003, Faheem Mitha wrote: > > Dear People, > > I need to make a label which both contains math notation as well as > substitutes a value for an object. In the following label, len and theta > are one dim variables, and I am substituting their values appropriately. > This label looks fine except that I want the greek symbol for theta to > appear instead of the word `theta'. How can I do so most easily? [snip] > main=paste("Monotonic Multigamma run (n=", > deparse(substitute(len)),", ", > expression(theta),"=", deparse(substitute(theta)),").") Hmm. Tried this, didn't work either. Inspired by pg 32 of "R for Beginners". main=paste("Monotonic Multigamma run", as.expression(substitute(n==length,list(length=len))), as.expression(substitute(theta==th,list(th=theta Faheem. __ [EMAIL PROTECTED] mailing list https://www.stat.math.ethz.ch/mailman/listinfo/r-help
[R] combining mathematical notation and value substitution
Dear People, I need to make a label which both contains math notation as well as substitutes a value for an object. In the following label, len and theta are one dim variables, and I am substituting their values appropriately. This label looks fine except that I want the greek symbol for theta to appear instead of the word `theta'. How can I do so most easily? I don't understand the underlying mechanisms well enough to work it out for myself. Thanks in advance. Please cc me. I'm not subscribed to the list. Faheem. main=paste("Monotonic Multigamma run (n=", deparse(substitute(len)),", ", expression(theta),"=", deparse(substitute(theta)),").") __ [EMAIL PROTECTED] mailing list https://www.stat.math.ethz.ch/mailman/listinfo/r-help
[R] cannot create postscript files with trellis graphics inside afunction
Dear People, If I define foo <-function() { x <- rnorm(500) trellis.device(postscript, file="plot.ps") densityplot(~x) dev.off() } and call foo() then plot.ps is just a blank plot. However, if I extract the lines inside foo and run them, ie. x <- rnorm(500) trellis.device(postscript, file="plot.ps") densityplot(~x) dev.off() then plot.ps is created without problems. The former method worked with the regular postscript() device, which I used to use all the time inside functions. I've never used lattice/trellis graphics before. Is this a bug or am I doing something wrong? Faheem. __ [EMAIL PROTECTED] mailing list https://www.stat.math.ethz.ch/mailman/listinfo/r-help