Advertisement
Guest User

Untitled

a guest
Jul 29th, 2015
179
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.80 KB | None | 0 0
  1. `+` <- function(x,y) x * y
  2. 2 + 3
  3. [1] 6
  4.  
  5. `%+%` <- function(x,y) pmax(x,y) #(use pmax for vectorization)
  6. `%*%` <- function(x,y) x + y
  7. 2 %+% 3
  8. [1] 3
  9. 2 %*% 3
  10. [1] 5
  11.  
  12. setClass("tropical",slots = c(x="numeric"))
  13.  
  14. # a show method always comes in handy
  15. setMethod("show","tropical",function(object){
  16. cat("tropical vectorn")
  17. print(object@x)
  18. })
  19.  
  20. # its also nice to have an extractor
  21. setMethod("[","tropical",function(x,i,j,...,drop) new("tropical",x=x@x[i]) )
  22.  
  23. setMethod("+",c("tropical","tropical")
  24. , function(e1,e2) new("tropical", x=pmax(e1@x,e2@x))
  25. setMethod("*",c("tropical","tropical")
  26. , function(e1,e2) new("tropical", x= e1@x + e2@x))
  27.  
  28.  
  29. # try it out
  30.  
  31. tr1 <- new("tropical",x=c(1,2,3))
  32. tr2 <- new("tropical",x=c(3,2,1))
  33.  
  34.  
  35. tr1 + tr2
  36. tr1 * tr2
  37. # this gives a warning about recycling
  38. tr1[1:2] + tr2
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement