Introduction to Mathematica

Arithmetics

Basic operations

To compute use SHIFT + ENTER or ENTER on the numeric pad.

1+2(* This is a comment/remark *)

For multiple lines just use ENTER.

1-5
3 5
18/6
2^3

For precendence use round brackets (or parentheses).

1+2*3
(1+2)*3
2^3^2
(2^3)^2

What will be the result?

1+2 4
15+5 3^2^2

Operators vs. functions

Instead of operators one can use functions. Notice the square brackets.

Plus[1,2,5]
Plus[1,Minus[5]]
Times[3,5]
Divide[18,6]
Power[2,3]

Some functions can have multiple arguments, others cannot.

Plus[a,b,c]
Minus[a]
Times[x,y,z]
Divide[a,b]
Power[x,y,z,w]

Plus[a,2,b,12]

Divide[a,b,c]

To see the full form of an expression:

FullForm[a x+(b-d)^c]

Constants

Pi

Pi
π

π

Euler number

E
E

Infinity

Infinity
∞

Try to make sense of the results:

∞+∞
∞-∞

∞ ∞
∞/∞

(1^∞)

Imaginary number

I
I

Sqrt[-1]

Miscellaneous

GoldenRatio
EulerGamma

12 Degree

Variables and their scope

Global variables

a=3;
b=5;
Trace[a^b+1]

?a
?b
?c

Local variables

Module[{n=3,m=3},n^m]
n
m

Clearing variables

a=12
Clear[a]

a

a=1;b=2;
Remove[a,b]

?a
?b

Other functions

There are three ways of applying functions

Clear[x,y,z,f,g,h]

Prefix

f[x]
f@x

FullForm@(a+b)
FullForm@a+b
FullForm[a+b]

Associativity

Clear[f,g,h,x]

f@g@h@x

Postfix

x//f
Sequence[x,y,z]//f

Associativity

x//f//g//h

Infix

x~f~y

Associativity

x~f~y~g~a~h~x

Elementary functions

?Sqrt
?Exp
?Log
?Sin
?ArcCos

User defined function

f[x_]:=Exp[Sin[x]]
g[x_,y_]:=x y((x^2)-(y^2))Exp[-(x^2)-(y^2)]

f[1.2]
g[0.1,1.7]

f[a+b]
g[a]

Plotting functions

Plot[{Sin[x],Cos[x]},{x,-π,3π}]

Plot[f[x],{x,-π,3π}]

Plot3D[g[x,y],{x,-3,3},{y,-3,3}]

Numeric Types

Integers

100!
765626^4+125377^4+999877^4
(9^17)((6^6)-187)

(9^17)

Rationals

1/2+1/4+1/8+1/16+1/32
(1/6) ((2+1)/4)-(1/(7^10))
1/(25^(1/2))

FullForm[1/5]

Reals

These are actually floating point numbers.

1.34 2.563
1.34 (10^(-17))/2.563

N[(17711/10946)]
N[(17711/10946),10]
Sqrt[2]
N[Sqrt[2],30]
N[π,1000]

Beware of using the decimal point.

Sqrt[2]
Sqrt[2.]

Complex numbers

Sqrt[-1]

(1+2I)(3-5I)
(1+2I)/(3-5I)

z=3+4I
Re[z]
Im[z]
Conjugate[z]
Arg[z]
Abs[z]
Clear[z]

ComplexExpand[(I+a)Exp[I+3]]
ComplexExpand[(((1+3I))^(1-2I))]

ComplexExpand[(I+1)Exp[I+3]]//N
ComplexExpand[(((1+3I))^(1-2I))]//N

TrigToExp[Sin[x]]
ExpToTrig[Exp[I t]]

Lists

About lists

The most important data structure in Mathematica is list.

The elements of a list are separated by comma.

A list can contain anything, even an other list.

An element can occur multiple times.

The order of elements are important.

Here are some examples of lists:

{}
{1}
{1,2,3}
{1,2,{3,{4,5},2},1,{1,1,{1}},{}}
{{},{{},{}},{{}}}

