# [R] Converting a function from Splus to R

Berend Hasselman bhh at xs4all.nl
Wed Feb 29 14:30:13 CET 2012

On 29-02-2012, at 14:02, Freddy Hernández wrote:

> I have a function written for Splus, when I run it in R I obtain get an error
> because the function has the elements "0.d0" and "2.d0". How can I change it
> to run in R?
>
> The function can be found in page 230 from
> http://www.stat.wisc.edu/~mchung/teaching/stat471/stat_computing.pdf
>
> Function is as follows:
>
> gauher <- function(n) {# Gauss-Hermite: returns x,w so that
> #\int_-\infty^\infty exp(-x^2) f(x) dx \doteq \sum w_i f(x_i)
> EPS <- 3.e-14
> PIM4 <- .7511255444649425D0
> MAXIT <- 10
> m <- trunc((n+1)/2)
> x <- w <- rep(-1,n)
> for (i in 1:m) {
> if (i==1) {
> z <- sqrt(2*n+1)-1.85575*(2*n+1)^(-.16667)
> } else if(i==2) {
> z <- z-1.14*n^.426/z
> } else if (i==3) {
> z <- 1.86*z-.86*x[1]
> } else if (i==4) {
> z <- 1.91*z-.91*x[2]
> } else {
> z <- 2.*z-x[i-2]
> }
> for (its in 1:MAXIT) {
> p1 <- PIM4
> p2 <- 0.d0
> for (j in 1:n) {
> p3 <- p2
> p2 <- p1
> p1 <- z*sqrt(2.d0/j)*p2-sqrt((j-1)/j)*p3
> }
> pp <- sqrt(2.d0*n)*p2
> z1 <- z
> z <- z1-p1/pp
> if(abs(z-z1) <= EPS) break
> }
> x[i] <- z
> x[n+1-i] <- -z
> w[i] <- 2/(pp*pp)
> w[n+1-i] <- w[i]
> }
> list(x=x,w=w)
> }

I know nothing of S.
But why don't you just try   "0.e0" and "2.e0" or better 0 and 2?
I'm guessing that these are numbers.

Berend