Need help with Problems 1-7. Python Coding. The second attached file gives the format for what the answer should be in.
LAB 8 - LINEAR TRANSFORMATIONS 1. Introduction to Linear Transformations Vectors provide a very useful way to represent images in computer graphics and animation. These images are aptly referred to as vector graphics, and use 2-dimensional arrangements of points which are connected by lines and curves to form polygons and other shapes. Vector graphics are found in PDF and ESP file formats (as well as others), and have many advantages over bitmap formats such as JPG and PNG which store images directly as a series of pixel val- ues. For example, unlike JPG images, vector graphics can resized arbitrarily large without the images becoming pixelated. Furthermore, because images are represented as a series of vectors, they can be resized, rotated, reflected, and modified in other ways using techniques from linear algebra, such as linear transformations. In this lab we will explore linear transformations from a geometric viewpoint, and see simple applications to graphics. A transformation is a function, or rule, which assigns a vector in Rm to each vector in Rn. For instance, a transformation L which maps vectors in R2 to vectors in R3 could be defined by L ([ x y ]) = x+ y2x2 − 5y y + 1 . Thus, for instance, L ([ 1 2 ]) = 5−9 3 . We use the notation L : Rn → Rm to denote a transformations that accepts vectors in Rn as input, and returns vectors in Rm as output. We call the set Rn of all possible input vectors the domain of L, and the set Rm where all of the output vectors lie the codomain of L. A linear transformation is a special type of transformation which respects vector addition and scalar multiplication. More precisely, we say that a transformation T : Rn → Rm is a Figure 1. The effect of magnifying vector graphics compared with magnifying a bitmap image. Image courtesy of Wikipedia. 1 2 LAB 8 LINEAR TRANSFORMATIONS linear transformation from Rn into Rm if T (av + bw) = aT (v) + bT (w) for all vectors v,w ∈ Rn and scalars a, b ∈ R. Every linear transformation T from Rn into Rm can be represented by an m× n matrix A, called the standard matrix of T , or equivalently the matrix representation of T . For example, the linear transformation T : R2 → R3 given by (1) T (v) = 2x+ yx− 3y y has the matrix representation A = 2 11 −3 0 1 because for all vectors x in R2 we have T (x) = Ax (check this yourself for several different vectors in R2). Applying the linear transformation T to a vector x is therefore the same as multiplying x on the left by the matrix representation of T . This results in a new vector x′, where each component of x′ is some linear combination of the components of x. For linear transformations from R2 to R2, this process takes the form Ax = [ a b c d ] [ x y ] = [ ax+ by cx+ dy ] = [ x′ y′ ] = x′. Problem 1. Define a function transform(x) which takes as input a NumPy vector x in R2, performs the linear transformation T from (1) above to x, and returns the resulting vector as a NumPy vector. Check that your function works by using it to compute transform([1,2]) which should return array([ 4, -5, 2]). 2. Linear transformations in R2 Linear transformations can be interpreted geometrically. It is easy to observe the effect of a linear transformation T on vectors in the plane by plugging an image into T and observing how the resulting image differs from the original. To demonstrate this we will use a data set contained in the file cougar.csv. The file cougar.csv contains a list of points of the form {(x1, y1), (x2, y2), . . . , (xn, yn)}. When each of these points are plotted we see the image of a cougar and the BYU ‘Y’ (see the top left image in Figure 2). This is a very crude example of a vector graphics format like the ones mentioned above. Instead of arranging the data in cougar.csv as a list of points, we could instead organize it as a single 2 × n matrix H, where each column of H contains the x- and y-coordinates of a single data point. More precisely, the jth column of H will contain the x-coordinate xj of LAB 8 LINEAR TRANSFORMATIONS 3 the jth point in the first row, with the corresponding y-coordinate yj in the second row. This gives us a matrix of the form H = [ x1 x2 . . . xn y1 y2 . . . yn ] = x1 x2 . . . xn . Matrix multiplication on the left of H transforms each point in our image (i.e. each column of H), resulting in another matrix H ′ whose columns are the transformed coordinate pairs: AH = A [ x1 x2 · · · xn y1 y2 · · · yn ] = A x1 x2 · · · xn = Ax1 Ax2 · · · Axn = x′1 x′2 · · · x′n = [ x′1 x′2 · · · x′n y′1 y ′ 2 · · · y′n ] = H ′. Thus multiplying the image matrix H by a 2 × 2 matrix A on the left will result in a new image matrix H ′, which will correspond to a transformed image. • Import data from cougar.csv into a python array called cougar. (See the instructions on how to do this in the notebook for this lab.) If you get an error message try importing again (the file doesn’t seem to completely upload on the first try sometimes). • Plot the image using the function showplot(H) which has already been defined in your notebook. Compare the result of calling showplot(cougar) with the image in Figure 2. Types of Linear Transformations. Linear transformations from R2 into R2 fall into several different families: • Stretch: Transformations which stretch or compress the vector along the standard coordinate axes. The matrix representation of such linear transformations are diagonal:[ a 0 0 b ] . If a = b, the transformation is called a dilation. The stretch in Figure 2 uses a = 12 to compress the plane along the x-axis and b = 65 to stretch it along the y-axis. • Shear: Transformations which slant vectors in the plane by a scalar factor horizon- tally or vertically (or both simultaneously). The matrix representations of such linear transformations are of the form: [ 1 a b 1 ] . Pure horizontal shears (i.e. b = 0) only skew the x-coordinate of the vector, while pure vertical shears (i.e. a = 0) only skew the y-coordinate. Figure 2 has a horizontal shear with a = 12 , b = 0. • Reflection: Transformations which reflect vectors about a line that passes through the origin. The reflection around the line spanned by the vector [a, b]T has the matrix 4 LAB 8 LINEAR TRANSFORMATIONS Original Stretch Shear Reflection Rotation Composition Figure 2. Images of the data in the array cougar under various linear transformations. representation 1 a2 + b2 [ a2 − b2 2ab 2ab b2 − a2 ] . The reflection in Figure 2 reflects the image about the line spanned by [ 1 2 , 1 ]T (i.e. a = 12 , b = 1). • Rotation: Transformations which rotate vectors around the origin. A counterclock- wise rotation of θ radians has the following matrix representation: [ cos θ − sin θ sin θ cos θ ] A negative value of θ performs a clockwise rotation. Choosing θ = 3π2 produces the rotation in Figure 2. LAB 8 LINEAR TRANSFORMATIONS 5 Problem 2. Define the following four functions: stretch(image,a,b), shear(image,a,b), reflect(image,a,b), and rotate(image,theta) which correspond to the families of lin- ear transformations defined above. Each function should accept as input a 2 × n array image as above, as well as the parameters needed to define the transformation (a and b for stretch, shear, and reflection, and theta for rotation). The functions should then return the array which is obtained from image by applying the corresponding linear transformation. Test your functions by applying the transformations defined above, plotting them using the function showplot, and comparing your results to Figure 2. Compositions of Linear Transformations. If L : Rp → Rn and K : Rn → Rm are linear transformations with standard matrices A and B respectively, then the composition function K ◦ L : Rp → Rm is also a linear transformation, defined by K ◦ L(x) = K(L(x)) for all x ∈ Rp. The standard matrix of K ◦ L is the matrix product BA. For example, if S is a matrix representing a shear and R is a matrix representing a rotation, then RS represents a shear followed by a rotation. While not every linear transformation from R2 to R2 is one of the four types described above, any linear transformation L : R2 → R2 can be written as a composition of these four types of transformations. In fact, once we learn about the singular value decomposition we will see that every linear transformation from R2 to R2 can be written as a rotation, followed by a stretch, followed by another rotation. The bottom right image in Figure 2 displays the composition of all four previous transformations, applied in order (stretch, shear, reflection, then rotation). Problem 3. Use the functions you defined in the Problem 2 to construct a single matrix which performs the following sequence of transformations (in order) when acting on an image. (1) Stretch the image vertically by a factor of 2. (2) Rotate the image π4 radians clockwise. (3) Reflect the image over the line 2y = −3x. (4) Stretch the image horizontally by a factor of 1/2. save the resulting 2× 2 matrix as comp_matrix. Hint: Instead of rewriting out the matrix representations of each type of function again, notice (for example) that the standard matrix of a shear with a = 3 and b = 0.2 can be obtained by calling shear(iden,3,0.2), where iden is the 2 × 2 identity matrix. The same is true for the functions stretch, reflect, and rotate for any values of the parameters a, b, and θ. 6 LAB 8 LINEAR TRANSFORMATIONS S x y x y T T (S) 4 units 8 units 9 u n it s 9 u n it s Figure 3. The effect of applying the linear transformation T defined by the matrix (2) to a triangle in the plane. The triangle is stretched by