Re: Matrix Multiplication

• To: mathgroup at smc.vnet.net
• Subject: [mg13764] Re: Matrix Multiplication
• From: murray at math.umass.edu (Murray Eisenberg)
• Date: Mon, 24 Aug 1998 05:07:14 -0400
• Organization: University of Massachusetts, Amherst
• References: <6imh1a\$gu1@smc.vnet.net>
• Sender: owner-wri-mathgroup at wolfram.com

```Des Penny (dpenny at iname.com) wrote:

: I'm trying to multiply a 3x1 matrix and a 1x2 matrix.  I should get a
: 3x2 matrix, but instead I get an error message:

: In: A=Array[a,{3,1}]
: Out: {{a[1,1]},{a[2,1]},{a[3,1]}}

: In: B=Array[b,{2}]
: Out: {b[1],b[2]}

: In: A.B
: Dot::"dotsh": "Tensors \!\({\({a[\(1, 1\)]}\), \({a[\(2, 1\)]}\),
: \({a[\(3, 1\)]}\)}\) and \!\({\(b[1]\), \(b[2]\)}\) have incompatible
: shapes."
: Out: {{a[1,1]},{a[2,1]},{a[3,1]}}.{b[1],b[2]}

Your B is _not_ a 1-by-2 matrix but rather just a simple list of length
two:

In: B = Array[b, {2}]
Out: {b[1],b[2]}

In: Dimensions[B]
Out: {2}

Hence the last dimension of your A, namely, its number of columns (1),
does not equal the first dimension of your B.

Change B to BB as follows:

In: BB = Array[b, {1, 2}]
Out: {{b[1,1],b[1,2]}}

In: Dimensions[BB]
Out: {1,2}

In: A . BB
Out: {{a[1,1] b[1,1],a[1,1] b[1,2]},
{a[2,1] b[1,1],a[2,1]b[1,2]},
{a[3,1] b[1,1],a[3,1] b[1,2]}}

--
Murray Eisenberg                       Internet:
murray at math.umass.edu
Mathematics & Statistics Dept.            Voice:  413-545-2859 (W)
University of Massachusetts                       413-549-1020 (H)
Amherst, MA 01003                           Fax:  413-545-1801

```