[sage-support] Re: Piecewise function definition
On Tue, Oct 27, 2009 at 10:55 AM, all_thumbs sch...@hsva.de wrote: Hi Dave, On Jul 22, 12:56 am, David Joyner wdjoy...@gmail.com wrote: ... I'm not sure what you are going to do with yourfunction. If it is just for plotting, say, I think you might just want to use def f(x,y): if case1: return whatever1 if case2: return whatever2 etc I have recently installed SAGE in the hope that I can cut down a bit on all the odd math tools that I use today. One of the very first things I tried, inspired by the calculus quick reference page, was the piecewise function. I quickly found out that a (very simple) function f defined with piecewise cannot be plotted with plot(f, ), but works fine with f.plot (). On the other hand, the same function defined as a python function as above, does not plot properly. Oddly enough, when I define abs(x) like this: def f(x): if x 0 : return -x else : return x only the last branch of the conditional statement is visible in the diagram. On the other hand, this branch is visible for all input values, x0 too! You could try: sage: f = lambda x: x 0 and 1 or -1 sage: plot(f,x,-2,2) So, even if only need to plot a function, SAGE seems to have some troubles with functions that are defined in pieces. Yes, the Piecewise class was written some time ago and badly needs revision. Hopefully someone will volunteer to work on it more. Johannes --~--~-~--~~~---~--~~ 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 -~--~~~~--~~--~--~---
[sage-support] Re: Piecewise function definition
I'm doing numerical minimization on it right now and the only reason I want/need it to be a sage function is so I can .subs() real values for my parameters. To get around that, I'm just going to use global variables for my parameters. It's kind of ugly, but it will work. It would be nice to be able to get symbolic derivatives too, but I don't need these either. Something like that ? class PieceOfFunction(object): def __init__(f,interval) self.f = f self.interval = interval def diff(): return PieceOfFunction(self.diff(),self.interval) class PiecewiseFunction(object): def __init__(listOfPieces): self.listOfPieces = listOfPieces def diff(): newList = [] for f in self.listOfPieces : newList.append(f.diff(),f.interval) retuen PiecewiseFunction(newList) I define a piece of function as a pair of a function and an interval, and then a piecewise function as a list of pieces of functions. I'm far from being sure that it is an optimal way, but as far as I understand your problem, I would do like that. Have a good day Laurent --~--~-~--~~~---~--~~ 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 URLs: http://www.sagemath.org -~--~~~~--~~--~--~---
[sage-support] Re: Piecewise function definition
Are you aware of the function piecewise(), which seems to do what you want? If there is a problem with using it, what is it? M. Hampton On Jul 21, 12:34 pm, Doug mcke...@gmail.com wrote: I'm trying to do something that seems very simple but isn't working. Hence the post here :) I want to define a very simple piecewise linear function. It's linear with slope alpha up to a knot at c and then it's linear with slope beta. Here's what I thought might work: f(x) = (x=c)*alpha*x + (xc)*(alpha*c + beta*(x-c)) Putting the inequalities in there caused a big mess. So I tried defining a Python Indicator function that turns Truth values into 0 or 1, and then I wrapped my relational expressions with it: def Indicator(cond): if (cond==True): return 1 else: return 0 This didn't work either: sage: foo(x) = Indicator(x4) ; foo x |-- 0 Any other ideas? I suppose I could in this case define my piecewise function as a Python function, but then I won't be able to do as much with it later (e.g., differentiate it). Thx as usual, Doug --~--~-~--~~~---~--~~ 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 URLs: http://www.sagemath.org -~--~~~~--~~--~--~---
[sage-support] Re: Piecewise function definition
Are you aware of the function piecewise(), which seems to do what you want? If there is a problem with using it, what is it? I wasn't aware of piecewise(), and although it doesn't seem as elegant or flexible as being able to use Indicator functions in my function definitions, I think it should work. That said, what I actually want is to define an additively separable function of two variables where one component is piecewise. But creating this function with a piecewise component isn't working. Here's what happens: sage: f1(x) = -1 sage: f2(x) = 2 sage: f = Piecewise([[(0,pi/2),f1],[(pi/2,pi),f2]]) sage: f Piecewise defined function with 2 parts, [[(0, 1/2*pi), x |-- -1], [(1/2*pi, pi), x |-- 2]] sage: g(x,y) = y + f(x) --- ValueErrorTraceback (most recent call last) /Users/dmckee/.sage/temp/eve/19724/ _Users_dmckee_Documents_work_research_mexbeq_sage_spline_numeric_solutions_sage_188.py in module() /Applications/sage/local/lib/python2.5/site-packages/sage/functions/ piecewise.pyc in __call__(self, x0) 605 if endpts[i] x0 endpts[i+1]: 606 return self.functions()[i](x0) -- 607 raise ValueError,Value not defined outside of domain. 608 609 def which_function(self,x0): ValueError: Value not defined outside of domain. I must be doing something dumb (again), right? Thanks again for all your help! Doug M. Hampton On Jul 21, 12:34 pm, Doug mcke...@gmail.com wrote: I'm trying to do something that seems very simple but isn't working. Hence the post here :) I want to define a very simple piecewise linear function. It's linear with slope alpha up to a knot at c and then it's linear with slope beta. Here's what I thought might work: f(x) = (x=c)*alpha*x + (xc)*(alpha*c + beta*(x-c)) Putting the inequalities in there caused a big mess. So I tried defining a Python Indicator function that turns Truth values into 0 or 1, and then I wrapped my relational expressions with it: def Indicator(cond): if (cond==True): return 1 else: return 0 This didn't work either: sage: foo(x) = Indicator(x4) ; foo x |-- 0 Any other ideas? I suppose I could in this case define my piecewise function as a Python function, but then I won't be able to do as much with it later (e.g., differentiate it). Thx as usual, Doug --~--~-~--~~~---~--~~ 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 URLs: http://www.sagemath.org -~--~~~~--~~--~--~---
[sage-support] Re: Piecewise function definition
On Tue, Jul 21, 2009 at 5:35 PM, Dougmcke...@gmail.com wrote: Are you aware of the function piecewise(), which seems to do what you want? If there is a problem with using it, what is it? I wasn't aware of piecewise(), and although it doesn't seem as elegant or flexible as being able to use Indicator functions in my function definitions, I think it should work. That said, what I actually want is to define an additively separable function of two variables where one component is piecewise. But creating this function with a piecewise component isn't working. Here's what happens: sage: f1(x) = -1 sage: f2(x) = 2 sage: f = Piecewise([[(0,pi/2),f1],[(pi/2,pi),f2]]) sage: f Piecewise defined function with 2 parts, [[(0, 1/2*pi), x |-- -1], [(1/2*pi, pi), x |-- 2]] sage: g(x,y) = y + f(x) --- ValueErrorTraceback (most recent call last) /Users/dmckee/.sage/temp/eve/19724/ _Users_dmckee_Documents_work_research_mexbeq_sage_spline_numeric_solutions_sage_188.py in module() /Applications/sage/local/lib/python2.5/site-packages/sage/functions/ piecewise.pyc in __call__(self, x0) 605 if endpts[i] x0 endpts[i+1]: 606 return self.functions()[i](x0) -- 607 raise ValueError,Value not defined outside of domain. 608 609 def which_function(self,x0): ValueError: Value not defined outside of domain. I must be doing something dumb (again), right? Piecewise functions of 2 variables are not yet implemented. Sorry. Thanks again for all your help! Doug M. Hampton On Jul 21, 12:34 pm, Doug mcke...@gmail.com wrote: I'm trying to do something that seems very simple but isn't working. Hence the post here :) I want to define a very simple piecewise linear function. It's linear with slope alpha up to a knot at c and then it's linear with slope beta. Here's what I thought might work: f(x) = (x=c)*alpha*x + (xc)*(alpha*c + beta*(x-c)) Putting the inequalities in there caused a big mess. So I tried defining a Python Indicator function that turns Truth values into 0 or 1, and then I wrapped my relational expressions with it: def Indicator(cond): if (cond==True): return 1 else: return 0 This didn't work either: sage: foo(x) = Indicator(x4) ; foo x |-- 0 Any other ideas? I suppose I could in this case define my piecewise function as a Python function, but then I won't be able to do as much with it later (e.g., differentiate it). Thx as usual, Doug --~--~-~--~~~---~--~~ 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 URLs: http://www.sagemath.org -~--~~~~--~~--~--~---
[sage-support] Re: Piecewise function definition
Piecewise functions of 2 variables are not yet implemented. Sorry. Ah, I see. If there was were primitive functions for LessThan(x,y), Equal(x,y), and GreaterThan(x,y), and they returned 0 or 1, I think that's all I would need: f(x,y) = y + LessThan(x,pi/2)*f1(x) + Equal(x,pi/2)*f2(x) + GreaterThan (x,pi/2)*f2(x) I don't suppose these exist or are easy to define? Thanks again for the super-quick replies! Doug --~--~-~--~~~---~--~~ 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 URLs: http://www.sagemath.org -~--~~~~--~~--~--~---
[sage-support] Re: Piecewise function definition
On Tue, Jul 21, 2009 at 6:08 PM, Dougmcke...@gmail.com wrote: Piecewise functions of 2 variables are not yet implemented. Sorry. Ah, I see. If there was were primitive functions for LessThan(x,y), Equal(x,y), and GreaterThan(x,y), and they returned 0 or 1, I think that's all I would need: f(x,y) = y + LessThan(x,pi/2)*f1(x) + Equal(x,pi/2)*f2(x) + GreaterThan (x,pi/2)*f2(x) I don't suppose these exist or are easy to define? I'm not sure what you are going to do with your function. If it is just for plotting, say, I think you might just want to use def f(x,y): if case1: return whatever1 if case2: return whatever2 etc Thanks again for the super-quick replies! Doug --~--~-~--~~~---~--~~ 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 URLs: http://www.sagemath.org -~--~~~~--~~--~--~---
[sage-support] Re: Piecewise function definition
I'm not sure what you are going to do with your function. If it is just for plotting, say, I think you might just want to use def f(x,y): if case1: return whatever1 if case2: return whatever2 etc I'm doing numerical minimization on it right now and the only reason I want/need it to be a sage function is so I can .subs() real values for my parameters. To get around that, I'm just going to use global variables for my parameters. It's kind of ugly, but it will work. It would be nice to be able to get symbolic derivatives too, but I don't need these either. --~--~-~--~~~---~--~~ 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 URLs: http://www.sagemath.org -~--~~~~--~~--~--~---