# [Rd] outer not applying a constant function

Gebhardt, Albrecht Albrecht.Gebhardt at aau.at
Sun Mar 19 10:14:56 CET 2017

```Hi,

the function outer can not apply a constant function as in the last line of the following example:

> xg <- 1:4
> yg <- 1:4
> fxyg <- outer(xg, yg, function(x,y) x*y)
> fconstg <- outer(xg, yg, function(x,y) 1.0)
Error in outer(xg, yg, function(x, y) 1) :
dims [product 16] do not match the length of object 

Of course there are simpler ways to construct a constant matrix, that is not my point.

It happens for me in the context of generating matrices of partial derivatives, and if on of these partial derivatives happens to be constant it fails.

So e.g this works:

library(Deriv)
f <- function(x,y) (x-1.5)*(y-1)*(x-1.8)+(y-1.9)^2*(x-1.1)^3
fx <- Deriv(f,"x")
fy <- Deriv(f,"y")
fxy <- Deriv(Deriv(f,"y"),"x")
fxx <- Deriv(Deriv(f,"x"),"x")
fyy <- Deriv(Deriv(f,"y"),"y")

fg   <- outer(xg,yg,f)
fxg  <- outer(xg,yg,fx)
fyg  <- outer(xg,yg,fy)
fxyg <- outer(xg,yg,fxy)
fxxg <- outer(xg,yg,fxx)
fyyg <- outer(xg,yg,fyy)

And with

f <- function(x,y) x+y

it stops working. Of course I can manually fix this for that special case, but thats not my point. I simply thought "outer" should be able to handle constant functions.

Best regards

Albrecht

[[alternative HTML version deleted]]

```

More information about the R-devel mailing list