On Sun, May 22, 2011 at 4:45 PM, Berkin Malkoc <malk...@gmail.com> wrote:
> > > > > I have something like: >> > y = x^2 >> > >> > I can evaluate it at one point: >> > y(2) # 4 >> > >> > I'd like to evaluate it at a group of points: >> > y( x ) # 2, 4, 9, 16, ... >> > >> > Can this "just work" like in IDL or MATLAB? How would one define x? >> > What is the quickest syntax if it can't just be a variable? Perhaps: >> > > For purely numerical work a la Matlab, you may want to use numpy arrays > (you will be using Python and not any Sage functionality): > > sage: import numpy > sage: x = numpy.array([1, 2, 3, 4]) > sage: def y(x): > ....: return x*x > ....: > sage: y(x) > array([ 1, 4, 9, 16]) > > -- > To post to this group, send email to sage-support@googlegroups.com > To unsubscribe from this group, send email to > sage-support+unsubscr...@googlegroups.com > For more options, visit this group at > http://groups.google.com/group/sage-support > URL: http://www.sagemath.org > if the function of the single variable is somewhat more complicated then one can use vectorize from numpy - sage: import numpy as np sage: x = np.array([1,2,3,4]) sage: x array([1, 2, 3, 4]) sage: def y(x): ....: if x < 3: ....: return 2*x ....: else: ....: return x*x ....: sage: y_vec = np.vectorize(y) this fails - sage: y(x) --------------------------------------------------------------------------- ValueError Traceback (most recent call last) .... while this works - sage: y_vec(x) array([ 2, 4, 9, 16]) hope it helps. Rajeev -- To post to this group, send email to sage-support@googlegroups.com To unsubscribe from this group, send email to sage-support+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/sage-support URL: http://www.sagemath.org