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 17:05:01 浏览: 64
在代码中,函数`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分解结果了。
相关问题
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_matrix(): 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)怎么修改
这个错误可能是因为 `is_zero_matrix` 方法被误用了。在这里,这个方法应该是用来检查一个矩阵是否为零矩阵的。可以将 `not AA[i+1:, i].is_zero_matrix():` 替换为 `not AA[i+1:, i].is_all_zeros():`。`is_all_zeros()` 方法可以检查一个矩阵的所有元素是否都为零。修改后的代码如下:
```
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_all_zeros():
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)
```
请注意,这只是一个修改建议,并不能保证这个修改能够完全解决问题,因为我并没有完整的代码和数据来进行测试。
import sympy as sp from sympy import Matrix sp.init_printing() def mat_simp(A): n, p = A.shape for i in range(n): for j in range(p): A[i, j] = A[i, j].simplify().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_matrix: 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]/B[:, i].norm())) O = S@O AA = S@AA return O.transpose(), AA A=sp.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)为什么这段代码运行不出来
这段代码可能运行不出来是因为在第一行代码中,你没有使用逗号分隔符来分隔`import`语句。你需要将第一行代码改为以下形式:
```
import sympy as sp
from sympy import Matrix
```
另外,`mat_simp`函数中,你需要使用`sp.simplify()`和`sp.expand()`来简化和展开表达式。你可以将代码改为以下形式:
```
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
```
最后,`DecompositionQR`函数中,`if`语句的条件需要改为`if not AA[i+1:, i].is_zero_matrix()`,并且在`S = mat_simp(mat(Ci-B[:, i]/B[:, i].norm()))`语句中,你需要将`B[:, i]`改为`B[:, i:i+1]`,因为它返回的是一个列向量。你可以将代码改为以下形式:
```
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_matrix():
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)
```
如果还是无法运行,请检查你的`SymPy`库是否正确安装。
阅读全文