Skip to content
Prev 59033 / 398502 Next

argument rationalization

Hi

I am writing a bunch of functions that take two, three or four
arguments.  These functions operate on vectors of the same length; 
but  I want the function
to behave sensibly if one or more arguments are scalars.  "+" does 
this for two arguments:


"+"(1:10,3)    # interpreted as "+"(1:10,rep(3,10))

But my functions can take more arguments.  Say f() takes three:

f(1:10,1:10,1:10)  # default
f(3,1:10,1:10)     # interpret as f(rep(3,10),1:10,1:10)
f(1:10,3,1:10)     # interpret as f(1:10,rep(3,10),1:10)
f(1:10,3,5)        # interpret as f(1:10,rep(3,10),rep(5,10))

and h() takes four:

h(2,4,5,1:10)    # interpret as h(rep(2,10),rep(4,10),rep(5,10),1:10)
h(2,3,1:10,1)    # interpret as h(rep(2,10),rep(3,10),1:10,rep(1:10)
h(1:20,3,1:20,1) # interpret as h(1:20,rep(3,20),1:20,rep(1,20))

I haven't got any that need five yet, but this may change in the future.
How do I implement this desired behaviour nicely?

(I pass the arguments to .C(), which is why I need this).