Re: Matrix Multiplication
- To: mathgroup@smc.vnet.net
- Subject: [mg12343] Re: Matrix Multiplication
- From: murray@math.umass.edu (Murray Eisenberg)
- Date: Thu, 7 May 1998 18:52:29 -0400
- Organization: University of Massachusetts, Amherst
- References: <6imh1a$gu1@smc.vnet.net>
Des Penny (dpenny@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@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