# 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.

edit retag close merge delete

Sort by » oldest newest most voted

The 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))

more

Using 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))

more

Using 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))

more
{"ft_lang_label":"__label__en","ft_lang_prob":0.8352672,"math_prob":0.98961484,"size":838,"snap":"2019-51-2020-05","text_gpt3_token_len":381,"char_repetition_ratio":0.22781774,"word_repetition_ratio":0.47761193,"special_character_ratio":0.47494033,"punctuation_ratio":0.1,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.99276346,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2019-12-11T21:48:31Z\",\"WARC-Record-ID\":\"<urn:uuid:5e863aa9-b227-47a8-b1fe-785f1eb4b665>\",\"Content-Length\":\"65978\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:537afd16-05b1-4705-954a-c10cd12ccb19>\",\"WARC-Concurrent-To\":\"<urn:uuid:54a1f5b3-9c2e-46d0-ba2d-194b548dae43>\",\"WARC-IP-Address\":\"140.254.118.68\",\"WARC-Target-URI\":\"https://ask.sagemath.org/question/43797/graphing-max-function-unexpected-behavior/?answer=43802\",\"WARC-Payload-Digest\":\"sha1:VBPS7IF2MIF66FI2SNUPARY7RSCPSHVO\",\"WARC-Block-Digest\":\"sha1:PFXNO3C5NKSI4MFBUJZ6JD5UUBAYOFHO\",\"WARC-Identified-Payload-Type\":\"application/xhtml+xml\",\"warc_filename\":\"/cc_download/warc_2019/CC-MAIN-2019-51/CC-MAIN-2019-51_segments_1575540533401.22_warc_CC-MAIN-20191211212657-20191212000657-00389.warc.gz\"}"}