📚 The CoCalc Library - books, templates and other resources
cocalc-examples / martinthoma-latex-examples / source-code / Pseudocode / Gaussian-elimination / Gaussian-elimination.tex
132939 viewsLicense: OTHER
\documentclass{article}1\usepackage[pdftex,active,tightpage]{preview}2\setlength\PreviewBorder{2mm}34\usepackage[utf8]{inputenc} % this is needed for umlauts5\usepackage[ngerman]{babel} % this is needed for umlauts6\usepackage[T1]{fontenc} % this is needed for correct output of umlauts in pdf7\usepackage{amssymb,amsmath,amsfonts} % nice math rendering8\usepackage{braket} % needed for \Set9\usepackage{algorithm,algpseudocode}1011\begin{document}12\begin{preview}13\begin{algorithm}[H]14\begin{algorithmic}15\Function{Gauss}{$A \in \mathbb{R}^{n \times {n+1}}$}16\For{($i=1$; $\;i \leq n$; $\;i$++)}17\State // Search for maximum in this column18\State $maxEl = |A_{i,i}|$19\State $maxRow = i$20\For{($k=i+1$; $\;k \leq n$; $\;k$++)}21\If{$|A_{k,i}| > maxEl$}22\State $maxEl = A_{k,i}$23\State $maxRow = k$24\EndIf25\EndFor26\\27\State // Swap maximum row with current row28\For{($k=i$; $\;k \leq n$; $\;k$++)}29\State $tmp = A_{maxRow,k}$30\State $A_{maxRow,k} = A_{i,k}$31\State $A_{i,k} = tmp$32\EndFor33\\34\State // Make all rows below this one 0 in current column35\For{($k=i+1$; $\;k \leq n$; $\;k$++)}36\State $c = -\frac{A_{k,i}}{A_{i,i}}$37\For{($j=i$; $\;j \leq n$; $\;j$++)}38\If{$i == j$}39\State $A_{k,j} = 0$40\Else41\State $A_{k,j} += c \cdot A_{i,j}$42\EndIf43\EndFor44\EndFor45\EndFor46\\47\State // Solve equation for an upper triangular matrix48\State $x = \Set{0} \in \mathbb{R^n}$49\For{($i=n$; $\;i \geq 1$; $\;i$-\--)}50\State $x_i = \frac{A_{i,n+1}}{A_{i,i}}$51\For{($k=i-1$; $\;k \geq 1$; $\;k$-\--)}52\State $A_{k,n+1} -= A_{k,i} \cdot x_{i}$53\EndFor54\EndFor55\\56\State \Return $x$57\EndFunction58\end{algorithmic}59\caption{Gaussian elimination}60\label{alg:seq1}61\end{algorithm}62\end{preview}63\end{document}646566