Clic on the little triangle   next to each topic to open that topic.

Variables

Variables do not need to be declared.

x=3
y=3.0+I

The type can be obtained with Head[]. Unused variable names are Symbol, which can be just about anything!

Head[y]
Head[q]

Functions are Heads-types

Head[f[x]]

Head[function[x]]

Unspecified functions can be differentiated, as a symbol.

D[f[z],z]

If the variable does not appear in the function, Mathematica is smart enough to give zero for its derivative

D[f[q],z]

Variables can be deleted with Unset[]

Unset[x]

Lists

A list of values

range=Table[s,{s,0,1,0.1}]

range=Table[1/s^2,{s,0,1,0.1}]

Head[range]

the generated values can also be used to generate derivate lists, such as the list of the exponential (or any other function of the listed values).

Table[Exp[s],{s,{-1,1,I,3,4999}}]

List of lists are made at fixed innermost index (i) running the outermost index (k)

Table[
{i,k}, {i, {i1,i2}},{k, {k1,k2}}
]
%//MatrixForm

Matrices are “Lists of List”. Any tensor can be built nesting lists inside lists.

lista={{1,a},{2,v},{3,c},{4,e}}

Head[lista]

lista

For convenience it is possible to show the lists in human-readable “Form”

MatrixForm[lista]

Lists can be transposed

MatrixForm[Transpose[lista]]

Lists can be Hermitian conjugated

MatrixForm[ConjugateTranspose[lista]]

The expression containing symbols can be simplified using assumptions

Assuming[{a∈Reals},Simplify[Conjugate[a]]]

Assuming[{a∈Reals},MatrixForm[Simplify[ConjugateTranspose[lista]]]]

The same effect can be achieved without nesting functions into functions, which gives nested square brackets. The same effect can be obtained “piping” the output of one function as the input of the next one. The pipe is connected using  //  symbol.

MatrixForm[Transpose[lista]]

lista//Transpose//MatrixForm

Map[Sin,{0,1,Pi}]//N
Sin/@{0,1,Pi}

N[Sin[1]]^2+N[Cos[1]]^2//Simplify

Values not numericized still respect symbolic identities, so the output is 1 as integer, not 1. as floating point number

Sin[1]^2+Cos[1]^2//Simplify

Elements of lists are obtained with the double square bracket notation  list[[k]]  to get the k-th element of list (the first element is 1, the last is -1)

lista[[2]]

Whole columns can be obtained with   All  . Double bracket can be beautified with list[[k]]. The thick bracket is typed Esc [ [ Esc

lista//MatrixForm

lista[[All,1]]

Pauli matrices as a list (easy to make loops on a list!)

τi={{{0,1},{1,0}},{{0,-I},{I,0}},{{1,0},{0,-1}}}

τi[[1]]//MatrixForm

τi[[1]].τi[[2]]-τi[[2]].τi[[1]]
%//MatrixForm

Pauli matrices as a function (less easy to make loops on a function! but easier notation [] instead of [[]] )

τ[1]={{0,1},{1,0}}
τ[2]={{0,-I},{I,0}}
τ[3]={{1,0},{0,-1}}

Manipulations

Symbolic substitution in an expression can be obtained with the function Replace, or the more thorough ReplaceAll.

ReplaceAll[z+x^2+z^4,{z->Sin[alpha],x^2->t}]

The same effect can be obtained with the /.  and //. operator.

pol=z+z^2+z^4/.z->Sin[alpha]

z^7+z^3
%//.{z->t,t^3->k}

Map allows to use a function, typically a lambda function, to elements of the list.

lista[[All,1]]
Map[(#^2)&,%]

Definition of Functions

arguments of the functions are given in square brackets and must be followed by a _

Prodotto[a_,b_,c_]:=a*b*c

Symbols respect properties of the operations. E.g. the ordinary products is commutative

Prodotto[a,b,c]==Prodotto[a,c,b]

A non commutative product (represented by the double star ** ) would not give an identity instead.

ProdottoNonCommutativo[a_,b_]:=a**b

ProdottoNonCommutativo[a,b]
ProdottoNonCommutativo[b,a]
ProdottoNonCommutativo[b,a]==ProdottoNonCommutativo[b,a]
ProdottoNonCommutativo[b,a]==ProdottoNonCommutativo[a,b]

If uncertain about what a function does we can call an inline help

?Prodotto

Functions can be deleted with Clear[]

Clear[Prodotto]

More complex functions may require the definition of intermediate variables, using local variables. They are declared with Module[]

?Module

FunzioneComplicata[a_,b_]:=Module[{somma,differenza},
somma=a+b;
differenza=a-b;
Print["The sum is ",somma];
Print["The difference is ",differenza];
(differenza/somma)*2
]

FunzioneComplicata[0.95,1.05]

reldiff=FunzioneComplicata[0.95,1.05]

reldiff

For complicated functions we can write our own (simple) help message with f::usage=”text”

FunzioneComplicata::usage="Computes the relative deviation between two numbers a,b as (a-b)/(((a + b)/2))"

?FunzioneComplicata

Arguments of the function can be optional.

Optional arguments have the advantage that they are not positional.

Options[OptionalFunction]={x->1,y->2}
OptionalFunction[opt:OptionsPattern[]]:=OptionValue[x]-OptionValue[y]

OptionalFunction[y->10,x->2]

OptionalFunction[x->2,y->10]

It does not matter in which order I give x and y when they are options.

Of course it does matter for a function with positional arguments.

NormalFunction[x_,y_]:=x-y

NormalFunction[2,10]

NormalFunction[10,2]