Generating some special lists

Constant list

ConstantArray[a,5]
ConstantArray[a,{5,3}]

Lists of arithmetic progression

Range[5]
Range[-3,3]
Range[0,10,3]
Range[1,0,-0.25]

Range[5,0]
Range[5,0,-1]

Random lists

Random integers with uniform distribution.

RandomInteger[5,10]
RandomInteger[{-7,5},10]
RandomInteger[{-3,10},{3,10,4}]

Random reals with uniform distribution.

RandomReal[{-1,1},10]

Random complex numbers with uniform distribution.

RandomComplex[{-1-2I,2+3I},5]

Random choice with replacement.

RandomChoice[{-3,1,101,9,0},10]

Random choice without replacement (random permutation).

RandomSample[Range[10]]

RandomSample[Range[10],5]

RandomSample[Range[10],11]

Tables

Table[k,{k,1,10}]
Table[{k,j},{k,1,2},{j,1,3}]
Table[{1,2k,(k^2),-1},{k,1,5}]

The order of the iterators is important. Think carefully about the error below.

Table[{k,j},{k,1,4},{j,1,k}]
Table[{k,j},{k,1,j},{j,1,4}]

Other forms.

Table[k,{k,1,2,0.1}]
Table[{x,y},{x,0,1,1/4},{y,-π,π,π/2}]

Table[{a,b},{a,{x,y,z}},{b,{-π,E,I,12.5}},{c,-1,1}]

Decreasing lists need explicit form.

Table[x,{x,2,-2}]
Table[x,{x,2,-2,-1}]

Tables in nice forms

TableForm[Table[x^y,{x,2,4},{y,{a,b,c,d}}]]

TableForm[
Table[x^y,{x,2,4},{y,{a,b,c,d}}],TableHeadings->{{x,y,z},{a,b,c,d}}
]

TableForm[Table[x y,{x,1,10},{y,1,10}],TableHeadings->Automatic]

Functions related to lists

Part[{a,b,c},3]
Part[{{a,b},{c,{d,e}},f},1,2]

{{a,b},{c,{d,e}},f}[[1,2]]

{{a,b},{c,{d,e}},f}[[1,2]]

First[{a,b,c,d}]
Last[{a,b,c,d}]
Most[{a,b,c,d}]
Rest[{a,b,c,d}]

Take[Range[10],4]
Take[Range[10],-4]
Take[Range[10],{4,10,2}]

Drop[Range[10],4]
Drop[Range[10],-4]
Drop[Range[10],{4,10,2}]

Length[{a,b,{c},{d,e,{f,g}}}]

Join[{1,2,3},{a,{b,c},d},{α,β}]

Flatten[{{1,{2,{3}}},{{{4}}},{a,{b,{c}}},{d}}]

Subsets[{1,2,3,4}]
Subsets[{1,2,3,4},{2,3}]

Tuples[{1,2,3,4},2]
Tuples[{1,2},3]
Tuples[{{1,2},{a,b,c},{α,β,γ,δ}}]

Permutations[Range[3]]
Permutations[Range[3],2]
Permutations[Range[3],{2}]

Partition[Range[10],1]
Partition[Range[10],2]
Partition[Range[10],3]
Partition[Range[10],3,1]
Partition[Range[10],3,5]

Total[{a,b,c}]
Accumulate[{a,b,c}]

RotateLeft[{a,b,c,d,e},3]
RotateRight[{a,b,c,d,e},3]
Reverse[{a,b,c,d,e}]

Map[f,{a,b,c,d}]
f/@{a,b,c,d}

Range[0,1,0.2]
Map[Sin,Range[0,1,0.2]]

Sin[Range[0,1,0.2]]

List arithmetics

3+2{a, b, c}
{a,b,c}^3
1/{a,b,c}
Log[{a,b,c}]
Sin[{x,y}]

