With all due deference to the work that went into making piecewise functions symbolic, it's too much overhead for purely numerical applications, as for example WKB approximations. Here's some sample code <http://sagecell.sagemath.org/?z=eJzFVW1vmzAQ_l6p_-GkfYhJnAKGduskpP2PKIkc4jBrBIhtknS_fmeHt6aoard14wvGfu65u-fOx1bsQBDgFArwvt7eAChhalUAAX1QhlTSZx74wFYk9LkHU8j4fs_txyy6HPUboT0nxSy0B96KAJtyn_CZ_bq9ub3ZorMDgTMd8cc3mpy9FYc5MGQRhNPCGeVcG0ggsOtvsjBC8dRcuNYYeEIYJQENA89zhJZyAANogNpwIxItcpGaUpEFmlBGIxrTe_pAP9Mv9HFJEcvr3CTWZ08HkOXlhudgty8bTVCOtAW5NBIUro3fnTohrHQt7BNUUqTiJLXA3A-1VEKDftpvylymsKuL1Miy0MA1yKKqTWtn81Aiw7MQsxH5jlohp9Mfp61upLTPkSsyqSde-41SYFBFvRdKpjxfW2kyxfOmvliPmvahOs65y8RbBMuWpKlRz8KrSpVnAmQeeitnjDk3DWOb4EDOHesKQX7cBdQ_2CuAPYRvG6ST6cEac6me1lyS62PmRxgieC8VYX9Tkfm1JE4P5P4zSRzxmQ7b4nd1mb9ZmOhDhUHSt7fK_5Yi_shb864O-df3pB0uIabUzhcy6XYnOIu5ynQSUhBHnu_WFpQ0s8Z7zsFGOdirHOyKIxrliF7liK444lGO-FWOuFOkqLeZQIa7IAhxIJsSi7UvjwKqMsdpzI0rqCwy3EBRdWt34kdr1g1wAgto67XA8t3dT10nuIkxd16wOTql8QcHSzowsLCZg9FgAGQW-AwZ0FHG6JpxQNgGM4DHDn5BL_v-qPLSaJsWvonLEWdelwv0q53MtPwpkkVM2ZJCWualSjC2u3saohtrv74IloQBfdnI3fO0l0WCLh4pLvk5wRV4w2BmbTTDC-Kzy-9pLLQ3-x7PIaQBVmBwZb6XJ9LEgpu_ACkxe4w=&lang=sage> for a uniform approximation to a wavefunction in a power potential constructing symbolic functions from numeric functions, and here's the equivalent code <http://sagecell.sagemath.org/?z=eJzFVF1v2jAUfUfiP1xpD7XhhsQGunVSpP0PBMgFN7MUEnCcju7X79pJCO3YRNeq80v8cc-559zreKsfQDNQCAXwr8MBgNWutgUwqA7Wsb2JJYcY5IqJWHEYQaZ2O-UX42lz1G8If86KsfAHfMVAjlTM1NivhoPhYEvJDgyOeCGfuq_Yka8URCCJRTOFRQDlqnKQQuLn30zhtFUb13CtSXjKJLIERcJ5IPSUZ2EAbWDllNNppXO9caVlC4KgxCnOcI63-Bm_4N0SKVbVuUt9zp4OIMvLe5WD3242WlGBtAsKNlIqXKc_nIZC-NJ1YV6P1ZkpC0GlaIvgx6Oy7Ka-4d2aTBBdUe-0NRuVr72pzKq87QxVssY-CfqyRkEDXyTLjuTUTRYJvgrBpK5trW_XgR1PLCsKimcnAf2grgJ1m75eVDB068HK2Ke1MuzlsYynJAkueJZv8xy9NB0co-d8jelAdMTzFv2r8-hq69N3tk79_nO7_7fZ2fve7b92-aNvc1FvM03yJ0ki4BO4kqTsykcN-zLXFSgX5Joiow2iqDpcuXdUmYqQW7Mhkw8mq8xPnS5mKJcImzIvbUpv2WSOgjzv89KtG4ZUJPi7j9N42pkijcTkDmmqjinNerWexuf0X9a9PPTjiMl81JQ1_ENRcIUwGnUy-Tl-_JxAtqhxi0quBU5D7OsTzhpQl68X_wx-keD8TsSyeSd7fD-7ut6X-yYwweTsllTfyx-s1UKbvwCI9enS&lang=sage> without using piecewise and just plotting the numeric functions.
The code without symbolic functions is expected to be faster, but it really is a lot faster. Wouldn't it make sense to have an option to avoid symbolic functions in some way? Maybe define a numerical_piecewise function? On Sunday, May 22, 2016 at 4:42:55 PM UTC-7, paulmasson wrote: > > Automatic creation of a symbolic function from a numeric one sounds like a > good idea to me, if feasible. I'm coming to Sage from Mathematica, where > one doesn't need to think about such issues. It's confusing to new users > when numeric functions fail unexpectedly. > > On Sunday, May 22, 2016 at 1:14:13 AM UTC-7, 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.