Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

The Piecewise constructor likes Sage functions and expressions, not Python ones. In the doc page it gives as example arguments f1(x) = -1, f2(x) = 2 (both functions) and x and x^2 (expressions).

Try something like

sage: x = var("x")
sage: g(x) = exp(-1/(1-x**2))
sage: g
x |--> e^(1/(x^2 - 1))
sage: g2(x) = sin(x)
sage: f = Piecewise([ [(-.5,.5), 0.44399*g], [(.5, 1), 0.44399*g+g2(x-0.5)*(x-0.5)] ], var=x)
sage: f
Piecewise defined function with 2 parts, [[(-0.500000000000000, 0.500000000000000), x |--> 0.443990000000000*e^(1/(x^2 - 1))], [(0.500000000000000, 1), x |--> (x - 0.500000000000000)*sin(x - 0.500000000000000) + 0.443990000000000*e^(1/(x^2 - 1))]]
sage: f.plot()

Working with Sage-level expressions and functions has other advantages, too:

sage: f(x) = x  # function
sage: diff(f,x)
x |--> 1
sage: f = x  # expression
sage: diff(f,x)
1
# but
sage: def f(x): # python function
....:     return x
....: 
sage: diff(f,x)
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)

/Applications/sage/local/lib/python2.6/site-packages/sage/all_cmdline.pyc in <module>()

/Applications/sage/local/lib/python2.6/site-packages/sage/calculus/functional.pyc in derivative(f, *args, **kwds)
    132         pass
    133     if not isinstance(f, Expression):
--> 134         f = SR(f)
    135     return f.derivative(*args, **kwds)
    136 

/Applications/sage/local/lib/python2.6/site-packages/sage/structure/parent.so in sage.structure.parent.Parent.__call__ (sage/structure/parent.c:6462)()

/Applications/sage/local/lib/python2.6/site-packages/sage/structure/coerce_maps.so in sage.structure.coerce_maps.DefaultConvertMap_unique._call_ (sage/structure/coerce_maps.c:3118)()

/Applications/sage/local/lib/python2.6/site-packages/sage/structure/coerce_maps.so in sage.structure.coerce_maps.DefaultConvertMap_unique._call_ (sage/structure/coerce_maps.c:3021)()

/Applications/sage/local/lib/python2.6/site-packages/sage/symbolic/ring.so in sage.symbolic.ring.SymbolicRing._element_constructor_ (sage/symbolic/ring.cpp:4326)()

TypeError: 

The Piecewise constructor likes Sage functions and expressions, not Python ones. In the doc page it gives as example arguments f1 and f2 with f1(x) = -1, f2(x) = 2 (both functions) and x and x^2 (expressions).

Try something like

sage: x = var("x")
sage: g(x) = exp(-1/(1-x**2))
sage: g
x |--> e^(1/(x^2 - 1))
sage: g2(x) = sin(x)
sage: f = Piecewise([ [(-.5,.5), 0.44399*g], [(.5, 1), 0.44399*g+g2(x-0.5)*(x-0.5)] ], var=x)
sage: f
Piecewise defined function with 2 parts, [[(-0.500000000000000, 0.500000000000000), x |--> 0.443990000000000*e^(1/(x^2 - 1))], [(0.500000000000000, 1), x |--> (x - 0.500000000000000)*sin(x - 0.500000000000000) + 0.443990000000000*e^(1/(x^2 - 1))]]
sage: f.plot()

Working with Sage-level expressions and functions has other advantages, too:

sage: f(x) = x  # function
sage: diff(f,x)
x |--> 1
sage: f = x  # expression
sage: diff(f,x)
1
# but
sage: def f(x): # python function
....:     return x
....: 
sage: diff(f,x)
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)

/Applications/sage/local/lib/python2.6/site-packages/sage/all_cmdline.pyc in <module>()

/Applications/sage/local/lib/python2.6/site-packages/sage/calculus/functional.pyc in derivative(f, *args, **kwds)
    132         pass
    133     if not isinstance(f, Expression):
--> 134         f = SR(f)
    135     return f.derivative(*args, **kwds)
    136 

/Applications/sage/local/lib/python2.6/site-packages/sage/structure/parent.so in sage.structure.parent.Parent.__call__ (sage/structure/parent.c:6462)()

/Applications/sage/local/lib/python2.6/site-packages/sage/structure/coerce_maps.so in sage.structure.coerce_maps.DefaultConvertMap_unique._call_ (sage/structure/coerce_maps.c:3118)()

/Applications/sage/local/lib/python2.6/site-packages/sage/structure/coerce_maps.so in sage.structure.coerce_maps.DefaultConvertMap_unique._call_ (sage/structure/coerce_maps.c:3021)()

/Applications/sage/local/lib/python2.6/site-packages/sage/symbolic/ring.so in sage.symbolic.ring.SymbolicRing._element_constructor_ (sage/symbolic/ring.cpp:4326)()

TypeError: