[SciPy-User] scalars vs array of length 1

Sebastian Haase seb.haase at gmail.com
Thu Jul 8 15:09:48 EDT 2010


On Thu, Jul 8, 2010 at 6:44 PM, Victor Eijkhout
<eijkhout at tacc.utexas.edu> wrote:
> I want to write a function that can accept both a scalar and a vector, when called on a vector it should return a vector of scalar application results. However, scalars seem to be treated differently from length-1 lists.
>
> %%%%% input
>
> from numpy import array,matrix,cos,sin,tan
>
> def f(x):
>    valueMatrix = array([cos(x),sin(x),tan(x)])
>    print valueMatrix.shape
>    valueMatrix = matrix(valueMatrix)
>    print valueMatrix.shape
>    print
>
> print "scalars"
> f(5)
> f([5])
>
> %%% output
>
> scalars
> (3,)
> (1, 3)
>
> (3, 1)
> (3, 1)
>
> %%%%
>
> How do I get that shape to be the same in both cases?
>
> Victor.

why are you mixing numpy.array and numpy.matrix !?
Are you sure you need both matrix and array ?
Matrixs are like arrays except for the the *-operator, and the shape
is always made to 2d (at least 2d maybe !!?? I don't know ....)
Most people need only array - ever .... [  ;-)  ]

- Sebastian Haase



More information about the SciPy-User mailing list