Introduction to Mathematica

Vito, March 2017

Class 4

Vectors and matrices

Rectangular matrices

rectangular=Table[10*i+j,{i,1,4},{j,1,2}]

TableForm[rectangular,TableHeadings->{None,{"column 1","column 2"}}]

Dimensions[rectangular]

Length[rectangular]

Length[rectangular[[1]] ]

Extracting one line is simple

rectangular[[3]]

Getting an element is a generalization of the indexing rule for single level lists

rectangular[[3,2]]

rectangular[[2,3]]

MessageList[-1]  (* MessageList[n] will ask for messages from instruction In[n]; the -1 means "the last instruction" *)

Getting a column seems weird at the beginning

rectangular[[All,1]]

MessageList[-1]

Last output is a void list. Even if it is void, it is an object, it exists, and it is possible to include it in subsequent commands

rectangular[[All,2]]

m={{11,12},{21,22}}

rectangular.m

m.rectangular

Transpose[rectangular]//MatrixForm

Back to the square matrices

Inverse[m]

m.Inverse[m]

N[m].Inverse[N[m] ]

Chop[%]

d={1,2}

p=LinearSolve[m,d]

m.p

m.p==d

Eigenvalues[m]

Eigenvectors[m]

r=Eigensystem[m]

r[[1]] (* just the eigenvalues *)

r[[2]] (* just the eigenvectors *)

One possibility for decomposing a return-values which is a multiple level list is giving a structured target for the assignment, example:

{λ,{v1,v2}}=Eigensystem[m]

λ

v1

m.v1//FullSimplify

λ v1//FullSimplify

Operations with lists: generating new vectors and new matrices from lists

Joining two lists is straightforward

a=Range[11,14]

b=Range[-0.7,0.5,.33]

Join[a,b] (* all elements in the same list *)

now, in two different lines, forming a matrix

Join[{a,b}]

It is possible to join two lists, eliminating repeated elements

i1=RandomInteger[{1,10},6]

i2=RandomInteger[{1,10},6]

Join[i1,i2]

Length[%]

Union[i1,i2] (* we sorted 12 numbers from 1 to 10 but likely a few numbers will not be sampled *)

Length[%]

Adding a column to a matrix is tricky

f=Range[31,34]

t=Join[rectangular,Transpose[{f}],2]  (* Last parameter tells Join[ ] to add the list at its 2nd level.
The added list MUST have two indices, 
then we added extra brackets to f, {f}.
Remember, Mathematica does not distinguish a row vector from a column vector - both are single level lists, which, at end, is natural and convenient.*)

t//MatrixForm

It is possible to eliminate dimensions

s=Flatten[t]

and group elements

Partition[s,4]

Partition[s,2]

Partition[s,5]  (* throws away the elements that don't fit in the matrix;there are options to avoid that *)

Exercício

5) A função densidade de probabilidade uniforme
Investigar a distribuição do erro de arredondamento na soma de um número grande de parcelas
i. Sorteie um conjunto de números aleatórios reais   u   com 12 elementos; sorteie valores maiores que 1.
ii. Crie outro conjunto com os valores de u arredondados de forma a apresentarem uma única casa decimal.
iii. Determine a diferença entre a soma de todos os elementos de u e a soma de todos os elementos do conjunto arredondado
iv. Reúna todos os itens anteriores em uma única sequencia de instruções, separadas por ; 
v. Faça uma tabela com um número grande dos erros de arredondamento
vi. Histograme a tabela e verifiique se o resultado combina com a previsão teórica. 
vii. Coloque a instrução Table dentro de Histogram e explore a dependência do erro com o número de parcelas da soma.