Lecture 6 More Arrays, Matrices, and Solving Linear Equations

1. Slicing
The rules for slicing are the same as the rules in Matlab:




2. The cube of an array:




3. Reshape
flatten a matrix


Remodify the shape and transpose.


Difference between 'resize()' and 'reshape()': resize will change the matrix dimension itself, reshape will return you a new one but the original one doesn't change.




4. Summary
Try these and make sure you understand.


Try these and make sure you understand.
A[][] treats the array A as a three-elements list. Each individual list inside the array is an elements, and they are aligned in one row.
A[ , ] treats the array A as a 3x3 matrix. The indices refer to the row and the coloumn address.
For example, A[0][0] means the first element in the first sublist in the 1-row list.






Concatenation of arrays:










5. Solve linear equations

Look at the following  equation set:


We need to convert it into this format first:


Then you will get this:

Compare this figure to the last one, you will find out how to extract the coefficients from the equation set and use 'Matrix Multiplication' to represent the linear equation. Remember why we bother ourselves to convert it in this format??? It was explained in last lecture:

This is computer friendly! We need to use this format to get Python to calculate it for us!!!!!

Now, the question is HOW?
Let's do it.
If 'A' is the coefficient matrix, X is the variable column, b is the constant column, then the matrix multiplication format can be represented by:
Ax=b

In  linear  Algebra,  the 'Inverse' of a matrix can be represented by A^(-1), you don't need to know how the matrix is inversed now. You only need to know the following:
1) A^(-1) * A = I
2) I * A = A
In whcih I is:

It is making sense that I * A is still A based on the matrix multiplication rules, for example:



Now, one more time:
1) A^(-1) * A = I
2) I * A = A

If we do this:


Then we can get:
x = A^(-1) * b

Which means we can solve the linear equation by A^(-1) * b.
b is given, A^(-1)  is the inverse matrix of A.

6. Python & linear algebra

Import the scipy.linalg package to enable the linear algebra functions.

Calculate the determinant of a matrix:



Inverse a matrix:


Solve a linear equation:

a. Using the scipy.linalg.solve() function to solve for it directly.





b. Inverse the coefficient matrix first:



It is pretty interesting that Python is pretty tolerant on the array's shape when doing the matrix multiplication:
Surprisingly, the following one also works but the returned solution has a different shape.




References:
https://docs.scipy.org/doc/numpy/user/quickstart.html



Tasks:

1. Transpose the matrix below.
 
2. Flatten the matrix created in Task 1.
3. Extract [4,5,6] from the array. (just print it out but not removing it from the original array)
4. Extract the middle column of the array:

5. Create array A = [1,2,3], B = [[4,5,6], [7,8,9]]. Using A and B to form C = [
[1,2,3], [4,5,6], [7,8,9] ]
6.
Create array AA = [[4,5,6], [7,8,9]], BB = [[0], [0]], using AA and BB to form CC = [[4,5,6,0], [7,8,9,0]]
7. For the following array AAA:

Access the array using the following command and explain the results using comments.
AAA[1,1]
AAA[1][1]
AAA[1]
AAA[:, 1]
AAA[:][1]

8. The equations below represent a typical electrical circuit with two voltage sources connected to five resistors with three closed loops.

 

    Where:

      R1 = 5 ohm;   R2 = 5 ohm;   R3 = 10 ohm;   R4 = 12 ohm;  

      V1 = 4 volts;  V2 = 8 volts; V3=6 volts    

 

Put the equations into matrix form and solve for the unknown currents i1 through i4  Create variables for the known Rs and Vs then contruct the matrices in terms of those variables. 

Send you code in the '.py' format to the email.