A basic question on the dot function

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. Best regards, JH

On 10/17/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 :
dot() also serves as Numpy's matrix multiply function. So it's trying to interpret that as a (3,N) matrix times a (3,N) matrix. See examples here: http://www.scipy.org/Numpy_Example_List_With_Doc#head-2a810f7dccd3f7c700d107... --bb

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

Julien Hillairet 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]).
When given two 2-D arrays, dot() essentially does matrix multiplication. The last dimension of the first argument is matched with the next-to-last dimension of the second argument. -- Robert Kern "I have come to believe that the whole world is an enigma, a harmless enigma that is made terrible by our own mad attempt to interpret it as though it had an underlying truth." -- Umberto Eco

2007/10/16, Bill Baxter <wbaxter@gmail.com>:
dot() also serves as Numpy's matrix multiply function. So it's trying to interpret that as a (3,N) matrix times a (3,N) matrix.
See examples here:
http://www.scipy.org/Numpy_Example_List_With_Doc#head-2a810f7dccd3f7c700d107...
--bb
2007/10/16, Charles R Harris <charlesr.harris@gmail.com>:
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
2007/10/16, Robert Kern <robert.kern@gmail.com>:
When given two 2-D arrays, dot() essentially does matrix multiplication. The last dimension of the first argument is matched with the next-to-last dimension of the second argument.
-- Robert Kern
Thank you for your answers. So, is there a "proper" solution to do the dot product as I had expected it ? Cheers, JH

On 10/16/07, Julien Hillairet <julien.hillairet@gmail.com> wrote:
2007/10/16, Bill Baxter <wbaxter@gmail.com>:
dot() also serves as Numpy's matrix multiply function. So it's trying to interpret that as a (3,N) matrix times a (3,N) matrix.
See examples here:
http://www.scipy.org/Numpy_Example_List_With_Doc#head-2a810f7dccd3f7c700d107...
--bb
2007/10/16, Charles R Harris < charlesr.harris@gmail.com>:
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
2007/10/16, Robert Kern <robert.kern@gmail.com>:
When given two 2-D arrays, dot() essentially does matrix multiplication. The last dimension of the first argument is matched with the next-to-last dimension of the second argument.
-- Robert Kern
Thank you for your answers. So, is there a "proper" solution to do the dot product as I had expected it ?
You might try tensordot. Without thinking it through too much: numpy.tensordot(a0, a1, axes=[-1,-1]) seems to do what you want. -- . __ . |-\ . . tim.hochberg@ieee.org

2007/10/16, Timothy Hochberg <tim.hochberg@ieee.org>:
You might try tensordot. Without thinking it through too much: numpy.tensordot(a0, a1, axes=[-1,-1]) seems to do what you want.
Thank you. However, it works only for this simple example, where a0 and a1 are similar. The tensor product increase the rank of the output, doesn't it ? Although the dot product decrease the rank. Is there a ¨proper" solution if a and b are general (3,N) vectors ? By example : In [16]: a = random.random_sample((3,5)) In [17]: b = random.random_sample((3,5)) what I'm searching for is : In [18]: dotprod2(a,b) Out[18]: array([ 0.28354876, 0.54474092, 0.22986942, 0.42822669, 0.98179793]) where I defined a "classical" (in the way I understand it. I may not understand it properly ?) dot product between these 2 vectors. def dotprod2(a,b): return sum(a*b,axis=0) or in maths notation : c_j = \sum_i a_{ij} b_{ij} Thank in advance, JH

what I'm searching for is :
In [18]: dotprod2(a,b) Out[18]: array([ 0.28354876, 0.54474092, 0.22986942, 0.42822669, 0.98179793])
where I defined a "classical" (in the way I understand it. I may not understand it properly ?) dot product between these 2 vectors.
def dotprod2(a,b): return sum(a*b,axis=0)
I think this is your best choice ;) Matthieu
participants (6)
-
Bill Baxter
-
Charles R Harris
-
Julien Hillairet
-
Matthieu Brucher
-
Robert Kern
-
Timothy Hochberg