How to catch exceptional values (+ in plot3d, one exceptional point ruins the whole plot).

45 views
Skip to first unread message

Emmanuel Charpentier

unread,
Oct 11, 2016, 3:23:58 PM10/11/16
to sage-support
My first problem is simila to one I already signaled : contrary to plot, plot3d doesn't plots surfaces when even one point has no nice, defined value.

Case in point (yes, I'm aware of complex_plot, but I needed a 3D model, for pedagogical reasons...) :

sage: var("x,y")
(x, y)
sage: plot3d(abs(gamma(x+I*y)),(x,-2.5,2.5),(y,-2.5,2.5))

[ Looong stack trace : see enclosed log. Snip... ]
TypeError: unable to coerce to a real number
 Indeed : my grid probably contains a non-positive integer. Let's write a function where such exceptions can be catched and givet "flag" values, that could be eliminated later. First try :

sage: def toto(x,y,cl=10.0):
....:         r=(CDF(RDF(x)+I*RDF(y)))
....:         return r
....:


Test it on a small matrix :

argums=matrix([[toto((x-5)/10,(y-5)/10) for y in range(11)] for x in range
....: (11)],ring=CDF)
sage: argums[4:7,4:7]


[-0.1 - 0.1*I         -0.1 -0.1 + 0.1*I]
[      -0.1*I          0.0        0.1*I]
[ 0.1 - 0.1*I          0.1  0.1 + 0.1*I]


My grid indeeds contains 0.

sage: vals=matrix(map(lambda t:map(lambda u:gamma(u), t), argums))
sage: vals[4:7,4:7]

[A number less than infinity A number less than infinity A number less than infinity]
[A number less than infinity                    Infinity A number less than infinity]
[A number less than infinity A number less than infinity A number less than infinity]


WTF ?

sage: vals.parent()
Full MatrixSpace of 11 by 11 dense matrices over The Unsigned Infinity Ring


Aaarghh.. All my values have been coerced to a two-element rng : infinite and non-infinite. Something like that must happen in the internals of plot3d. (No, I didn't yet looked at them...).

Trying to coerce the results to CDF fails (see attached log).

Second problem : I can't for the life of me think of a way to catch this problem in the function generating the value. One cannot test for infinity. One possible way would be to test for (1/r).is_zero(), but this is bound to fail on functions that might have zeroes...

A possible workaround would be to collect the values not in a matrix, but in a list of lists (no coercion to a common ring), and post-process it later (in an hypothetical new version of plot3d, which would be able to omit "holes" in the function...).

Other possible workaround : coerce such infinite values to something in CDF marking infinite values (this must exist, since CDF has an is_finte() method...). But touching to corections is tricky...

Anyway, all these workarounds do not solve the current impossibility to plot surfaces not entirely defined...

Any ideas ?

--
Emmanuel Charpentier
GammaTest.log

William Stein

unread,
Oct 11, 2016, 4:08:00 PM10/11/16
to sage-support
On Tue, Oct 11, 2016 at 12:23 PM, Emmanuel Charpentier
<emanuel.c...@gmail.com> wrote:
> My first problem is simila to one I already signaled : contrary to plot,
> plot3d doesn't plots surfaces when even one point has no nice, defined
> value.
>
> Case in point (yes, I'm aware of complex_plot, but I needed a 3D model, for
> pedagogical reasons...) :
>
> sage: var("x,y")
> (x, y)
> sage: plot3d(abs(gamma(x+I*y)),(x,-2.5,2.5),(y,-2.5,2.5))


> Any ideas ?

var("x,y")

def f(x,y):
try: return abs(gamma(x+I*y))
except: return 0

plot3d(f,(x,-2.5,2.5),(y,-2.5,2.5))

>
> --
> Emmanuel Charpentier
>
> --
> You received this message because you are subscribed to the Google Groups
> "sage-support" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to sage-support...@googlegroups.com.
> To post to this group, send email to sage-s...@googlegroups.com.
> Visit this group at https://groups.google.com/group/sage-support.
> For more options, visit https://groups.google.com/d/optout.



--
William (http://wstein.org)
Screen Shot 2016-10-11 at 1.07.02 PM.png

Emmanuel Charpentier

unread,
Oct 12, 2016, 1:00:13 AM10/12/16
to sage-support
Okay : you catch *all* exceptions. I didn't know you could do that. I tried to filter on the nature of the exception (didn't work : catching ValueError raised a TypeError later) or the value (how to do the comparison ?).

[ Brown paper bag goes on head... ]

Thank you !

--
Emmanuel Charpentier

Jeroen Demeyer

unread,
Oct 12, 2016, 2:01:54 AM10/12/16
to sage-s...@googlegroups.com
On 2016-10-11 22:07, William Stein wrote:
> try: return abs(gamma(x+I*y))
> except: return 0

That is almost never what you want, since this will also catch things
like KeyboardInterrupt. Better use

try:
...
except Exception:
...

Emmanuel Charpentier

unread,
Oct 16, 2016, 5:42:59 AM10/16/16
to sage-support
Thank you, Jeroen ! That is indeed much better. I'm still a bit wet behind the ears about Python...

Too bad I didn't see this until after posting this ...

--
Emmanuel Charpentier
Reply all
Reply to author
Forward
0 new messages