© 2008 by the Rector and Visitors of the University of Virginia.

The information contained on the University of Virginia’s Department of Information Technology and Communication (ITC) website is provided as a public service with the understanding that ITC makes no representations or warranties, either expressed or implied, concerning the accuracy, completeness, reliability or suitability of the information, including warrantees of title, non-infringement of copyright or patent rights of others. These pages are expected to represent the University of Virginia community and the State of Virginia in a professional manner in accordance with the University of Virginia’s Computing Policies.

ITCWeb: Frequently Asked Questions about Mathematica Frequently Asked Questions about Mathematica

The following are questions that have been submitted to the ITC Research Computing Support Group email address res-consult@virginia.edu concerning the technical computing software Mathematica.

  1. I am having mathematica perform an algebraic operation that has rather complex combinatorics involved but I get an "out of memory" error. How much is the limit?

  2. How can I interface Mathematica to Matlab?

  3. How can I globally set the numeric precision for a sequence of Mathematica calculations?

  4. How can I use Mathematica to numerically integrate a function only over the interval for which the funtion is positive?

  5. How can I create and save graphics when I run Mathematica in batch mode?

  6. When determining the roots of an equation using the "Solve" command, how can tell Mathematica I only want the real, positive roots?

  7. How can I change the look of a graphic printout and why is it different the what is displayed on the screen?

  8. How can I call an external C program from within Mathematica?

  9. How can I plot a two-dimensional Gaussian density function (symmetric in x and y, zero mean, and variance one)?

  10. How can I recall previous commands in Mathematica?


---

I am having Mathematica perform an algebraic operation that has rather complex combinatorics involved but I get an "out of memory" error. How much is the limit?

You can determine the amount of memory currently being used to store
all the data in a Mathematica session with the Mathematica command,

  MemoryInUse[]

and the maximum amount of memory used in a session with the command,

  MaxMemoryUsed[]

Finally, you can constrain the amount of memory used to evaluate an
expression expr to b bytes with the Mathematica command,

  MemoryConstrained[expr,b,failexpr]

where failexpr is the string returned when the constraint is exceeded.

DATE: 4/03/96    

return to top

How can I interface Mathematica to Matlab?

You can do this using Mathematica's Mathlink API. To see examples of how to
do this, go to the Mathematica Web site for contributed software at,

   www.mathsource.com

and do a keyword search on "Matlab". 

DATE: 8/07/2000 

return to top

How can I globally set the numeric precision for a sequence of Mathematica calculations?

This can be done by setting the $MinPrecision variable. This is discussed
in of the Mathematica Book, and you can view it on-line by typing 
'mathematica' to invoke the notebook front-end, selecting the Help Browser from
the Help menu, and searching on $MinPrecision. 

DATE: 8/27/97   

return to top

How can I use Mathematica to numerically integrate a function only over the interval for which the funtion is positive?

NIntegrate does not have an option restrict the integration interval
to when the integrand is positive, but what you can do is define a 
new piecewise function g[x,y] that is equal to f[x,y] when that term is > 0, 
and equal to 0 otherwise. Then substitute g[x,y] for f[x,y] in the integrand 
of NIntegrate.  

Below is an example of the Mathematica code of how to define and display this
new piecewise function.

In[20]:= f[x_,y_]:=Sin[x]Cos[y]

In[22]:= Plot3D[f[x,y],{x,0,2Pi},{y,0,2Pi}]

In[23]:= g[x_,y_]:=Sin[x]Cos[y] /; Sin[x]Cos[y] > 0

In[24]:= g[x_,y_]:=0 /; Sin[x]Cos[y] <= 0

In[25]:= Plot3D[g[x,y],{x,0,2Pi},{y,0,2Pi}]

The relevant material in the Mathematica Book are Sections 2.3.5 (Putting 
Constraints on Patterns) and 2.5.8 (Conditionals).
 
DATE: 8/27/97  

return to top

How can I create and save graphics when I run Mathematica in batch mode?

The following example is Mathematica script file nameds test_graphic.m
that uses the Mathematica plotting command ParametricPlot3D to create
a graphic and the Display command to save it as a postscript file 
named test_graphic.ps. Note that the DisplayFunction -> Identity 
option of ParametricPlot3D suppresses the X-window display of the 
plot. The commands in test_graphic.m are,

a=ParametricPlot3D[{u*Cos[u]*(4 + Cos[v + u]), u*Sin[u]*(4 + Cos[v + u]),
  u*Sin[v + u]}, {u, 0, 4*Pi}, {v, 0, 2*Pi}, PlotPoints -> {60, 12}, 
  DisplayFunction -> Identity];
Display["test_graphic.ps",a,"EPS"];
Exit;

The Unix command to run the script file is,

   math < test_graphic.m > std_out 2&>1

DATE:  4/24/98

return to top

When determining the roots of an equation using the "Solve" command, how can tell Mathematica I only want the real, positive roots?

Here are two ways to do it. The first uses more general techniques and
the second is more elegant.

