第 1 题斐波那契数与特征值
100 分摘要将斐波那契递推写成矩阵形式,证明对称矩阵的特征向量正交,利用谱分解推导通项公式,并在不使用循环的情况下用 NumPy 实现。
- 线性代数特征分解NumPy
Let us consider the following sequence:
Before starting this problem, make sure to run the following code first without any change:
# DO NOT CHANGE
import numpy as np
import matplotlib.pyplot as plt
""" END OF THIS PART """
-
Beyond importing libraries/modules/classes/functions in the preceeding cell, you are NOT allowed to import anything else for the following purposes:
-
As a part of your final solution. For instance, if a problem asks you to build a model without using sklearn but you use it, then you will not earn points.
-
Temporarily import something to assist you to get a solution. For instance, if a problem asks you to manually compute eigenvalues but you temporarily use
np.linalg.eigto get an answer and then delete your code, then you violate the rule.
Rule of thumb: Each part has its particular purpose to intentionally test you something. Do not attempt to find a shortcut to circumvent the rule.
-
-
All coding tasks shall run on CPUs, not GPUs.
The recursive equation that defines the sequence in this problem can be written in a matrix form:
where .
Compute .
-
Reasoning is not required.
-
The value of each entry in must be exact. For instance, the following values are exact: , , , , , . However, their float approximations are not exact.
查看官方解答收起解答
官方解答
In this part, you are asked to prove a general result that holds for any real-valued symmetric matrix.
Let be a real-valued symmetric matrix.
Consider the following eigenvalue equation
where and .
Let and be two distinct real eigenvalues and be two eigenvectors associated with them, respectively.
Prove that
That is, vectors and are orthogonal.
查看官方解答收起解答
官方解答
We use two approaches to compute
Approach 1
We have
Approach 1
We have
where the second equality follows from the property that is a symmetric matrix.
Two approaches should yield the same result. Equating (1) and (2), we have
Because , we must have
This completes the proof.
Let us go back to our matrix .
In the following eigenvalue equation
compute two eigenvalues and whose values are in a descending order.
- Reasoning is required.
查看官方解答收起解答
官方解答
We solve the following characteristic equation:
Thus, we compute that satisfies
Hence, all eigenvalues are
In the remaining parts of this problem, for all non-coding tasks, when you need to use those eigenvalues,
Since matrix is symmetric, let us do the spectral decomposition of it.
That is, you should write in the following form:
where
-
is an orthonormal matrix.
-
is a diagnomal matrix with
In this task, you need to compute . Reasoning is required.
查看官方解答收起解答
官方解答
To compute an eigenvector assocaited with the eigenvalue for , we solve the following equation:
Since the determintant of the 2-by-2 matrix on the L.H.S. is 0, the rank of this matrix is 1. Thus, it is sufficient to only solve the following equation:
One solution is
Thus, the th orthonormal vector is
Therefore,
Use the spectral decomposition result to derive a closed form of for any .
-
Reasoning is required.
-
Your answer shall be written in terms of , , and and .
查看官方解答收起解答
官方解答
For , we have
Thus, for , we have
For , we can
and
Therefore, the above answer holds for all .
Compute
-
Reasoning is required.
-
Your answer shall be written in terms of one or two eigenvalues.
查看官方解答收起解答
官方解答
We notice that and .
Hence,
Define a class called My_Fib.
-
Attributes:
-
Q: This attribute is matrix 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 -
f1: The value of -
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 , , .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 .
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 , , , 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: .
-
The y-label is: .
-
The title is: Fibounacci sequence.
-
-
查看官方解答收起解答
官方解答
class My_Fib:
def __init__(self):
self.lambdas = np.array([(1 + 5**.5)/2, (1 - 5**.5)/2])
self.Q = np.stack([self.lambdas / np.sqrt(1 + self.lambdas**2), np.ones(2) / np.sqrt(1 + self.lambdas**2)])
def compute_fib(self, f0, f1, indices):
fib_values = (np.power(self.lambdas, np.array(indices)[:, np.newaxis]) / np.sqrt(1 + self.lambdas**2)) \
@ (self.Q.T @ np.array([f1, f0]))
fib_values = fib_values.astype(np.int32)
print(fib_values)
return fib_values
def plot_fib(self, f0, f1, indices):
fib_values = self.compute_fib(f0, f1, indices)
plt.plot(indices, fib_values, marker = "x", linestyle = "--", color = "red")
plt.xlabel('$n$')
plt.ylabel('$F_n$')
plt.title('Fibounacci sequence')