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.Tue, 02 Oct 2012 11:20:42 +0200Piece-wise functions and plottinghttps://ask.sagemath.org/question/9377/piece-wise-functions-and-plotting/Hi,
I have a piece-wise defined function that I want to plot (and potentially do other symbolic stuff with) and I was wondering how to do this. The problem is that I am defining my function as a Python function:
<pre>def F(x,y):
if( x <= y ):
return x*y
return x+y
</pre>
So I am gluing together two pieces, and I would like to be able to do
<pre>(x,y) = var('x,y')
contour_plot( F(x,y), (x,0,1), (y,0,1) )</pre>
But the problem is that this only plots the second part. This occurs because x <= y evaluates as false (they are variables) and F(x,y) is always evaluated as x+y. In Mathematica there is the <b>Which</b> function that works on symbolic expression to make piece-wise definitions. Is there an equivalent in Sage? Is there another way to do this? If I had a function (say, <i>delta</i>) that just evaluated as 1 if the symbolic expression was true, and 0 if not, I could craft the function as:
<pre>F(x,y) = delta( x<=y ) * (x*y) + delta( x>y ) * (x+y)</pre>
But as it is, I think there is no way to do this. Is there?
Thanks a lot for your help,
EdgarEdgarTue, 02 Oct 2012 11:20:42 +0200https://ask.sagemath.org/question/9377/Contour Plot not working with pseudo-piecewise functionhttps://ask.sagemath.org/question/8370/contour-plot-not-working-with-pseudo-piecewise-function/Hi,
I'm fairly new to Sage and don't know if this is a bug or just my own limited understanding. I want to plot a function 'f(g1, g2)' but only in some relevant area of the plot. All other points should display/evaluate to zero. This should work fine in Octave or numpy but Sage for some reason gives me an error-message which I fail to understand.
Function assignment like so:
def f(g1, g2):
if g1*g2 > 0.0:
if abs(g2) < 1/abs(g1):
return sqrt(0.5*((1+g1*g2)*abs(g1+g2))/(sqrt((1.0-g1*g2)^3)*g1*g2))
else:
return 0
Then the Plot:
var('g1 g2')
contour_plot(f(g1,g2), (g1,-4,4), (g2,-4,4),plot_points=500, cmap='afmhot_r', colorbar=True, contours=srange(1,6,0.5))
and at last the error-message:
ValueError: zero-size array to ufunc.reduce without identity
Strangely, if I assign a single number to 'contours' (or leave it out to get the default value) the error-message does not show but instead I get an empty/white plot.
If I do not use a conditional/if-statement in my function-assignment the contour-plot shows just fine (except that it didn't do what I wanted).
Does anyone know how to fix this or why it doesn't work or at least why I'm being silly for even trying it that way?
Thanks in advance
Jonathan.BSun, 09 Oct 2011 19:05:01 +0200https://ask.sagemath.org/question/8370/