Services & Resources / Wolfram Forums
MathGroup Archive
*Archive Index
*Ask about this page
*Print this page
*Give us feedback
*Sign up for the Wolfram Insider

MathGroup Archive 1995

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Making Mathematica Functions Evaluate Rapidly?

  • To: mathgroup at
  • Subject: [mg2274] Re: [mg2206] Making Mathematica Functions Evaluate Rapidly?
  • From: Allan Hayes <hay at>
  • Date: Thu, 19 Oct 1995 01:35:14 -0400

"A. E. Siegman" <siegman at>
>Subject: [mg2206] Making Mathematica Functions Evaluate Rapidly?
has some quesions using compiled functions in Compile; delayed and  
immediate evaluation; small imaginary values and making Pi,E..  
numerical (see message attached).

I hope that the following may be of use

Allan Hayea
hay at

****** Compiling Compiled Functions ******

This is usually gives a much slower function than compiling the  
uncompiled version.

Here are some comparisons (I turn off the messages that some of the  
following computations generate).


(*preliminary symbol definition*)
   s = 1+x + x^2 + x^3 +x^4;

(*some functions*)
   f = Function[x, 1+x + x^2 + x^3 + x^4];
   c = Compile[x, 	1+x + x^2 + x^3 + x^4];
   dc := Compile[x, 1+x + x^2 + x^3 + x^4];
   cc = Compile[x ,c[x]];        (*bad: c[x] not evaluated*)
   cf = Compile[x, f[x]];        (*bad: c[x] not evaluated*)
   cs = Compile[x ,s];           (*bad: e not evaluated*)

      TableHeadings -> {#}
   ]&[{"f","c","dc","cc","cf", "cs"}]
	f   1.23333 Second
	c   0.116667 Second
	dc  0.133333 Second
	cc  0.733333 Second
	cf  1.75 Second
	cs  3.41667 Second

(*ways out - beside manually typing in the uncompiled version*)

A way out from the difficulties of cc, cf and cs is to force  
evaluation inside Compile (a compiled function given a symbolic  
input works like the corresponding uncompiled function: for example

	         2    3    4
	1 + y + y  + y  + y



   Compile[x, Evaluate[c[x]]]
	                               2    3    4
	CompiledFunction[{x}, 1 + x + x  + x  + x , -CompiledCode-]

Which is the same as  c.


   Compile[x, Evaluate[e]] === c[x]
   Compile[x, Evaluate[f[x]]] === c[x]

This also works in more complicated cases: Try

   Compile[x, Evaluate[c[x]/(1+ Sin[e]) + Take[f[x],-2]]]

Evaluate affects the whole expression not just the outer function.

   Compile[x, Evaluate[f[1+c[x]]]]

Sometimes more may be needed.

   Compile[x, Evaluate[ReleaseHold[f[1+Hold[c[x]]]]]]

***** Small Imaginary Parts  *****

	-1. + 1.10215 10    I
You can use Chop
***** Making Pi, E ... numerical *****


But Compile seems to cope

   Compile[x, Pi x][2]

Begin forwarded message:

>From: "A. E. Siegman" <siegman at>
>To: mathgroup at
>Subject: [mg2206] Making Mathematica Functions Evaluate Rapidly?
>Organization: Stanford University

I frequently want to evaluate (plot, numerically integrate, calculate
numerical moments of) functions which start out complex, e.g., something

      f[x,y,z] = (1/ f1[x,y,z] ) Exp[ f2[x,y,z] ]

where f1[x,y,z] and f2[x,y,z] may contain various purely real
coefficients, call 'em a,b,c,... which have fixed (predefined) values, as
well as the variables x,y,z , and some explicit I's (the imaginary unit
I), and some standard functions, e.g., Cos[], Sin[], etc.. In other  
all input values are purely real, and all I's are explicit.

Then, what I really want to do is to generate purely real outputs,  

      g1[x,y,z] = Abs[f[x,y,z]]


      g2[x,y,z] = Abs[f[x,y,z]]^2

in a form that will evaluate as rapidly as possible (on a PowerMac).

What's the best way to do this?  Should I define the initial functions
using = or := ?  At what stage should I compile (and how)?  Can I compile
a function that contains other functions that have already been compiled?

I sometimes seem to get very small complex values coming out of the
compiled versions of g1 or g2 (i.e., the Abs[] functions), even though
I've used _Real on x,y,z in the compilation, which makes me think  
they may
being evaluated with complex values even though everything is supposedly
real.  Do I need to use ComplexExpand somehow on f[x,y,z] to separate it
into Re and Im parts, then square them independently to get Abs[]^2 ?

And finally, what if Pi or Sqrt[2] or ... are also contained in the
functions f1, f2 ?  Do I need to do something to force those into
numerical form also?

(My experience is that depending on just how I set up a calculation like
the above I get wildly different speeds -- but there seems to be no
systematic way to know how to get the fastest evaluation.)

Email replies to siegman at appreciated  -- thanks.

  • Prev by Date: Mathematica 2.2.3 under Win95
  • Next by Date: Looking for a program
  • Previous by thread: Re: Making Mathematica Functions Evaluate Rapidly?
  • Next by thread: Spherical trig application