Question: bizarre hang with a simple procedure

Define a slightly messy function - in this case a sum of random Gaussians:

spikes:=sort([seq(RandomTools[Generate](distribution(Uniform(0,5))),i=1..300)]):
Spikes:=add(rand()/1e12*exp(-(Z-spikes[i])^2/2/.001^2)/(1+Z)^(5/2),i=1..nops(spikes)):

Takes no time at all to plot, or evaluate 10^3 times:

time(plot(Spikes,Z=0..5,numpoints=1000,adaptive=0));


Now put the whole thing in a procedure A(z), so that we can use A(z) as a function unless it's called with a numeric argument: 


A:=proc(zz)
if not zz::numeric then return 'procname'(args) fi;
return evalf(eval(Spikes,Z=zz));
end proc:
A(2);

Now it takes ages and 100's of megs of memory to evaluate! (Note: evaluating 100 not 1000 points):

time(plot(A(z),z=0..5,numpoints=100,adaptive=0));
time(seq(A(z),z=0..5,5/100));

 

I have been using this if statement for years with no problems like this, but much exploring leads me to the conclusion that it's that. But why, and how do I fix it? More generally, what's the 'correct' way to write a procedure which evaluates to its name unless all arguments are numeric?

 

Thanks in advance!

Please Wait...