Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # MatveyPanchenko DSAI-03
- from copy import deepcopy
- tr = lambda m: [list(r) for r in zip(*m)]
- def rref(a, eps=1e-12):
- A = deepcopy(a)
- r = c = 0
- rows, cols = len(A), len(A[0])
- piv = []
- while r < rows and c < cols:
- k = next((i for i in range(r, rows) if abs(A[i][c]) > eps), None)
- if k is None:
- c += 1
- continue
- A[r], A[k] = A[k], A[r]
- p = A[r][c]
- A[r] = [x / p for x in A[r]]
- for i in range(rows):
- if i != r and abs(A[i][c]) > eps:
- f = A[i][c]
- A[i] = [u - f * v for u, v in zip(A[i], A[r])]
- piv.append(c)
- r += 1
- c += 1
- return A, piv
- row_basis = lambda a: [r for r in rref(a)[0] if any(abs(x) > 1e-12 for x in r)]
- def col_basis(a):
- _, piv = rref(a)
- return [[a[i][j] for i in range(len(a))] for j in piv]
- def null_basis(a):
- R, piv = rref(a)
- n = len(R[0])
- free = [j for j in range(n) if j not in piv]
- B = []
- for f in free:
- v = [0] * n
- v[f] = 1
- for i, p in enumerate(piv):
- v[p] = -R[i][f]
- B.append(v)
- return B
- if __name__ == "__main__":
- r = int(input("rows: "))
- A = [list(map(float, input().split())) for _ in range(r)]
- print("Row space:", row_basis(A))
- print("Column space:", col_basis(A))
- print("Row null space:", null_basis(tr(A)))
- print("Column null space:", null_basis(A))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement