Mathematica Miscellanea
- You can use the extra package Utilities`Notation` to tailor
notations in Mathematica to your needs.
- The command UnitStep[ ] from the standard package
Calculus`DiracDelta` can be used to define piecewise functions over
an interval.
- There is a convention in Mathematica that all functions intended for later
use should be given a definite "usage message," which documents their basic usage.
This message is retrieved by typing ?f.
In[1]:= f[x_] := x^2
In[2]:= f::usage = "f[x] gives the square of
x"
Out[2]= f[x] gives the square of x
In[3]:= ?f
f[x] gives the square of x
- The file
Configuration/Kernel/init.m stores Mathematica
commands that are read at startup.
- If you would like Mathematica to automatically load the initialization file for a
directory or a package whenever you start the kernel, then you can copy the
directory/package initialization file into the appropriate place in the
AddOns/Autoload/ directory.
- You can use Length[expr] to get the number of
elements in an expression.
In[1]:= Length[a + bx + c]
Out[1]= 3
- You can interrupt a Mathematica calculation by typing
CONTROL-C if you are using a text-based
interface. To interrupt a Mathematica calculation when using a notebook
interface, you can type
COMMAND-COMMA or
ALT-COMMA.
- You can show the current default setting for a particular option by using
Options[function, option].
In[1]:= Options[Integrate,
GenerateConditions]
Out[1]= {GenerateConditions ->
Automatic}
- Subscripted variables in Version 3.0 and Version 4.0 are not symbols. Rather,
they are parsed as expressions. There are many instances in which you would want
to have a subscripted variable treated as a regular symbol. You can do this with the
package Utilities`Notation`.
- Solve and FindRoot typically return a solution in the
following form.
In[1]:= Solve[{x^2 + y^2 == 1, x + y == .5},
{x, y}]
Out[1]= {{x -> -0.411438, y -> 0.911438},
{x -> 0.911438, y -> -0.411438}}
What Mathematica is returning is a collection of rules. To extract the rules for
x out of the above result, evaluate the following.
In[2]:= x /. %
Out[2]= {-0.411438, 0.911438}
- If you use Mathematica with a text-based interface, there is usually a special
mechanism for executing external commands. With such an interface, Mathematica
takes any line of input that starts with an exclamation point and executes the text on
the remainder of the line as an external command. The following line is taken as
a "shell escape" and executes the Unix command date.
In[1]:= !date
Mon Mar 23 8:16:04 CST 1998
- In Mathematica, Accuracy is the number of significant digits to
the right of the decimal point, and Precision is the number of significant
digits. For numbers less than 1, Accuracy will always be larger than
Precision. An example follows.
In[1]:=
x = N[Pi 10^-99]
Out[1]=
3.14159 10-99
In[2]:=
Precision[x]
Out[2]=
16
In[3]:=
Accuracy[x]
Out[3]=
114
- Modules in Mathematica allow you to treat names of variables as local.
Sometimes, however, you want the names to be global but the values to be local.
You can use Block to do this in Mathematica.
In[1]:=
x + 5
Out[1]=
5 + x
In[2]:=
Block[{x = y + 7}, %]
Out[2]=
12 + y
There is no global value for x.
In[3]:=
x
Out[3]=
x
- The form Function[{x}, body]
is the same as
Function[body] or body&, where
x in body is replaced by #.
The following three expressions all produce the same value when applied to an argument.
In[1]:=
Function[{x}, f[x] + g[x]]
In[2]:=
Function[f[#] + g[#]]
In[3]:=
(f[#] + g[#])&
- Replacement rules allow you to see a transformation applied step by step.
Transformations with functions will continue until all transformations are exhausted.
Please see the following example.
In[1]:=
d[a_ + b__, x_] := d[a, x] + d[b, x]
In[2]:=
d[x_^n_, x_] := n x^(n - 1)
In[3]:=
d[y^2 + y^4, y]
Out[3]=
2 y + 4 y3
The first rule is applied, splitting up the sum. Then the second rule is applied to each
part of the sum. The following is the step-by-step process of the preceding example.
In[4]:=
Clear[d]; drules = {d[a_ + b__, x_]
:> d[a, x] + d[b, x], d[x_^n_, x_]
:> n x^(n - 1)};
ReplaceAll (/.) will replace all patterns that match,
but it will not reapply the replacement.
In[5]:=
newval = d[y^2 + y^4, y] /. drules
Out[5]=
d[y2, y] + d[y4, y]
In[6]:=
newval /. drules
Out[6]=
2 y + 4 y3
If you do not want to see the process step by step, you can use
ReplaceRepeated (//.).
In[7]:=
d[y^2 + y^4, y] //. drules
Out[7]=
2 y + 4 y3
- To create Greek letters on the fly without the use of a palette, hit the escape key
(found in the upper left-hand corner of your keyboard), then the name of the letter,
and finally the escape key again. For example, typing
a produces a
lowercase alpha.
Notice that Greek letters can be used in text cells as well as in
computations.
 ![[Graphics:keystrokes.txtgr1.gif]](images/keystrokes1.gif)
![[Graphics:keystrokes.txtgr2.gif]](images/keystrokes2.gif) ![[Graphics:keystrokes.txtgr3.gif]](images/keystrokes3.gif)
A listing of all keyboard shortcuts can be found by clicking the
Master Index button in the Help Browser and
typing "keyboard shortcuts"
(without the quotes) in the search field.
The Help Browser is accessed by selecting
Help Browser... in the Help menu.
- You can set Mathematica to output everything in traditional
form. In the
Cell menu, select
Default Output FormatType and then
TraditionalForm.
After you do this, Mathematica will return all calculations in "pretty type."
- You can get quick help without the Help Browser by typing ? followed
by a command name. Mathematica will return some information as to how to use that
command.
?Plot
If you are unsure of the spelling of a particular Mathematica
command, wildcard characters (the asterisk, SHIFT-8) can be
used to return everything containing a particular letter pattern.
?Plot*
- You can try these examples using the command Table, which should
give you a good idea of some of the different uses of Table.
Table[x!, {x, 10}]
Table[x!, {x, 1, 20, 2}]
Table[1/(x + y), {x, 4}, {y, 4}]
Table[1/(x + y), {x, 4}, {y, 4}] // TableForm
Table[1/(x + y), {x, 6}, {y, x}] // TableForm
- ComplexExpand and PowerExpand can be very useful
when working with real-valued expressions or expressions that represent real or
positive numbers. For example, try the following.
PowerExpand[Sqrt[x^2] + Log[Exp[y]]]
ComplexExpand[Re[Sin[x + I y]]]
ComplexExpand is useful if some or all of your variables have
real values. The transformations invoked by PowerExpand involve
other requirements, such as assuming that certain variables are positive. You can
read more about these functions in the Help Browser.
- You can use a remote kernel. There are many situations in which you may want
to run the Mathematica notebook front end on one computer but the
Mathematica kernel on a different ("remote") computer. This
technique is frequently used for running large kernel calculations on a fast central
computer while running the notebook front end on a smaller desktop machine.
Information on how to set up a remote kernel on your computer is available in the system
information for your computer. In the
Getting Started/Demos section of the Help Browser,
see the section on running the kernel remotely.
Return to the Mathematica
tips
index.
| |