6 question like the picture. The lab need dowload an app. Follow the guide to dowload the app to fish the lab. Then save it in .py file.
Exercise 1
Define the letters for python to be the number corresponding to their place in the alphabet. Then set the variable named python, equal to the sum of the
letters.
In [ ]: python = None
Hints and Tips
If you ever get stuck or need more info on a specific NumPy function, an excellent way to get help is by visiting the NumPy documentation at
https://numpy.org/doc/1.171. One great way to make use of this resource is the quick search button on the left side; you can type in any function in NumPy and it
will show up with documentation on what it takes in, and some example use cases.
To call on a function from the NumPy library, you will want to use np.name, where name is the name of the function. For example, if you wanted to call numpy’s
cosine function on an input of 3 radians, you would use np.cos(3).
Exercise 2
Syntax is very important in making sure your script works. Fix the code below so it runs. It should be the equivalent of 25 – (100 – 7eStcos(pi/3))
In [ ]: z = 25-(100–7np. exp (5+np. cos (np. pi/3))
Matrices in Python
The first thing to learn when dealing with matrices is how to construct a matrix (or vector) using NumPy.
To define a matrix using NumPy, we use the square brackets “)”: “T” tells NumPy we are starting to create a matrix and “l’ tells NumPy that we have finished
with our construction. The entries of the matrix should be entered in rows from left to right. To separate entries we can insert a comma”,”
Once we finish with a given row and want to move to the next, we can make the next row by repeating the same process again through using square brackets
to create another row. Each row will be separated by a comma.
You can collect these rows into a matrix by encasing these rows in a final set of square brackets
However, to set these matrices in a form usable by NumPy, you will need to convert this constructed matrix into a Numpy array object. This can be easily done
by using the np.array(A) function, where A is the matrix you want to convert to a Numpy array.
In [ ]: A = [[2, 1),
[4, 3]]
A = np. array (A)
A
Exercise 3
Input the following matrix as a Numpy array in the fibonacci variable.
–
fibonacci =
1 1 2 3
5 8 13 21
34 55 89 144
233 377 610 987
In [ ]: fibonacci = None
Matrix operations and manipulations
Sometimes we need to get at certain parts of a matrix only, maybe just a certain row or even a single entry.
This is done with regular parentheses, “” and “)”. For example, to see the (1,2) entry of the matrix A above (i.e. the entry in the first row and second column)
we use the command
In [ ]: A[0, 1]
NOTE: In Python, indexing starts at 0, so to see the entry (1, 2), you will want to access it as [0, 1].
We also can use the colon “:” to mean ‘all’, as in the command
In [ ]: A[], :]
which will give us the entire second row of the matrix A. The colon can also be used to represent a range of rows or columns: the command
In [ ]: fibonacci(1:4, 0]
will give us the entries of Fibonacci from the second through fourth rows in the first column.
Exercise 4
Using the commands introduced above, construct a new matrix (call it whatever you’d like) from fibonacci that consists of the last two rows and the two middle
columns of fibonacci. (So it will be a 2×2 matrix.) Save the result in the new_fibonacci variable.
In [ ]: new_fibonacci = None
Random matrices
From time to time, we will be working with random matrices in this class. However, when you are asked to create a random matrix, this does not mean you
should just create a matrix using numbers that pop up in your head. Instead, we will let Numpy do the “thinking”. To create random matrices, we use the
np.random.rand() command.
Notice the parentheses after the command. When using a command that requires some type of input, that input must be put in parentheses.
In [ ]: np. random. rand (3, 3)
and hitting return tells NumPy to create a random 3 x 3 matrix whose entries are decimal values between 0 and 1.
Exercise 5
Create 3 random 4 x 4 matrices A, B, and C and calculate the following value. (You should store each of them in a variable with some name of your choosing.)
In theory, do you expect the answer to be a zero matrix? Is it in practice?
(A+B+C)-(A + C +B)
In [ ]: calculated_matrix = None
In [ ]: calculated_matrix
Enter your response here:
Exercise 6
How do you find sin-‘(1) using NumPy? (Hint: Start by looking at the NumPy documentation). Save the value into the variable below.
In [ ]: arcsinl = None