ASKSAGE: Sage Q&A Forum - RSS feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Wed, 07 Sep 2011 21:17:15 +0200"and zero everywhere else"https://ask.sagemath.org/question/8295/and-zero-everywhere-else/I'm trying to create a symbolic function in two variables that returns cos(x)cos(y) for -pi/2<=x<=pi/2 and -pi/2<=y<=pi/2, and zero everywhere else.
I've dug around in the piecewise documentation and I'm running up against a few limitations:
-- no infinite intervals prevents the "everywhere else" construct.
-- I don't think there's a way to express the fact that 3*pi/2 == -pi/2
-- I haven't had much luck defining a piecewise function in two variables.
Is there a way to do this, or am I doomed to a python function and if/else statements?
Thanks--
Greg
Thu, 25 Aug 2011 20:52:32 +0200https://ask.sagemath.org/question/8295/and-zero-everywhere-else/Comment by G B for <p>I'm trying to create a symbolic function in two variables that returns cos(x)cos(y) for -pi/2<=x<=pi/2 and -pi/2<=y<=pi/2, and zero everywhere else.</p>
<p>I've dug around in the piecewise documentation and I'm running up against a few limitations:</p>
<p>-- no infinite intervals prevents the "everywhere else" construct.</p>
<p>-- I don't think there's a way to express the fact that 3*pi/2 == -pi/2</p>
<p>-- I haven't had much luck defining a piecewise function in two variables.</p>
<p>Is there a way to do this, or am I doomed to a python function and if/else statements?</p>
<p>Thanks--</p>
<p>Greg</p>
https://ask.sagemath.org/question/8295/and-zero-everywhere-else/?comment=21299#post-id-21299It's only "doom" because I'd feel like I failed. I'm documenting a new algorithm, and I'm giving Sage another go as a means of doing live documentation. I've been trying to keep as much of the flow symbolic as possible because a sequence of equations is easier for the audience to follow than Python. I could, of course, simply typeset equations for documentation and implement them in Python, but executing the symbolics directly minimizes the chance that the math and the execution diverge. For this particular equation, I'm modeling an antenna pattern, and the model really only fits for the main lobe. The back lobe should be modeled as zero.Fri, 26 Aug 2011 21:47:47 +0200https://ask.sagemath.org/question/8295/and-zero-everywhere-else/?comment=21299#post-id-21299Comment by G B for <p>I'm trying to create a symbolic function in two variables that returns cos(x)cos(y) for -pi/2<=x<=pi/2 and -pi/2<=y<=pi/2, and zero everywhere else.</p>
<p>I've dug around in the piecewise documentation and I'm running up against a few limitations:</p>
<p>-- no infinite intervals prevents the "everywhere else" construct.</p>
<p>-- I don't think there's a way to express the fact that 3*pi/2 == -pi/2</p>
<p>-- I haven't had much luck defining a piecewise function in two variables.</p>
<p>Is there a way to do this, or am I doomed to a python function and if/else statements?</p>
<p>Thanks--</p>
<p>Greg</p>
https://ask.sagemath.org/question/8295/and-zero-everywhere-else/?comment=21301#post-id-21301What I mean by 3pi/2==-pi/2 is that I want to accommodate the periodicity of the cosine. I have rotational symmetry in my problem, and don't want to concern myself with whether the input allows negative angles, or allows angles greater than 2pi. I suppose that's a separate problem from the question I asked...Thu, 25 Aug 2011 23:08:03 +0200https://ask.sagemath.org/question/8295/and-zero-everywhere-else/?comment=21301#post-id-21301Comment by kcrisman for <p>I'm trying to create a symbolic function in two variables that returns cos(x)cos(y) for -pi/2<=x<=pi/2 and -pi/2<=y<=pi/2, and zero everywhere else.</p>
<p>I've dug around in the piecewise documentation and I'm running up against a few limitations:</p>
<p>-- no infinite intervals prevents the "everywhere else" construct.</p>
<p>-- I don't think there's a way to express the fact that 3*pi/2 == -pi/2</p>
<p>-- I haven't had much luck defining a piecewise function in two variables.</p>
<p>Is there a way to do this, or am I doomed to a python function and if/else statements?</p>
<p>Thanks--</p>
<p>Greg</p>
https://ask.sagemath.org/question/8295/and-zero-everywhere-else/?comment=21302#post-id-21302Yeah, this isn't really (currently) doable in Sage very nicely. Of course, you could just do a Python function, as you say. The Piecewise class was useful at its time (and still works well for what it was designed to do), but isn't really able to do more advanced things. I'm not sure what you mean about 3*pi/2==-pi/2; this isn't true even if you are talking about the outputs of your particular function, since your function is zero outside your "box".Thu, 25 Aug 2011 22:02:46 +0200https://ask.sagemath.org/question/8295/and-zero-everywhere-else/?comment=21302#post-id-21302Comment by niles for <p>I'm trying to create a symbolic function in two variables that returns cos(x)cos(y) for -pi/2<=x<=pi/2 and -pi/2<=y<=pi/2, and zero everywhere else.</p>
<p>I've dug around in the piecewise documentation and I'm running up against a few limitations:</p>
<p>-- no infinite intervals prevents the "everywhere else" construct.</p>
<p>-- I don't think there's a way to express the fact that 3*pi/2 == -pi/2</p>
<p>-- I haven't had much luck defining a piecewise function in two variables.</p>
<p>Is there a way to do this, or am I doomed to a python function and if/else statements?</p>
<p>Thanks--</p>
<p>Greg</p>
https://ask.sagemath.org/question/8295/and-zero-everywhere-else/?comment=21300#post-id-21300Why are you trying to create such a function? Knowing this might help us suggest something more useful than symbolic functions. And why is writing a python function "doom"?Fri, 26 Aug 2011 08:56:00 +0200https://ask.sagemath.org/question/8295/and-zero-everywhere-else/?comment=21300#post-id-21300Answer by benjaminfjones for <p>I'm trying to create a symbolic function in two variables that returns cos(x)cos(y) for -pi/2<=x<=pi/2 and -pi/2<=y<=pi/2, and zero everywhere else.</p>
<p>I've dug around in the piecewise documentation and I'm running up against a few limitations:</p>
<p>-- no infinite intervals prevents the "everywhere else" construct.</p>
<p>-- I don't think there's a way to express the fact that 3*pi/2 == -pi/2</p>
<p>-- I haven't had much luck defining a piecewise function in two variables.</p>
<p>Is there a way to do this, or am I doomed to a python function and if/else statements?</p>
<p>Thanks--</p>
<p>Greg</p>
https://ask.sagemath.org/question/8295/and-zero-everywhere-else/?answer=12610#post-id-12610This can be accomplished with the Heaviside step function, something that should be familiar in engineering (especially electrical engineering) contexts. Try `sage: heaviside?` to see the docsting. I'll give a one variable example that you can easily turn into a two variable example for your application:
sage: g(x) = (1-heaviside(x-pi/2)) * heaviside(x + pi/2)
sage: plot(g(x), (x, -pi, pi))
sage: plot( g(x) * cos(x), (x, -pi, pi))
Tue, 30 Aug 2011 01:21:47 +0200https://ask.sagemath.org/question/8295/and-zero-everywhere-else/?answer=12610#post-id-12610Comment by kcrisman for <p>This can be accomplished with the Heaviside step function, something that should be familiar in engineering (especially electrical engineering) contexts. Try <code>sage: heaviside?</code> to see the docsting. I'll give a one variable example that you can easily turn into a two variable example for your application:</p>
<pre><code>sage: g(x) = (1-heaviside(x-pi/2)) * heaviside(x + pi/2)
sage: plot(g(x), (x, -pi, pi))
sage: plot( g(x) * cos(x), (x, -pi, pi))
</code></pre>
https://ask.sagemath.org/question/8295/and-zero-everywhere-else/?comment=21294#post-id-21294Hmm, great point! Not a trick I often think to use, but of course available. Does the two-variable example do integration, differentiation right as well? Tue, 30 Aug 2011 12:39:58 +0200https://ask.sagemath.org/question/8295/and-zero-everywhere-else/?comment=21294#post-id-21294Comment by G B for <p>This can be accomplished with the Heaviside step function, something that should be familiar in engineering (especially electrical engineering) contexts. Try <code>sage: heaviside?</code> to see the docsting. I'll give a one variable example that you can easily turn into a two variable example for your application:</p>
<pre><code>sage: g(x) = (1-heaviside(x-pi/2)) * heaviside(x + pi/2)
sage: plot(g(x), (x, -pi, pi))
sage: plot( g(x) * cos(x), (x, -pi, pi))
</code></pre>
https://ask.sagemath.org/question/8295/and-zero-everywhere-else/?comment=21274#post-id-21274That did it! Thanks!Wed, 07 Sep 2011 21:17:15 +0200https://ask.sagemath.org/question/8295/and-zero-everywhere-else/?comment=21274#post-id-21274