1

Ok, I managed

x<-seq(-1.2,1.2,0.01)
y<-x
f=outer(x,y,function(x,y)abs(x)+abs(y))
contour(x,y,f,asp=1)

This results exactly into what I was after...

Now let's modify the program

x<-seq(-1.2,1.2,0.01)
y<-x
f=outer(x,y,function(x,y)sum(abs(c(x,y))))

This results in

Error in dim(robj) <- c(dX, dY) : 
  dims [product 58081] do not match the length of object [1]

I don't understand where the problem is...

Kind regards

tschm
  • 2,905
  • 6
  • 33
  • 45
  • Ok, working hard to understand the R philosophy. I have defined the function norm1 <- function(x) sum(abs(x)) where x is a vector. Is this already wrong? Where does all the vectorization come in here? – tschm Sep 10 '18 at 21:06
  • i was hoping there is a somewhat cleaner solution. I don't have to use outer. All I want is some sort of mesh (induced by vectors x and y). Each grid point is a pair of two numbers. For those points I compute almost arbitrary functions... – tschm Sep 10 '18 at 21:11
  • Ah, so sum returns a scalar. Somewhere I have read that everything in R is a vector, even the scalars. They are vectors of length 1? – tschm Sep 10 '18 at 21:12
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/179771/discussion-between-tschm-and-). – tschm Sep 10 '18 at 21:13
  • What works really nice and it not to mentally puzzling is apply(expand.grid(x,y),1,f). Here I create a high and skinny matrix with two columns (all possible combinations of x and y) and then apply f along the rows of this matrix. – tschm Sep 11 '18 at 04:56

0 Answers0