Lab 07: Matrices
7.1.
In the first part of this lab, we will work with matrices of integers:
type Matrix = List[List[Int]]
You can use higher-order functions of for expressions at your leisure.
7.1.1. Write a function which computes the sum of all elements from a matrix.
7.1.2. Write a function which computes the scalar multiplication of a matrix:
def scalarMult(const: Int, m: Matrix): Matrix = ???
7.1.3. Write a function which adds two matrices. Use the function zip
from the Scala standard library. Use the following test to figure out what zip
does: List(1,2,3).zip(List(4,5,6))
.
def add(m1: Matrix, m2: Matrix): Matrix = ???
7.1.4. Write a function which returns the first column of a matrix, as a single line:
def singleLine(m: Matrix): List[Int] = ???
7.1.5. Write a function which returns the rest of a matrix, without its first column:
def remCol(m: Matrix): Matrix = ???
7.1.6. Write a function which performs matrix transposition. Use wikipedia if necessary to reacquaint yourself with transposition.
def transpose(m: Matrix): Matrix = ???
7.1.7. (!) Implement multiplication of two matrices. Think about the sequence of steps you want to perform. Transposition will be your first step.
def mult(m1: Matrix, m2: Matrix): Matrix = ???
7.2. Matrices as images
The format called BPM (more details are available here) encodes images as matrices of Int
, where each integer value designates the intensity of each pixel (from 0 to 255). In this lab, we will consider grayscale images only. Some examples are shown below:
0 0 1 0 0 0 1 0 1 0 0 1 1 1 0 letter A 1 0 0 0 1 1 0 0 0 1 5 4 3 2 1 5 4 3 2 1 5 4 3 2 1 shader 5 4 3 2 1 5 4 3 2 1
Add the following type definition for the rest of your lab:
type Img = List[List[Int]]
Also, in order to benefit from visualisation, instead of using a worksheet, you can create a new Scala project, containing an object with a main method:
object Matrix extends App{ // define your functions here // write your test code here }
7.2.1. Write a function which converts an image to a string (Hint: you can draw inspiration from a similar one from the lecture):
def show(m: Img): String = ???
7.2.2. Write a function which performs a horizontal flip on an image. Try to first visualise (you may use pen and paper) how the transformation would look like.
def hFlip(img: Img): Img = ???
7.2.3. Write a function which performs vertical flip.
def vFlip(img: Img): Img = ???
7.2.4. Write a function which performs a 90 degrees rotation to the right. (Hint: you need an ingredient from the previous section. Also, note that there are multiple possible implementations.)
def rot90Right(img: Img): Img = ???
7.2.5. Write a function which performs a 90 degrees rotation to the left.
def rot90Left(img: Img): Img = ???
7.2.6. Write a function which inverts an image (values 0 become 255, 1 - 254, and so forth).
7.2.7. Write a function which crops a given image, using two, two-dimensional coordinates: the higher-left point x and y, and the lower-right point x and y. An example is shown below:
val img = List(List(0,0,1,0,0), List(0,1,0,1,0), List(0,1,1,1,0), List(1,0,0,0,1), List(1,0,0,0,1)) /* 0 0 1 0 0 * 0 1 0 1 0 1 0 1 * 0 1 1 1 0 cropping from 1,1 to 2,3 yields: 1 1 1 * 1 0 0 0 1 * 1 0 0 0 1 */ def cropAt(img: Img, xSt:Int, ySt:Int, xEnd: Int, yEnd: Int): Img = ??
7.2.8. Write a function which returns a list of all positions which have pixels of larger intensity than x
def largerPos(img: Img, int: Int): List[(Int,Int)] = ???
7.2.9. Write a function which adds x
to the intensity of each pixel.
def contrast(x: Int)(img: Img): Img = ???
7.2.10. Write a function which takes two images X
and Y
and glues them on the horizontal axis (the resulting image will be XY
)
def hglue(img1: Img, img2: Img): Img = ???
7.2.11. Write a function which takes two images X
and Y
and glues them on the vertical axis:
def vglue(img1: Img, img2: Img): Img = ???
7.2.12. Define a function that takes a square image, and draws two diagonal lines of intensity 1 across it. Use _.until(_)
and _.toList
.
def diag(img: Img): Img = ???