SHARE
    TWEET
    UsSe3wa

    Untitled

    Apr 16th, 2025
    459
    0
    Never
    Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
    Python 1.71 KB | None | 0 0
    1. # MatveyPanchenko DSAI-03
    2. import sympy as sp, math
    3. def rref(M, e=1e-12):
    4. A, m, n, r, c, piv = [row[:] for row in M], len(M), len(M[0]), 0, 0, []
    5. while r < m and c < n:
    6. k = next((i for i in range(r, m) if abs(A[i][c]) > e), None)
    7. if k is None: c += 1; continue
    8. A[r], A[k] = A[k], A[r]
    9. d = A[r][c]; A[r] = [x / d for x in A[r]]
    10. for i in range(m):
    11. if i != r and abs(A[i][c]) > e:
    12. f = A[i][c]; A[i] = [u - f * v for u, v in zip(A[i], A[r])]
    13. piv.append(c); r += 1; c += 1
    14. return A, piv
    15. def null_space(M):
    16. R, piv = rref(M); n = len(R[0]); free = [j for j in range(n) if j not in piv]
    17. if not free: return [[0]*n]
    18. B = []
    19. for f in free:
    20. v = [0]*n; v[f] = 1
    21. for i, p in enumerate(piv): v[p] = -R[i][f]
    22. B.append(v)
    23. return B
    24. si = lambda A, l: [[A[i][j] - (l if i == j else 0) for j in range(3)] for i in range(3)]
    25. def char_coeffs(A):
    26. a, b, c = A[0]; d, e, f = A[1]; g, h, i = A[2]
    27. tr = a + e + i
    28. s2 = a*e + a*i + e*i - b*d - c*g - f*h
    29. det = a*(e*i - f*h) - b*(d*i - f*g) + c*(d*h - e*g)
    30. return 1, -tr, s2, -det
    31. A = [list(map(float, input().split())) for _ in range(3)]
    32. p = char_coeffs(A)
    33. λ = sp.symbols('λ')
    34. poly = p[0]*λ**3 + p[1]*λ**2 + p[2]*λ + p[3]
    35. vals = [complex(sp.N(v)) for v in sp.solve(poly, λ)]
    36. vals = [v.real if abs(v.imag) < 1e-10 else v for v in vals]
    37. print("Eigenvalues:")
    38. for v in vals:
    39. print(v)
    40. print("Eigenvectors:")
    41. for l in vals:
    42. v = next((u for u in null_space(si(A, l)) if any(abs(x) > 1e-12 for x in u)), [0]*3)
    43. n = math.sqrt(sum(abs(x)**2 for x in v))
    44. v = [x / n for x in v] if n > 1e-12 else v
    45. print(v)
    Advertisement
    Add Comment
    Please, Sign In to add comment
    Public Pastes
    We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
    Not a member of Pastebin yet?
    Sign Up, it unlocks many cool features!

    AltStyle によって変換されたページ (->オリジナル) /