[SciPy-user] Mathematica Element-wise Multiplication
Johann Cohen-Tanugi
cohen@slac.stanford....
Sun Dec 16 23:10:15 CST 2007
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. I guess there is a
reason behind this situation, but given the fact that it is there, I am
wondering whether T or transpose of a row array could in fact return
what it is expected to, aka the 2d column array. Is there any reason not
to have this functionality?
best,
Johann
David Cournapeau wrote:
> Johann Cohen-Tanugi wrote:
>
>> Actually I do not manage to use .T or .transpose() method on 1D arrays :
>>
>> In [42]: a = array([[ 0.0, 0.0,
>> 0.0],[10.0,10.0,10.0],[20.0,20.0,20.0],[30.0,30.0,30.0]]) <--this is
>> example 3 of thiis indeed very nice tutorial on broadcasting
>> In [43]: b = array([1.0,2.0,3.0,4.0])
>> In [44]: a+b
>> ValueError: shape mismatch: objects cannot be broadcast to a single
>> shape <----- fine, mismatch of trailing dimensions
>>
>> In [45]: b.transpose()
>> Out[45]: array([ 1., 2., 3., 4.]) <------ ominous : no change
>>
> This is confusing if you are coming from matlab and similar softwares
> (it was for me, at least). In matlab, any array is rank 2 by default
> (let's put aside rank > 2 for now). That is, in matlab, a = [1, 2, 3]
> gives you a row array, which is interpreted as a matrix: size(a) gives
> you [1, 3]. In numpy, this is not the case: there is a real difference
> between "row arrays", "column arrays" and vectors.
>
> a = array([1, 2, 3])
> a.ndim <----------- is 1 (rank 1)
> a = array([[1], [2], [3]])
> a.ndim <----------- is 2 (rank 2: column)
> a = array([[1, 2, 3]])
> a.ndim <----------- is 2 (rank 2: row)
>
> In Matlab, there is no such difference, and it is really ingrained in
> the software (for example, at the C api level, the function to get the
> number of dimensions, alas the 'rank', always returns at least 2). To
> solve your problem, you should have a new dimension:
>
> b = array([1., 2, 3])
> b.ndim <----------- 1
> b[:, numpy.newaxis]
> b.ndim <----------- 2 (this will be a column array: b is now exactly the
> same as array([[1.], [2], [3]])
> b = array([1., 2, 3])
> b[numpy.newaxis, :]
> b.ndim <----------- 2 (this will be a row array: b is now exactly the
> same as array([[1., 2, 3]])
>
> cheers,
>
> David
> _______________________________________________
> SciPy-user mailing list
> SciPy-user@scipy.org
> http://projects.scipy.org/mailman/listinfo/scipy-user
>
More information about the SciPy-user
mailing list