home *** CD-ROM | disk | FTP | other *** search
/ Big Green CD 8 / BGCD_8_Dev.iso / NEXTSTEP / UNIX / Educational / R-0.49-MI / R-0.49-I / demos / dynload / zero.R < prev   
Encoding:
Text File  |  1997-03-17  |  961 b   |  33 lines

  1. # Notes:
  2. #
  3. # 1. Because of the difference in scoping rules between R and S
  4. #    it is not necessary (and indeed an error) to assign the function
  5. #    in frame 1.  The function f.check can see the function f because
  6. #    it exists in the environment where f is defined.
  7. #
  8. # 2. It is also not necessary (although permissible) to wrap a
  9. #    "list" around the function f.check in the .C call.  R passes
  10. #    such functions through to the underlying C code in "undigested"
  11. #    form.  Corresponding, the underlying C code does not need to
  12. #    extract the function from the passed "list".
  13.  
  14. dyn.load(system.file("demos/dynload", "zero.so"))
  15.  
  16. zero <- function(f, guesses, tol=1e-7) {
  17.     f.check <- function(x) {
  18.         x <- f(x)
  19.         if(!is.numeric(x)) stop("Need a numeric result")
  20.         as.double(x)
  21.     }
  22.     z <- .C("zero_find",
  23.         f.check,
  24.         ans=as.double(guesses),
  25.         as.double(tol))
  26.     z$ans[1]
  27. }
  28.  
  29. cube1 <- function(x) (x^2+1)*(x-1.5)
  30. x0 <- zero(cube1, c(0,5))
  31. print(x0)
  32. print(x0,15)
  33.