[Numpy-discussion] A basic question on the dot function
Charles R Harris
charlesr.harris@gmail....
Tue Oct 16 13:23:36 CDT 2007
On 10/16/07, Julien Hillairet <julien.hillairet@gmail.com> wrote:
>
> Hello,
>
>
> First of all, I'm sorry if this question had already been asked. I've
> searched on the gmane archive and elsewhere on internet, but I didn't found
> the answer to my question.
>
> As expected, the dot product of 2 'classical' vectors works fine :
>
> In [50]: a0 = numpy.array([1,2,3])
> In [51]: numpy.dot(a0,a0)
> Out[51]: 14
>
> What I don't understand, is why the dot product of a (3,N) vector gives an
> error :
>
> In [52]: a1 = numpy.array([[1,2,3],[1,2,3],[1,2,3],[1,2,3]])
> In [54]: numpy.dot(a1,a1)
> <type 'exceptions.ValueError'> Traceback (most recent call
> last)
> <type 'exceptions.ValueError'>: objects are not aligned
>
> instead of what I've expected ; an array([14 14 14 14]).
>
> Of course, I've found an alternative in doing :
>
> In [61]: numpy.sum(a1*a1,axis=1)
> Out[61]: array([14, 14, 14, 14])
>
> But I like to understand my mistakes or misunderstands :) So, could
> someone explained me what I've missed ? Thanks in advance.
Dot is matrix multiplication, not the "dot" product you were expecting. It
is also a bit ambiguous, as you see with the 1-D vectors, where you got what
you expected.
Chuck
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://projects.scipy.org/pipermail/numpy-discussion/attachments/20071016/853db2e2/attachment.html
More information about the Numpy-discussion
mailing list