Ask Your Question
1

numerical_integral in piecewise function

asked 2016-05-18 00:10:47 +0100

updated 2016-05-23 02:17:40 +0100

If I define a Python function with a numerical integral

def g(x):
    var('u')
    return numerical_integral(u,0,x)[0]

then it behaves as expected for example when plotting with plot(g). If it is included as part of a piecewise function like

f = piecewise([[(-1,0),-x],[(0,1),g]])
plot(f)

then the plot shows up just fine for Sage 6.9 (which was running on sagecell.sagemath.org), but for Sage 7.2 (which is running on the test server) one gets the error message

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-1-474c66ac1c47> in <module>()
      4 
      5 
----> 6 f = piecewise([[(Integer(0),Integer(1)),g]])
      7 plot(f)

/home/sc_serv/sage/src/sage/misc/lazy_import.pyx in sage.misc.lazy_import.LazyImport.__call__ (/home/sc_serv/sage/src/build/cythonized/sage/misc/lazy_import.c:3628)()
    384             True
    385         """
--> 386         return self._get_object()(*args, **kwds)
    387 
    388     def __repr__(self):

/home/sc_serv/sage/local/lib/python2.7/site-packages/sage/functions/piecewise.py in __call__(self, function_pieces, **kwds)
    149                     function = function()
    150                 else:
--> 151                     function = function(var)
    152             function = SR(function)
    153             if var is None and len(function.variables()) > 0:

<ipython-input-1-474c66ac1c47> in g(x)
      1 def g(x):
      2     var('u')
----> 3     return numerical_integral(u,Integer(0),x)[Integer(0)]
      4 
      5 

/home/sc_serv/sage/src/sage/gsl/integration.pyx in sage.gsl.integration.numerical_integral (/home/sc_serv/sage/src/build/cythonized/sage/gsl/integration.c:3387)()
    329       else:
    330          _a=a
--> 331          _b=b
    332          W = <gsl_integration_workspace*> gsl_integration_workspace_alloc(n)
    333          sig_on()

/home/sc_serv/sage/src/sage/symbolic/expression.pyx in sage.symbolic.expression.Expression.__float__ (/home/sc_serv/sage/src/build/cythonized/sage/symbolic/expression.cpp:10403)()
   1384             return float(self._eval_self(float))
   1385         except TypeError:
-> 1386             raise TypeError("unable to simplify to float approximation")
   1387 
   1388     def __complex__(self):

TypeError: unable to simplify to float approximation

Am I missing something simple or is this a bug in Sage 7.2?

There is an existing ticket that appears relevant: http://trac.sagemath.org/ticket/14801

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
2

answered 2016-05-23 02:28:59 +0100

piecewise has been completely rewritten as a symbolic function in Sage 7.2 and requires its component arguments to be symbolic as well. Symbolic functions are constructed from numeric functions using function. Here's an example of how to include numerical_integral in a piecewise function:

def numeric(self, x, **kwds):
  var('u')
  return numerical_integral(u,0,x)[0] 

symbolic = function('symbolic', nargs=1, evalf_func=numeric)

f = piecewise([[(0,1),symbolic(x)]])
plot( f, 0, 1 )

The resulting plot can be seen here. Note that it is critical to include the arguments self and **kdws in the definition of the numeric function.

edit flag offensive delete link more

Your Answer

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

Add Answer

Question Tools

1 follower

Stats

Asked: 2016-05-18 00:10:47 +0100

Seen: 494 times

Last updated: May 23 '16