You could try sage: R.<x> = QQ[] sage: f1 = x sage: f2 = R(1) sage: f = Piecewise([[(0,1),f1],[(1,2),f2]]) sage: f.plot()
sage: f(3/2) 1 or sage: f1 = lambda x: x sage: f2 = lambda x: 1 sage: f = Piecewise([[(0,1),f1],[(1,2),f2]]) sage: f(3/2) 1 sage: f.plot() or sage: x = var("x") sage: f1(x) = x sage: f2(x) = 1 sage: f = Piecewise([[(0,1),f1],[(1,2),f2]]) sage: f(3/2) 1 sage: list_plot([f(x) for x in srange(0, 2, step = 0.1)], plotjoined=True) Piecewise was written before SR and, as William pointed out, badly needs to be rewritten so they work together better. For example, in this case f.plot() will fail with a strange error. On Thu, Mar 25, 2010 at 11:08 PM, Dan Drake <dr...@kaist.edu> wrote: > Hello, > > I'm trying to use a piecewise function, and it doesn't seem to work very > well. If I do > > f = Piecewise([((0, 1), x), ((1, 2), 1)], x) > > and try to evaluate f(3/2), it complains that Integer objects are not > callable. The docstring for Piecewise says "if the optional var is > specified, then any symbolic expressions in the list will be converted > to symbolic functions using fcn.function(var)" -- shouldn't it also > try to convert non-symbolic expressions? I can do SR(1), but I don't > want to get into explaining to my students what that means. > > With the above definition, I also can't easily plot the function: if I > do > > plot(f, 0, 2) > > I get "ValueError: free variable: x |--> x" and if I do > > plot(f(x), x, 0, 2) > > it complains "value not defined outside of domain". The only way I could > get the function to plot is with > > plot(lambda x: f(x), 0, 2) > > which seems like an unnecessary workaround. > > If I define the function with > > f = Piecewise([((0,1), x), ((1,2), SR(1))]) > > I still have problems; when I try to evaluate f(3/2), it says "the > number of arguments must be less than or equal to 0"!? > > I also can't seem to get diff() to work with this function, even though > f.derivative() works. > > Are these known problems? Am I not using Piecewise properly? > > Dan > > -- > --- Dan Drake > ----- http://mathsci.kaist.ac.kr/~drake > ------- > > -----BEGIN PGP SIGNATURE----- > Version: GnuPG v1.4.9 (GNU/Linux) > > iEYEARECAAYFAkusJSsACgkQr4V8SljC5LrrZQCeOOAda3+8EYthVj0FVDNvyrYV > IM4AoIOPOVn/ViU/9SguNDOGwZhKVyO1 > =iu3A > -----END PGP SIGNATURE----- > > -- To post to this group, send an email to sage-devel@googlegroups.com To unsubscribe from this group, send an email to sage-devel+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/sage-devel URL: http://www.sagemath.org To unsubscribe from this group, send email to sage-devel+unsubscribegooglegroups.com or reply to this email with the words "REMOVE ME" as the subject.