Re: Matrix Multiplication
- To: mathgroup@smc.vnet.net
- Subject: [mg12335] Re: Matrix Multiplication
- From: A Hayes <hay@haystack.demon.co.uk>
- Date: Thu, 7 May 1998 18:52:21 -0400
- References: <6imh1a$gu1@smc.vnet.net>
Des Penny 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]} Des, {b[1],b[2]} is not a 1x2 matrix, and {{a[1,1]},{a[2,1]},{a[3,1]}} has dimensions {1,3}. So they are definitely incompatible. How about A = Array[a,{3,1}] {{a[1, 1]}, {a[2, 1]}, {a[3, 1]}} B = Array[b,{2}] {b[1], b[2]} A.{B} {{a[1, 1] b[1], a[1, 1] b[2]}, {a[2, 1] b[1], a[2, 1] b[2]}, {a[3, 1] b[1], a[3, 1] b[2]}} Or, if acceptable, B2=Array[b,{1,2}] {{b[1, 1], b[1, 2]}} A.B2 {{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]}} Allan -- Allan Hayes Training and Consulting Leicester, UK hay@haystack.demon.co.uk http://www.haystack.demon.co.uk voice: +44 (0)116 271 4198 fax: +44 (0)116 271 8642