sage: g(x,u) = integral(u^2,u,0,x) 
sage: f=piecewise([ [(0,1), g(x,u) ] ]) 
sage: f 
piecewise(x|-->1/3*x^3 on (0, 1); x)

On Sunday, May 22, 2016 at 10:14:13 AM UTC+2, Volker Braun wrote:
>
> Piecewise functions are symbolic functions now; The problem is that your g 
> doesn't define a symbolic function, so you can't use it as input to 
> piecewise:
>
> sage: g(x)
> ...
> TypeError: unable to simplify to float approximation. You can manually 
> define a symbolic function whose numeric evaluation is prescribed:
>
> sage: def gnum(self, x, **kwds):
> ....:         var('u')
> ....:         return numerical_integral(u^2,0,x)[0] 
> sage: gsym = function('gsym', nargs=1, evalf_func=gnum)
> sage: gsym(x)
> gsym(x)
> sage: gsym(1)
> gsym(1)
> sage: gsym(1).n()
> 0.3333333333333333
>
> Then you can use it to define a piecewise function
>  
> sage: piecewise([[(0,1), gsym(x)]])
>
> We should probably automatically try that if g(x) raises an exception, 
> though there might be further implication...
>
>
>
> On Sunday, May 22, 2016 at 1:38:06 AM UTC+2, paulmasson wrote:
>>
>> Another problem I've encountered concerns including numerical integrals 
>> in piecewise functions. This used to work in Sage 6.9:
>>
>> def g(x):
>>     var('u')
>>     return numerical_integral(u^2,0,x)[0]
>>
>> f=piecewise([ [(0,1), g ] ])
>>
>> but now gives the error message
>>
>>
>> ---------------------------------------------------------------------------
>> TypeError                                 Traceback (most recent call last)
>> <ipython-input-1-d490d0695d4b> in <module>()
>>       4 
>>       5 
>> ----> 6 f=piecewise([ [(Integer(0),Integer(1)), g ] ])
>>       7 
>>
>> /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-d490d0695d4b> in g(x)
>>       1 def g(x):
>>       2     var('u')
>> ----> 3     return numerical_integral(u**Integer(2),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
>>
>>
>> which makes no sense since a numerical integral is already a float. Is 
>> there some other new behavior I'm missing?
>>
>>

-- 
You received this message because you are subscribed to the Google Groups 
"sage-devel" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to sage-devel+unsubscr...@googlegroups.com.
To post to this group, send email to sage-devel@googlegroups.com.
Visit this group at https://groups.google.com/group/sage-devel.
For more options, visit https://groups.google.com/d/optout.

Reply via email to