2.33 Perform outer product and outer sum between two vector

Problem: Given 2 vectors, perform outer product and outer sum between them. The outer operation takes the first element in one vector and performs this operation on each element in the second vector. This results in first row. This is repeated for each of the elements in the first vector. The operation to perform can be any valid operation on these elements.

Mathematica

using symbolic vectors. Outer product

Remove["Global`*"] 
 
v1={a,b,c}; 
v2={e,f,g}; 
Outer[Times,v1,v2]
 

{{a e, a f, a g}, 
{b e, b f, b g}, 
{c e, c f, c g}}
 

Outer sum

Outer[Plus,v1,v2]
 

{{a+e, a+f, a+g}, 
{b+e, b+f, b+g}, 
{c+e, c+f, c+g}}
 

using numerical vectors. Outer product

v1={1,2,3}; 
v2={4,5,6}; 
Outer[Times,v1,v2]
 

{{4,  5,  6}, 
{8,  10, 12}, 
{12, 15, 18}}
 

Outer sum

Outer[Plus,v1,v2]
 

  {{5, 6, 7}, 
   {6, 7, 8}, 
   {7, 8, 9}}
 

 

Matlab

Outer product

v1=[1 2 3]; 
v2=[4 5 6]; 
 
v1'*v2
 

ans = 
     4     5     6 
     8    10    12 
    12    15    18
 

Outer sum

v1=[1 2 3]; 
v2=[4 5 6]; 
meshgrid(v1)+meshgrid(v2)'
 

ans = 
     5     6     7 
     6     7     8 
     7     8     9
 

 

Maple

Due to Carl Love from the Maple newsgroup

restart; 
 
Outer:= proc(OP, L1, L2) 
    local a,b; 
   [seq](seq(OP(a,b), b in L2), a in L1) 
end proc: 
 
L1:=[a,b,c]; 
L2:=[d,e,f]; 
Outer(`*`,L1,L2);
 

[a*d,a*e,a*f,b*d, 
  b*e, b*f, c*d, c*e, c*f]
 

Outer(`+`,L1,L2);
 

[a+d, a+e, a+f, b+d, 
  b+e, b+f, c+d, c+e, c+f]