MathGroup Archive 1997

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

Search the Archive

Re: Compiled Functions and Mathgroup Archive

  • To: mathgroup at
  • Subject: [mg8935] Re: Compiled Functions and Mathgroup Archive
  • From: Paul Abbott <paul at>
  • Date: Sat, 4 Oct 1997 22:08:12 -0400
  • Organization: University of Western Australia
  • Sender: owner-wri-mathgroup at

Peter Jay Salzman wrote:

> 1
> -
> I'm writing a numerical Schroedinger equation solver and stumbled on the
> 'Compiled' function.  The MMA book mentioned that sometimes functions run
> faster when you don't compile them.

It can be a lot faster to use MathLink to link to external compiled
code.  See, e.g., our second year quantum mechanics courseware at

In addition you can use the InterCall Mathematica package to easily link
in compiled code without needing to write MathLink template files.  On
MathSource <> you will find the
following two listings:

0204-657: Schroedinger's Equation (March 1993)
          Author: Terry Robb

          Schroedinger's equation is numerically solved by calling an
          external subroutine from within Mathematica. By importing the
          Fortran subroutine (named SCHROED, which implements a
          Crank-Nicholson scheme) using InterCall, it is possible to do
          various experiments -- for example shooting a wave-packet at a
          potential barrier and watching an animation of the packet
          mostly reflected but also partially tunnelling through the
          The potential function V[x,t], required by the Fortran SCHROED
          routine, can be written as a Mathematica function which gives
          lot of flexibility for interactive experimentation. This
          demonstrates one such experiment.

          0011: (772 Kb)

0204-679: Animating Schroedinger's Equation in Two Dimensions (April
          Author: Terry Robb

          An efficient method for solving parabolic partial differential
          equations is implemented in Mathematica using InterCall and an
          external C routine. As an application, the two-dimensional
          time-dependent Schroedinger's equation is solved for various
          initial conditions and potential functions. Four different
          numerical experiments are given: scattering of a particle from
          cylindrical potential barrier; a double slit experiment;
          interaction of wave-packets; and stirring a wave-packet with a
          potential "stick". The resulting animations make excellant
          demonstrations of the properties of Schroedinger's equation. 
          technique used can also be applied to other similar parabolic
          partial differential equations.

          0011: (1.7 Mb)
> I know that people must have asked this question before, but when should
> I use Compile to generate faster code?  My program obviously involes a
> tremendous number of repetitive arithmetic operations, so it seems like I
> should compile things like
> wavefunction[r_] = (alpha/Pi)^.75  E^(-.5 alpha r^2)
> or
> norm[wave] = Sqrt[ (4 Pi (dr)^3 Sum[ (Abs[ wave[[i+1]] ]*i)^2,
>      {i, 0, arsize-1} ]
> where arsize is the gridsize and will be on the order of thousands.

I assume that you can use matrix (or vector) method instead of
element-by-element computation?  Using version 3.0 you can get very
considerable speed-ups by compiling matrix operations.

> 2
> -
> Also, is there an archive of Mathgroup questions so I can look up
> questions like this in the future?  It's only useful if it's searchable.

See and  However it is not
searchable.   I have Cc:d this to suggestions at so that they
can consider making the archive searchable (which I agree would be
_very_ useful).

> 3
> -
> How can I have MMA print something like:
>     The gridsize is arsize
> but replace arsize with the contents of the variable arsize

You can, of course, use Print.  A better way is to use the built-in
message command.  After defining a string such as

   In[1]:= GridSize::size = "The gridsize is `1`."; 

then, for

   In[2]:= arsize = 0.2;

get your code to call message, e.g.,

   In[2]:= Message[GridSize::size,arsize]
   GridSize::"size": "The gridsize is 0.2."

Have a look at Messages.m in 

to complete see a list of the built-in messages.
> 4
> -
> I print a bunch of parameters for the current 'run', and would like to
> prompt myself "If this is OK, press enter to continue but if it's not OK,
> press any other key to end the program".

You can use Input to do this:

  In[3]:= ?Input

  "Input[ ] interactively reads in one Mathematica expression. 
   Input["prompt"] requests input, using the specified string as a


Paul Abbott                                   Phone: +61-8-9380-2734
Department of Physics                           Fax: +61-8-9380-1014
The University of Western Australia           
Nedlands WA  6907                     mailto:paul at 

            God IS a weakly left-handed dice player

  • Prev by Date: Re: NIntegrate where terms of integrand have unknown constant coefficients
  • Next by Date: Beware of Simplify[] bottlenecks in v 3.0
  • Previous by thread: Re: Compiled Functions and Mathgroup Archive
  • Next by thread: Re: