Ask Your Question
1

Coercion problem while defining piecewise function

asked 2011-01-24 18:52:38 -0500

Jose Lykon gravatar image

updated 2011-01-25 00:32:02 -0500

niles gravatar image

In a nutshell: I begin by defining the following function:

def g(x):
return math.exp(-1/(1 - x^2))

which poses no problem. Then, I try to use this function g to define a new function, as follows:

f = Piecewise([ [(-.5,.5), 0.44399*g(x)] ])

which returns the following error:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)

/Applications/sage/<ipython console> in <module>()

/Applications/sage/<ipython console> in g(x)

/Applications/sage/local/lib/python2.6/site-packages/sage/rings/fraction_field_element.so in sage.rings.fraction_field_element.FractionFieldElement.__float__ (sage/rings/fraction_field_element.c:8540)()

/Applications/sage/local/lib/python2.6/site-packages/sage/rings/polynomial/polynomial_element.so in sage.rings.polynomial.polynomial_element.Polynomial.__float__ (sage/rings/polynomial/polynomial_element.c:8243)()

TypeError: cannot coerce nonconstant polynomial to float

-----------

I am not sure of what is going wrong here. Any ideas/solutions would be deeply appreciated.

-jl

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
2

answered 2011-01-24 19:35:02 -0500

DSM gravatar image

updated 2011-01-24 19:35:44 -0500

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: 
edit flag offensive delete link more

Comments

That was very clear and informative. Thanks a bunch!

Jose Lykon gravatar imageJose Lykon ( 2011-01-25 03:55:29 -0500 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

Stats

Asked: 2011-01-24 18:52:38 -0500

Seen: 382 times

Last updated: Jan 25 '11