tensor                package:tensor                R Documentation

_T_e_n_s_o_r _p_r_o_d_u_c_t _o_f _a_r_r_a_y_s

_D_e_s_c_r_i_p_t_i_o_n:

     The tensor product of two arrays is notionally an outer product of
     the arrays collapsed in specific extents by summing along the
     appropriate diagonals.  For example, a matrix product is the
     tensor product along the second extent of the first matrix and the
     first extent of the second.  Thus 'A %*% B' could also be
     evaluated as 'tensor(A, B, 2, 1)', likewise 'A %*% t(B)' could be
     'tensor(A, B, 2, 2)'.

_U_s_a_g_e:

     tensor(A, B, alongA = integer(0), alongB = integer(0))

_A_r_g_u_m_e_n_t_s:

    A, B: Numerical vectors, matrices or arrays

  alongA: Extents in 'A' to be collapsed

  alongB: Extents in 'B' to be collapsed

_D_e_t_a_i_l_s:

     This code does the `obvious' thing, which is to perm the "along"
     extents to the end (for 'A') and beginning (for 'B') of the two
     objects and then do a matrix multiplication and reshape.

_V_a_l_u_e:

     Generally, an array with dimension comprising the remaining
     extents of 'A' concatenated with the remaining extents of 'B'.

     If both 'A' and 'B' are completely collapsed then the result is a
     scalar *without* a 'dim' attribute.  This is quite deliberate and
     consistent with the general rule that the dimension of the result
     is the sum of the original dimensions less the sum of the collapse
     dimensions (and so could be zero). A 1D array of length 1 arises
     in a different set of circumstances, eg if 'A' is a 1 by 5 matrix
     and 'B' is a 5-vector then 'tensor(A, B, 2, 1)' is a 1D array of
     length 1.

_S_h_o_r_t_c_u_t_s:

     Some special cases of 'tensor' may be independently useful, and
     these have got shortcuts as follows.

       %*t%   Matrix product 'A %*% t(B)'
       %t*%   Matrix product 't(A) %*% B'
       %t*t%  Matrix product 't(A) %*% t(B)'

_A_u_t_h_o_r(_s):

     Jonathan Rougier, J.C.Rougier@durham.ac.uk

_S_e_e _A_l_s_o:

     'matmult', 'aperm'

_E_x_a_m_p_l_e_s:

       A <- matrix(1:6, 2, 3)
       dimnames(A) <- list(happy = LETTERS[1:2], sad = NULL)
       B <- matrix(1:12, 4, 3)
       stopifnot(A %*% t(B) == tensor(A, B, 2, 2))

       A <- A %o% A
       C <- tensor(A, B, 2, 2)
       stopifnot(all(dim(C) == c(2, 2, 3, 4)))
       D <- tensor(C, B, c(4, 3), c(1, 2))
       stopifnot(all(dim(D) == c(2, 2)))

       E <- matrix(9:12, 2, 2)
       s <- tensor(D, E, 1:2, 1:2)
       stopifnot(s == sum(D * E), is.null(dim(s)))

