Gaussian-elimination.tex 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. \documentclass{article}
  2. \usepackage[pdftex,active,tightpage]{preview}
  3. \setlength\PreviewBorder{2mm}
  4. \usepackage[utf8]{inputenc} % this is needed for umlauts
  5. \usepackage[ngerman]{babel} % this is needed for umlauts
  6. \usepackage[T1]{fontenc} % this is needed for correct output of umlauts in pdf
  7. \usepackage{amssymb,amsmath,amsfonts} % nice math rendering
  8. \usepackage{braket} % needed for \Set
  9. \usepackage{algorithm,algpseudocode}
  10. \begin{document}
  11. \begin{preview}
  12. \begin{algorithm}[H]
  13. \begin{algorithmic}
  14. \Function{Gauss}{$A \in \mathbb{R}^{n \times {n+1}}$}
  15. \For{($i=1$; $\;i \leq n$; $\;i$++)}
  16. \State // Search for maximum in this column
  17. \State $maxEl = |A_{i,i}|$
  18. \State $maxRow = i$
  19. \For{($k=i+1$; $\;k \leq n$; $\;k$++)}
  20. \If{$|A_{k,i}| > maxEl$}
  21. \State $maxEl = A_{k,i}$
  22. \State $maxRow = k$
  23. \EndIf
  24. \EndFor
  25. \\
  26. \State // Swap maximum row with current row
  27. \For{($k=i$; $\;k \leq n$; $\;k$++)}
  28. \State $tmp = A_{maxRow,k}$
  29. \State $A_{maxRow,k} = A_{i,k}$
  30. \State $A_{i,k} = tmp$
  31. \EndFor
  32. \\
  33. \State // Make all rows below this one 0 in current column
  34. \For{($k=i+1$; $\;k \leq n$; $\;k$++)}
  35. \State $c = -\frac{A_{k,i}}{A_{i,i}}$
  36. \For{($j=i$; $\;j \leq n$; $\;j$++)}
  37. \If{$i == j$}
  38. \State $A_{k,j} = 0$
  39. \Else
  40. \State $A_{k,j} += c \cdot A_{i,j}$
  41. \EndIf
  42. \EndFor
  43. \EndFor
  44. \EndFor
  45. \\
  46. \State // Solve equation for an upper triangular matrix
  47. \State $x = \Set{0} \in \mathbb{R^n}$
  48. \For{($i=n$; $\;i \geq 1$; $\;i$-\--)}
  49. \State $x_i = \frac{A_{i,n+1}}{A_{i,i}}$
  50. \For{($k=i-1$; $\;k \geq 1$; $\;k$-\--)}
  51. \State $A_{k,n+1} -= A_{k,i} \cdot x_{i}$
  52. \EndFor
  53. \EndFor
  54. \\
  55. \State \Return $x$
  56. \EndFunction
  57. \end{algorithmic}
  58. \caption{Gaussian elimination}
  59. \label{alg:seq1}
  60. \end{algorithm}
  61. \end{preview}
  62. \end{document}