multiply a lign matrix with a column matrix should return a scalar( matlab yes, numpy no)!!!

Hello, if i multiply two matrix, one with a unique line and the second one with a unique column, i should have a scalar:
line matrix([[1, 3, 1]]) col matrix([[2], [2], [2]]) line*col matrix([[10]])
Matlab give me a scalar, Numpy does not... Do you know why? Regards, Laurent

laurent.feron@free.fr wrote:
Hello,
if i multiply two matrix, one with a unique line and the second one with a unique column, i should have a scalar:
line matrix([[1, 3, 1]]) col matrix([[2], [2], [2]]) line*col matrix([[10]])
Matlab give me a scalar, Numpy does not...
Matlab does not have the concept of scalar: everything is a matrix (size(1) returns (1, 1) in matlab). NumPy matrix objects are more or less the same: every operation between two matrices return a matrix. Although natural coming from a matlab background, I would advise against using matrices because they look more familiar. I did the same mistake when I started using NumPy, and getting used to NumPy arrays took me more time. cheers, David

Hi Laurent, I'm not sure why this was implemented this way (probably due to numerical precision, because the numpy datatypes offer a greater variety than python), but in any case you can easily convert the result to a float:
float(line*col) 10 (You probably knew this, but well... :) )
Regards, Nikolas Am 03.02.2010 um 09:08 schrieb laurent.feron@free.fr:
Hello,
if i multiply two matrix, one with a unique line and the second one with a unique column, i should have a scalar:
line matrix([[1, 3, 1]]) col matrix([[2], [2], [2]]) line*col matrix([[10]])
Matlab give me a scalar, Numpy does not...
Do you know why?
Regards, Laurent _______________________________________________ NumPy-Discussion mailing list NumPy-Discussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpy-discussion

On 2/3/2010 3:08 AM, laurent.feron@free.fr wrote:
if i multiply two matrix, one with a unique line and the second one with a unique column, i should have a scalar
What definition of matrix multiplication is that?? If you really want a scalar product, ask for it:: >>> import numpy as np >>> m1 = np.mat('0 1 2') >>> m2 = m1.T >>> np.dot(m1.flat,m2.flat) 5 Alan Isaac

Thanks all for your replies... yes i start with numpy. i translate a dtw algorithm for voice recogniton from matlab to python Rgds, Laurent ----- Mail Original ----- De: "Alan G Isaac" <aisaac@american.edu> À: "Discussion of Numerical Python" <numpy-discussion@scipy.org> Envoyé: Mercredi 3 Février 2010 15h36:07 GMT +01:00 Amsterdam / Berlin / Berne / Rome / Stockholm / Vienne Objet: Re: [Numpy-discussion] multiply a lign matrix with a column matrix should return a scalar( matlab yes, numpy no)!!! On 2/3/2010 3:08 AM, laurent.feron@free.fr wrote:
if i multiply two matrix, one with a unique line and the second one with a unique column, i should have a scalar
What definition of matrix multiplication is that?? If you really want a scalar product, ask for it:: >>> import numpy as np >>> m1 = np.mat('0 1 2') >>> m2 = m1.T >>> np.dot(m1.flat,m2.flat) 5 Alan Isaac _______________________________________________ NumPy-Discussion mailing list NumPy-Discussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpy-discussion
participants (4)
-
Alan G Isaac
-
David Cournapeau
-
laurent.feron@free.fr
-
Nikolas Tezak