[SciPy-User] scalars vs array of length 1

Victor Eijkhout eijkhout@tacc.utexas....
Thu Jul 8 11:44:49 CDT 2010


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.



More information about the SciPy-User mailing list