Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download

📚 The CoCalc Library - books, templates and other resources

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