Linear algebra with pictures

Preliminaries

Import a picture

First set the working directory. 

This is the code to set the working directory on my office PC. It is commented out below since I used the code which I need in the classroom.

NotebookDirectory[]

SetDirectory[NotebookDirectory[]]

(*  
Off[General::"spell1"];
SetDirectory["C:\\Dropbox\\Work\\myweb\\Courses\\204_201440"]
*)

The code below is to when I am logged into the campus network in the classroom.

Off[General::"spell1"];
SetDirectory["U:\\myweb\\Courses\\204_201440"]

You need to figure set the working directory on your PC in which you have this Mathematica file and the picture file.

Now import a sample picture:

MaxPic=Import["Max_2.jpg"]

What is MaxPic?

MaxPic

Now show MaxPic:

Show[MaxPic]

To manipulate graphics we need to see what is inside MaxPic: (it is a lot of numbers and some Mathematica commands)

Understand what is the code inside the picture

The cell below contains a lot of information. Remove the semicolon to see the information.

FullForm[MaxPic]

This is a lot of stuff. Let us see how many basic components are there:

Length[MaxPic]

Now see what each individual component is:

MaxPic[[4]]

So, this is a Mathematica command.

MaxPic[[3]]

So, this defines the range for the picture.

MaxPic[[2]]

So, this defines the size of the picture. One can play with each one of these commands and see how it effects the outcome. However we will continue ...

The part below contains the color information about each pixel. Again if you want to see it remove ;

MaxPic[[1]]

Here, again, at the end we have some Mathematica commands and a lot of numbers preceding the commands.

Length[MaxPic[[1]]]

MaxPic[[1,4]]

MaxPic[[1,3]]

MaxPic[[1,2]]

The code below has only the color information for each pixel. Again, to see it remove  ;

MaxPic[[1,1]]

The above list is where the basic information is: a lot of triples, each triple is color of a pixel. In this list there are 160 rows each containing 160 triples. Thus, this picture has the resolution of 160x160.

Length[MaxPic[[1,1]]]

The above number tells that there are 160 rows in the list.

Length[MaxPic[[1,1,1]]]

The above number tells that in the first row there are 160 triples.

160 160

The conclusion

The core information is in the following list

MaxPicM=MaxPic[[1,1]]; (* Max's picture in Mathematica's language *)

This list consists of 160 lists of 160 triples of numbers between 0 and 255. Those triples define colors for each pixel. There are  160 x 16 = 25600 pixels.

For example, the pixel at the position (120,2) is colored by the following color:

MaxPicM[[120,2]]

It is even more convenient to convert the color information to numbers between 0 and 1

MaxPicM01=N[MaxPicM/255];

So, below is the color information for the pixel (120,102)

MaxPicM01[[120,102]]

MaxPic in Mathematica

Max in the coordinate system