You can first convert the output of Solve to a list and then
manipulate the list. Here is an example:

In[8]:=
Solve[x^4 - 5*x^2 - 3 == 0, x]

Out[8]=
{{x -> -I*Sqrt[1/2*(-5 + Sqrt[37])]}, 
  {x -> I*Sqrt[1/2*(-5 + Sqrt[37])]}, 
  {x -> -Sqrt[1/2*(5 + Sqrt[37])]}, 
  {x -> Sqrt[1/2*(5 + Sqrt[37])]}}

In[9]:=
x1 = x /. %

-----------------------------------------
The /. operator applies the transformation rules given by Solve to the 
variable x to create a list of solution called x1.
-----------------------------------------

Out[9]=
{-I*Sqrt[1/2*(-5 + Sqrt[37])], I*Sqrt[1/2*(-5 + Sqrt[37])], 
  -Sqrt[1/2*(5 + Sqrt[37])], Sqrt[1/2*(5 + Sqrt[37])]}

In[15]:=
x2 = Select[x1, Im[#] == 0 & ]

------------------------------------------
The second argument of Select defines a pure function that
Select uses to test if Im[ ]==0 for each element and only passes those
that pass the test to a new list x2. 
------------------------------------------

Out[15]=
{-Sqrt[1/2*(5 + Sqrt[37])], Sqrt[1/2*(5 + Sqrt[37])]}

In[16]:=
x3 = Select[x2, Re[#] > 0 & ]

------------------------------------------
The second argument of Select defines a pure function that
Select uses to test if Re[ ]>0 for each element. 
------------------------------------------

Out[16]=
{Sqrt[1/2*(5 + Sqrt[37])]}

The more elegant second method goes as follows. Instead of
Im[x] = 0 and Re[x] > 0, I can just use Abs[x]==x. I can also
incorporate this into the Solve command

Solve[x^4 - 5*x^2 - 3 == 0 && Abs[x] == x, x]

DATE:  08/08/2000

return to top

How can I change the appearance of a graphic printout and why is it different the what is displayed on the screen?

Print inherently has higher resolution than monitor displays. You can increase
the font size of the graphic with the TextStyle option. For example,

In[9]:=  a=ParametricPlot3D[{u Cos[u] (4+Cos[v+u]),u Sin[u] (4+Cos[v+u]),u
Sin[v+u]},{u,0,4 Pi}, {v,0,2 Pi}, PlotPoints -> {60,12},
TextStyle->{FontSize->12}]

You can change the size and location of the printed image with the ImageSize
and ImageOffset options for Display. For example,

In[2]:= Display["test_graphic.ps",a,"EPS",ImageSize -> 72*5,
ImageOffset->{72*3,72*4}]

The units are in printer's points and there are 72 per inch, si I've set the 
image size at 5 inches with and offset of 3 and 4 inches.

DATE:  08/09/2000

return to top

How can I call an external C program from within Mathematica on a Sun workstation?

I did the first example f.c in section 2.12.3 of the Mathematica Book (Setting 
Up External Functions to be Called From Mathematica). First, you need to use 
the the cc compiler in /opt/SUNWspro/bin. I've put that in your PATH variable.

ultra2-1: /home/jms2h $ which cc
/opt/SUNWspro/bin/cc
ultra2-1: /home/jms2h $ 

Then you need to link in two extra libraries, socket and name service library
(nsl).

spudnut: /home/teh1m/math $ mcc -lsocket -lnsl f.tm f.c -o f.exe
f.c:
f.tm.c:
spudnut: /home/teh1m/math $ 

The socket and nsl libraries are included as part of the AIX C compiler so
the extra links are not necessary on blue.unix. I tried it there also
and it worked.

DATE:  08/09/2000

return to top

How can I plot a two-dimensional Gaussian density function (symmetric in x and y, zero mean, and variance one)?

Here is the Mathematica example for generating the grid and contour plots of 
a 2-D Guassian density function assuming zero means and unit variances. When 
I defined the 2X2 correlation matrix R, I further assumed E[xy]=0.6.

R={{1.0,0.6},{0.6,1.0}}
f[x_,y_]:= 1/(2*Pi) * 1/Sqrt[Det[R]] * Exp[-1/2 * ({x,y}.Inverse[R].{x,y})]
Plot3D[f[x,y], {x,-3,3},{y,-3,3},PlotPoints -> 30,PlotRange -> All]
ContourPlot[f[x,y], {x,-3,3},{y,-3,3},PlotPoints -> 30,PlotRange -> All]

DATE:  08/09/2000

return to top

How can I recall previous commands in Mathematica?


Mathematica has no mechanism to do command recall using the arrow keys.
If you are using the notebook front-end (invoked with the command 
'mathematica') then you can scroll back and cut and paste previous commands. 
If you are using the text-based front-end (invoked with the command 'math'), 
you can preceed the 'math' command with the Unix k command, i.e. 'k math' so 
that you can Korn shell-like editing and history on the mathematica commands.

DATE:  09/18/2000

return to top

---