Hi all,

I have been searching all sorts of documentation, reference cards, cheat sheets but can't find why R's
crossprod(A, B) which is identical to A%*%B
does not produce the same as Matlabs
cross(A, B)
Supposedly both calculate the cross product, and say so, or where do I go wrong?

R is only doing sums in the crossprod however, as indicated by

(z <- crossprod(1:4)) # = sum(1 + 2^2 + 3^2 + 4^2) in
http://127.0.0.1:13073/library/base/html/crossprod.html

R's result of
g <- rbind(c(1, 2, 3), c( 4, 5, 6))
h <- rbind(c(4, 5, 6), c(1, 2, 3))
c <-  crossprod(g, h)
is

> c
    [,1] [,2] [,3]
[1,]    8   13   18
[2,]   13   20   27
[3,]   18   27   36

because there are only sums involved, however, I cant work out ALL of them for the 3D example, something like
        [,1]                     [,2]                     [,3]
[1,] 1*4+4*1 ? ? # #sum of elementwise multiplication of first column of first matrix with first column of second matrix of second matrix
[2,]   ?                            2*5+5*2      ?
? cant figure out how these were derived, definitely this is NOT identical to e.g. http://en.wikipedia.org/wiki/Cross_product

I can work it out for the tcrossprod
> c <- tcrossprod(g, h)
> c
    [,1] [,2]
[1,]   32   14
[2,]   77   32

because
           [,1]                     [,2]
[1,] 1*4+2*5+3*6 1*1+2*2+3*3 #sum of elementwise multiplication of first line of first matrix with first line of second matrix sum of elementwise multiplication of first line of first matrix with second line of second matrix [2,] 4*4+5*5+6*6 4*1+5*2+6*3 #sum of elementwise multiplication of second line of first matrix with first line of second matrix sum of elementwise multiplication of second line of first matrix with second line of second matrix


outer product is something completely different again , no need to show it here


Matlab's result in contrast of
g = [1 2 3 ; 4 5 6]
h = [4 5 6; 1 2 3]
c = cross(g,h)

is
c =

   -3     6    -3
    3    -6     3


which is in accordance to
http://en.wikipedia.org/wiki/Cross_product
and any math text book

>> d = dot(g, h)

d =

   32    50    72
which is of course something different again

What exactly does crossprod mean in R? and why does R's cross product not do the same as indicated in references for cross products?

base::crossprod
doesnt tell me much:
function (x, y = NULL)
.Internal(crossprod(x, y))
<environment: namespace:base>

Where exactly do I look up the core of the function on Windows ? sorry, I HAVE been trying to find this for hours now
Thanks in advance,
Susanne
______________________________________________
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

Reply via email to