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, 03 Apr 2019 14:31:28 +0200Graphing max function - unexpected behaviorhttps://ask.sagemath.org/question/43797/graphing-max-function-unexpected-behavior/ My minimal working example is:
(x,y) = var('x y')
def f(x,y):
return max(abs(x),abs(y))
table([[f(x,y) for y in range(-3,4)] for x in range(-3,4)])
The output is this table:
3 3 3 3 3 3 3
3 2 2 2 2 2 3
3 2 1 1 1 2 3
3 2 1 0 1 2 3
3 2 1 1 1 2 3
3 2 2 2 2 2 3
3 3 3 3 3 3 3
That looks like it ought to. But when I try
contour_plot(f(x,y),(x,-3,3),(y,-3,3))
I get something that looks a lot different. It seems I don't have enough karma to upload the image, but its looks more like the one that would be associated to this table:
table([[f(0,y) for y in range(-3,4)] for x in range(-3,4)])
3 2 1 0 1 2 3
3 2 1 0 1 2 3
3 2 1 0 1 2 3
3 2 1 0 1 2 3
3 2 1 0 1 2 3
3 2 1 0 1 2 3
3 2 1 0 1 2 3
I have the issue isolated in a Sage worksheet on CoCalc. I don't have enough karma to include a link, either.Sun, 30 Sep 2018 21:22:00 +0200https://ask.sagemath.org/question/43797/graphing-max-function-unexpected-behavior/Answer by slelievre for <p>My minimal working example is:</p>
<pre><code>(x,y) = var('x y')
def f(x,y):
return max(abs(x),abs(y))
table([[f(x,y) for y in range(-3,4)] for x in range(-3,4)])
</code></pre>
<p>The output is this table:</p>
<pre><code> 3 3 3 3 3 3 3
3 2 2 2 2 2 3
3 2 1 1 1 2 3
3 2 1 0 1 2 3
3 2 1 1 1 2 3
3 2 2 2 2 2 3
3 3 3 3 3 3 3
</code></pre>
<p>That looks like it ought to. But when I try</p>
<pre><code>contour_plot(f(x,y),(x,-3,3),(y,-3,3))
</code></pre>
<p>I get something that looks a lot different. It seems I don't have enough karma to upload the image, but its looks more like the one that would be associated to this table:</p>
<pre><code>table([[f(0,y) for y in range(-3,4)] for x in range(-3,4)])
3 2 1 0 1 2 3
3 2 1 0 1 2 3
3 2 1 0 1 2 3
3 2 1 0 1 2 3
3 2 1 0 1 2 3
3 2 1 0 1 2 3
3 2 1 0 1 2 3
</code></pre>
<p>I have the issue isolated in a Sage worksheet on CoCalc. I don't have enough karma to include a link, either.</p>
https://ask.sagemath.org/question/43797/graphing-max-function-unexpected-behavior/?answer=45999#post-id-45999Using lambda functions is another way to do this.
This is often convenient and useful for plotting in Sage:
- no need to define symbolic variables
- works whether the function is a symbolic function or not
In this case you would do:
sage: contour_plot(lambda x, y: max(abs(x), abs(y)), (-3, 3), (-3, 3))
Wed, 03 Apr 2019 14:31:28 +0200https://ask.sagemath.org/question/43797/graphing-max-function-unexpected-behavior/?answer=45999#post-id-45999Answer by leingang for <p>My minimal working example is:</p>
<pre><code>(x,y) = var('x y')
def f(x,y):
return max(abs(x),abs(y))
table([[f(x,y) for y in range(-3,4)] for x in range(-3,4)])
</code></pre>
<p>The output is this table:</p>
<pre><code> 3 3 3 3 3 3 3
3 2 2 2 2 2 3
3 2 1 1 1 2 3
3 2 1 0 1 2 3
3 2 1 1 1 2 3
3 2 2 2 2 2 3
3 3 3 3 3 3 3
</code></pre>
<p>That looks like it ought to. But when I try</p>
<pre><code>contour_plot(f(x,y),(x,-3,3),(y,-3,3))
</code></pre>
<p>I get something that looks a lot different. It seems I don't have enough karma to upload the image, but its looks more like the one that would be associated to this table:</p>
<pre><code>table([[f(0,y) for y in range(-3,4)] for x in range(-3,4)])
3 2 1 0 1 2 3
3 2 1 0 1 2 3
3 2 1 0 1 2 3
3 2 1 0 1 2 3
3 2 1 0 1 2 3
3 2 1 0 1 2 3
3 2 1 0 1 2 3
</code></pre>
<p>I have the issue isolated in a Sage worksheet on CoCalc. I don't have enough karma to include a link, either.</p>
https://ask.sagemath.org/question/43797/graphing-max-function-unexpected-behavior/?answer=43798#post-id-43798Using one of the related questions that came up after I submitted, I think I found the correct way to get the expected plot:
contour_plot(f,(x,-3,3),(y,-3,3))
However, this still doesn't work as expected:
contour_plot(max(abs(x),abs(y)),(x,-3,3),(y,-3,3))Sun, 30 Sep 2018 21:25:03 +0200https://ask.sagemath.org/question/43797/graphing-max-function-unexpected-behavior/?answer=43798#post-id-43798Answer by tmonteil for <p>My minimal working example is:</p>
<pre><code>(x,y) = var('x y')
def f(x,y):
return max(abs(x),abs(y))
table([[f(x,y) for y in range(-3,4)] for x in range(-3,4)])
</code></pre>
<p>The output is this table:</p>
<pre><code> 3 3 3 3 3 3 3
3 2 2 2 2 2 3
3 2 1 1 1 2 3
3 2 1 0 1 2 3
3 2 1 1 1 2 3
3 2 2 2 2 2 3
3 3 3 3 3 3 3
</code></pre>
<p>That looks like it ought to. But when I try</p>
<pre><code>contour_plot(f(x,y),(x,-3,3),(y,-3,3))
</code></pre>
<p>I get something that looks a lot different. It seems I don't have enough karma to upload the image, but its looks more like the one that would be associated to this table:</p>
<pre><code>table([[f(0,y) for y in range(-3,4)] for x in range(-3,4)])
3 2 1 0 1 2 3
3 2 1 0 1 2 3
3 2 1 0 1 2 3
3 2 1 0 1 2 3
3 2 1 0 1 2 3
3 2 1 0 1 2 3
3 2 1 0 1 2 3
</code></pre>
<p>I have the issue isolated in a Sage worksheet on CoCalc. I don't have enough karma to include a link, either.</p>
https://ask.sagemath.org/question/43797/graphing-max-function-unexpected-behavior/?answer=43802#post-id-43802The issue comes from the fact that the `max(a,b)` function is a Python builtin, that basically asks whether `a<b` and if not returns `a`. However, the symbolic expressions `abs(x)` and `abs(y)` are not comparable,
sage: bool(abs(x) < abs(y))
False
sage: bool(abs(x) > abs(y))
False
Hence, the `max` function will always return the first argument:
sage: max(abs(x),abs(y))
abs(x)
sage: max(abs(y),abs(x))
abs(y)
Here, you want to deal with the max wiewed as a symbolic expression, so you have to use `max_symbolic` instead:
sage: max_symbolic(abs(x),abs(y))
max(abs(x), abs(y))
sage: contour_plot(max_symbolic(abs(x),abs(y)),(x,-3,3),(y,-3,3))
Mon, 01 Oct 2018 15:32:37 +0200https://ask.sagemath.org/question/43797/graphing-max-function-unexpected-behavior/?answer=43802#post-id-43802Comment by leingang for <p>The issue comes from the fact that the <code>max(a,b)</code> function is a Python builtin, that basically asks whether <code>a<b</code> and if not returns <code>a</code>. However, the symbolic expressions <code>abs(x)</code> and <code>abs(y)</code> are not comparable,</p>
<pre><code>sage: bool(abs(x) < abs(y))
False
sage: bool(abs(x) > abs(y))
False
</code></pre>
<p>Hence, the <code>max</code> function will always return the first argument:</p>
<pre><code>sage: max(abs(x),abs(y))
abs(x)
sage: max(abs(y),abs(x))
abs(y)
</code></pre>
<p>Here, you want to deal with the max wiewed as a symbolic expression, so you have to use <code>max_symbolic</code> instead:</p>
<pre><code>sage: max_symbolic(abs(x),abs(y))
max(abs(x), abs(y))
sage: contour_plot(max_symbolic(abs(x),abs(y)),(x,-3,3),(y,-3,3))
</code></pre>
https://ask.sagemath.org/question/43797/graphing-max-function-unexpected-behavior/?comment=43822#post-id-43822Very informative, thank you.Tue, 02 Oct 2018 18:38:27 +0200https://ask.sagemath.org/question/43797/graphing-max-function-unexpected-behavior/?comment=43822#post-id-43822