{2,3,4}+{a,b,c}
{2,3,4}-{a,b,c}
{2,3,4}^{a,b,c}
{2,3,4}{a,b,c}
{2,3,4}/{a,b,c}

Graphical representation of lists and tables

data=Table[{k,Sin[k]+0.2RandomReal[{-1,1}]},{k,-π,π,0.1}];

ListPlot[data]

ListLinePlot[data]

Algebra

Polynomials

Variables[1+x y+(y^3) z+3 x y (z^2)]
Exponent[1+x y+(y^3) z+3 x y (z^2),{x,y,z,u}]

Coefficient[4(x^4)+3(x^3)+2 (x^2)+x-1,x,3]
Coefficient[4(x^4)+3(x^3)+2 (x^2)+x-1,(x^2),2]
Coefficient[4(x^4)+3(x^3)+2 (x^2)+x-1,(x^(2/3)),3/2]

Clear[a,b,c]
CoefficientList[(x^3)+3x-6,x]
CoefficientList[a (x^2)+b x+c,x]
CoefficientList[(a-x)^3,x]

CoefficientRules[1+x y+(y^3) z+3 x y (z^2)+(x^5),{x,y,z}]

Clear[x,y]
Collect[(1+2y+x)^3,x]//TraditionalForm
Collect[(1+2y+x)^3,y]//TraditionalForm

HornerForm[(x^5)+7(x^4)-4(x^3)-(x^2)+3x+1,x]//TraditionalForm

Simplification

Simplify[(((x+y)(((Cos[x])^4)-2((Cos[x])^2)+1))/(((Tan[x])^3)Sin[x]((Cos[x])^3)((x^2)-(y^2))))]

FullSimplify

Expanding and factoring

Expand[(((a+b))^3)(x-y)]
Factor[4 (x^4)-12 (x^3)+5 (x^2)+12 x-9]

Factor[(x^2)-2]
Factor[(x^2)-2,Extension->{(Sqrt[2])}]

Factor[(x^2)-(y^2)]
Factor[(x^2)+(y^2)]
Factor[(x^2)+(y^2),Extension->{I}]

Numerator, denominator, cancellation

Numerator[(((x^3)-(y^6))/(x-(y^2)))]
Denominator[(((x^3)-(y^6))/(x-(y^2)))]
Cancel[(((x^3)-(y^6))/(x-(y^2)))]

Numerator[Tan[x]]
Numerator[Tan[x],Trig->True]

Together

Together[(((x^2)-x y)/(x+y))+((3y)/((x^2)-(y^2)))-3+x]

Decomposition

Apart[(4/(x((x^2)-1)))]
Apart[((2x+3)/((x^2)(x+2)((x^2)-1)((x^2)+2x+2)))]

Denominator does not have rational roots:

Apart[(1/((x^2)-2))]
Apart[(1/((x^2)-4/9)),x]

Apart@Factor[(1/((x^2)-2)),Extension->(Sqrt[2])]

Apart@Factor[(1/((x^2)+2)),Extension->I(Sqrt[2])]

Apart[(1/((x+a)(x+b))),x]

Trigonometry

TrigExpand@Sin[x+y]
TrigExpand@Cos[x+y]
TrigExpand@Tan[x+y]
TrigExpand@Sin[2x]
TrigExpand[Cos[5x]]

TrigFactor[((Cos[x])^2)-((Sin[x])^2)]
TrigFactor[((Cos[x])^3)-3 Cos[x] ((Sin[x])^2)]

TrigReduce[((Cos[x])^2)]
TrigReduce[((Cos[x])^2)-((Sin[x])^2)]
TrigReduce[((Cos[x])^3)-3 Cos[x] ((Sin[x])^2)]
TrigReduce[((Cos[x])^3)+((Cos[3x])^2)Sin[x]-Sin[2x]]

Logic

Logical values:

False
True

Logical functions

Negation (logical complement)

!False
Not[True]

AND, OR

True&&True
And[True,True,True,a,b]
False||False
Or[False, False, True, False]

NAND, NOR, XOR, XNOR

