python - Name and function for the following matrix operation in R -


i trying reproduce [50 x 50] matrix generated python as:

n = 50                                          = np.linspace(-5, 5, n).reshape(-1,1) b = np.sum(a**2, 1).reshape(-1, 1) + np.sum(b**2, 1) 

using r. problem result sort of matrix, cannot reproduced through:

n = 50      = seq(-5, 5, length.out = n) b = a^2 + b^2 

which generates vector.

i not familiar object names in python, see np.sum(a**2, 1).reshape(-1, 1) produces looks [50 x 1] column vector:

array([[  2.50000000e+01],        [  2.30008330e+01],        ...        [  2.10849646e+01],        [  2.30008330e+01],        [  2.50000000e+01]]) 

while np.sum(b**2, 1):

array([  2.50000000e+01,   2.30008330e+01,   2.10849646e+01,          1.92523948e+01,   1.75031237e+01,   1.58371512e+01,          ...          1.27551020e+01,   1.42544773e+01,   1.58371512e+01,          1.75031237e+01,   1.92523948e+01,   2.10849646e+01,          2.30008330e+01,   2.50000000e+01]) 

looks transposed of same vector. have operation of form [50 x 1] * [1 x 50] = [50 x 50].

what generic name of operation? , how can reproduce in r?

you looking ?outer believe. per file, returns:

the outer product of arrays x , y ... array dimension c(dim(x), dim(y))

so, specific example, try:

outer(a^2,b^2,fun=`+`) #         [,1]     [,2]     [,3] #[1,] 50.00000 48.00083 46.08496  ...to col 50 #[2,] 48.00083 46.00167 44.08580  ...to col 50 #[3,] 46.08496 44.08580 42.16993  ...to col 50 # ...to row 50 

Comments