Maticové operace a inverzní matice
Proč to potřebuji?
Matice jsou základním nástrojem lineární algebry. Umožňují kompaktně zapsat soustavy rovnic, transformace v počítačové grafice, přechodové matice v Markovových řetězcích a mnoho dalšího. V tomto tématu se naučíš s maticemi počítat — sčítat je, násobit číslem i mezi sebou, transponovat a hledat inverzní matici.
Předpoklady: Soustavy rovnic a GEM (Téma 1).
Teorie
Definice matice
Matice typu $(m, n)$ (neboli $m \times n$) je obdélníkové schéma $m \cdot n$ reálných čísel uspořádaných do $m$ řádků a $n$ sloupců:
$$\mathbf{A} = \begin{pmatrix} a_{11} & a_{12} & \cdots & a_{1n} \\ a_{21} & a_{22} & \cdots & a_{2n} \\ \vdots & \vdots & \ddots & \vdots \\ a_{m1} & a_{m2} & \cdots & a_{mn} \end{pmatrix}$$Číslo $a_{ij}$ je prvek v $i$-tém řádku a $j$-tém sloupci. Stručně píšeme $\mathbf{A} = (a_{ij})_{m \times n}$.
Jednotková matice $\mathbf{E}_n$ je čtvercová matice řádu $n$, která má na hlavní diagonále jedničky a všude jinde nuly:
$$\mathbf{E}_2 = \begin{pmatrix} 1 & 0 \\ 0 & 1 \end{pmatrix}, \qquad \mathbf{E}_3 = \begin{pmatrix} 1 & 0 & 0 \\ 0 & 1 & 0 \\ 0 & 0 & 1 \end{pmatrix}$$Platí $\mathbf{A} \cdot \mathbf{E}_n = \mathbf{E}_m \cdot \mathbf{A} = \mathbf{A}$ pro matici $\mathbf{A}$ typu $(m, n)$.
Součet matic a násobení skalárem
Sčítat lze pouze matice stejného typu. Součet matic $\mathbf{A} = (a_{ij})$ a $\mathbf{B} = (b_{ij})$ typu $(m, n)$ je matice $\mathbf{C} = \mathbf{A} + \mathbf{B}$, kde $c_{ij} = a_{ij} + b_{ij}$ — sčítáme po složkách.
Násobek matice $\mathbf{A} = (a_{ij})$ skalárem $\alpha \in \mathbb{R}$ je matice $\alpha \mathbf{A}$, kde každý prvek vynásobíme číslem $\alpha$: $(\alpha \mathbf{A})_{ij} = \alpha \cdot a_{ij}$.
Součin matic
Součin matice $\mathbf{A} = (a_{ik})$ typu $(m, r)$ a matice $\mathbf{B} = (b_{kj})$ typu $(r, n)$ je matice $\mathbf{C} = \mathbf{A} \cdot \mathbf{B}$ typu $(m, n)$, jejíž prvky jsou:
$$c_{ij} = \sum_{k=1}^{r} a_{ik} \cdot b_{kj} = a_{i1}b_{1j} + a_{i2}b_{2j} + \cdots + a_{ir}b_{rj}$$Prvek $c_{ij}$ vzniká jako skalární součin $i$-tého řádku matice $\mathbf{A}$ a $j$-tého sloupce matice $\mathbf{B}$.
Součin $\mathbf{A} \cdot \mathbf{B}$ existuje, pouze když počet sloupců $\mathbf{A}$ = počet řádků $\mathbf{B}$:
$$\underset{(m \times \boldsymbol{r})}{\mathbf{A}} \cdot \underset{(\boldsymbol{r} \times n)}{\mathbf{B}} = \underset{(m \times n)}{\mathbf{C}}$$Například: matice $(2,3) \cdot$ matice $(3,1)$ = matice $(2,1)$ — lze. Ale matice $(3,1) \cdot$ matice $(2,3)$ — nelze!
Obecně platí $\mathbf{A} \cdot \mathbf{B} \neq \mathbf{B} \cdot \mathbf{A}$! Dokonce i když oba součiny existují, výsledky se mohou lišit. Násobení matic je však asociativní: $(\mathbf{A} \cdot \mathbf{B}) \cdot \mathbf{C} = \mathbf{A} \cdot (\mathbf{B} \cdot \mathbf{C})$.
Transponovaná matice
Matice transponovaná k matici $\mathbf{A}$ typu $(m,n)$ je matice $\mathbf{A}^T$ typu $(n,m)$, která vznikne záměnou řádků a sloupců: $(a^T)_{ij} = a_{ji}$.
Příklad: Je-li $\mathbf{A} = \begin{pmatrix} 4 & 3 & 0 \\ 7 & 0 & 5 \end{pmatrix}$, pak $\mathbf{A}^T = \begin{pmatrix} 4 & 7 \\ 3 & 0 \\ 0 & 5 \end{pmatrix}$.
Mocniny matic
Pro čtvercovou matici $\mathbf{A}$ řádu $n$ definujeme:
$$\mathbf{A}^0 = \mathbf{E}_n, \quad \mathbf{A}^1 = \mathbf{A}, \quad \mathbf{A}^2 = \mathbf{A} \cdot \mathbf{A}, \quad \mathbf{A}^k = \underbrace{\mathbf{A} \cdot \mathbf{A} \cdots \mathbf{A}}_{k\text{-krát}}$$Inverzní matice
Matice inverzní ke čtvercové matici $\mathbf{A}$ řádu $n$ je taková matice $\mathbf{A}^{-1}$, pro kterou platí:
$$\mathbf{A} \cdot \mathbf{A}^{-1} = \mathbf{A}^{-1} \cdot \mathbf{A} = \mathbf{E}_n$$Inverzní matice existuje, právě když je matice $\mathbf{A}$ regulární (tj. $\det \mathbf{A} \neq 0$).
Pro matici $\mathbf{A} = \begin{pmatrix} a & b \\ c & d \end{pmatrix}$, kde $ad - bc \neq 0$, platí:
$$\mathbf{A}^{-1} = \frac{1}{ad - bc} \begin{pmatrix} d & -b \\ -c & a \end{pmatrix}$$Výraz $ad - bc$ se nazývá determinant matice $\mathbf{A}$ (podrobněji v Tématu 3).
Inverzní matici ke čtvercové matici $\mathbf{A}$ řádu $n$ najdeme tak, že sestavíme rozšířenou matici $(\mathbf{A} \mid \mathbf{E}_n)$ a řádkovými úpravami ji převedeme na tvar $(\mathbf{E}_n \mid \mathbf{A}^{-1})$:
$$(\mathbf{A} \mid \mathbf{E}_n) \xrightarrow{\text{GEM}} (\mathbf{E}_n \mid \mathbf{A}^{-1})$$Pokud se na levé straně nepodaří vytvořit jednotkovou matici, matice $\mathbf{A}$ nemá inverzi (je singulární).
Řešení maticových rovnic
Rovnici $\mathbf{A} \cdot \mathbf{X} = \mathbf{B}$ vyřešíme vynásobením zleva inverzní maticí:
$$\mathbf{A} \cdot \mathbf{X} = \mathbf{B} \quad \Longrightarrow \quad \mathbf{X} = \mathbf{A}^{-1} \cdot \mathbf{B}$$Pozor: protože násobení matic není komutativní, musíme násobit ze správné strany! U rovnice $\mathbf{X} \cdot \mathbf{A} = \mathbf{B}$ násobíme zprava: $\mathbf{X} = \mathbf{B} \cdot \mathbf{A}^{-1}$.
Pro operaci inverze matic platí:
- $(\mathbf{A}^{-1})^{-1} = \mathbf{A}$
- $(\mathbf{A} \cdot \mathbf{B})^{-1} = \mathbf{B}^{-1} \cdot \mathbf{A}^{-1}$ (pozor na pořadí!)
- $\mathbf{E}_n^{-1} = \mathbf{E}_n$
Řešené příklady
Jsou dány matice $\mathbf{A} = \begin{pmatrix} 2 & -3 \\ -1 & 1 \end{pmatrix}$ a $\mathbf{B} = \begin{pmatrix} 2 & 3 \\ 4 & 5 \end{pmatrix}$. Vypočtěte $\mathbf{A} + \mathbf{B}$ a $-5 \cdot \begin{pmatrix} 1 & -2 \\ 0 & 2 \end{pmatrix}$.
Sčítáme po složkách (obě matice jsou typu $2 \times 2$):
$$\mathbf{A} + \mathbf{B} = \begin{pmatrix} 2 & -3 \\ -1 & 1 \end{pmatrix} + \begin{pmatrix} 2 & 3 \\ 4 & 5 \end{pmatrix} = \begin{pmatrix} 2+2 & -3+3 \\ -1+4 & 1+5 \end{pmatrix} = \begin{pmatrix} 4 & 0 \\ 3 & 6 \end{pmatrix}$$Každý prvek vynásobíme číslem $-5$:
$$-5 \cdot \begin{pmatrix} 1 & -2 \\ 0 & 2 \end{pmatrix} = \begin{pmatrix} -5 \cdot 1 & -5 \cdot (-2) \\ -5 \cdot 0 & -5 \cdot 2 \end{pmatrix} = \begin{pmatrix} -5 & 10 \\ 0 & -10 \end{pmatrix}$$Spočtěte součiny $\mathbf{A} \cdot \mathbf{B}$ a $\mathbf{B} \cdot \mathbf{A}$ pro matice $\mathbf{A} = \begin{pmatrix} 1 & 2 \\ 3 & 4 \end{pmatrix}$ a $\mathbf{B} = \begin{pmatrix} 5 & 4 \\ 6 & 11 \end{pmatrix}$. Platí $\mathbf{A} \cdot \mathbf{B} = \mathbf{B} \cdot \mathbf{A}$?
Prvek na pozici $(i,j)$ = skalární součin $i$-tého řádku $\mathbf{A}$ a $j$-tého sloupce $\mathbf{B}$:
$$c_{11} = 1 \cdot 5 + 2 \cdot 6 = 5 + 12 = 17$$ $$c_{12} = 1 \cdot 4 + 2 \cdot 11 = 4 + 22 = 26$$ $$c_{21} = 3 \cdot 5 + 4 \cdot 6 = 15 + 24 = 39$$ $$c_{22} = 3 \cdot 4 + 4 \cdot 11 = 12 + 44 = 56$$ $$\mathbf{A} \cdot \mathbf{B} = \begin{pmatrix} 17 & 26 \\ 39 & 56 \end{pmatrix}$$V tomto výjimečném případě $\mathbf{A} \cdot \mathbf{B} = \mathbf{B} \cdot \mathbf{A}$. Obecně to ale neplatí!
Spočtěte $\mathbf{A} \cdot \mathbf{B}$, kde
$$\mathbf{A} = \begin{pmatrix} 2 & 3 & 0 \\ 0 & 2 & 1 \end{pmatrix}, \quad \mathbf{B} = \begin{pmatrix} 5 & 3 \\ 0 & 1 \\ -1 & 4 \end{pmatrix}$$$\mathbf{A}$ je typu $(2, 3)$, $\mathbf{B}$ je typu $(3, 2)$. Počet sloupců $\mathbf{A}$ = 3 = počet řádků $\mathbf{B}$ — součin existuje a bude typu $(2, 2)$.
Určete inverzní matici k matici $\mathbf{A} = \begin{pmatrix} 3 & 4 \\ 5 & 7 \end{pmatrix}$ pomocí vzorce pro 2×2 matici.
Determinant je nenulový, takže inverzní matice existuje.
Najděte inverzní matici k matici $\mathbf{A} = \begin{pmatrix} 0 & 0 & 1 \\ 1 & 0 & 0 \\ 0 & 1 & 0 \end{pmatrix}$ metodou $(A \mid E) \to (E \mid A^{-1})$.
Na levé straně je již jednotková matice!
Najděte inverzní matici k $\mathbf{B} = \begin{pmatrix} 0 & -2 & 0 \\ 0 & 0 & 4 \\ 0 & 0 & 1/4 \end{pmatrix}$.
V prvním sloupci jsou samé nuly — matice nemá v prvním sloupci žádný nenulový prvek pro pivot. To znamená, že při eliminaci nemůžeme dostat na pozici $(1,1)$ jedničku bez pomoci řádkových výměn. Jenže i po jakékoli výměně řádků bude první sloupec stále celý nulový.
Matice s celým nulovým sloupcem je singulární — její determinant je nulový.
Inverzní matice neexistuje.
Určete matici $\mathbf{X}$, pro kterou platí $\begin{pmatrix} 1 & 2 \\ 3 & 4 \end{pmatrix} \cdot \mathbf{X} = \begin{pmatrix} 3 & 5 \\ 5 & 9 \end{pmatrix}$.
Označme $\mathbf{A} = \begin{pmatrix} 1 & 2 \\ 3 & 4 \end{pmatrix}$ a $\mathbf{B} = \begin{pmatrix} 3 & 5 \\ 5 & 9 \end{pmatrix}$. Pak $\mathbf{A} \cdot \mathbf{X} = \mathbf{B}$, tedy $\mathbf{X} = \mathbf{A}^{-1} \cdot \mathbf{B}$.
Determinant: $\det \mathbf{A} = 1 \cdot 4 - 2 \cdot 3 = 4 - 6 = -2$.
$$\mathbf{A}^{-1} = \frac{1}{-2} \begin{pmatrix} 4 & -2 \\ -3 & 1 \end{pmatrix} = \begin{pmatrix} -2 & 1 \\ \frac{3}{2} & -\frac{1}{2} \end{pmatrix}$$Cvičení k procvičení
Cvičení 1: Řešte maticovou rovnici $\mathbf{A} - 2\mathbf{X} - \mathbf{B} = 3\mathbf{E}_2$, kde $\mathbf{A} = \begin{pmatrix} 2 & -3 \\ -1 & 1 \end{pmatrix}$, $\mathbf{B} = \begin{pmatrix} 2 & 3 \\ 4 & 5 \end{pmatrix}$.
$\mathbf{X} = \frac{1}{2}(\mathbf{A} - \mathbf{B} - 3\mathbf{E}_2) = \begin{pmatrix} -3/2 & -3 \\ -5/2 & -7/2 \end{pmatrix}$.
Cvičení 2: Určete součiny matic, pokud existují:
a) $\begin{pmatrix} 0 & -1 \\ 0 & 1 \end{pmatrix} \cdot \begin{pmatrix} 2 & 3 \\ 0 & 2 \end{pmatrix}$; b) $\begin{pmatrix} 2 & 3 & 0 \\ 0 & 2 & 1 \end{pmatrix} \cdot \begin{pmatrix} 0 & 2 \\ 2 & 3 \\ 1 & 0 \end{pmatrix}$; c) $\begin{pmatrix} 2 & 3 & 1 \\ 4 & 5 & 0 \end{pmatrix} \cdot \begin{pmatrix} 0 & 1 \\ -1 & 0 \end{pmatrix}$.
a) $\begin{pmatrix} 0 & -2 \\ 0 & 2 \end{pmatrix}$; b) $\begin{pmatrix} 6 & 13 \\ 5 & 6 \end{pmatrix}$; c) nelze definovat (rozměry nesedí: $(2 \times 3) \cdot (2 \times 2)$).
Cvičení 3: Určete součiny $\mathbf{A} \cdot \mathbf{B}$ a $\mathbf{B} \cdot \mathbf{A}$ a ověřte, zda platí $\mathbf{A} \cdot \mathbf{B} = \mathbf{B} \cdot \mathbf{A}$:
$\mathbf{A} = \begin{pmatrix} 2 & 5 \\ -1 & 2 \end{pmatrix}$, $\mathbf{B} = \begin{pmatrix} 3 & -5 \\ 1 & 2 \end{pmatrix}$.
$\mathbf{A} \cdot \mathbf{B} = \begin{pmatrix} 11 & 0 \\ -1 & 9 \end{pmatrix}$, $\mathbf{B} \cdot \mathbf{A} = \begin{pmatrix} 11 & 5 \\ 0 & 9 \end{pmatrix}$. Neplatí $\mathbf{A} \cdot \mathbf{B} = \mathbf{B} \cdot \mathbf{A}$.
Cvičení 4: Spočtěte $\mathbf{A} \cdot \mathbf{A}^T$ a $\mathbf{A}^T \cdot \mathbf{A}$ pro $\mathbf{A} = \begin{pmatrix} 4 & 3 & 0 \\ 7 & 0 & 5 \end{pmatrix}$.
$\mathbf{A} \cdot \mathbf{A}^T = \begin{pmatrix} 25 & 28 \\ 28 & 74 \end{pmatrix}$, $\mathbf{A}^T \cdot \mathbf{A} = \begin{pmatrix} 65 & 12 & 35 \\ 12 & 9 & 0 \\ 35 & 0 & 25 \end{pmatrix}$.
Cvičení 5: Určete mocniny matice $\mathbf{A} = \begin{pmatrix} a & b \\ 0 & c \end{pmatrix}$. Spočtěte $\mathbf{A}^2$ a $\mathbf{A}^3$.
$\mathbf{A}^2 = \begin{pmatrix} a^2 & ab + bc \\ 0 & c^2 \end{pmatrix}$, $\mathbf{A}^3 = \begin{pmatrix} a^3 & a^2b + abc + bc^2 \\ 0 & c^3 \end{pmatrix}$.
Cvičení 6: Určete inverzní matici k $\mathbf{A} = \begin{pmatrix} 2 & 0 \\ -1 & -2 \end{pmatrix}$ pomocí vzorce pro 2×2.
$\det \mathbf{A} = -4$, $\mathbf{A}^{-1} = -\frac{1}{4}\begin{pmatrix} -2 & 0 \\ 1 & 2 \end{pmatrix} = \begin{pmatrix} 1/2 & 0 \\ -1/4 & -1/2 \end{pmatrix}$.
Cvičení 7: Určete inverzní matici pomocí GEM k matici $\mathbf{A} = \begin{pmatrix} 5 & 0 & 0 \\ 0 & -2 & 0 \\ 0 & 0 & 4 \end{pmatrix}$.
$\mathbf{A}^{-1} = \begin{pmatrix} 1/5 & 0 & 0 \\ 0 & -1/2 & 0 \\ 0 & 0 & 1/4 \end{pmatrix}$.
Cvičení 8: Určete matici $\mathbf{X}$, pro kterou platí $\begin{pmatrix} 3 & -1 \\ 5 & -2 \end{pmatrix} \cdot \mathbf{X} \cdot \begin{pmatrix} 5 & 6 \\ 7 & 8 \end{pmatrix} = \begin{pmatrix} 14 & 16 \\ 9 & 10 \end{pmatrix}$.
$\mathbf{X} = \begin{pmatrix} 1 & 2 \\ 3 & 4 \end{pmatrix}$.
Cvičení 9: Určete sloupcový vektor $\vec{x}$ řešení soustavy $\mathbf{A} \cdot \vec{x} = \vec{b}$ pomocí inverzní matice $\mathbf{A}^{-1}$:
$\begin{pmatrix} 5 & -8 \\ 5 & 4 \end{pmatrix} \cdot \vec{x} = \begin{pmatrix} 0 \\ 6 \end{pmatrix}$.
$\vec{x} = \mathbf{A}^{-1}\vec{b} = \begin{pmatrix} 0{,}8 \\ 0{,}5 \end{pmatrix}$.
Shrnutí
Klíčové vzorce a pojmy
- Matice typu $(m, n)$: $m$ řádků, $n$ sloupců
- Součet: $(\mathbf{A}+\mathbf{B})_{ij} = a_{ij} + b_{ij}$ (jen matice stejného typu)
- Násobení skalárem: $(\alpha\mathbf{A})_{ij} = \alpha \cdot a_{ij}$
- Součin: $(\mathbf{A}\mathbf{B})_{ij} = \sum_k a_{ik}b_{kj}$ (počet sloupců $\mathbf{A}$ = počet řádků $\mathbf{B}$)
- Transpozice: $(\mathbf{A}^T)_{ij} = a_{ji}$
- Inverze 2×2: $\mathbf{A}^{-1} = \frac{1}{ad-bc}\begin{pmatrix} d & -b \\ -c & a \end{pmatrix}$
- Inverze pomocí GEM: $(\mathbf{A} \mid \mathbf{E}) \xrightarrow{\text{GEM}} (\mathbf{E} \mid \mathbf{A}^{-1})$
- Maticová rovnice: $\mathbf{A}\mathbf{X} = \mathbf{B} \Rightarrow \mathbf{X} = \mathbf{A}^{-1}\mathbf{B}$
- Předpokládáš, že $\mathbf{A}\mathbf{B} = \mathbf{B}\mathbf{A}$ — obecně neplatí!
- Násobíš matice s nekompatibilními rozměry
- Při inverzi maticové rovnice násobíš ze špatné strany
- Zapomeneš zkontrolovat, zda je determinant nenulový před výpočtem inverze