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.
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?
How can I interface Mathematica to Matlab?
How can I globally set the numeric precision for a sequence of Mathematica
calculations?
How can I use Mathematica to numerically integrate a function only over the
interval for which the funtion is positive?
How can I create and save graphics when I run Mathematica in batch mode?
When determining the roots of an equation using the "Solve" command, how
can tell Mathematica I only want the real, positive roots?
How can I change the appearance of a graphic printout and why is it different
the what is displayed on the screen?
How can I call an external C program from within Mathematica on a Sun
workstation?
How can I plot a two-dimensional Gaussian density function (symmetric in x
and y, zero mean, and variance one)?
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
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
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
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
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
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
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
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
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