Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- `+` <- function(x,y) x * y
- 2 + 3
- [1] 6
- `%+%` <- function(x,y) pmax(x,y) #(use pmax for vectorization)
- `%*%` <- function(x,y) x + y
- 2 %+% 3
- [1] 3
- 2 %*% 3
- [1] 5
- setClass("tropical",slots = c(x="numeric"))
- # a show method always comes in handy
- setMethod("show","tropical",function(object){
- cat("tropical vectorn")
- print(object@x)
- })
- # its also nice to have an extractor
- setMethod("[","tropical",function(x,i,j,...,drop) new("tropical",x=x@x[i]) )
- setMethod("+",c("tropical","tropical")
- , function(e1,e2) new("tropical", x=pmax(e1@x,e2@x))
- setMethod("*",c("tropical","tropical")
- , function(e1,e2) new("tropical", x= e1@x + e2@x))
- # try it out
- tr1 <- new("tropical",x=c(1,2,3))
- tr2 <- new("tropical",x=c(3,2,1))
- tr1 + tr2
- tr1 * tr2
- # this gives a warning about recycling
- tr1[1:2] + tr2
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement