# [SciPy-user] Mathematica Element-wise Multiplication

David Cournapeau david@ar.media.kyoto-u.ac...
Mon Dec 17 21:46:39 CST 2007

```Johann Cohen-Tanugi wrote:
> Matthieu Brucher wrote:
>
>> 2007/12/17, Johann Cohen-Tanugi <cohen@slac.stanford.edu
>> <mailto:cohen@slac.stanford.edu>>:
>>
>>     thanks for these precisions, David. Reading it, I still come to think
>>     that it is a potential source of confusion to let a "row array" have a
>>     transpose or T method, that essentially does nothing.
>>
>>
>>
>> In object oriented code, this can happen often, but it is not a
>> problem. It does what you want : inverse the axis, even if there is
>> only one axis.
>>
> hmmm...... okay... What I wanted was to transpose a 1D array into a
> vector, or vice-versa, with the linear algebra behavior in mind. I
> understand that numpy does not follow this, but I cannot believe that
> this behavior *is* what everybody wants! Tom's initial email was
> symptomatic, and Stefan's response, with the proposal to use the T
> method even more so!
>
> Assuming that this natural linear algebra could be retrieved when, and
> *only* when, the array is 1D, I do not see how such an implementation
> could break codes that depend on it, because I don't see why someone
> would call 'a.T' just to have 'a' again.... But it is probably my lack
> of imagination.
>
Indeed, it is a lack of imagination :) I can assure you that it would
break a lot of my code, for example. I really understand your problem:
you can see that on numpy ML that 18 months ago, I was exactly in the
same situation as you. I used matlab a lot, was quite proficient in it,
and was confused by this. But it really makes sense once you forget
about matlab. I personally do not use numpy.matrix; not that it does not
make sense to use them (when you really want to do linear algebra), but
I think it just prevents you from using numpy at 100 %.
> Anyway, enough of this. I am sure the developers know better than me...
>
IMHO, the 'problem' is coming from the fact that arrays are used for two
things in matlab/numpy/etc....: for linear algebra, of course, but also,
perhaps even more important, for speed reasons. For various reasons, it
is really hard to do a fast script language. In particular when looping,
and vectorizing code is a good way to avoid most of them. When for
example I use a rank 2 array to compute some statistics on it, I do not
really think about it as an array (the maths on it is not, for sure),
but as a list of arrays; I use arrays because it is much faster.

If I had a choice, I would write the relevant code with loops (most of
the time, it is clearer using loops). This is in clear contrast with
linear algebra code where handling directly matrices and vectors is much
clearer than doing loops manually. Matlab, for historical reasons I
guess, is more geared towards linear algebra, whereas numpy is more
geared toward 'using arrays for speed reasons'; in particular,
broadcasting is much more powerful in numpy that it is in matlab.

cheers,

David
```