Пример #1
0
def test_QR_non_square():
    A = Matrix([[9, 0, 26], [12, 0, -7], [0, 4, 4], [0, -3, -3]])
    Q, R = A.QRdecomposition()
    assert Q.T * Q == eye(Q.cols)
    assert R.is_upper()
    assert A == Q * R

    A = Matrix([[1, -1, 4], [1, 4, -2], [1, 4, 2], [1, -1, 0]])
    Q, R = A.QRdecomposition()
    assert Q.T * Q == eye(Q.cols)
    assert R.is_upper()
    assert A == Q * R
Пример #2
0
def test_QR():
    A = Matrix([[1,2],[2,3]])
    Q, S = A.QRdecomposition()
    R = Rational
    assert Q == Matrix([[5**R(-1,2), (R(2)/5)*(R(1)/5)**R(-1,2)], [2*5**R(-1,2), (-R(1)/5)*(R(1)/5)**R(-1,2)]])
    assert S == Matrix([[5**R(1,2), 8*5**R(-1,2)], [0, (R(1)/5)**R(1,2)]])
    assert Q*S == A
    assert Q.T * Q == eye(2)

    A = Matrix([[1,1,1],[1,1,3],[2,3,4]])
    Q, R = A.QRdecomposition()
    assert Q*Q.T == eye(Q.rows)
    assert R.is_upper()
    assert A == Q*R
from sympy import Matrix, Rational

V = Matrix([[6, Rational(17,2)],[Rational(17,2), 12]])

Q, R = V.QRdecomposition()

print(Q)
print(R)
Пример #4
0
#!/usr/bin/env python
# -*- coding: utf-8 -*-

from sympy import (
    Matrix, )

# A = QR of an orthogonal matrix Q and an upper triangular matrix R
A = Matrix([[1, 1, 1], [1, 1, 3], [2, 3, 4]])
Q, R = A.QRdecomposition()
print(R)
print(Q * Q.transpose())
print(Q * Q.T)
print(A)
print(Q * R)

# LU decomposition of a matrix is the factorization of a given square matrix into two triangular matrices, one upper triangular matrix and one lower triangular matrix
L, U, _ = A.LUdecomposition()
print(L)
print(U)
Пример #5
0
import numpy as np
from sympy import Matrix

A = np.array([[2, 3, 1, 0], [4, -1, 9, 0], [3, 8, -2, 0], [1, -2, 4, 0]])
# 将numpy.array转为sympy.Matrix
MA = Matrix(A)
print("------------------行简化阶梯型-------------------")
print(MA.rref()) # 行简化阶梯型


print("------------------对角化矩阵-------------------")
M = Matrix([[3,-2,4,-2],[5,3,-3,-2],[5,-2,2,-2],[5,-2,-3,3]])
P, D = M.diagonalize() # 对角化矩阵
print(P)
print("对角矩阵 D:")
print(D)

print("------------------LU 分解-------------------")
print(MA.LUdecomposition()) # LU 分解
print("------------------QR 分解-------------------")
print(MA.QRdecomposition()) # QR 分解
# (把矩阵分解成一个正交矩阵与一个上三角矩阵的积 ,Q 是正交矩阵(意味着 QTQ = I)而 R 是上三角矩阵,此外,原矩阵A不必为正方矩阵; 如果矩阵A大小为n*m,则矩阵Q大小为n*m,矩阵R大小为m*m。)