Day 1 - Lecture - Mathematica 101 - 0 - 0
Day 1 - Lecture - Mathematica 101 - 0 - 0
Basic input
1) Notebook Shortcuts
2+2
2) Formula input
Ctrl + /
Ctrl + 2
3) Symbols
Esc + ? + Esc:
H* a *L Α
H* inf *L ¥
H* \ell *L { ;
4) Help
Ctr + K : Auto-complete
Ctr + Shift + K : Hint for arguments
NI
? NIntegrate
NIntegrate@ f , 8x, xmin , xmax <D gives a numerical approximation to the integral à
xmax
f dx.
xmin
?? NIntegrate
NIntegrate@ f , 8x, xmin , xmax <D gives a numerical approximation to the integral à
xmax
f dx.
xmin
? *Integrate
System‘
Integrate NIntegrate
? *Bessel*
System‘
6) Stop evaluation
7) Advanced
Item["Select &Cell",FrontEndExecute[
Module[{Set[nb,InputNotebook[]]},
SelectionMove[nb,All,Cell];]
],MenuKey["c", Modifiers->{Shift, Control}], MenuEvaluator->Automatic],
Day 1 - Lecture - Mathematica 101_0.nb 5
Syntaxes
1) = and :=
a = 1;
F1@x_D = x + a;
F2@x_D := x + a;
8F1@ΠD, F2@ΠD<
81 + Π, 1 + Π<
Now we change a
a = 2;
8F1@ΠD, F2@ΠD<
81 + Π, 2 + Π<
a = 1;
F1@x_D = HPrint@"F1::I am evaluated"D; xL;
F2@x_D := HPrint@"F2::I am evaluated"D; xL;
F1::I am evaluated
F1@1D
F2@1D
F2::I am evaluated
Since := forces Mathematica to look into the r.h.s. we can apply additional conditions.
That is done using /;
a := b ; b > 0
a := Print@"b ise negative!!!"D ; b < 0
2) == and ===
6
2) == and ===
81 == 1, 1 == 2<
8True, False<
8True, False<
but!
8a b, a === b<
8a b, False<
3) Logical operations
. x ® Zeta@3D
x
x^2 + 1
Zeta@3D
1 + Zeta@3D2
H1 + x H1 + x H1 + xLLL . 1 + x ® 1
1 + x H1 + xL
H1 + x H1 + x H1 + xLLL . 1 + x ® 1
Day 1 - Lecture - Mathematica 101_0.nb 7
? //.
expr . rules repeatedly performs replacements until expr no longer changes.
H1 + ΠL * 2 - 2 - 2 Π
ExpB1 - F . Exp@a_D ® Exp@Simplify@aDD
Π
H1 + ΠL * 2 - 2 - 2 Π
ExpB1 - F . Exp@a_D ¦ Exp@Simplify@aDD
Π
F1@x_D = x + 1;
Usual form
F1@ΠD
1+Π
Π F1
1+Π
F1 Π
1+Π
They have different priority. First the functions with @ are applied.
G@g2 g1 Π f1 f2D
G@f2@f1@g2@g1@ΠDDDDD
Sin Hx + x ^ 2 + x ^ 3 + x ^ 4L
G F@1, 2, 3, 4D
G@1, 2, 3, 4D
Plus F@1, 2, 3, 4D
10
10
f@0, x_D = 0;
f@5, 1D Timing
822.5019, 33.4025<
Clear@fD;
f@0, x_D = 0;
f@5, 1D Timing
80.011858, 33.4025<
10
Patterns
1) Basic usage
Patterns are used throughout Mathematica to represent classes of expressions. A simple example of a pattern is the
expression f@x_D. This pattern represents the class of expressions with the form f@anythingD.
1 + Π2
Collect@ex, Sin@_DD
H7 + Cos@yDL Sin@xD3 + 12 Cos@2 xD Sin@xD Sin@3 xD + Sin@xD2 HCos@2 xD + H84 + 12 Cos@yDL Sin@3 xDL
FreeQ@ex, Sin@_DD
False
FreeQ@ex, Tan@_DD
True
True
2) Simple examples
h@8x_, y_<D := x ^ y
Day 1 - Lecture - Mathematica 101_0.nb 11
h@1, 2D
h@1, 2D
h@8Sin@xD, 3<D
Sin@xD3
FullForm@8b, b ^ 2, b ^ 3<D
? _.
p : v is a pattern object which represents an expression of the form p, which, if omitted, should be replaced by v.
f@ΠD
8Π, 0<
f@Π, 1D
8Π, 1<
g@n_IntegerD := Hn - 1L !
h1@n_ ; MyQ@nDD := Hn - 1L !
h2@a bD := p@n_, m_D := Hn + m - 1L ! ; MyQ@n m D
5) Many arguments
f1@x_D = 8x<;
f2@x__D = 8x<;
f3@1, x___D = 81, x<;
h@1, 2, 3, 2, 4D
h@1, 3, 4D hh@2D
h@1, 2, 1, 2, 4D
6) Examples
8x, xΠ , x<
This finds all ways that the sum can be written in two parts.
Day 1 - Lecture - Mathematica 101_0.nb 13
8g@a, b + cD, g@b, a + cD, g@c, a + bD, g@a + b, cD, g@a + c, bD, g@b + c, aD<
Sometimes it is convenient to name the pattern to use in rhs of substitute or in the definition of the function
1) Simplify, FullSimplify
ã-ä Φ + ãä Φ
2 Cos@ΦD
Making assumptions
-x - 1 - y2 z
H- 1Ln
Simplify@Exp@a + b + c + dD, a + b + c 1D
ã1+d
H- 4 + xL x + H1 + xL2
H1 + 2 Cos@xD Sin@xDL2
HCos@xD + Sin@xDL4
Day 1 - Lecture - Mathematica 101_0.nb 15
2) Factor, Expand
Factor@x ^ 10 - 1D
H- 1 + xL H1 + xL I1 - x + x2 - x3 + x4 M I1 + x + x2 + x3 + x4 M
% Expand
- 1 + x10
Factor@x ^ 8 - 1D
H- 1 + xL H1 + xL I1 + x2 M I1 + x4 M
H- 1 + xL H- ä + xL Hä + xL H1 + xL I- ä + x2 M Iä + x2 M
Ha@1D + a@2DL x@1D2 + 2 Ha@1D + a@2DL x@1D x@2D + Ha@1D + a@2DL x@2D2
3) Collect
Iã-ä x - ãä x M
10
ä Iã-2 ä x - ã2 ä x M
1 5
- +
1024 32
16
Collect@a, Exp@_DD
63 105 5ä 105 5ä 15 15 45 5ä
- - ã-2 ä x - + ã2 ä x + ã-4 ä x + ã4 ä x - + ã-6 ä x -
256 512 16 512 16 128 128 1024 32
45 5ä 5 5 1 ä 1 ä
- ã6 ä x + ã-8 ä x + ã8 ä x - - ã-10 ä x - + ã10 ä x
1024 32 512 512 1024 32 1024 32
1 1 ä 1 ä
- - ã-2 ä y - + ã2 ä y
2 4 2 4 2
63 63 63 ä 63 63 ä
- - ã-2 ä y - + ã2 ä y +
512 1024 512 1024 512
105 5ä 425 25 ä 215 185 ä
ã2 ä x - - + - ã-2 ä y - - ã2 ä y +
1024 32 2048 1024 2048 1024
45 5ä 365 35 ä 275 125 ä
ã-6 ä x - - + + ã-2 ä y - - ã2 ä y +
2048 64 4096 2048 4096 2048
15 15 15 ä 15 15 ä
ã-4 ä x - - ã-2 ä y - + ã2 ä y +
256 512 256 512 256
15 15 15 ä 15 15 ä
ã4 ä x - - ã-2 ä y - + ã2 ä y +
256 512 256 512 256
1 ä 65 15 ä 63 17 ä
ã10 ä x - - + + ã-2 ä y - - ã2 ä y +
2048 64 4096 2048 4096 2048
5 5 5ä 5 5ä
ã-8 ä x - - ã-2 ä y - + ã2 ä y +
1024 2048 1024 2048 1024
5 5 5ä 5 5ä
ã8 ä x - - ã-2 ä y - + ã2 ä y +
1024 2048 1024 2048 1024
1 ä 63 17 ä 65 15 ä
ã-10 ä x - + - + ã-2 ä y + - ã2 ä y +
2048 64 4096 2048 4096 2048
45 5ä 275 125 ä 365 35 ä
ã6 ä x - + - + ã-2 ä y + - ã2 ä y +
2048 64 4096 2048 4096 2048
105 5ä 215 185 ä 425 25 ä
ã-2 ä x - + - + ã-2 ä y + + ã2 ä y
1024 32 2048 1024 2048 1024
Day 1 - Lecture - Mathematica 101_0.nb 17
4) Coefficient
Iã-ä x - ãä x M
10
ä Iã-2 ä x - ã2 ä x M
1 5
- +
1024 32
105 5ä
- -
512 16
:- >
105 5ä 105 5ä 45 5ä
+ ,- - ,- +
512 16 512 16 1024 32
18
: >
63 105 5ä 105 5ä 45 5ä
,- + ,- - ,- +
256 512 16 512 16 1024 32
5) Integrate
x
TimingBIntegrateB , xFF
x^2 + 1
:0.003425, 1 + x2 >
, 8x, a, b<F;F
x
TimingBIntegrateB
x^2 + 1
85.3159, Null<
NSeries@a__D := Normal@Series@aDD;
, 8x, a, b<FF
x
res = TimingBMyIntegrateB
x^2 + 1
:0.001689, - 1 + a2 + 1 + b2 >
:0., b2 + 1 - a2 + 1 >
80.001689, 5.10018<
5.10018
Day 1 - Lecture - Mathematica 101_0.nb 19
6) Series
x2 x3 x4 x5 x6 x7 x8 x9 x10
x- + - + - + - + -
2 3 4 5 6 7 8 9 10
. x ® 10 ^ 8- 2, - 3, - 4<, 20F
f@xD - ser@xD
NB
x ^ 11
Example
Gamma matrices
1) we use "." for non-commutative product. That is not the only possibility.
One can use Ä or some of other symbols as well.
Clear@CircleTimesD;
SetAttributes @∆, OrderlessD;
SetAttributes @CircleTimes, 8Flat, OneIdentity<D;
ee2 = ee . rule1
To make Ä linear:
Let’s check
16 Γ@2D
res . dim ® 4
16 Γ@bD