MathGroup Archive 2006

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

Search the Archive

Re: Multiplying Elements of arrays: Inner product

  • To: mathgroup at smc.vnet.net
  • Subject: [mg68520] Re: Multiplying Elements of arrays: Inner product
  • From: Peter Pein <petsie at dordos.net>
  • Date: Wed, 9 Aug 2006 04:18:11 -0400 (EDT)
  • References: <eb9p9g$t1m$1@smc.vnet.net>
  • Sender: owner-wri-mathgroup at wolfram.com

Gopinath Venkatesan schrieb:
> I have to dot multiply cd (3x3 matrix) by ab (3x1 column vector).

then you should do it: Dot[cd,ab] or simply cd.ab

>  Also I defined values for the first row and the diagonal elements of cd matrix, as well as defined the column vector ab with values. Now ab = {1, 2, 3}, and cd = {{1, 2, 3}, {b[2,1], 1, b[2,3]}, {b[3,1], b[3,2], 1}}.
> 
> I expect the ef to be {14, 2+b[2,1]+3b[2,3], 3+b[3,1]+2b[3,2]} column vector. But it returns the first element in unevaluated form as 1.1+2.2+3.3. Why is this so? How to make it evaluate them? Thanks for the suggestions. Please see below for my example.
> 
> ab = Array[a, 3];
> cd = Array[b, {3, 3}];
> ef = Array[c, 3];
> Do[cd[[i, i]] = 1, {i, 1, 3}];
> cd[[1, 2]] = 2;
> cd[[1, 3]] = 3;
> Do[ab[[i]] = i, {i, 1, 3}];
> Do[ef[[i]] = 0, {i, 1, 3}];
> Do[ef[[i]] = ef[[i]] + cd[[i, k]].ab[[k]], {i, 1, 3}, {k, 1, 3}];
> 
> Gopinath
> 

Hi Gopinath,

first, assign values to ab and cd:
In[1]:=
  cd = {ab = {1, 2, 3}, {b[2, 1], 1, b[2, 3]}, {b[3, 1], b[3, 2],1}};

In[2]:=
  (* (very long) - use the definition of matrix -multiplication *)
   ef = {0, 0, 0};
   Do[ef[[i]] += cd[[i,j]]*ab[[j]], {j, 3}, {i, 3}]; ef
Out[3]=
   {14, 2 + b[2, 1] + 3*b[2, 3], 3 + b[3, 1] + 2*b[3, 2]}

In[4]:=
  (* (long:) simplify first method by using "row dot col" *)
   ef = {0, 0, 0};
   Do[ef[[i]] += cd[[i]] . ab, {i, 3}]; ef
Out[5]=
   {14, 2 + b[2, 1] + 3*b[2, 3], 3 + b[3, 1] + 2*b[3, 2]}
In[6]:= (* (short:) use the built in matrix multiplication *)
   ef = cd . ab
Out[6]=
   {14, 2 + b[2, 1] + 3*b[2, 3], 3 + b[3, 1] + 2*b[3, 2]}

Hope that helps,
   Peter


  • Prev by Date: Re: Multiplying Elements of arrays: Inner product
  • Next by Date: Re: MemberQ
  • Previous by thread: Re: Multiplying Elements of arrays: Inner product
  • Next by thread: Re: Multiplying Elements of arrays: Inner product