Now I show how to use the above list (MaxPicM01) (representing Max's picture) in a coordinate system. The command below puts Max's picture in the unit square:

Show[Graphics[
Table[{PointSize[0.012],RGBColor[MaxPicM01[[k,j,1]],MaxPicM01[[k,j,2]],MaxPicM01[[k,j,3]]],Point[{(j/160),(k/160)}]},{j,1,160},{k,1,160}]
],
AspectRatio->Automatic,ImageSize->400,Frame->True,
PlotRange->{{0,1},{0,1}}];

A digression: Play with colors

This is just a digression, but I cannot resist not showing how to play with colors. For example I can make a black-and-white picture just by adding the numbers for the colors and dividing by 3*255 and using this the Mathematica command GrayLevel

Show[Graphics[
Table[{PointSize[0.01],GrayLevel[((0.3MaxPicM01[[k,j,1]]+0.6MaxPicM01[[k,j,2]]+0.1MaxPicM01[[k,j,3]]))],Point[{(j/160),(k/160)}]},{j,1,160},{k,1,160}]
],
AspectRatio->Automatic,ImageSize->400,Frame->True,
PlotRange->{{-0,1},{-0,1}}];

However, I can decide to do something funny and permute numbers for the colors, place blue at red's place, red at green's and green at blue's place to get the following strange result:

Show[Graphics[
Table[{PointSize[0.01],RGBColor[MaxPicM01[[k,j,3]],MaxPicM01[[k,j,1]],MaxPicM01[[k,j,2]]],Point[{(j/160),(k/160)}]},{j,1,160},{k,1,160}]
],
AspectRatio->Automatic,ImageSize->400,Frame->True,
PlotRange->{{0,1},{0,1}}];

If I square each color's number the picture will become darker. Can you explain this?

Show[Graphics[
Table[{PointSize[0.012],RGBColor[(((MaxPicM01[[k,j,1]]))^2),(((MaxPicM01[[k,j,2]]))^2),(((MaxPicM01[[k,j,3]]))^2)],Point[{(j/160),(k/160)}]},{j,1,160},{k,1,160}]
],
AspectRatio->Automatic,ImageSize->400,Frame->True,
PlotRange->{{0,1},{0,1}}];

If I take the square-root of each color's number the picture will become lighter. Can you explain this?

Show[Graphics[
Table[{PointSize[0.012],RGBColor[(((MaxPicM01[[k,j,1]]))^(1/2)),(((MaxPicM01[[k,j,2]]))^(1/2)),(((MaxPicM01[[k,j,3]]))^(1/2))],Point[{(j/160),(k/160)}]},{j,1,160},{k,1,160}]
],
AspectRatio->Automatic,ImageSize->400,Frame->True,
PlotRange->{{0,1},{0,1}}];

Show[Graphics[
Table[{PointSize[0.012],RGBColor[MaxPicM01[[k,j,1]],MaxPicM01[[k,j,2]],(MaxPicM01[[k,j,3]])],Point[{(j/160),(k/160)}]},{j,1,160},{k,1,160}]
],
AspectRatio->Automatic,ImageSize->400,Frame->True,
PlotRange->{{0,1},{0,1}}];

Back to the coordinate system

Show[Graphics[{
Table[{PointSize[0.01],RGBColor[MaxPicM01[[k,j,1]],MaxPicM01[[k,j,2]],MaxPicM01[[k,j,3]]],Point[{(j/160),(k/160)}]},{j,1,160},{k,1,160}],
{PointSize[0.04],Point[{0,0}]},{Text[0,{0,0},{1,1},TextStyle->{FontFamily->"Courier",FontSize->18,FontWeight->"Bold"}]}
}],
AspectRatio->Automatic,ImageSize->400,Frame->True,
PlotRange->{{-2,2},{-2,2}}];

Now play with matrices:

mM={{2,0},{0,2}}; MatrixForm[mM]

Show[Graphics[{
Table[{PointSize[0.01],RGBColor[MaxPicM01[[k,j,1]],MaxPicM01[[k,j,2]],MaxPicM01[[k,j,3]]],Point[mM.{(j/160),(k/160)}]},{j,1,160},{k,1,160}],
{PointSize[0.04],Point[{0,0}]},{Text[0,{0,0},{1,1},TextStyle->{FontFamily->"Courier",FontSize->18,FontWeight->"Bold"}]}
}],
AspectRatio->Automatic,ImageSize->400,Frame->True,
PlotRange->{{-2,2},{-2,2}}];

mM={{1,1},{0,1}}; MatrixForm[mM]

Show[Graphics[{
Table[{PointSize[0.01],RGBColor[MaxPicM01[[k,j,1]],MaxPicM01[[k,j,2]],MaxPicM01[[k,j,3]]],Point[mM.{(j/160),(k/160)}]},{j,1,160},{k,1,160}],
{PointSize[0.04],Point[{0,0}]},{Text[0,{0,0},{1,1},TextStyle->{FontFamily->"Courier",FontSize->18,FontWeight->"Bold"}]}
}],
AspectRatio->Automatic,ImageSize->400,Frame->True,
PlotRange->{{-2,2},{-2,2}}];

mM={{1,0},{-1,1}}; MatrixForm[mM]

Show[Graphics[{
Table[{PointSize[0.01],RGBColor[MaxPicM01[[k,j,1]],MaxPicM01[[k,j,2]],MaxPicM01[[k,j,3]]],Point[mM.{(j/160),(k/160)}]},{j,1,160},{k,1,160}],
{PointSize[0.04],Point[{0,0}]},{Text[0,{0,0},{1,1},TextStyle->{FontFamily->"Courier",FontSize->18,FontWeight->"Bold"}]}
}],
AspectRatio->Automatic,ImageSize->400,Frame->True,
PlotRange->{{-2,2},{-2,2}}];

mM={{0,1},{1,0}}; MatrixForm[mM]

Show[Graphics[{
Table[{PointSize[0.01],RGBColor[MaxPicM01[[k,j,1]],MaxPicM01[[k,j,2]],MaxPicM01[[k,j,3]]],Point[mM.{(j/160),(k/160)}]},{j,1,160},{k,1,160}],
{PointSize[0.04],Point[{0,0}]},{Text[0,{0,0},{1,1},TextStyle->{FontFamily->"Courier",FontSize->18,FontWeight->"Bold"}]}
}],
AspectRatio->Automatic,ImageSize->400,Frame->True,
PlotRange->{{-2,2},{-2,2}}];

mM={{0,-1},{1,0}}; MatrixForm[mM]

Show[Graphics[{
Table[{PointSize[0.01],RGBColor[MaxPicM01[[k,j,1]],MaxPicM01[[k,j,2]],MaxPicM01[[k,j,3]]],Point[mM.{(j/160),(k/160)}]},{j,1,160},{k,1,160}],
{PointSize[0.04],Point[{0,0}]},{Text[0,{0,0},{1,1},TextStyle->{FontFamily->"Courier",FontSize->18,FontWeight->"Bold"}]}
}],
AspectRatio->Automatic,ImageSize->400,Frame->True,
PlotRange->{{-2,2},{-2,2}}];

mM={{1,2},{-1,1}};MatrixForm[mM]

Show[Graphics[{
Table[{PointSize[0.01],RGBColor[MaxPicM01[[k,j,1]],MaxPicM01[[k,j,2]],MaxPicM01[[k,j,3]]],Point[mM.{(j/160),(k/160)}]},{j,1,160},{k,1,160}],
{PointSize[0.04],Point[{0,0}]},{Text[0,{0,0},{1,1},TextStyle->{FontFamily->"Courier",FontSize->18,FontWeight->"Bold"}]}
}],
AspectRatio->Automatic,ImageSize->400,Frame->True,
PlotRange->{{-2,2},{-2,2}}];

mM={{2.8,-0.3},{-0.5,1.8}}; MatrixForm[mM]

Show[Graphics[{
Table[{PointSize[0.01],RGBColor[MaxPicM01[[k,j,1]],MaxPicM01[[k,j,2]],MaxPicM01[[k,j,3]]],Point[mM.{(j/160),(k/160)}]},{j,1,160},{k,1,160}],
{PointSize[0.04],Point[{0,0}]},{Text[0,{0,0},{1,1},TextStyle->{FontFamily->"Courier",FontSize->18,FontWeight->"Bold"}]}
}],
AspectRatio->Automatic,ImageSize->400,Frame->True,
PlotRange->{{-2,2},{-2,2}}];

mM={{Cos[1],-Sin[1]},{Sin[1],Cos[1]}}; MatrixForm[mM]

Show[Graphics[{
Table[{PointSize[0.01],RGBColor[MaxPicM01[[k,j,1]],MaxPicM01[[k,j,2]],MaxPicM01[[k,j,3]]],Point[mM.{(j/160),(k/160)}]},{j,1,160},{k,1,160}],
{PointSize[0.04],Point[{0,0}]},{Text[0,{0,0},{1,1},TextStyle->{FontFamily->"Courier",FontSize->18,FontWeight->"Bold"}]}
}],
AspectRatio->Automatic,ImageSize->400,Frame->True,
PlotRange->{{-1.6,1.6},{-1.6,1.6}}];

Table[Show[Graphics[{
Table[{PointSize[0.01],RGBColor[MaxPicM01[[k,j,1]],MaxPicM01[[k,j,2]],MaxPicM01[[k,j,3]]],Point[{{Cos[t],-Sin[t]},{Sin[t],Cos[t]}}.{(j/160),(k/160)}]},{j,1,160},{k,1,160}],
{PointSize[0.04],Point[{0,0}]},{Text[0,{0,0},{1,1},TextStyle->{FontFamily->"Courier",FontSize->18,FontWeight->"Bold"}]}
}],
AspectRatio->Automatic,ImageSize->400,Frame->True,
PlotRange->{{-1.6,1.6},{-1.6,1.6}}],{t,0,2 Pi,(Pi/16)}];SelectionMove[EvaluationNotebook[],All,GeneratedCell];
FrontEndTokenExecute["OpenCloseGroup"]

Now some three dimensional stuff

mM={{1,0},{0,0},{0,1}};MatrixForm[mM]

Show[
Graphics3D[{
{Table[{PointSize[0.0075],Apply[RGBColor,MaxPicM01[[k,j]]],Point[mM.{(j/160),(k/160)}]},{j,1,160},{k,1,160}]},
{PointSize[0.04],Point[{0,0,0}]},{Text[0,{0,0,0},{1,1},TextStyle->{FontFamily->"Courier",FontSize->18,FontWeight->"Bold"}]}
}],
PlotRange->{{-1,2},{-1,2},{-1,2}},
AspectRatio->Automatic,
Boxed->True,Axes->True,AxesLabel->{x1,x2,x3},DisplayFunction->$DisplayFunction
];

mM={{0,0},{1,0},{0,1}};MatrixForm[mM]

Show[
Graphics3D[{
{Table[{PointSize[0.0075],Apply[RGBColor,MaxPicM01[[k,j]]],Point[mM.{(j/160),(k/160)}]},{j,1,160},{k,1,160}]},
{PointSize[0.04],Point[{0,0,0}]},{Text[0,{0,0,0},{1,1},TextStyle->{FontFamily->"Courier",FontSize->18,FontWeight->"Bold"}]}
}],
PlotRange->{{-1,2},{-1,2},{-1,2}},
AspectRatio->Automatic,
Boxed->True,Axes->True,AxesLabel->{x1,x2,x3},DisplayFunction->$DisplayFunction
];

A simpler example

mM={{1,0},{1,0},{0,1}};MatrixForm[mM]

xmin=-0.5;xmax=1.5;xst=.1;
ymin=-0.5;ymax=1.5;yst=.1;

pts=Flatten[Table[{x,y},{x,xmin,xmax,xst},{y,ymin,ymax,yst}],1];

plo1=Show[
Graphics[{
{({PointSize[0.04],RGBColor[((#[[1]]-xmin)/(xmax-xmin)),((#[[2]]-xmin)/(xmax-xmin)),0],Point[#]})&/@pts},
{PointSize[0.04],Point[{0,0}]},
{Text[0,{0,0},{1,1},TextStyle->{FontFamily->"Courier",FontSize->18,FontWeight->"Bold"}]}}],
PlotRange->{{-2,2},{-2,2}},
AspectRatio->Automatic,Frame->True,DisplayFunction->Identity
];

plo2=Show[
Graphics3D[{
{({PointSize[0.04],RGBColor[((#[[1]]-xmin)/(xmax-xmin)),((#[[2]]-xmin)/(xmax-xmin)),0],Point[mM.#]})&/@pts},
{PointSize[0.04],Point[{0,0,0}]},{Text[0,{0,0,0},{1,1},TextStyle->{FontFamily->"Courier",FontSize->18,FontWeight->"Bold"}]}
}],
PlotRange->{{-2,2},{-2,2},{-2,2}},
AspectRatio->Automatic,
Boxed->True,Axes->True,AxesLabel->{x1,x2,x3},DisplayFunction->Identity
];

Show[GraphicsArray[{plo1,plo2},ImageSize->500,GraphicsSpacing->.15]];

mM={{1,0},{1,1},{1,1}};MatrixForm[mM]

xmin=-0.5;xmax=1.5;xst=.1;
ymin=-0.5;ymax=1.5;yst=.1;

pts=Flatten[Table[{x,y},{x,xmin,xmax,xst},{y,ymin,ymax,yst}],1];

plo1=Show[
Graphics[{
{({PointSize[0.04],RGBColor[((#[[1]]-xmin)/(xmax-xmin)),((#[[2]]-xmin)/(xmax-xmin)),0],Point[#]})&/@pts},
{PointSize[0.04],Point[{0,0}]},
{Text[0,{0,0},{1,1},TextStyle->{FontFamily->"Courier",FontSize->18,FontWeight->"Bold"}]}}],
PlotRange->{{-2,2},{-2,2}},
AspectRatio->Automatic,Frame->True,DisplayFunction->Identity
];

plo2=Show[
Graphics3D[{
{({PointSize[0.04],RGBColor[((#[[1]]-xmin)/(xmax-xmin)),((#[[2]]-xmin)/(xmax-xmin)),0],Point[mM.#]})&/@pts},
{PointSize[0.04],Point[{0,0,0}]},{Text[0,{0,0,0},{1,1},TextStyle->{FontFamily->"Courier",FontSize->18,FontWeight->"Bold"}]}
}],
PlotRange->{{-2,2},{-2,2},{-2,2}},
AspectRatio->Automatic,
Boxed->True,Axes->True,AxesLabel->{x1,x2,x3},DisplayFunction->Identity
];

Show[GraphicsArray[{plo1,plo2},ImageSize->500,GraphicsSpacing->.15]];

Not Linear Algebra, but Multivariable Calculus

Show[
Graphics3D[{
{Table[{PointSize[0.01],Apply[RGBColor,MaxPicM01[[k,j]]],Point[{Cos[Pi(j/160.)-((2Pi)/2)],Sin[Pi(j/160.)-((2 Pi)/2)],Pi(k/160.)}]},{j,1,160},{k,1,160}]}
}],
PlotRange->{{-1,1},{-1,1},{-.1,Pi +.1}},
AspectRatio->Automatic,ImageSize->400,
Boxed->True,Axes->True,DisplayFunction->$DisplayFunction
];

Show[
Graphics3D[{
{Table[{PointSize[0.01],Apply[RGBColor,MaxPicM01[[k,j]]],Point[{Sin[ArcCos[-1+2(k/160.)]]Cos[Pi(j/160.)-((2Pi)/2)],Sin[ArcCos[-1+2(k/160.)]]Sin[Pi(j/160.)-((2 Pi)/2)],-1+2(k/160.)}]},{j,1,160,1},{k,1,160,1}]}
}],
PlotRange->{{-1.1,1.1},{-1,1.1},{-1.1,1.1}},
AspectRatio->Automatic,ImageSize->400,
Boxed->True,Axes->True,DisplayFunction->$DisplayFunction,ViewPoint->{1.211, -2.745, 1.165}
];