Matrices for computation

and application to a MATh.en.JEANS research workshop theme

April 4, 2025 - Yves Papegay - yap@informathiques.fr

This notebook is thefinal version of the support of the sixth session of the course “Online teacher training and community of practices - Digital tools for STEAM education and Makers pedagogy, November 2024 - June 2025” , an activity of the “MEJ MAKERS ERASMUS+ project (2023-2-FR01-KA210-SCH-000176068)” - visit the course dedicated web page for details.

Matrices

In computer science, a matrix is a two-dimensional data structure that organizes elements in rows and columns. It is commonly represented as a list of lists in several programming languages, namely in Wolfram Language.

Matrices are widely used in various applications, including scientific computing (linear operations), Image processing (image representation), computer graphics (geometric transformations), machine learning (neurons), ...

A matrix is typically indexed as M[row][col], where row and col define the position of an element. Operations like addition, multiplication, transposition, and inversion are fundamental in matrix manipulation.

Basic representation and operation

Representation

A={{(Subscript[a, 1,1]),(Subscript[a, 1,2])},{(Subscript[a, 2,1]),(Subscript[a, 2,2])}};
MatrixForm[A]

A

Transposition

F={{(Subscript[f, 1,1]),(Subscript[f, 1,2]),(Subscript[f, 1,3])},{(Subscript[f, 2,1]),(Subscript[f, 2,2]),(Subscript[f, 2,3])}};
MatrixForm[F]
MatrixForm[Transpose[F]]

Addition

B={{(Subscript[b, 1,1]),(Subscript[b, 1,2])},{(Subscript[b, 2,1]),(Subscript[b, 2,2])}};
MatrixForm[A+B]

Multiplication

MatrixForm[B].MatrixForm[F]==MatrixForm[B.F]

MatrixForm[Transpose[F]].MatrixForm[B]==MatrixForm[Transpose[F].B]

Defining a matrix

with iterators

Table[i+2j,{i,1,5},{j,1,3}]//MatrixForm

Array[f,{5,3}]//MatrixForm

