MathGroup Archive 1998

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Matrix Multiplication



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




  • Prev by Date: Re: Matrix Multiplication
  • Next by Date: Re: MATLAB to Mathematica Program Conversion Needed
  • Prev by thread: Re: Matrix Multiplication
  • Next by thread: runga kutta and importing data