Part 9 (15 points, coding task)
Define a class called My_Fib.
-
Attributes:
-
Q: This attribute is matrix \mathbf{Q} computed above. It is a numpy array with shape(2,2). -
lambdas: This attribute is a numpy array with shape(2,)that includes two eigenvalues computed above.
-
-
Method
__init__:- All atttribute values shall be initialized when an object in this class is constructed.
-
Method
compute_fib:-
This method computes the sequence values on designated indices.
-
You must use the spectral decomposition result to write this method.
-
You are not allowed to use any loop.
-
Inputs:
-
f0: The value of F_0 -
f1: The value of F_1 -
indices: a list/tuple/range object that includes indices with which the sequence values shall be computed.
For instance, if
indicestakes the value[3, 5, 9], we need to compute F_3, F_5, F_9.In our test cases, you are guaranteed that
len(indices)is at least 1. You do not need to worry about the size of the test cases or whether the sequence values are too big (that is, on your side, you do not need to worry about those corner cases). -
-
Outputs:
-
Return a numpy array
fib_valueswith shape(len(indices),)and datatypeint32. -
fib_values[i]takes the value of F_{indices[i]}.
For instance, if
indicestakes the value[3, 5, 9], thenfib_valueshas shape(3,). The values offib_values[0],fib_values[1],fib_values[2]are F_3, F_5, F_9, respectively. -
-
Inside this method:
- Print
fib_values.
- Print
-
-
Method
plot_fib:-
This method plots indices vs. sequence values on those indices.
-
Inputs: The same as the method
compute_fib(f0, f1, indices). -
Outputs: None.
-
In your plot,
-
All data points with input indices are with marker
x. -
The linestyle is
--. -
The color is red.
-
The x-lable is: n.
-
The y-label is: F_n.
-
The title is: Fibounacci sequence.
-
-