Array[#1+2#2&,{5,3}]//MatrixForm

with dedicated functions

IdentityMatrix[4]//MatrixForm

DiagonalMatrix[{a,b,c,d}]//MatrixForm

DiagonalMatrix[{a,b,c},1]//MatrixForm

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

RotationMatrix[θ,{0,0,1}]//MatrixForm

sparse array

SparseArray[{{1,1}->1,{2,2}->2,{3,3}->3,{1,3}->4},4]//MatrixForm

randomly

(m=RandomInteger[{0,1},{10,20}])//MatrixForm

Visualization

m/.{1->Black,0->White}//MatrixForm

with ArrayPlot

ArrayPlot[m]

ArrayPlot[m,Mesh->All]

ArrayPlot[SparseArray[{{x_,x_}->1,({x_,y_}/;Abs[x-y]<3)->1/(Abs[x-y]+1)},{10,10}],Mesh->True]

ArrayPlot[Table[Sin[x y],{x,-40,40},{y,-40,40}]]

with MatrixPlot

MatrixPlot[m]

MatrixPlot[Table[Evaluate[Sum[Sin[RandomReal[10,2].{x,y}],{5}]],{x,0,5,.1},{y,0,5,.1}]]

Images

ArrayPlot[m]

Image[m]

CurrentImage[]

ImageData[%30]

img=;

ImageDimensions[img]

ImageData[img]

Convolutions and correlations

Dimension 1

ListConvolve[{x,y},{a,b,c,d,e}]

ListCorrelate[{x,y},{a,b,c,d,e}]

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

ListConvolve[{1/3,1/3,1/3},{a,b,c,d,e}]

data=Table[Sqrt[x]+RandomReal[],{x,30}];
ListLinePlot[{data,ListConvolve[{1,1,3,1,1}/7,data],ListCorrelate[{1,1,3,1,1}/7,data]}]

Dimension 2

BoxMatrix[2]/25//MatrixForm

ImageConvolve[,BoxMatrix[2]/25]

ImageConvolve[,{{1,1,1},{1,-8,1},{1,1,1}}]

ImageConvolve[,{{1,1,1},{1,-8,1},{1,1,1}}]

{{-1,0,1},{-2,0,2},{-1,0,1}}//MatrixForm

ImageConvolve[,{{-1,0,1},{-2,0,2},{-1,0,1}}]

ImageConvolve[,Transpose[{{-1,0,1},{-2,0,2},{-1,0,1}}]]

Exemple : jeu de la vie

Clear[jeuvie]
jeuvie[mat_]:=ListConvolve[{{1,1,1},{1,-8,1},{1,1,1}},mat,{{2,2},{2,2}},0]/.{-5->1,3->1,-6->1,_Integer->0}
jeuvie[mat_,n_]:=NestList[jeuvie,mat,n]

Puffer=Map[Plus[#,+{15,10}]&,{{1,4},{2,5},{3,1},{3,5},{4,2},{4,3},{4,4},{4,5},{8,1},{9,2},{9,3},{10,3},{11,3},{12,2},{15,1},{15,4},{16,5},{17,1},{17,5},{18,2},{18,3},{18,4},{18,5}}];

ListAnimate[Map[ArrayPlot[#,ImageSize->600]&,jeuvie[SparseArray[Puffer->1,{50,300}],400]]]

Cristal Evolution

From hexagonal lattice to matrix

m//MatrixForm

ArrayPlot[m]

Graphics[Table[{RandomColor[],RegularPolygon[i{(1/2)+1,-((Sqrt[3])/2)}+j{((-1)/2)-1,-((Sqrt[3])/2)},1,6]},{i,1,50},{j,1,50}]]

Graphics[{RegularPolygon[{0,0},1,6],Red,RegularPolygon[{1,0},1,6]}]

Graphics[{LightGray,RegularPolygon[{0,0},1,6],LightPink,RegularPolygon[{(1/2)+1,-((Sqrt[3])/2)},1,6]},Axes->True]

Graphics[{LightGray,RegularPolygon[{0,0},1,6],LightPink,RegularPolygon[{((-1)/2)-1,-((Sqrt[3])/2)},1,6]},Axes->True]

CirclePoints[6]

{EdgeForm[Thin],Table[{m[[i,j]]/.{1->Black,0->White},RegularPolygon[i{-3/2,-Sqrt[3]/2}+j{3/2,-Sqrt[3]/2},1,6]},{i,1,10},{j,1,20}]}//Graphics

HexaPlot[m_,c_]:=Graphics[{EdgeForm[Thin],Table[{m[[i,j]]/.{1->c,0->White},RegularPolygon[i{-3/2,-Sqrt[3]/2}+j{3/2,-Sqrt[3]/2},1,6]},{i,1,Length[m]},{j,1,Length[m[[1]]]}]}]

HexaPlot[m,Blue]

textmat=Normal[SparseArray[Map[Plus[{5,5}+#]&,{{1,1},{2,1},{2,2},{3,3},{3,4},{3,5},{3,6},{4,3},{4,4},{4,5},{4,6},{5,3},{5,4},{5,6}}]->1,{15,15}]];

HexaPlot[textmat,RGBColor[{0,255,200}]]

Evolution

kernel={{1,1,0},{1,7,1},{0,1,1}}

HexaPlot[kernel,Gray]

ListConvolve[kernel,textmat,{{2,2},{2,2}},0]/.{2->0,0->0,_Integer->1}//ArrayPlot

ArrayPlot[textmat]

evolue[mat_]:=ListConvolve[kernel,mat,{{2,2},{2,2}},0]/.{2->0,0->0,_Integer->1}

HexaPlot[evolue[textmat],RGBColor[{0,255,200}]]

HexaPlot[evolue[evolue[evolue[BoxMatrix[0,21]]]],RGBColor[{0,255,200}]]

ListAnimate[Map[HexaPlot[#,RGBColor[{0,255,200}]]&,NestList[evolue,BoxMatrix[0,41],20]]]