[Numpy-discussion] dot function or dot notation, matrices, arrays?
Charles R Harris
Sat Dec 19 11:22:25 CST 2009
On Sat, Dec 19, 2009 at 9:45 AM, Wayne Watson
> Dag Sverre Seljebotn wrote:
> > Wayne Watson wrote:
> >> I'm trying to compute the angle between two vectors in three dimensional
> >> space. For that, I need to use the "scalar (dot) product" , according to
> >> a calculus book (quoting the book) I'm holding in my hands right now.
> >> I've used dot() successfully to produce the necessary angle. My program
> >> works just fine.
> >> In the case of the dot(function), one must use np.dev(x.T,x), where x is
> >> 1x3.
> >> I'm not quite sure what your point is about dot()* unless you are
> >> thinking in some non-Euclidean fashion. One can form np.dot(a,b) with a
> >> and b arrays of 3x4 and 4x2 shape to arrive at a 3x2 array. That's
> >> definitely not a scalar. Is there a need for this sort of calculation in
> >> non-Euclidean geometry, which I have never dealt with?
> > There's a difference between 1D and 2D arrays that's important here. For
> > a 1D array, np.dot(x.T, x) == np.dot(x, x), since there's only one
> > dimension.
> A 4x1, 1x7, and 1x5 would be examples of a 1D array or matrix, right?
No, they are all 2D. All matrices are 2D. An array is 1D if it doesn't have
a second dimension, which might be confusing if you have only seen vectors
represented as arrays. To see the number of dimensions in a numpy array, use
In : array([[1,2],[3,4]])
In : array([[1,2],[3,4]]).shape
Out: (2, 2)
In : array([1,2, 3,4])
Out: array([1, 2, 3, 4])
In : array([1,2, 3,4]).shape
> Are you saying that instead of using a rotational matrix like
> theta = 5.0 # degrees
> m1 = matrix([ ,])
> rotCW = matrix([ [cosD(theta), sinD(theta)], [-sinD(theta),
> cosD(theta)] ])
> m2= rotCW*m1
> that I should use a 2-D array for rotCW? So why does numpy have a matrix
> class? Is the class only used when working with matplotlib?
Numpy has a matrix class because python lacks operators, so where * normally
means element-wise multiplication the matrix class uses it for matrix
multiplication, which is different. Having a short form for matrix
multiplication is sometimes a convenience and also more familiar for folks
coming to numpy from matlab.
> To get the scalar value (sum of squares) I had to use a transpose, T, on
> one argument.
That is if the argument is 2D. It's not strictly speaking a scalar product,
but we won't go into that here ;)
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the NumPy-Discussion