ASKSAGE: Sage Q&A Forum - Latest question feedhttp://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Mon, 30 Dec 2019 09:46:55 -0600Using piecewise-defined functions for recursive integer sequencehttp://ask.sagemath.org/question/49260/using-piecewise-defined-functions-for-recursive-integer-sequence/I have
![image description](https://matheplanet.de/matheplanet/nuke/html/uploads/b/50970_4_56555555.png)
with `div --> //` ("integer part" of division).
I wonder how to use piecewise-defined functions for calculating `n(i)` and `Q(i)`. <br>
This does not work: <br>
r = 1
m = 7
n(i) = piecewise([([0,0], r//m), ((0,oo), 10*(n(i-1)-Q(i-1)*m))])
Q(i) = piecewise([([0,0], 0), ((1,oo), n(i)//m)])
print n(0)
print n(1)
print n(5)
![image description](https://matheplanet.de/matheplanet/nuke/html/uploads/b/50970_45_9999999999999.jpg)
geroyxMon, 30 Dec 2019 09:46:55 -0600http://ask.sagemath.org/question/49260/How do i plot a piecewise function with functional constraints?http://ask.sagemath.org/question/49060/how-do-i-plot-a-piecewise-function-with-functional-constraints/ I have to plot a function T(x,x) which equals x*y if x*y>0 and x+y otherwise. I have defined the function:
def T(x,y):
if (x*y>0):
return x*y
else:
return x+y
but it does not work.mattiavTue, 17 Dec 2019 08:34:09 -0600http://ask.sagemath.org/question/49060/How do I define a piecewise function with functional restrictions?http://ask.sagemath.org/question/49058/how-do-i-define-a-piecewise-function-with-functional-restrictions/ I have to define a piecewise function T(x,y) which equals x*y if x*y>0 and x+y for x*y<=0. How do I do this?
I have tried in this way:
def T(x,y):
if (x>0):
return x*y
else:
return x+y
but it does not work. How can I solve this problem?mattiavTue, 17 Dec 2019 08:30:25 -0600http://ask.sagemath.org/question/49058/Defining functions over symbolic domainshttp://ask.sagemath.org/question/44217/defining-functions-over-symbolic-domains/ Is there a way to define a function that takes a value (say $x$) on the interval $[-L, L]$ and is zero everywhere else?
I tried the following code but it doesn't work. It gives me an error. This is because **piecewise** only accepts real intervals. Is there an alternative way of defining this? I want to be able to integrate/differentiate such types of functions so my understanding is that I also cannot use **def** here.
L = var('L', domain = 'positive')
f = piecewise([((-oo, -L), 0), ([-L, L], x), ((L, oo), 0)])
vaibhavkarveWed, 07 Nov 2018 14:04:09 -0600http://ask.sagemath.org/question/44217/piecewise defined function via defhttp://ask.sagemath.org/question/41060/piecewise-defined-function-via-def/I've made the following experiment with Sage:
def f(x):
if (0<= x<= 1/2):
return 1
else:
return 0
assume(0<= x<= 1/2)
show(f(x))
show(f(1/3))
However I get outputs 0 and 1 respectively. Can someone clarify please? Thanks.newuserSat, 10 Feb 2018 20:39:46 -0600http://ask.sagemath.org/question/41060/ode and piecewise functionhttp://ask.sagemath.org/question/37847/ode-and-piecewise-function/ Hi all,
I am trying to solve, using Sage, an ode which includes a piecewise. For that I wrote the following piece of code which raises an error :
f = piecewise([(RealSet.unbounded_below_open(0),0), (RealSet.unbounded_above_closed(0),10)])
u = function('u')(x)
eqn = 2*diff(u,x) + u == f(x)
u = desolve(eqn, u, ivar=x)
show(expand(u))
Since the error vanishses when I replace the function `f` by either of the functions `exp` or `log`, I guess the problem is coming from the piecewise function. Could anyone help me solve this issue and explain what is wrong here?
ThankssokingThu, 08 Jun 2017 09:38:47 -0500http://ask.sagemath.org/question/37847/Integrate piecewise function with change of variablehttp://ask.sagemath.org/question/37114/integrate-piecewise-function-with-change-of-variable/I would like to integrate a piecewise defined function while operating a change of variable. I start by defining the function and another variable involved in the change of variable:
phi(x) = piecewise([([-1,1], (1-abs(x))*(1-abs(x))*(1+2*abs(x)))]);
phi(x) = phi.extension(0);
h=pi/n;
h=h.n();
What I would like to do is integrate the function `phi(x/h-1)` between `0` and `pi` so I try it and results in
integral(phi(x/h-1),x,0,pi)
ValueError: substituting the piecewise variable must result in real number
So I then try to use another variable which I try to define to be 'real'
t=var('t')
assume(t,'real');
integral(phi(t/h-1),t,0,pi)
but it results in the same error... Now I try the "lambda" method since it worked when calling the `plot` function with the same change of variable; but fail again
integral(lambda t: phi(t/h-1),t,0,pi)
TypeError: unable to convert <function <lambda> at 0x16d71f140> to a symbolic expression
Now I try to use another integration method with `definite_integral` but get the same errors, only different for the "lambda" method
definite_integral(lambda x: phi(x/h-1),x,0,pi)
TypeError: cannot coerce arguments: no canonical coercion from <type 'function'> to Symbolic Ring
Is there any way around this? I really do not know what else to try...
jrojasquTue, 28 Mar 2017 18:28:56 -0500http://ask.sagemath.org/question/37114/Substitute piecewise function variablehttp://ask.sagemath.org/question/37066/substitute-piecewise-function-variable/I have the following piecewise function:
phi = piecewise([([-1,1], (1-abs(x))*(1-abs(x))*(1+2*abs(x)))]);
phi = phi.extension(0);
It appears to be a valid function since I can obtain/plot its values for any 'x'. But whenever I try to substitute the variable, it does not work. For example,
phi(2)
0
but if I declare another variable 'h' and try to input that variable into the piecewise function, it does not appear to work:
h=pi/2
phi(h)
TypeError: self must be a numeric expression
At first I thought that 'h' was not a 'numeric' or 'real' value, but when I test it, it is a real value:
h.is_real()
True
How can I overcome this? How can I successfully operate a variable substitution in my piecewise function?jrojasquFri, 24 Mar 2017 14:39:26 -0500http://ask.sagemath.org/question/37066/Parametric plot with piecewise inputhttp://ask.sagemath.org/question/35404/parametric-plot-with-piecewise-input/ I'm trying to plot a parametric function, which is defined piecewise. For some reason, the plot just jumps to the last piece of the parametric function and plots that.
Here's what I want to plot.
t = var('t')
r = 2
def f(x):
if 0 <=x <=r:
return (x, -r)
elif r<x<=r + pi*r:
return (cos(x-r), -(r + sin(x-r)))
elif r + pi*r < x <= 3*r + pi*r:
return (-x + 2*r + pi*r, r)
elif 3*r + pi*r < x <= 3*r + 2*pi*r:
return (cos(x - 3*r), -(r + sin(x-r)))
else:
return (x - 4*r - 2*pi*r, -r)
parametric_plot(f(t), (t, 0, 4*r + 2*pi*r))
It just returns a straight line of length 4*r + 2*pi*r with y coordinate -rjford1906Thu, 03 Nov 2016 14:33:56 -0500http://ask.sagemath.org/question/35404/numerical_integral in piecewise functionhttp://ask.sagemath.org/question/33433/numerical_integral-in-piecewise-function/ 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](http://sagecell.sagemath.org/?z=eJwljEEKwyAQAO-B_GFv2YUNmGuhLxEJkq4iWBs2mvj8pnTmNJd5SYCInR7jADenV5zaRP9SqU0LlPYWTZvPaypVovqMjQ13ssaNw88AT9iTbHKlQ9BanBc2xHN3bNHwQhydu6d7_lQM9AWMCiCN&lang=sage)), but for Sage 7.2 (which is running on the [test](http://cosmos.mat.uam.es:8888/?z=eJwljEEKgCAQRfdBd5hdI7iwbdBJREJsFMEsJq2On9F_m_c2fyUPAR8x9R20XZZxqIP4i6lUzpDrRhydTUvMhQLbhFUq-QitTN99eJjhiOTojieh1qjkKGQwph0daS_om7xY8h3k&lang=sage) 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
paulmassonTue, 17 May 2016 17:10:47 -0500http://ask.sagemath.org/question/33433/Piecewise function of several variables and how to display ithttp://ask.sagemath.org/question/31087/piecewise-function-of-several-variables-and-how-to-display-it/Hi,
I need to define a piecewise function of several variables. For a function of one variable I would use probably `Piecewise`. I would like to work with a function G of two variables t and s that is of this form:
def G(t,s):
if (t < s):
return 1
else:
return 0
Although this works, I would like to have a nice displaying of this function, like a function generated by `Piecewise` does. Is there another way how to define this function?
janThu, 26 Nov 2015 07:32:15 -0600http://ask.sagemath.org/question/31087/