Beruflich Dokumente
Kultur Dokumente
1. %
The symbol % refers to the last computed result. Beware, this may not be the result directly above the
cell with % if you evaluate cells out of order. The symbol %% is the result before the last result, %%%
the result before that. It is better to assign values you want to reuse to explicit variable names.
In[297]:=
Out[297]=
In[298]:=
Out[298]=
In[299]:=
Out[299]=
In[300]:=
Out[300]=
1+2
3
%+3
6
%+4
10
% + %% + %%%
19
2. List
Lots of functions allow and some require a list as one of its arguments, A list is usually delimited by
braces { }, but this is short-hand for the List function.
In[301]:=
Out[301]=
81, 2, 3, 4, 5<
81, 2, 3, 4, 5<
02.1-Commands.nb
In[302]:=
Out[302]=
List@1, 2, 3, 4, 5D
81, 2, 3, 4, 5<
3. Table
A function that creates lists is the Table command, taking two arguments, an expression possibly
involving some indexing variable and a specification for the range of that variable. Thus, for example,
In[303]:=
Out[303]=
81, 2, 3, 4, 5, 6, 7, 8, 9, 10<
gives a list with generic entry given by i for values of i from 1 to 10 (by default in steps of 1). The
second argument is a list giving a variable, a start value and an end value, which is standard for many
functions in Mathematica. Here optionally you can also add an increment size as a fourth component of
the range specification.
4. MatrixForm
Table can also form two dimensional lists, that is a list of lists, by using two index variables and taking a
third argument specifying a range for the second index variable. A matrix in Mathematica is represented as a list of its rows. To display a matrix as a matrix you can use MatrixForm, one of a number of
functions that dont evaluate to anything but control the output of the expression.
In[304]:=
Out[304]=
In[305]:=
881, 2, 3, 4, 5<, 82, 4, 6, 8, 10<, 83, 6, 9, 12, 15<, 84, 8, 12, 16, 20<,
85, 10, 15, 20, 25<, 86, 12, 18, 24, 30<, 87, 14, 21, 28, 35<,
88, 16, 24, 32, 40<, 89, 18, 27, 36, 45<, 810, 20, 30, 40, 50<<
MatrixForm@Table@i * j, 8i, 1, 10<, 8j, 1, 5<DD
Out[305]//MatrixForm=
1
2
3
4
5
6
7
8
9
10
2
4
6
8
10
12
14
16
18
20
3
6
9
12
15
18
21
24
27
30
4
8
12
16
20
24
28
32
36
40
5
10
15
20
25
30
35
40
45
50
5. TableForm
Sometimes you want to display a table of results. Many functions take optional additional arguments in
the form of rules, in this case rules specifying the formatting of the table with column headers.
02.1-Commands.nb
In[306]:=
Out[306]=
In[307]:=
-1 +
12
>, : , >, : ,
6 2
4
>, : ,
3
2
3
2
>, :
1+
,
12
>, : , 1>>
2
2
3
Out[307]//TableForm=
x
0
sinHxL
0
12
-1+
1
2
3
2
1
2
3
5
12
1+
6. FullForm
If you ever wonder what Mathematica really has as an expression, the FullForm command prints out
results using the names of commands rather than special notations. You wont need this often, but it
may occasionally help clarify puzzling results.
In[308]:=
Sin@Pi 12D
-1 +
Out[308]=
2
In[309]:=
FullForm@Sin@Pi 12DD
Out[309]//FullForm=
7. Length
The Length function gives the length of a list, or in fact the number of arguments in any expression.
In[310]:=
Out[310]=
In[311]:=
Out[311]=
80.1, 0.11, 0.12, 0.13, 0.14, 0.15, 0.16, 0.17, 0.18, 0.19, 0.2<
Length@%D
11
02.1-Commands.nb
In[312]:=
Out[312]=
In[313]:=
Out[313]=
In[314]:=
x^2 + x + 1
1 + x + x2
Length@%D
3
FullForm@%%D
Out[314]//FullForm=
8. Dimensions
A matrix is a list of rows, each row with the same number of elements. To determine how many rows
and columns a matrix has you can use the Dimensions function. Of course this may not be so interesting if you have created the matrix and already know the dimensions. But you can also form an operate
on three dimensional lists, say a list of matrices all having the same number of rows and columns, and
keeping track of indexing in such structures can sometimes be confusing. The Dimensions function
gives a list of the dimensions of such multi-dimensional lists down to the level where all component lists
have the same length. Thus in a list of lists not all of the same length, Dimensions will return just the
length of the top level list, since variable length rows do not make a rectangular matrix.
In[315]:=
Out[315]=
In[316]:=
Out[316]=
In[317]:=
Out[317]=
9. Part
The Part function extracts the n-th element from a list, or really from any expression which is a function
applied to a number of arguments. The double square bracket notation x[[n]] is usually used for the
Part function.
In[318]:=
Out[318]=
In[319]:=
Out[319]=
Negative arguments index backward from the right end of the list
In[320]:=
Out[320]=
%%@@- 1DD
100
The notation x[[i,j]] accesses the i,j-th entry of a two dimensional list. Note that x[[i]] gives the whole i-th
row in a matrix x.
02.1-Commands.nb
The notation x[[i,j]] accesses the i,j-th entry of a two dimensional list. Note that x[[i]] gives the whole i-th
row in a matrix x.
In[321]:=
Out[321]=
In[322]:=
Out[322]=
In[323]:=
Out[323]=
881, 2, 3, 4, 5<, 82, 4, 6, 8, 10<, 83, 6, 9, 12, 15<, 84, 8, 12, 16, 20<,
85, 10, 15, 20, 25<, 86, 12, 18, 24, 30<, 87, 14, 21, 28, 35<,
88, 16, 24, 32, 40<, 89, 18, 27, 36, 45<, 810, 20, 30, 40, 50<<
%@@5, 3DD
15
%%@@5DD
Part applies just as well to non-rectangular matrices, and generalizes to arbitrary multi-dimensional
lists. But it also applies to arbitrary expressions.
In[324]:=
Out[324]=
In[325]:=
Out[325]=
In[326]:=
x^2 + x + 1
1 + x + x2
%@@3DD
x2
FullForm@%%D
Out[326]//FullForm=
Finally, there are a few special argument forms used for Part. The argument All takes all possible
values of the index in that position. A range of indices is denoted by i;;j.
In[327]:=
Out[327]=
881, 2, 3, 4, 5<, 82, 4, 6, 8, 10<, 83, 6, 9, 12, 15<, 84, 8, 12, 16, 20<,
85, 10, 15, 20, 25<, 86, 12, 18, 24, 30<, 87, 14, 21, 28, 35<,
88, 16, 24, 32, 40<, 89, 18, 27, 36, 45<, 810, 20, 30, 40, 50<<
%@@All, 5DD
85, 10, 15, 20, 25, 30, 35, 40, 45, 50<
%%@@1 ;; 3, 1 ;; 2DD
10. N
The N function converts exact numerical quantities to approximate decimal numbers. Numbers written
with a decimal point are automatically decimal numbers with machine precision. Without a decimal
point a number is interpreted as an integer and computations with integers are done exactly, with as
many digits as necessary. The optional second argument specifies the number of significant figures to
generate and display.
The N function converts exact numerical quantities to approximate decimal numbers. Numbers written
with a decimal point are automatically decimal numbers with machine precision. Without a decimal
point a number is interpreted as an integer and computations with integers are done exactly, with as
many digits as necessary. The optional second argument specifies the number of significant figures to
generate and display.
02.1-Commands.nb
In[330]:=
Out[330]=
In[331]:=
Out[331]=
N@22 7D
3.14286
N@Pi, 100D
3.1415926535897932384626433832795028841971693993751058209749445923078164062862089
98628034825342117068
11. NumberForm
The NumberForm command is another wrapper function that doesnt change the value but controls the
formatting of output, in this case controlling the formatting of numbers. The simplest form controls the
number of significant digits displayed.
In[332]:=
Out[332]=
In[333]:=
N@PiD
3.14159
NumberForm@%, 10D
Out[333]//NumberForm=
3.141592654
Note that the first command computes the machine precision form of , approximately 16 decimal
digits. The NumberForm command doesnt calculate more digits of precision than is already available
in a number, it only shows the specified number of digits.of precision. A second version of this command sets the maximum number of digits and the number of digits to the right of the decimal.
In[334]:=
Out[334]//NumberForm=
34.48
12. Round
The Round function rounds an approximate number to the nearest integer, or with an optional second
argument rounds to the nearest multiple of some increment.
In[335]:=
Out[335]=
In[336]:=
Out[336]=
Round@1.2345D
1
Round@1.2345, 0.01D
1.23
13. Chop
Occasionally in a long computation, round-off errors accumulate so that a value which should be zero
ends up to be slightly off, usually on the order of 10-15. The Chop function sets to 0 values that are
sufficiently close to zero, usually within 10-10 but with an optional second argument allowing the tolerance to be specified. This can be espcially useful to get rid of imaginary parts of expressions that
should be real.
Occasionally in a long computation, round-off errors accumulate so that a value which should be zero
ends up to be slightly off, usually on the order of 10-15. The Chop function sets to 0 values that are
sufficiently close to zero, usually within 10-10 but with an optional second argument 02.1-Commands.nb
allowing the tolerance to be specified. This can be espcially useful to get rid of imaginary parts of expressions that
should be real.
In[337]:=
Out[337]=
In[338]:=
Out[338]=
In[339]:=
Out[339]=
1. 3 + 1. 9 + 1. 27 + 1. 81 + 1 243
0.497942
243 * % - 81 - 27 - 9 - 3 - 1
- 1.42109 10-14
Chop@%D
0
In[342]:=
2 * z3 * y 4 * x y
8 x y2 z3
xyzxy
1
Out[342]=
y2
In[344]:=
Out[344]=
In[345]:=
Out[345]=
2^3^4
2 417 851 639 229 258 349 412 352
2 ^ H3 ^ 4L
02.1-Commands.nb
In[346]:=
Out[346]=
2 ^ H3 * 4L
4096
The square root function is entered as Sqrt and displays as a radical. This is one of the few Mathematica functions that is an abbreviation but not standard mathematical usage. It translates as raising to the
1/2 power.
In[347]:=
Sqrt@2D
Out[347]=
In[348]:=
N@%D
Out[348]=
In[349]:=
1.41421
Sqrt@2.D
Out[349]=
1.41421
In[350]:=
Sqrt@xD
x
Out[350]=
In[351]:=
FullForm@%D
Out[351]//FullForm=
20 - 22. I, E, Pi
Mathematica works also with complex numbers, exact and approximate. The square root of -1, i in
standard mathematical notation is denoted by a capital I in keeping with Mathematicas convention of
capitalizing built-in functions, but displays as a stylized letter to distinguish it from the variable i.
In[352]:=
Out[352]=
In[353]:=
Out[353]=
Sqrt@- 1D
H1 + 2 * IL * H2 + 3 * IL
-4 + 7
The base of the natural log is entered as E and displays as a stylized e to distinguish it from a variable.
In[354]:=
Out[354]=
In[355]:=
Out[355]=
N@%D
2.71828
Pi is .
In[356]:=
Out[356]=
Pi
02.1-Commands.nb
In[357]:=
Out[357]=
N@%D
3.14159
23 - 42. Sin, Cos, Tan, Cot, Sec, Csc, ArcSin, ArcCos, ArcTan, ArcCot, ArcSec,
ArcCsc, Exp, Log, Log10, Abs, Sign, Min, Max, Floor
Sin is sin, Cos is cos, Tan is tan, Cot is cot, Sec is sec, Csc is csc, ArcSin is arcsin, ArcCos is arccos,
ArcTan is arctan, ArcCot is arccot, ArcSec is arcsec, ArcCsc is ArcCsc, Exp is exp is the exponential
function ex , Log is ln (or log) the log to base e, Log10 is log10 the log to the base 10, Abs[x] is |x|, Sign
is sgn, Min is min, Max is max, and Floor is the greatest integer function.
In[358]:=
Out[358]=
In[359]:=
Out[359]=
In[360]:=
Out[360]=
Log@xD
Log@10D
In[361]:=
Out[361]=
Simplify@Hx ^ 3 - 1L Hx ^ 2 - 1LD
1 + x + x2
Out[362]=
1+x
In[363]:=
Out[363]=
In[364]:=
Simplify@Sin@2 xD Cos@xDD
2 Sin@xD
Simplify@Sin@ArcTan@xDDD
x
Out[364]=
1 + x2
10
02.1-Commands.nb
The Expand command expands products of sums, e.g., multiplying polynomials to get a polynomial in
the form of a sum of products of powers of variables and coefficients. ExpandAll applies Expand in all
parts of an expression. Factor tries to factor integer polynomials.
In[365]:=
Out[365]=
In[366]:=
Out[366]=
In[367]:=
Out[367]=
Expand@Hx + 1L Hx + 2L Hx + 3LD
6 + 11 x + 6 x2 + x3
Factor@%D
H1 + xL H2 + xL H3 + xL
Occasionally you will have a square root of an even power of a variable. Mathematica doesnt simplify
x2 could be x or -x. PowerExpand expands such expressions assuming a positive x
this since
In[368]:=
Sqrt@x ^ 2D
x2
Out[368]=
In[369]:=
Out[369]=
PowerExpand@%D
x
Together combines sum of fractions, Apart breaks up a rational function as a sum of fractions.
In[370]:=
Together@1 Hx + 1L + 1 Hx + 2L + 1 Hx + 3LD
11 + 12 x + 3 x2
Out[370]=
In[371]:=
H1 + xL H2 + xL H3 + xL
Apart@%D
1
1
+
Out[371]=
1+x
1
+
2+x
3+x
51. Set
You can tell Mathematica to remember the result of a computation by assigning the result to a variable
using the Set command. An equals sign = is used for Set command. Thus
In[372]:=
Out[372]=
In[373]:=
Out[373]=
In[374]:=
Out[374]=
interestrate = 2.5
2.5
principal = 25 000.
25 000.
interest = principal * interestrate 100
625.
52. SetDelayed
The Set (=) command evaluates the right-hand side of the assignment before assignment. The SetDelayed command is denoted by := and makes a definition to a variable or function of an expression to be
evaluated only when the variable or function is used.
02.1-Commands.nb
11
The Set (=) command evaluates the right-hand side of the assignment before assignment. The SetDelayed command is denoted by := and makes a definition to a variable or function of an expression to be
evaluated only when the variable or function is used.
In[375]:=
In[376]:=
linfcn
Out[376]=
In[377]:=
Out[377]=
In[378]:=
Out[378]=
In[379]:=
Out[379]=
In[380]:=
Out[380]=
intercept + 3 x
slope = 2
2
linfcn
intercept + 2 x
slope = 3
3
linfcn
intercept + 3 x
53. Blank
You can define functions using Set, but the left-hand side must use a pattern with Blank, denoted by an
underscore character after a variable, to denote a dummy variable that can get any argument.
In[381]:=
Out[381]=
In[382]:=
Out[382]=
parabola@x_D = x ^ 2 + x + 1
1 + x + x2
parabola@2D
7
Often however you should use SetDelayed instead of Set for function definitions. You need to use
SetDelayed for definitions of functions that you cant evaluate until you have the value of an argument.
In[383]:=
In[384]:=
Out[384]=
More complex patterns, definitions by cases, and recursive definitions are allowed in defining functions,
but you need to be careful not to get have infinite recursive definitions.
In[385]:=
12
02.1-Commands.nb
In[387]:=
Out[387]=
In[388]:=
Out[388]=
sayifinteger@2D
is integer
sayifinteger@2.1D
is not integer
54. Information
The Information command asks the Mathematica kernel what definitions have been made for a symbol.
Typing ?symbol gets information on symbol.
In[389]:=
testfcn@x_D := x ^ 2
In[390]:=
Information@testfcnD
Global`testfcn
testfcn@x_D := x2
In[391]:=
? testfcn
Global`testfcn
testfcn@x_D := x2
55. Clear
Sometimes you have assigned to a variable which you now want treated as a variable without a value.
I avoid this by using longer names for symbols I define and leaving single letter names for variables.
The Clear command removes definitions of variables and functions. If you make an assignment of a
variable that already has a definition that definition is usually replaced. The exception is if you make
definitions a function with different argument patterns. To be sure you dont have conflicting definitions
you may want to first Clear any existing definitions.
In[392]:=
Out[392]=
In[393]:=
Out[393]=
x=4
4
x^2 + x + 1
21
In[394]:=
Clear@xD
In[395]:=
x^2 + x + 1
Out[395]=
1 + x + x2
02.1-Commands.nb
13
A rule specifies a value corresponding to a variable (or other expression possibly with patterns) without
making a global assignment. The notation typed as -> converts to an arrow. Rules are used for specifying options to various functions (see TableForm above) and used to express solutions sets (see Solve
below). You substitute in an expression according to a rule or list of rules using the function ReplaceAll
usually expressed using the notation /. between expression and rules.
In[396]:=
Out[396]=
x + y . 8x 2, y 3<
5
58. Equal
The single equals sign = is used for assignment. An equation is represented in Mathematica with
Equal, denoted by a double equals sign == which translates to a condensed symbol . The Equal
function evaluates to True if its arguments are identically equal and False if they are evidently not
equal, but otherwise, as in the case where there are variables to be determined, remains in the form of
an equation.
In[397]:=
Out[397]=
In[398]:=
Out[398]=
7x+5 2x-3
5 + 7 x -3 + 2 x
Simplify@%D
8+5x 0
In[400]:=
Out[400]=
In[401]:=
Out[401]=
In[402]:=
::x
-b -
I25 + 3
69 M, y
I25 - 3
Solve@a * x ^ 2 + b * x + c 0, xD
b2 - 4 a c
2a
>, :x
-b +
69 M>, :x
b2 - 4 a c
2a
Reduce@a * x ^ 2 + b * x + c 0, xD
-b -
Out[402]=
b2 - 4 a c
a 0 && x
2a
Ka 0 && b 0 && x -
c
b
-b +
2
, y 25 -
25 + 3
69
>>
b2 - 4 a c
2a
O Hc 0 && b 0 && a 0L
25 + 3
69
>>
14
02.1-Commands.nb
In[404]:=
Out[404]=
In[405]:=
Out[405]=
NSolve@x ^ 2 2, xD
63 - 64. D, Derivative
The partial derivative of a function f with respect to a variable x is computed by D[f,x]. If f is given by an
expression, D evaluates the derivative of this expression. If f is a symbol the notation f ' is the derivative function, denoted by Derivative[1][f], which is evaluated if f is defined. The Derivative function
applies to a function not an expression returning another function, and is not usually what you want
except in the case you are taking f ' for a symbol f . Higher order and mixed partial derivatives are
expressed adding more arguments to D for additional variables.
In[406]:=
Out[406]=
D@a * x ^ 2 + b * x + c, xD
b+2ax
In[407]:=
testfunc@x_D := Sin@x ^ 4D
In[408]:=
testfunc '@xD
Out[408]=
In[409]:=
Out[409]=
In[410]:=
Out[410]=
In[411]:=
Out[411]=
4 x3 Cos@x4 D
D@testfunc@xD, xD
4 x3 Cos@x4 D
Derivative@1D@testfuncD@xD
4 x3 Cos@x4 D
Derivative@1D@SinD
Cos@1D &
02.1-Commands.nb
In[412]:=
Out[412]=
In[413]:=
Out[413]=
15
D@Sin@xD, x, xD
- Sin@xD
D@x ^ 3 * y ^ 5, x, yD
15 x2 y4
Integrate@x ^ 5, xD
x6
Out[414]=
6
In[415]:=
Out[415]=
3
In[416]:=
Out[416]=
2
In[417]:=
Out[417]=
In[418]:=
a bottom3
b top2
- bottom c + c top +
a top3
+
3
x5
x Sin@xD x
Out[418]=
8
In[419]:=
Out[419]=
In[420]:=
Out[420]=
10.6667
0.125
16
02.1-Commands.nb
In[421]:=
Out[421]=
x-
6
In[422]:=
Out[422]=
x5
+ O@xD6
120
Limit@Sin@xD x, x 0D
1
69. DSolve
Differential equations can be solved using DSolve.
In[423]:=
Out[423]=
x3
- -x C@1D + C@2D>>
tion, i.e., it calculates the summation notation bi=a f HiL. The Total function adds up the elements of a list.
Thus Sum is the Total of a corresponding Table, except that Sum can also compute some symbolic
sums.
In[424]:=
Out[424]=
In[425]:=
Out[425]=
2
In[426]:=
Out[426]=
In[427]:=
Out[427]=
n H1 + nL
81, 2, 3, 4, 5, 6, 7, 8, 9, 10<
Total@%D
55
02.1-Commands.nb
In[428]:=
17
1.0
0.8
0.6
Out[428]=
0.4
0.2
-1.0
In[429]:=
-0.5
0.5
1.0
0.5
sinHxL
Out[429]=
cosHxL
-0.5
-1.0
Funtions of two variables can be display using Plot3D, giving a surface of variable height above the x-y
plane. The picture can be rotated and tilted to view the surface from different viewpoints. Two ranges
of coordinates need to specified.
18
02.1-Commands.nb
In[430]:=
1.0
0.5
Out[430]=
1.0
0.0
0.5
-0.5
-1.0
-1.0
0.0
-0.5
-0.5
0.0
0.5
1.0
-1.0
Data in the form of a list of ordered pairs can be plotted using ListPlot. If the data in sorted in order of
increasing x value, then the option Joined->True will join the points rather than just draw points.
In[431]:=
Out[431]=
In[432]:=
data = 881, 2<, 82, 3<, 83, 3<, 84, 1<, 85, 2<<
881, 2<, 82, 3<, 83, 3<, 84, 1<, 85, 2<<
2.5
Out[432]= 2.0
1.5
02.1-Commands.nb
In[433]:=
19
2.5
Out[433]= 2.0
1.5
In[435]:=
Out[435]=
In[436]:=
Out[436]=
In[437]:=
Out[437]=
In[438]:=
Out[438]=
In[439]:=
CDF@NormalDistribution@10, 2D, xD
10 - x
1
ErfcB
Out[439]=
20
02.1-Commands.nb
In[440]:=
0.15
Out[440]= 0.10
0.05
5
In[441]:=
10
15
20
0.8
0.6
Out[441]=
0.4
0.2
10
15
20
0.20
0.15
Out[442]=
0.10
0.05
10
02.1-Commands.nb
In[443]:=
0.4
0.3
Out[443]=
0.2
0.1
2
In[444]:=
10
Out[444]=
0.15
0.10
0.05
10
0.20
0.15
Out[445]=
0.10
0.05
10
21
22
02.1-Commands.nb
In[446]:=
Out[446]=
0.10
0.05
10
In[448]:=
Out[448]=
Function@x, x ^ 2 + x + 1D
FunctionAx, x2 + x + 1E
%@2D
7
This would ordinarily not be any simpler than writing the formula f and substituting with a rule for the
variable x, but it does protect the variable x when x has a value, where the substitution method would
fail.
In[449]:=
Out[449]=
In[450]:=
Out[450]=
In[451]:=
Out[451]=
In[452]:=
x=2
2
Function@x, x ^ 2 + x + 1D
FunctionAx, x2 + x + 1E
%@3D
13
Clear@xD
There is a particular useful shorthand notation for this form of function specification. The symbol #,
shorthand for the Slot function, is used for a generic argument and an ampersand & after an expression
in # makes this a function of the #. Thus an anonymous function is defined by
02.1-Commands.nb
In[453]:=
^2 + + 1 &
Out[453]=
12 + 1 + 1 &
In[454]:=
FullForm@%D
23
Out[454]//FullForm=
In application, we have
In[455]:=
Out[455]=
^ 2 + + 1 &@3D
13
Anonymous functions of more than one variable are represented with generic arguments #1, #2,...
In[456]:=
Out[456]=
1 ^ 2 + 2 ^ 2 &@3, 4D
25
In[458]:=
Out[458]=
81, 2, 3, 4, 5, 6, 7, 8, 9, 10<
Map@ ^ 2 &, data1D
The MapThread function takes a function of several variables and a list of lists, each of the same
length, and applies the function to first elements from the lists, then to the second elements, etc.,
returning a list of all of the results, essentially from applying the function to arguments taken from each
column of the matrix of values.
In[459]:=
Out[459]=
The Thread function does something similar to Map and MapThread. If (the evaluated form of) an
expression is a function applied to one or several lists, then Thread applied to this expression gives the
list of applying the function instead to the elements of the list arguments. The trick is that this only
works when the function applied to lists wouldnt already evaluate anyway. Thus because f is not
defined we can thread.
In[460]:=
Out[460]=
In[461]:=
Out[461]=
f@data1D
f@81, 2, 3, 4, 5, 6, 7, 8, 9, 10<D
Thread@f@data1DD
8f@1D, f@2D, f@3D, f@4D, f@5D, f@6D, f@7D, f@8D, f@9D, f@10D<
24
02.1-Commands.nb
In[462]:=
Out[462]=
In[463]:=
Out[463]=
An example where this is useful is when you have a list of variables and a list of values and you want
the list of rules assigning successive variables to successive values.
In[464]:=
Out[464]=
The real surprise is that many functions are designed to automatically thread over lists. So we have for
instance
In[465]:=
Out[465]=
In[466]:=
Out[466]=
10 * 81, 2, 3<
810, 20, 30<
giving vector addition and scalar multiplication as instances of the threading mechanism. We also have
the less intuitive
In[467]:=
Out[467]=
In[468]:=
Out[468]=
10 + 81, 2, 3<
811, 12, 13<
In[470]:=
testit@2D
Out[470]=
12
The If function takes two or three (or even four arguments), evaluating the first argument, and if the
value is True, evaluating and returning the second (then) argument, while if the result from the first
argument is False, evaluating and returning the third (else) argument, (and if neither True nor False
evaluating and returning a fourth argument if present).
In[471]:=
02.1-Commands.nb
In[472]:=
Out[472]=
In[473]:=
Out[473]=
In[474]:=
Out[474]=
25
testit2@10D
Yes
testit2@0D
No
testit2@yD
If@y > 1, Yes, NoD
The While command gives a structured loop, first argument a test whether to continue and the second
argument repeatedly evaluated as long as the test is true. The For implements a loop with some
initialization (usually of an index variable), some test applied to determine whether to stop, an increment
expression to evaluate at the end of each evaluation of a fourth expression.
In[475]:=
Out[475]=
In[476]:=
Out[476]=
There are a number of other procedural programming functions available. You should probably stick to
interactively evaluating expressions, defining intermediate results and simple functions, since it is much
easier to experiment, change, and understand. You should not write elaborate programs that will be
difficult to debug and understand.
In[478]:=
Out[478]=
In[479]:=
MatrixForm@mat1D
Out[479]//MatrixForm=
In[480]:=
Out[480]=
In[481]:=
Out[481]=
1 2
O
3 4
mat1.81, 1<
83, 7<
mat1.mat1
The determinant of a square matrix is computed by Det. The inverse of an invertible square matrix is
given by Inverse. The transpose of a matrix is given by Transpose.
26
02.1-Commands.nb
The determinant of a square matrix is computed by Det. The inverse of an invertible square matrix is
given by Inverse. The transpose of a matrix is given by Transpose.
In[482]:=
Out[482]=
In[483]:=
Det@mat1D
-2
MatrixForm@Inverse@mat1DD
Out[483]//MatrixForm=
In[484]:=
-2
3
2
-2
MatrixForm@Transpose@mat1DD
Out[484]//MatrixForm=
1 3
O
2 4
In[486]:=
Out[486]=
101. FinancialData
Mathematica has a number of functions that link into external databases. For our purposes the most
interesting of these is the FinancialData function. It can download current or historical data on stock
prices and indices, exchange rates, etc.
In[487]:=
entities = FinancialData@D
Out[487]=
8^A1BSC, ^A1CYC, ^A1DOW, ^A1ENE, ^A1FIN, ^A1HCR, ^A1IDU, ^A1NCY, ^A1SGI, ^A1SGITR,
^A1TEC, ^A1TLS, ^A1UTI, 145 477, Z:XBNCG, Z:XBNCH, Z:XBNCI, Z:XBPHF,
Z:XBPHG, Z:XBPHH, Z:XBPHI, Z:XBREF, Z:XBREG, Z:XBREH, Z:XBREI, Z:ZZ6P, Z:ZZ6Q<
Show Less Show More Show Full Output Set Size Limit...
In[488]:=
Out[488]=
Length@entitiesD
145 503
02.1-Commands.nb
In[489]:=
Out[489]=
In[490]:=
Out[490]=
In[491]:=
Out[491]=
In[492]:=
Out[492]=
FinancialData@"^SPX"D
1831.37
FinancialData@"^SPX", "StandardName"D
SAndP500
FinancialData@"AAPL", "StandardName"D
AppleInc
apple2012 = FinancialData@"AAPL", "Price", 882012, 1, 1<, 82012, 12, 31<<D
27
28
02.1-Commands.nb
In[493]:=
Out[493]=
882012,
882012,
882012,
882012,
882012,
882012,
882012,
882012,
882012,
882012,
882012,
882012,
882012,
882012,
882012,
882012,
882012,
882012,
882012,
882012,
882012,
882012,
882012,
882012,
882012,
882012,
882012,
882012,
882012,
882012,
882012,
882012,
882012,
882012,
882012,
882012,
882012,
882012,
882012,
882012,
882012,
882012,
882012,
882012,
Out[493]=
02.1-Commands.nb
882013,
882013,
882013,
882013,
882013,
882013,
882013,
882013,
882013,
882013,
882013,
882013,
882013,
882013,
882013,
882013,
882013,
882013,
882013,
882013,
882013,
882013,
882013,
882013,
882013,
882013,
882013,
882013,
882013,
882013,
882013,
882013,
882013,
882013,
882013,
882013,
882013,
882013,
882013,
882013,
882013,
882013,
882013,
882013,
882013,
882013,
882013,
882013,
882013,
882013,
882013,
882013,
1,
1,
2,
2,
2,
2,
2,
2,
2,
3,
3,
3,
3,
3,
3,
4,
4,
4,
4,
4,
4,
4,
4,
5,
5,
5,
5,
5,
5,
5,
6,
6,
6,
6,
6,
6,
6,
7,
7,
7,
7,
7,
7,
7,
8,
8,
8,
8,
8,
8,
8,
9,
29
30
02.1-Commands.nb
In[494]:=
882013,
882013,
882013,
882013,
882013,
882013,
882013,
882013,
882013,
882013,
882013,
882013,
882013,
882013,
882013,
882013,
882013,
882013,
882013,
882013,
882013,
882013,
882013,
882013,
882013,
882013,
882013,
DateListPlot@apple2012D
650
600
Out[494]=
550
500
450
400
Jan
Apr
Jul
Oct
Jan
02.1-Commands.nb
In[495]:=
DateListPlot@apple2013D
550
500
Out[495]=
450
400
Jan
In[496]:=
Apr
Jul
Oct
Jan
DateListPlot@Join@apple2012, apple2013DD
650
600
550
Out[496]=
500
450
400
2012
2013
2014
31