ASKSAGE: Sage Q&A Forum - Individual question feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Thu, 08 Nov 2018 10:48:17 -0600Defining functions over symbolic domainshttps://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)])
Wed, 07 Nov 2018 14:04:09 -0600https://ask.sagemath.org/question/44217/defining-functions-over-symbolic-domains/Answer by eric_g for <p>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 <strong>piecewise</strong> 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 <strong>def</strong> here.</p>
<pre><code>L = var('L', domain = 'positive')
f = piecewise([((-oo, -L), 0), ([-L, L], x), ((L, oo), 0)])
</code></pre>
https://ask.sagemath.org/question/44217/defining-functions-over-symbolic-domains/?answer=44227#post-id-44227A solution is to use the symbolic function `unit_step`:
sage: L = var('L', domain = 'positive')
sage: f(x) = unit_step(L+x)*unit_step(L-x)*x
sage: diff(f(x), x)
-x*dirac_delta(L - x)*unit_step(L + x) + x*dirac_delta(L + x)*unit_step(L - x) + unit_step(L + x)*unit_step(L - x)
sage: integrate(f(x), (x, 0, 3*L))
1/2*L^2
sage: integrate(f(x), (x, -3*L, 3*L))
0
Thu, 08 Nov 2018 10:48:17 -0600https://ask.sagemath.org/question/44217/defining-functions-over-symbolic-domains/?answer=44227#post-id-44227