[Numpy-discussion] dot function or dot notation, matrices, arrays?

Christopher Barker Chris.Barker@noaa....
Sat Dec 19 12:18:09 CST 2009


Wayne Watson wrote:
> Yes, flat sounds useful here. However, numpy isn't bending over 
> backwards to tie in conventional mathematical language into it.

exactly -- it isn't bending over at all! (well a little -- see below). 
numpy was designed for general purpose computational needs, not any one 
branch of math. nd-arrays are very useful for lots of things. In 
contrast, Matlab, for instance, was originally designed to be an easy 
front-end to linear algebra package. Personally, when I used Matlab, I 
found that very awkward -- I was usually writing 100s of lines of code 
that had nothing to do with linear algebra, for every few lines that 
actually did matrix math. So I much prefer numpy's way -- the linear 
algebra lines of code are longer an more awkward, but the rest is much 
better.

The Matrix class is the exception to this: is was written to provide a 
natural way to express linear algebra. However, things get a bit tricky 
when you mix matrices and arrays, and even when sticking with matrices 
there are confusions and limitations -- how do you express a row vs a 
column vector? what do you get when you iterate over a matrix? etc.

There has been a bunch of discussion about these issues, a lot of good 
ideas, a little bit of consensus about how to improve it, but no one 
with the skill to do it has enough motivation to do it.

As for your problem, I think a 3-d euclidean vector is well expressed as 
a (3,) shape array, and then you don't need flat, etc.

In [6]: v1 = np.array((1,2,3), dtype=np.float)

In [7]: v2 = np.array((3,1,2), dtype=np.float)

In [8]: np.dot(v1,v2)
Out[8]: 11.0

-Chris







> I don't recall flat in any calculus books. :-) Maybe I've been away so 
> long from it, that it is a common math concept? Although I doubt that.
> 
> 
> Alan G Isaac wrote:
>> On 12/19/2009 11:45 AM, Wayne Watson wrote:
>>   
>>> A 4x1, 1x7, and 1x5 would be examples of a 1D array or matrix, right?
>>>
>>> Are you saying that instead of using a rotational matrix  ...
>>> 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?
>>>
>>> To get the scalar value (sum of squares) I had to use a transpose, T, on
>>> one argument.
>>>     
>>
>> At this point, you have raised some long standing issues.
>> There are a couple standard replies people give to some of them.
>> E.g.,
>>
>> 1. don't use matrices, OR
>> 2. don't mix the use of matrices and arrays
>>
>> Matrices are *always* 2d (e.g., a "row vector" or a "column vector" is 2d).
>> So in fact you should find it quite natural that that transpose was needed.
>> Matrices change * to matrix multiplication and ** to matrix exponentiation.
>> I find this very convenient, especially in a teaching setting, so I use
>> NumPy matrices all the time.   Many on this list avoid them completely.
>>
>> Again, if you want a *scalar* as the product of vectors for which you
>> created matrix objects (e.g., a and b), you can just use flat:
>> np.dot(a.flat,b.flat)
>>
>> hth,
>> Alan Isaac
>> _______________________________________________
>> NumPy-Discussion mailing list
>> NumPy-Discussion@scipy.org
>> http://mail.scipy.org/mailman/listinfo/numpy-discussion
>>
>>   
> 


-- 
Christopher Barker, Ph.D.
Oceanographer

Emergency Response Division
NOAA/NOS/OR&R            (206) 526-6959   voice
7600 Sand Point Way NE   (206) 526-6329   fax
Seattle, WA  98115       (206) 526-6317   main reception

Chris.Barker@noaa.gov


More information about the NumPy-Discussion mailing list