FAQ: Mathematica

1. How to start Mathematica

Mathematica has two interfaces: command line interface and Graphical User Interface (GUI).

To start the command line interface, enter command

math

and to start the graphics GUI, enter command

mathematica

GUI Mathematica is supported on MobileVisLab only. Command line based Mathematica can be run on clusters Atlas4/5/6/7 as well.

2. How to run large and long CPU hours Mathematica jobs?

Mathematica programs that are large and need longer computing time to run can be submitted to LSF batch queue(s) to run as batch mode. To do this, firstly you need to open the notebook (*.nb) file in a Mathematica GUI, and then save the file as a plain text file (*.txt) via

File ⇒ Save As… ⇒ Choose Plain Text (*.txt) as Files of type:  Save.

When you have the text file (e.g. prog1.txt), you can submit it to be executed in a batch computing queue. If the program takes less than 4GB memory, you can submit it to run in queue “serial” using the below job submission script (e.g.  pbs_job1.txt):

#!/bin/bash

#PBS -P Project_mathematica
#PBS -q serial
#PBS -l select=1:ncpus=1:mem=5GB
#PBS -j oe
#PBS -N Job_mathematica

cd $PBS_O_WORKDIR;   ## This line is needed, do not modify.

##--- Put your exec/application commands below ---
## If your mathematica program is < prog1.txt >.

math < prog1.txt

You shall save the “prog1.txt” and “pbs_job1.txt” in the same working folder, then you can submit the job via “qsub” command as:

# qsub pbs_job1.txt"

If the program uses more than 4GB memory, you can submit it to run in large memory queues “openmp”. The job submission script will be:

#!/bin/bash

#PBS -P Project_mathematica
#PBS -q openmp
#PBS -l select=1:ncpus=1:mem=20GB
#PBS -j oe
#PBS -N Job_mathematica

cd $PBS_O_WORKDIR;   ## This line is needed, do not modify.

##--- Put your exec/application commands below ---
## If your mathematica program is < prog1.txt >.

math < prog1.txt"

You can also list out the Mathematica job submission script via command “hpc pbs mathematica” in the HPC system terminal.

If you code the data results printed on the screen in the notebook program, you can find the data results in the job output file as defined as “stdout.o” (you can change the filename to other names); if you code the data results saved into a file, then you shall find the result file in the folder of where prog1.txt is located.

3. How do I clear all of the variables in Mathematica

To clear any global variables that you’ve created during a Mathematica session, use the following command:

ClearAll["Global`*"];

To clear all global variables and remove them, do the following command:

ClearAll["Global`*"];
 Remove["Global`*"];

Note that neither of these commands will clear any of the packages that you have loaded; they will also not clear any Mathematica system variables, such as those variables that keep track of your command history.

If your needs are more extensive, then you will want to use the CleanSlate command. If you have Mathematica 3.0 or later, this command is already available in the CleanSlate package.

In[1]:= <<Utilities`CleanSlate`

If you load this package at the beginning of your session, you can return your session to its original state with the CleanSlate command.

In[2]:= x = 1
 Out[2]= 1
 
 In[3]:= CleanSlate[]
 Out[3] = {CleanSlate`, Global`, System`}
 
 In[4]:= x
 Out[4]= x

You can add the command to your init.m file to load the CleanSlate everytime you start the kernel.

4. How do I write a function which evaluates only when the argument is a number?

Most kernel functions will return unevaluated when given undefined symbols as arguments, for example

In[1]:= Sin[x]
 Out[1]= Sin[x]

The standard way users write functions does not have this property.

In[2]:= f[q_] := q^2
 In[3]:= f[x]
 Out[3]= x2

The way to get f[] to return unevaluated for non-numerical arguments is to write it in a way that is defined only for numerical arguments.

In[4]:= Clear[f]
 In[5]:= f[q_?NumberQ] := q^2
 In[6]:= f[x]
 Out[6]= f[x]
 In[7]:= f[2.718]
 Out[7]= 7.38752

The pattern q_?NumberQ will match one of anything, but only if it is a number. In f[x], the x does not meet this criterion, so the f[x] is just echoed as an undefined expression. This technique can be used to limit a function to just matrix arguments, integer arguments, etc, or to use the same function name with different definitions, depending on the type of argument.

5. Can Mathematica import MatLab M-files?

Mathematica cannot import MatLab .M files. However, it can import Matlab .MAT files. The Mathematica Import[ ] function, when invoked with “MAT” as the third argument, will read Matlab version 4 matrix format. This is a special binary format that is used when you invoke the save() command in MatLab. The Math Works changed over to a proprietary binary format for Matlab version 5. We do not support this format, but it is possible to invoke save() in MatLab version 5 so that it generates the older format instead. You should consult your MatLab documantation on details about how to use save().