Simple Calculations
What follows is a very short interactive introduction covering just the features you need
initially. For a more complete introduction to calculations in Mathematica, use the
Tour of Mathematica in the Getting Started section of the Help Browser.
Use of RETURN and
SHIFT-RETURN
In Mathematica, line breaks are entered by pressing
RETURN on the main
keyboard; calculations are passed to the kernel by pressing
SHIFT-RETURN
or ENTER on the numeric keypad.
![[Graphics:../Images/index_gr_3.gif]](../Images/index_gr_3.gif)
![[Graphics:../Images/index_gr_4.gif]](../Images/index_gr_4.gif)
Terminating Input
There is no need to terminate input. Everything in the current input cell
is evaluated as soon as you press SHIFT-RETURN
or ENTER. However, terminating the input line with a
semicolon suppresses output in Mathematica.
![[Graphics:../Images/index_gr_5.gif]](../Images/index_gr_5.gif)
Some programmers switching from other languages may add semicolons out of habit. If
you do not get output from Mathematica when you expect it, check to make sure you
have not accidentally suppressed it.
Syntax
Mathematica Keywords
Mathematica syntax is cohesive and intuitive. You need to follow only a couple of
easy rules:
-
Mathematica keywords are usually full English words or concatenated English words.
The first letter of each piece of a Mathematica command is capitalized.
For example:
Plot, Plot3D, ListPlot, ListDensityPlot,
MeijerG
Some of the most common operations have the usual mathematical shortcuts
(e.g., Tr for trace of a matrix).
-
The rules for using brackets are just as simple. Arguments to
Mathematica functions are always enclosed in square brackets [ ]. Lists,
matrices, and arrays are always enclosed in curly brackets { }. Matrices and arrays
are implemented simply as lists of lists.
Some examples of bracket use:
![[Graphics:../Images/index_gr_6.gif]](../Images/index_gr_6.gif)
![[Graphics:../Images/index_gr_7.gif]](../Images/index_gr_7.gif)
![[Graphics:../Images/index_gr_8.gif]](../Images/index_gr_8.gif)
![[Graphics:../Images/index_gr_9.gif]](../Images/index_gr_9.gif)
![[Graphics:../Images/index_gr_10.gif]](../Images/index_gr_10.gif)
Most Mathematica functions are automatically threaded over lists.
![[Graphics:../Images/index_gr_13.gif]](../Images/index_gr_13.gif)
![[Graphics:../Images/index_gr_14.gif]](../Images/index_gr_14.gif)
Multiple Functions
Any Mathematica function can accept any sensible Mathematica output immediately.
This means that you can pass function arguments and results directly without having to write
external files or conversion code.
For example, this code calculates and plots the eigenvalues of a square random matrix.
![[Graphics:../Images/index_gr_15.gif]](../Images/index_gr_15.gif)
![[Graphics:../Images/index_gr_16.gif]](../Images/index_gr_16.gif)
![[Graphics:../Images/index_gr_17.gif]](../Images/index_gr_17.gif)
Here is the same code written as one step.
![[Graphics:../Images/index_gr_20.gif]](../Images/index_gr_20.gif)
Symbolic and Numeric Arguments
Nearly all Mathematica commands accept numeric as well as symbolic input. Mathematica automatically chooses the algorithms based on the kind of input you give.
Symbolic Input Gives Symbolic Results
![[Graphics:../Images/index_gr_23.gif]](../Images/index_gr_23.gif)
![[Graphics:../Images/index_gr_24.gif]](../Images/index_gr_24.gif)
![[Graphics:../Images/index_gr_25.gif]](../Images/index_gr_25.gif)
![[Graphics:../Images/index_gr_26.gif]](../Images/index_gr_26.gif)
In general, Mathematica assumes that symbolic arguments are complex numbers and
gives the results in complex form.
![[Graphics:../Images/index_gr_27.gif]](../Images/index_gr_27.gif)
![[Graphics:../Images/index_gr_28.gif]](../Images/index_gr_28.gif)
This assumption is an important safeguard against forgetting or
discarding results that depend on branch cuts, for example. However, there
are some functions that assume symbols are real numbers, such as
PowerExpand, which expands all powers of products and powers.
![[Graphics:../Images/index_gr_29.gif]](../Images/index_gr_29.gif)
![[Graphics:../Images/index_gr_30.gif]](../Images/index_gr_30.gif)
Other functions that modify expressions of real or complex variables include
ComplexExpand and ExpToTrig. These functions can be accessed from
the Algebraic Manipulation palette. You may restrict your calculation to real numbers
if you are sure you need only real-valued results. The mechanics of the conversion is covered
in The Mathematica Book.
Numeric Input Gives Exact Numeric Results
Mathematica keeps numbers and values exact as long as possible to prevent roundoff
errors and error propagation. It always returns exact results unless the user instructs it
otherwise.
![[Graphics:../Images/index_gr_31.gif]](../Images/index_gr_31.gif)
![[Graphics:../Images/index_gr_32.gif]](../Images/index_gr_32.gif)
Notice that Mathematica knows that .
Programs using approximations can produce results for this example that differ from zero
by several hundredths.
All internal Mathematica routines, except dedicated approximate numerical routines
like NIntegrate or FindRoot, will return exact results when given integer
inputs.
![[Graphics:../Images/index_gr_34.gif]](../Images/index_gr_34.gif)
![[Graphics:../Images/index_gr_35.gif]](../Images/index_gr_35.gif)
![[Graphics:../Images/index_gr_36.gif]](../Images/index_gr_36.gif)
![[Graphics:../Images/index_gr_37.gif]](../Images/index_gr_37.gif)
Floating Point Input Gives Floating Point Results to the Required Precision
![[Graphics:../Images/index_gr_38.gif]](../Images/index_gr_38.gif)
![[Graphics:../Images/index_gr_39.gif]](../Images/index_gr_39.gif)
![[Graphics:../Images/index_gr_40.gif]](../Images/index_gr_40.gif)
![[Graphics:../Images/index_gr_41.gif]](../Images/index_gr_41.gif)
When it finds a decimal point, Mathematica automatically returns numbers to machine
precision.
Forcing Numerical Evaluation
You can force Mathematica to return approximate numerical results to any required
precision by using numerical functions or by using
N[expression, precision].
![[Graphics:../Images/index_gr_42.gif]](../Images/index_gr_42.gif)
![[Graphics:../Images/index_gr_43.gif]](../Images/index_gr_43.gif)
![[Graphics:../Images/index_gr_44.gif]](../Images/index_gr_44.gif)
![[Graphics:../Images/index_gr_45.gif]](../Images/index_gr_45.gif)
![[Graphics:../Images/index_gr_46.gif]](../Images/index_gr_46.gif)
![[Graphics:../Images/index_gr_47.gif]](../Images/index_gr_47.gif)
An added advantage to using floating point input is speed. When given floating point input
with a precision of less than $MachinePrecision, usually 16 significant digits,
Mathematica uses fast floating point algorithms instead of exact, but slower bignum
algorithms.
Lists
There Is Only One Kind of List
One of the big advantages of Mathematica is that there is only one universal list
type. Matrices, tensors, and arrays of any number of dimensions are implemented as
lists of lists. Mathematica figures out the appropriate operation for each list on
its own. For example, it automatically switches to 2D Fourier transforms if you give the
Fourier function a two-dimensional matrix. Therefore, every list can be used as
input to any function as long as it is mathematically feasible. The same list can be used as
input for normal functions, statistical functions, graphics commands, or any other
appropriate function.
A list is always enclosed in curly brackets and the individual elements are separated by commas.
![[Graphics:../Images/index_gr_48.gif]](../Images/index_gr_48.gif)
![[Graphics:../Images/index_gr_49.gif]](../Images/index_gr_49.gif)
Matrices are simply a list of row vectors, delimited by { }.
![[Graphics:../Images/index_gr_50.gif]](../Images/index_gr_50.gif)
![[Graphics:../Images/index_gr_51.gif]](../Images/index_gr_51.gif)
To give some examples of how lists work, we can create a noisy dataset
using one of the most common ways to create lists, the Table
command.
![[Graphics:../Images/index_gr_52.gif]](../Images/index_gr_52.gif)
![[Graphics:../Images/index_gr_53.gif]](../Images/index_gr_53.gif)
The list can be displayed in different forms.
![[Graphics:../Images/index_gr_54.gif]](../Images/index_gr_54.gif)
|
1
|
1.1087867880938835`
|
|
2
|
2.3653082990113714`
|
|
3
|
3.5305588298308916`
|
|
4
|
4.15327406955195`
|
|
5
|
5.157271475096308`
|
|
6
|
6.28417990393837`
|
|
7
|
7.448429179488507`
|
|
8
|
8.667842219365623`
|
|
9
|
9.772513060907505`
|
|
10
|
10.883934329215432`
|
|
11
|
11.921947262243041`
|
|
12
|
13.603011457017821`
|
|
13
|
14.704069207924537`
|
|
14
|
15.664297553744325`
|
|
15
|
16.629677451917722`
|
|
16
|
16.945402649876144`
|
|
17
|
19.982942551735594`
|
|
18
|
20.8525720486004`
|
|
19
|
22.338287198467075`
|
|
20
|
21.995644895538305`
|
![[Graphics:../Images/index_gr_55.gif]](../Images/index_gr_55.gif)
![[Graphics:../Images/index_gr_56.gif]](../Images/index_gr_56.gif)
Note once again that the list is accepted as input by all functions without change.
![[Graphics:../Images/index_gr_57.gif]](../Images/index_gr_57.gif)
![[Graphics:../Images/index_gr_60.gif]](../Images/index_gr_60.gif)
![[Graphics:../Images/index_gr_61.gif]](../Images/index_gr_61.gif)
![[Graphics:../Images/index_gr_62.gif]](../Images/index_gr_62.gif)
![[Graphics:../Images/index_gr_65.gif]](../Images/index_gr_65.gif)
![[Graphics:../Images/index_gr_66.gif]](../Images/index_gr_66.gif)
![[Graphics:../Images/index_gr_67.gif]](../Images/index_gr_67.gif)
Selecting Parts of Lists
Parts of lists, matrices, and multidimensional arrays can be accessed
through a variety of commands and shortcuts. The most basic way is by
position, using Take and its shortcut [[ ]]. It simply takes
the specified ranges and presents them as output.
![[Graphics:../Images/index_gr_70.gif]](../Images/index_gr_70.gif)
![[Graphics:../Images/index_gr_80.gif]](../Images/index_gr_80.gif)
![[Graphics:../Images/index_gr_81.gif]](../Images/index_gr_81.gif)
![[Graphics:../Images/index_gr_82.gif]](../Images/index_gr_82.gif)
![[Graphics:../Images/index_gr_83.gif]](../Images/index_gr_83.gif)
![[Graphics:../Images/index_gr_84.gif]](../Images/index_gr_84.gif)
![[Graphics:../Images/index_gr_89.gif]](../Images/index_gr_89.gif)
![[Graphics:../Images/index_gr_93.gif]](../Images/index_gr_93.gif)
![[Graphics:../Images/index_gr_94.gif]](../Images/index_gr_94.gif)
To take elements from the end of a list, simply use negative ranges.
![[Graphics:../Images/index_gr_95.gif]](../Images/index_gr_95.gif)
![[Graphics:../Images/index_gr_96.gif]](../Images/index_gr_96.gif)
Other functions for selecting pieces of lists according to their position in the list include
functions such as Drop, First, and Part.
Partition and Flatten
To change the form or dimensions of lists, you can use Flatten and Partition.
![[Graphics:../Images/index_gr_97.gif]](../Images/index_gr_97.gif)
![[Graphics:../Images/index_gr_98.gif]](../Images/index_gr_98.gif)
MatrixForm[Partition[flat, 3]]
![[Graphics:../Images/index_gr_99.gif]](../Images/index_gr_99.gif)
Matrix Operations
Mathematica supports all common matrix operations for symbolic and numeric matrices.
For a list of the available operations, see the chapters on linear algebra starting at
Section 3.7.4 of The Mathematica Book.
Advanced Topic: Selecting Pieces by Value or Criteria
Mathematica also offers a number of ways to select pieces of lists by value or criteria.
The easiest way is using Select. To make the most of Select and many other
functions, understanding the concept of pure functions is essential. As this goes beyond
introductory material, please consult the corresponding sections in The Mathematica Book
(2.2, especially Sections 2.2.5 and 2.2.7).
|