Plot an undefined function


When I plotted an undefined function, Mathematica also spends considerable time to process it. It returns a blank figure necessarily. What confuses me is where the Plot spends the time.

For example, assuming that I forgeted to define the function f, I execute the code below. After a moment, I will get a blank graphics.

Plot3D[Im@f[1/4, u + I v], {u, -2, 2}, {v, -2, 2}, Mesh -> None,  
       PlotStyle -> Directive[Opacity[0.7], Orange], ImageSize -> Large, PlotPoints -> 50]

During this moment, what happened to Mathematica?


Posted 2013-04-03T13:07:49.337

Reputation: 691



If you enclose the Plot command with Timing, you can see that it does take some time, on my computer, about 0.05 seconds.

 Timing[Plot3D[ ... ]]

You can see what it's doing using

 Trace[Plot3D[ ... ]]

The answer is that it's doing a lot! It's going through lots of possible options, lots of system calls, lots of Charting, some Legends, and then some Graphics3D=$fail. Then you get the empty plot. All that in only 0.05 seconds!

bill s

Posted 2013-04-03T13:07:49.337

Reputation: 62 963

1It takes 20 seconds for me! – cmc – 2013-04-03T13:51:48.997

It takes 20 seconds in Mathematica 8 and 0.04 second in Mathematica 9. – cmc – 2013-04-03T13:54:16.040

1@cmc I get 2.1 and 0.03, respectively. It runs faster on 9 because it processes the function differently, figuring out it is undefined pretty quickly. – rcollyer – 2013-04-03T14:02:44.593

@rcollyer Those 2 seconds still seem quite excessive, and if the function is defined as f[x_, y_] /; x > 0 && y > 0 := 1, then it takes 2 seconds on v9 too. Also, these 2 seconds are measured on a relatively fast computer. I can imagine that a 5-6 year old laptop or a netbook would take 20 seconds. – Szabolcs – 2013-04-03T14:38:42.170

@bill s, thanks for the Trace. I have always wondered if such a command was available (yet, never went to look for it). – Sos – 2013-04-03T14:40:05.857

@rcollyer Is it possible that lots and lots of things go wrong inside Plot when it gets a non-numerical value, generating myriads of messages, but the errors are simply hidden by Quiet? EDIT: not really

– Szabolcs – 2013-04-03T14:40:19.460

@Szabolcs my suspicion is that it is the difference between numeric and symbolic processing. That latter always takes longer. Why isn't it shortcircuited earlier? I can't speculate. – rcollyer – 2013-04-03T15:10:36.280