Nand[False, True]
Nor[False,False]
Xor[True,False]
Xnor[False, False]

Implication and equivalence

True\[Implies]False

Implies[False,True]
Equivalent[False,True]

Truth tables

In case of two variables (a,b): (0,0), (1,0), (0,1), (1,1).
In case of three variables (a,b,c): (0,0,0), (1,0,0), (0,1,0), (1,1,0), (0,0,1), (1,0,1), (0,1,1), (1, 1, 1).

BooleanTable[a||b,{a,b}]

BooleanTable[a&&b,{a,b}]

BooleanTable[Implies[a,b],{a,b}]

BooleanTable[Xor[a,b],{a,b}]

BooleanTable[And[a,Xor[b,c]],{a,b,c}]

Normal forms

Disjunctive Normal Form

BooleanConvert[Implies[a,b]]//TraditionalForm
BooleanConvert[Xnor[a,Nor[b,c]]]//TraditionalForm

Conjunctive Normal Form

BooleanConvert[Xnor[a,Nor[b,c]],"CNF"]//TraditionalForm

Using only AND

BooleanConvert[Xnor[a,Nor[b,c]],"AND"]//TraditionalForm

Using only NAND

BooleanConvert[Xnor[a,Nor[b,c]], "NAND"]//TraditionalForm

Using only implications

BooleanConvert[Xnor[a,Nor[b,c]], "Implies"]//TraditionalForm

Relations as logical functions

Operator form

2==3
2≠3
2<3
(5^8)≥(9^6)
3>2>1
1≤10≤5
!(3==3&&(5<2||6>2||5<0)&&False)

3>2>1
3>2&&2>1

Function form

Less[a,b]
GreaterEqual[a,b]
Equal[a,b]
Not[Equal[a,b]]

Test functions

EvenQ[11]
OddQ[11]

PrimeQ[1001]

IntegerQ[12.2]
NumberQ[x]

SameQ[a+(b^2),a+b^2]

Matrices

Matrices and vectors

v={1,2,3};
u={{1,2,3}};
w={{1},{2},{3}};
M={{3,4,-1},{7,9,0},{-3,0,1}};
MM={{3,4,-1},{7,9,0},{-3,0,1},{1,1,1},{3,-9,7}};
MatrixForm[v]
MatrixForm[u]
MatrixForm[w]
MatrixForm[M]
MatrixForm[MM]

MatrixForm[M].MatrixForm[v]

M.v
v.M
MM.v

M
v
M v

u.M
M.u
M.w
w.M

Matrix algebra

A={{1,2},{3,4}};
B={{1,1},{-4,5}};
MatrixForm@A
MatrixForm@B

A+B//MatrixForm
A.B//MatrixForm
A B//MatrixForm
A/B

A^3//MatrixForm
MatrixPower[A,3]//MatrixForm
A.A.A//MatrixForm

A.B//MatrixForm
B.A//MatrixForm

A//MatrixForm
Transpose[A]//MatrixForm

Inverse[A]//MatrixForm

Inverse[A].A//MatrixForm
A.Inverse[A]//MatrixForm

Det[A]
Tr[A]

Clear[a,b,c,d]
Det[{{a,b},{c,d}}]
Tr[{{a,b},{c,d}}]
Inverse[{{a,b},{c,d}}]//MatrixForm

Special matrices

IdentityMatrix[3]//MatrixForm
IdentityMatrix[{3,6}]//MatrixForm
IdentityMatrix[{6,3}]//MatrixForm

UnitVector[5,2]
UnitVector[5,5]

DiagonalMatrix[{a,b,c}]//MatrixForm
DiagonalMatrix[{a,b,c},2]//MatrixForm
DiagonalMatrix[{a,b,c},-2]//MatrixForm

RandomInteger[9,{6,3}]//MatrixForm

Geometric transformations

ScalingMatrix[{a,b}]//MatrixForm
RotationMatrix[α]//MatrixForm
ShearingMatrix[α,{1,0},{0,1}]