# Algebra : Math Question (answered!)

## 2285 days ago by bigdata2016

# Naguib: i defined these methods for your code to work def power_method(A,b): X= matrix(RDF,7,1,[1,1,1,1,1,1,1]) for i in range(b): Y=A*X C=max(max(Y)) X=Y/C show("Dominant Eigen value = ") show(C) show("Dominant Eigen vector = ") show(X) return C def power_method_err(A,b): C=power_method(A,b) GC=max(A.eigenvalues()) show("Power method Eigen value = ") show(C) show("Eigen value = ") show(GC) show("Error = ") show(abs(GC-C)) return abs(GC-C)
A = matrix(QQ, [[1, 2, 1, 2, 1, 2, 0],[0, 1, -2, 5, 0, 1, 0],[2, 0, 0, 1, 1, 1, 0],[1, 0, 1, 2, 4, 0, 1],[1, 1, 2, 0, 4, 1, 1],[1, 1, 6, 3, 0, 7, 0],[1, 1, 3, -1, 1, 6, 0]]) show(A)
 \newcommand{\Bold}[1]{\mathbf{#1}}\left(\begin{array}{rrrrrrr} 1 & 2 & 1 & 2 & 1 & 2 & 0 \\ 0 & 1 & -2 & 5 & 0 & 1 & 0 \\ 2 & 0 & 0 & 1 & 1 & 1 & 0 \\ 1 & 0 & 1 & 2 & 4 & 0 & 1 \\ 1 & 1 & 2 & 0 & 4 & 1 & 1 \\ 1 & 1 & 6 & 3 & 0 & 7 & 0 \\ 1 & 1 & 3 & -1 & 1 & 6 & 0 \end{array}\right)
time power_method(A, 20)
 \newcommand{\Bold}[1]{\mathbf{#1}}\verb|Dominant|\phantom{\verb!x!}\verb|Eigen|\phantom{\verb!x!}\verb|value|\phantom{\verb!x!}\verb|=| \newcommand{\Bold}[1]{\mathbf{#1}}10.3955390244 \newcommand{\Bold}[1]{\mathbf{#1}}\verb|Dominant|\phantom{\verb!x!}\verb|Eigen|\phantom{\verb!x!}\verb|vector|\phantom{\verb!x!}\verb|=| \newcommand{\Bold}[1]{\mathbf{#1}}\left(\begin{array}{r} 0.425898059316 \\ 0.256582409347 \\ 0.259252851635 \\ 0.385847148649 \\ 0.457429869078 \\ 1.0 \\ 0.7245244067 \end{array}\right) 10.3955390244 Time: CPU 0.02 s, Wall: 0.02 s 10.3955390244 Time: CPU 0.02 s, Wall: 0.02 s
time max(A.eigenvalues())
 10.39553902441960? Time: CPU 0.03 s, Wall: 0.04 s 10.39553902441960? Time: CPU 0.03 s, Wall: 0.04 s
time power_method_err(A, 20)
 \newcommand{\Bold}[1]{\mathbf{#1}}\verb|Dominant|\phantom{\verb!x!}\verb|Eigen|\phantom{\verb!x!}\verb|value|\phantom{\verb!x!}\verb|=| \newcommand{\Bold}[1]{\mathbf{#1}}10.3955390244 \newcommand{\Bold}[1]{\mathbf{#1}}\verb|Dominant|\phantom{\verb!x!}\verb|Eigen|\phantom{\verb!x!}\verb|vector|\phantom{\verb!x!}\verb|=| \newcommand{\Bold}[1]{\mathbf{#1}}\left(\begin{array}{r} 0.425898059316 \\ 0.256582409347 \\ 0.259252851635 \\ 0.385847148649 \\ 0.457429869078 \\ 1.0 \\ 0.7245244067 \end{array}\right) \newcommand{\Bold}[1]{\mathbf{#1}}\verb|Power|\phantom{\verb!x!}\verb|method|\phantom{\verb!x!}\verb|Eigen|\phantom{\verb!x!}\verb|value|\phantom{\verb!x!}\verb|=| \newcommand{\Bold}[1]{\mathbf{#1}}10.3955390244 \newcommand{\Bold}[1]{\mathbf{#1}}\verb|Eigen|\phantom{\verb!x!}\verb|value|\phantom{\verb!x!}\verb|=| \newcommand{\Bold}[1]{\mathbf{#1}}10.39553902441960? \newcommand{\Bold}[1]{\mathbf{#1}}\verb|Error|\phantom{\verb!x!}\verb|=| \newcommand{\Bold}[1]{\mathbf{#1}}1.15800702361 \times 10^{-11} 1.15800702361e-11 Time: CPU 0.02 s, Wall: 0.02 s 1.15800702361e-11 Time: CPU 0.02 s, Wall: 0.02 s