import sympy as sp from sympy import Matrix def mat_simp(A): n, p = A.shape for i in range(n): for j in range(p): A[i, j] = sp.simplify(A[i, j]).expand().simplify() return A def mat(u): n = len(u) M = sp.zeros(n, n) for j in range(n): cj = sp.Matrix([int(i == j) for i in range(n)]) Mcj = cj-2*cj.dot(u)/u.norm()**2*u for i in range(n): M[i, j] = Mcj[i] return M def DecompositionQR(A): n, p = A.shape if n != p or A.det() == 0: raise Exception("Les données ne sont pas correctes") AA = A.copy() O = sp.eye(n) for i in range(n): if not AA[i+1:, i].is_zero: B = AA.copy() for j in range(i): B[j, i] = 0 Ci = sp.Matrix([int(k == i) for k in range(n)]) S = mat_simp(mat(Ci-B[:, i:i+1]/B[:, i:i+1].norm())) O = S@O AA = S@AA return O.transpose(), AA A = Matrix([2, 0, 2, 2, 1, 1, 2, 2, 0, 1, 1, 2, 0, 0, 2, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9]).reshape(5, 5) DecompositionQR(A)为什么没有输出
时间: 2023-12-07 19:05:01 浏览: 61
sympy.github.com:SymPy的网页(sympy.org)
在代码中,函数`DecompositionQR(A)`没有输出结果,因为它只是将矩阵A进行QR分解,并将正交矩阵O和上三角矩阵R返回。如果想要输出结果,需要在调用函数时打印返回值,例如:
```
A = Matrix([2, 0, 2, 2, 1, 1, 2, 2, 0, 1, 1, 2, 0, 0, 2, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9]).reshape(5, 5)
O, R = DecompositionQR(A)
print("Orthogonal matrix O:")
print(O)
print("Upper triangular matrix R:")
print(R)
```
这样就可以输出QR分解结果了。
阅读全文