This commit is contained in:
jakubknczny 2021-05-31 23:16:20 +02:00
parent 6ed8b516e3
commit 1b6e700a07
7 changed files with 50 additions and 16 deletions

8
.idea/.gitignore vendored Normal file
View File

@ -0,0 +1,8 @@
# Default ignored files
/shelf/
/workspace.xml
# Datasource local storage ignored files
/dataSources/
/dataSources.local.xml
# Editor-based HTTP Client requests
/httpRequests/

6
.idea/misc.xml Normal file
View File

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectRootManager" version="2" languageLevel="JDK_16" project-jdk-name="gpu" project-jdk-type="Python SDK">
<output url="file://$PROJECT_DIR$/out" />
</component>
</project>

8
.idea/modules.xml Normal file
View File

@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/.idea/qr.iml" filepath="$PROJECT_DIR$/.idea/qr.iml" />
</modules>
</component>
</project>

9
.idea/qr.iml Normal file
View File

@ -0,0 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$" />
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>

View File

6
.idea/vcs.xml Normal file
View File

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="" vcs="Git" />
</component>
</project>

29
main.py
View File

@ -108,18 +108,22 @@ class QR:
print("Incorrect type.") print("Incorrect type.")
raise Exception raise Exception
def solve_least_squares(self, A: np.ndarray, b: np.array): def solve_least_squares(self, A: np.ndarray, b: np.array, is_givens):
Q, R = self.perform_householder_QR(A) if is_givens:
Q, R = self.perform_givens_QR(A)
else:
Q, R = self.perform_householder_QR(A)
x = solve(R, np.dot(Q.T, b)) x = solve(R, np.dot(Q.T, b))
return x return x
def __design_matrix(self, A: np.ndarray): def __design_matrix(self, A: np.ndarray):
return np.hstack((np.ones(A.shape[0]).reshape(-1, 1), A[:, :-1])) return np.hstack((np.ones(A.shape[0]).reshape(-1, 1), A[:, :-1]))
def fit_poly(self, A: np.ndarray): def fit_poly(self, A: np.ndarray, is_givens: bool):
return self.solve_least_squares( return self.solve_least_squares(
np.dot(self.__design_matrix(A), self.__design_matrix(A).T), np.dot(self.__design_matrix(A), self.__design_matrix(A).T),
A[:, -1:].reshape(-1, 1)) A[:, -1:].reshape(-1, 1),
is_givens=is_givens)
""" """
@ -147,10 +151,7 @@ if __name__ == "__main__":
print(Q) print(Q)
print(R) print(R)
print('solve least squares') print('solve least squares')
b_v = np.asarray([1, 1, ]) print(qr.solve_least_squares(matrix[:2, :-1], matrix[:2, -1:], is_givens=True))
print(matrix)
print(b_v)
def PolyCoefficients(x, coeffs): def PolyCoefficients(x, coeffs):
""" Returns a polynomial for ``x`` values for the ``coeffs`` provided. """ Returns a polynomial for ``x`` values for the ``coeffs`` provided.
@ -164,21 +165,17 @@ if __name__ == "__main__":
def f(a, b): def f(a, b):
return a + 2 * b return a + 2 * b ^ 2
x1 = np.asarray(range(0, 6)) x1 = np.asarray(range(0, 4))
x2 = np.asarray(range(0, 6)) x2 = np.asarray(range(0, 4))
y = f(x1, x2) y = f(x1, x2)
mat = np.asmatrix([x1, x2, y]) mat = np.asmatrix([x1, x2, y])
plt3d = plt.figure().gca(projection='3d') plt3d = plt.figure().gca(projection='3d')
xx, yy = np.meshgrid(range(10), range(10)) xx, yy = np.meshgrid(range(10), range(10))
plt3d.plot_surface(xx, yy, f(xx, yy), alpha=0.2) plt3d.plot_surface(xx, yy, f(xx, yy), alpha=0.2)
print(mat.T) plt3d.plot_surface(xx, yy, PolyCoefficients(xx, qr.fit_poly(mat.T, is_givens=True)), alpha=0.2)
print(matrix)
print(matrix.shape, mat.T.shape)
# plt3d.plot_surface(xx, yy, PolyCoefficients(xx, qr.fit_poly(matrix)), alpha=0.2)
plt3d.plot_surface(xx, yy, PolyCoefficients(xx, qr.fit_poly(mat.T)), alpha=0.2)
plt.show() plt.show()