In this notebook you will explore linear transformations, visualize their results and master matrix multiplication to apply various linear transformations.
Packages
Run the following cell to load the package you'll need.
A transformation is a function from one vector space to another that respects the underlying (linear) structure of each vector space. Referring to a specific transformation, you can use a symbol, such as T. Specifying the spaces containing the input and output vectors, e.g. R2 and R3, you can write T:R2→R3. Transforming vector v∈R2 into the vector w∈R3 by the transformation T, you can use the notation T(v)=w and read it as "T of v equals to w" or "vector w is an image of vector v with the transformation T".
The following Python function corresponds to the transformation T:R2→R3 with the following symbolic formula:
Can you see now what should be the values of the elements ai,j of matrix A to make the equalities (5) correct? Find out the answer in the following code cell:
Every linear transformation can be carried out by matrix multiplication. And vice versa, carrying out matrix multiplication, it is natural to consider the linear transformation that it represents. It means you can associate the matrix with the linear transformation in some way. This is a key connection between linear transformations and matrix algebra.
As discussed above in section 3, a linear transformation L:R2→R2 can be represented as a multiplication of a 2×2 matrix and a coordinate vector v∈R2. Note that so far you have been using some random vector v∈R2. (e.g. v=[35]). To have a better intuition of what the transformation is really doing in the R2 space, it is wise to choose vector v in a less random way.
A good choice would be vectors of a standard basis e1=[10] and e2=[01]. Let's apply linear transformation L to each of the vectors e1 and e2: L(e1)=Ae1 and L(e2)=Ae2. If you put vectors {e1,e2} into columns of a matrix and perform matrix multiplication
A[e1e2]=[Ae1Ae2]=[L(e1)L(e2)],(3)
you can note that [e1e2]=[1001] (identity matrix). Thus, A[e1e2]=AI=A, and
A=[L(e1)L(e2)].(4)
This is a matrix with the columns that are the images of the vectors of the standard basis.
This choice of vectors {e1,e2} provides opportinuty for the visual representation of the linear transformation L (you will see the examples below).
Horizontal scaling (factor 2 in this example) can be defined considering transformation of a vector e1=[10] into a vector [20] and leaving vector e2=[01] without any changes. The following function T_hscaling() corresponds to the horizontal scaling (factor 2) of a vector. The second function transform_vectors() applies defined transformation to a set of vectors (here two vectors).
You can get a visual understanding of the transformation, producing a plot which displays input vectors, and their transformations. Do not worry if the code in the following cell will not be clear - at this stage this is not important code to understand.
You can observe that the polygon has been stretched in the horizontal direction as a result of the transformation.
There are many more standard linear transformations to explore. But now you have the required tools to apply them and visualize the results.
A large number of basic geometric shapes is used in computer graphics. Such shapes (e.g. triangles, quadrilaterals) are defined by their vertexes (corners). Linear transformations are often used to generate complex shapes from the basic ones, through scaling, reflection, rotation, shearing etc. It provides opportunity to manipulate those shapes efficiently.
The software responsible for rendering of graphics, has to process the coordinates of millions of vertexes. The use of matrix multiplication to manipulate coordinates helps to merge multiple transformations together, just applying matrix multiplication one by one in a sequence. And another advantage is that the dedicated hardware, such as Graphics Processing Units (GPUs), is designed specifically to handle these calculations in large numbers with high speed.
So, matrix multiplication and linear transformations give you a super power, especially on scale!
Here is an example where linear transformations could have helped to reduce the amount of work preparing the image:
All of the subleafs are similar and can be prepared as just linear transformations of one original leaf.
Let's see a simple example of two transformations applied to a leaf image. For the image transformations you can use an OpenCV library. First, upload and show the image:
Of course, this is just a very simple leaf image (not a real example in preparation of the proper art work), but it will help you to get the idea how a few transformations can be applied in a row. Try to rotate the image 90 degrees clockwise and then apply a shear transformation, which can be visualized as:
Rotate the image:
Applying the shear you will get the following output:
What if you will apply those two transformations in the opposite order? Do you think the result will be the same? Run the following code to check that:
Comparing last two images, you can clearly see that the outputs are different. This is because linear transformation can be defined as a matrix multiplication. Then, applying two transformations in a row, e.g. with matrices A and B, you perform multiplications B(Av)=(BA)v, where v is a vector. And remember, that generally you cannot change the order in the matrix multiplication (most of the time BA=AB). Let's check that! Define two matrices, corresponding to the rotation and shear transformations:
Now check that the results of their multiplications M_rotation_90_clockwise @ M_shear_x and M_shear_x @ M_rotation_90_clockwise are different:
This simple example shows that you need to be aware of the mathematical objects and their properties in the applications.