Hello! In "A Primer on Scientific Programming with Python", they define a vectorized function as a scalar function that, when called with a vector argument, returns a vector with the values of the function for each of the values stored in the argument vector.
I am trying to define a constant vectorized function, that is, one that returns 1. (for instance) when called with a scalar argument x and that returns array([1.,1.,1.]) when the argument is a three-element array, etc. Here are a few snippets of an interactive session displaying my failed attempts: --------------------------- In [1]: from numpy import * In [2]: def f(x): ...: return 1. In [3]: x=linspace(0,1,5) In [4]: print f(x) ------> print(f(x)) 1.0 ------------------------- That's not what I want, that's a scalar, not a five element array. Next option: ------------------------- In [13]: def g(x): ....: return where(True,1.,0.) In [14]: print g(x) -------> print(g(x)) 1.0 --------------------------- Still not right. But here is something strange. The values in x are non- negative, therefore the test x>=0 yelds True for each element in x. One might then think that g(x) as defined above would, in this case, be equivalent to h(x) defined below: -------------------------- In [16]: def h(x): ....: return where(x>=0,1.,0) --------------------------- However, --------------------------- In [18]: print h(x) -------> print(h(x)) [ 1. 1. 1. 1. 1.] --------------------------- So my questions are: 1. What's the proper way to vectorize a constant function (I know that we really don't have to, but I'd like to do it in a first step towards a more sophisticated calculation)? 2. Why do g(x) and h(x) return different results? 3. Why doesn't f(x) work? Thanks Jose _______________________________________________ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor