Last time we saw that Python lists provide a useful way to package data. In particular we could use a list to model a vector.

Now look at say, scalar multiplication.

\[\begin{split}3 \cdot \left(\begin{array}{r}
1 \\
2 \\
3
\end{array}\right) =
\left(\begin{array}{r}
3 \\
6 \\
9
\end{array}\right)\end{split}\]

Unfortunately Python does not directly interpret lists as vectors. For example:

```
>>> L=[1,2,3]
>>> 3*L
[1, 2, 3, 1, 2, 3, 1, 2, 3]
>>>
```

Python did a concatenation, built a list by joining the original list with two additional copies of that list.

We will have to write a functions to view a list as a vector and perform the operations we want.

These are examples of the three vector function we’d like you to write. You’ll find stubs for these functions in vecFunctions.py.

```
>>> V_1=[1,2,3]
>>> V_2=[4,5,6]
>>> ScalarMult(3,V_1) #****
[3, 6, 9]
>>> AddVectors(V_1,V_2) #****
[5, 7, 9]
>>> Dot(V_1,V_2) #****
32
```

We will be using a list of lists to represent a
matrix. Recall that we used a list of numbers to represent a vector.
Let’s assume that our matrix is represented by a list of *row
vectors*. Consider this matrix:

\[\begin{split}\left(\begin{array}{rrrr}
4 & -2 & 1 & 11 \\
-2 & 4 & -2 & -16 \\
1 & -2 & 4 & 17
\end{array}\right)\end{split}\]

We will model it as:

```
[[4, -2, 1, 11], [-2, 4, -2, -16], [1, -2, 4, 17]]
```

We’ll need some functions to work with these representations. First let’s make a function to make the list look something like a matrix.

The *showMatrix (mat)* function will be useful for looking at a
matrix.

```
def showMatrix(mat):
"Print out matrix"
for row in mat:
print(row)
```

Let A be a matrix

```
>>> A = [[4, -2, 1, 11], [-2, 4, -2, -16], [1, -2, 4, 17]]
>>> showMatrix(A)
[4, -2, 1, 11]
[-2, 4, -2, -16]
[1, -2, 4, 17]
```

Notice that *showMatrix()* does not do any data checking. For example,
[‘a’,7, [1,2,3],’pi’]) has rows of differing lengths.

```
>>> showMatrix(['a',7, [1,2,3],'pi'])
a
7
[1, 2, 3]
pi
```

**Note:** If this were a programming class, our code would need to
test that the data was appropriate – e.g. each row vector list has the same
number of elements and each element of the row vector list is a
number. For our work right now, you need not build check into your code.

You will need to start with the file matrixFunctions.py and complete 5 functions.

```
def Transpose(mat):
"return transpose of mat"
def GetCol(mat, col):
"return column col from matrix mat"
def GetRow(mat, row):
"return row row from matrix mat"
def ScalarMultMatrix(a,mat):
"multiply a scalar times a matrix"
def AddMatrices(A,B):
"add two matrices"
```

Here is some commentary on what you’ll find in the file matrixFunctions.py.

These two one line functions are useful for writing code that is human readable. They also make clear how we are representing a matrix as a list.

```
def rows(mat):
"return number of rows"
return(len(mat))
def cols(mat):
"return number of cols"
return(len(mat[0]))
```

These functions are stubs. You’ll need to finish the code.

```
def GetCol(mat, col):
"return column col from matrix mat"
return("write GetCol()")
def Transpose(mat):
"return transpose of mat"
return("write Transpose()")
def GetRow(mat, row):
"return row row from matrix mat"
return("write GetRow()")
def ScalarMultMatrix(a,mat):
"multiply a scalar times a matrix"
return("write ScalarMultMatrix()")
def AddMatrices(A,B):
"add two matrices"
return("write AddMatrics()")
def MultiplyMat(mat1,mat2):
"multiply two matrices"
return("write MultiplyMat()")
```

Multiplying matrices is a bit of a challenge. You should use some of your vector functions rather than “reinvent the wheel.”

Finally, here is some test data and a print statement. Notice that we can enter a matrix list in a way that makes it look something like a matrix.

```
A= [[4,-2,1,11],
[-2,4,-2,-16],
[1,-2,4,17]]
Ae= [[4,-2,1],
[-2,4,-2],
[1,-2,4]]
B=[11,-16,17]
C=[2,3,5]
print("running matrixFunction.py file")
```

Here I ran the file, which yields some information about your group and the print line...

```
>>> ================================ RESTART ================================
>>>
matrixFunctions.py for group 17: student 1, student 2,
running matrixFunction.py file
>>>
```

... and typed a few lines to test the code.

```
>>> showMatrix(A)
[4, -2, 1, 11]
[-2, 4, -2, -16]
[1, -2, 4, 17]
>>> rows(A)
3
>>> cols(A)
4
>>>
```

`modified: Mon Feb 1 19:16:38 PST 2016`