# tensor() function and sets

Jonathan Rougier Jonathan Rougier <J.C.Rougier@durham.ac.uk>
Wed, 21 Jul 1999 18:34:36 +0100 (BST)

```Hi Martin,

> after looking a bit longer at your examples and some thinking,
> I'm now quite convinced that  your tensor() function *IS* useful for us
> and comes quite handy in several cases.
> {real S/R hackers doing all this in their sleep, but most of us
>  are not "real S/R hackers" in this sense ...}

Well, I wrote it because I have to use it all the time!  A lot of the work
of our group at Durham involves means and variances of arrays rather than
vectors. I spent some time unzipping all the arrays into vectors and then
using matrix operations (hence I wrote the kronecker() function), before I
realised that the natural thing to do was to work in tensors and think of
variances as following from the expectations of outer products.

> I think it still would be nice to have reasonable default arguments da & db.

I did consider this, but bitter experience has taught me to be
conservative and always specify explicitly the extents through which the
outer product collapses.  I think it is important to know and state
exactly what is required in these operations.  Furthermore, as I look
through the calculations I have done recently, no obvious pattern that
might be a reasonable default emerges.  However, I am open suggestions!
In the meantime, I'll write a help page for the function as it stands
tomorrow morning and send it on to you.

Regarding sets, might I suggest that if included in the base then
intersect() might be generalised to arbitrary numbers of arguments.  You
will probably howl with anguish at the recursion, but I use

"intersect" <-
function(x, ...)
if (nargs() == 1) x else x[x %in% Recall(...)]

which seems to be effective.

Cheers, Jonathan.

Jonathan Rougier                       Science Laboratories
Department of Mathematical Sciences    South Road
University of Durham                   Durham DH1 3LE

"[B]egin upon the precept ... that the things we see are to be
weighed in the scale with what we know"  (Meredith, 1879, The Egoist)

-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-devel mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-devel-request@stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._

```