William Stein wrote:
> On Mon, Jun 15, 2009 at 7:34 PM, Jason Grout<jason-s...@creativetrax.com> 
> wrote:
>> Bill Hart wrote:
>>> Can I ask what applications this "Hadamard product" has?
> 
> I've never used it, but I guess it must be really really important in
> numerical computation, since most shockingly it is the *default* for
> A*B in numpy!!
> 
> sage: import numpy
> sage: a = numpy.array([[1,2],[3,4]])
> sage: a*a
> array([[1, 4],
>        [9, 16]], dtype=object)
> 
> The above seems so utterly insanely wrong to a mathematician like me,
> it boggles my mind every time I see it :-).

I tend to really think of array manipulation and linear algebra as 
totally different things -- NumPy does the former, Sage the latter.

To take the JPEG example for Hadamard product from Wikipedia (where you 
e.g. need to multiply each pixel in a transformed image block by a 
coefficient), I'd be quite happy have a 2D image block and multiply that 
(componentwise) with another 2D array.

BUT, expressed as linear algebra, I would definitely let the image be a 
vector, and the coefficients a diagonal matrix which operates on it.

I tend to just avoid using the word "matrix" for a 2D NumPy array. 
Matrices and vectors are concepts in linear algebra -- if I have a 3D 
array in NumPy it can definitely be a vector in writing, and a 1D array 
can be a diagonal matrix in writing, and so on.

If that was too vague: Another example is that to actually multiply a 
diagonal matrix with another matrix in NumPy's array expression I'd tend 
to do

vector_of_diagonal * the_matrix

which works because of broadcasting:

In [11]: np.array([[1,2,3]]).T * np.identity(3)
Out[11]:
array([[ 1.,  0.,  0.],
        [ 0.,  2.,  0.],
        [ 0.,  0.,  3.]])

The "Sage way" of doing that would likely be to have a seperate 
DiagonalMatrix type or so; so in a sense NumPy is rawer and more 
lower-level. Both have their place.


-- 
Dag Sverre

--~--~---------~--~----~------------~-------~--~----~
To post to this group, send email to sage-devel@googlegroups.com
To unsubscribe from this group, send email to 
sage-devel-unsubscr...@googlegroups.com
For more options, visit this group at http://groups.google.com/group/sage-devel
URLs: http://www.sagemath.org
-~----------~----~----~----~------~----~------~--~---

Reply via email to