---
title: "MML Book Exercises rešavanje"
author: "Zoran Ovcin"
date: "2025-12-17"
output: html_document
---

\def\R{\mathbb{R}}
\def\Z{\mathbb{Z}}

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```

Ovde rešavamo zadatke iz [MML Book](https://mml-book.github.io/)

## MML-book Chapter 4. Matrix Decompositions, Exercises 

Koristićemo R i **matlib** biblioteku.
```{r}
library(matlib)
```
Tekst je u formatu Rmd (R markdown) i može se knit aplikacijom pretvoriti u html (ili pdf) dokument.

### 4.1
$\begin{vmatrix} 1 & 3 & 5\\ 2 & 4 & 6\\ 0 & 2 & 4\\ \end{vmatrix} = 1 \cdot \begin{vmatrix}  4 & 6\\ 2 & 4\\ \end{vmatrix} -3 \cdot \begin{vmatrix}  2 & 6\\ 0 & 4\\ \end{vmatrix} + 5 \cdot \begin{vmatrix}  2 & 4\\ 0 & 2\\ \end{vmatrix} =$
$4 \cdot 4 - 2 \cdot 6 - 3 \cdot  ( 2 \cdot 4  -0 \cdot 6 ) + 5 \cdot ( 2 \cdot 2 - 0 \cdot 4 ) = 0$

```{r E04.01}
#########
#  4.1  #
#########

A = matrix(c(1,3,5,2,4,6,0,2,4),ncol = 3, byrow= "T"); det(A)
```
### 4.2
Razvićemo po drugoj koloni: 
$\begin{vmatrix}
 2 &  0 &  1 &  2 &  0\\
 2 & -1 &  0 &  1 &  1\\
 0 &  1 &  2 &  1 &  2\\
-2 &  0 &  2 & -1 &  2\\
 2 &  0 &  0 &  1 &  1\\
\end{vmatrix} =
-1 \cdot
\begin{vmatrix}
 2 &  1 &  2 &  0\\
 0 &  2 &  1 &  2\\
-2 &  2 & -1 &  2\\
 2 &  0 &  1 &  1\\
\end{vmatrix} 
- 1 \cdot
\begin{vmatrix}
 2 &  1 &  2 &  0\\
 2 &  0 &  1 &  1\\
-2 &  2 & -1 &  2\\
 2 &  0 &  1 &  1\\
\end{vmatrix} = \cdots = 6$
```{r E04.02}
#########
#  4.2  #
#########

A = matrix(c(2,0,1,2,0,2,-1,0,1,1,0,1,2,1,2,-2,0,2,-1,2,2,0,0,1,1),ncol = 5);  det(A)
```
### 4.3
a. $\begin{bmatrix} 1 & 0 \\ 1 & 1  \end{bmatrix}  \begin{bmatrix} x_1 \\ x_2  \end{bmatrix} = \lambda \begin{bmatrix} x_1 \\ x_2  \end{bmatrix} \Leftrightarrow \begin{bmatrix} 1-\lambda & 0 \\ 1 & 1-\lambda  \end{bmatrix}  \begin{bmatrix} x_1 \\ x_2  \end{bmatrix} = \begin{bmatrix} 0 \\ 0  \end{bmatrix} \Leftrightarrow \begin{vmatrix} 1-\lambda & 0 \\ 1 & 1-\lambda  \end{vmatrix}  = (1 - \lambda )^2 = 0 \Leftrightarrow \lambda_{1,2} = 1$ i
$\begin{bmatrix} 0 & 0 \\ 1 & 0  \end{bmatrix}  \begin{bmatrix} x_1 \\ x_2  \end{bmatrix} = \begin{bmatrix} 0 \\ 0  \end{bmatrix} \Leftrightarrow x_1 = 0$ što daje jednodimenzionalni prostor koji odgovara $\lambda = 1: E_{1} = \mbox{span} ( [0, 1]^T )$.
b.  $\begin{bmatrix} -2 & 2 \\ 2 & 1  \end{bmatrix}  \begin{bmatrix} x_1 \\ x_2  \end{bmatrix} = \lambda \begin{bmatrix} x_1 \\ x_2  \end{bmatrix} \Leftrightarrow \begin{bmatrix} -2-\lambda & 2 \\ 2 & 1-\lambda  \end{bmatrix}  \begin{bmatrix} x_1 \\ x_2  \end{bmatrix} = \begin{bmatrix} 0 \\ 0  \end{bmatrix} \Leftrightarrow \begin{vmatrix} -2-\lambda & 2 \\ 2 & 1-\lambda \end{vmatrix}  = \lambda^2 + \lambda - 6 = 0$<br>
Dobijamo $\lambda = 2 \vee \lambda = -3$.<br>
Za $\lambda = 2$:
$\begin{bmatrix} -4 & 2 \\ 2 & -1 \end{bmatrix}  \begin{bmatrix} x_1 \\ x_2  \end{bmatrix} = \begin{bmatrix} 0 \\ 0  \end{bmatrix} \Leftrightarrow x_2 = 2 x_1$ što daje jednodimenzionalni prostor $E_{2} = \mbox{span} ( [1, 2]^T )$.<br>
Za $\lambda = -3$:
$\begin{bmatrix} 1 & 2 \\ 2 & 4 \end{bmatrix}  \begin{bmatrix} x_1 \\ x_2  \end{bmatrix} = \begin{bmatrix} 0 \\ 0  \end{bmatrix} \Leftrightarrow x_1 = - 2 x_2$ što daje jednodimenzionalni prostor $E_{-3} = \mbox{span} ( [-2, 1]^T )$.
```{r E04.03b}
#########
#  4.3  #
#########

# b
A = matrix(c(-2,2,2,1),ncol = 2, byrow = "T"); A
eigen(A)->ea; ea
round(ea$vectors %*% diag(-c(sqrt(5),-sqrt(5))),digits=8)
```
### 4.4
```{r E04.04}
#########
#  4.4  #
#########

A = matrix(c(0,-1,1,1,-1,1,-2,3,2,-1,0,0,1,-1,1,0),ncol = 4, byrow = "T"); A
eigen(A)->ea; ea
zapsmall(ea$values)
lambda=Re(ea$values); zapsmall(lambda)
vecs=Re(ea$vectors); zapsmall(vecs)
vecs=vecs %*% diag(c(sqrt(3),2,-sqrt(2),-sqrt(2)))
round(vecs,digits=8)
```
Vidimo da su karakteristične vrednosti i karakteristični prostori koji im odgovaraju:<br>
$\lambda = 2$, $E_{2} = \mbox{span} ( [1,0,1,1]^T )$,<br>
$\lambda = 1$, $E_{1} = \mbox{span} ( [1,1,1,1]^T )$,<br>
$\lambda = -1$ algebarski dvostruki, geometrijski jednostruki: $E_{-1} = \mbox{span} ( [0,1,1,0]^T )$.

### 4.5
* $\begin{bmatrix} 1 & 0 \\ 0 & 1  \end{bmatrix}^{-1} = \begin{bmatrix} 1 & 0 \\ 0 & 1  \end{bmatrix} = \mbox{diag} ( 1, 1 )$ invertibilna i dijagonalizabilna
* $\mbox{det} ( \begin{bmatrix} 1 & 0 \\ 0 & 0  \end{bmatrix} ) = 0$, $\begin{bmatrix} 1 & 0 \\ 0 & 0  \end{bmatrix} = \mbox{diag} ( 1, 0 )$, nije  invertibilna, jeste dijagonalizabilna
* $\begin{bmatrix} 1 & 1 \\ 0 & 1  \end{bmatrix}^{-1} = \begin{bmatrix} 1 & -1 \\ 0 & 1  \end{bmatrix}$<br>
$\mbox{det} ( \begin{bmatrix} 1-\lambda & 1 \\ 0 & 1-\lambda  \end{bmatrix} ) = 0 \Rightarrow \lambda = 1$ dvostruka nula.<br>
Za $\lambda = 1$:
$\begin{bmatrix} 0 & 1 \\ 0 & 0 \end{bmatrix}  \begin{bmatrix} x_1 \\ x_2  \end{bmatrix} = \begin{bmatrix} 0 \\ 0  \end{bmatrix} \Leftrightarrow x_2 = 0$ što daje jednodimenzionalni prostor $E_{1} = \mbox{span} ( [1, 0]^T )$.<br>
Jeste  invertibilna, nije dijagonalizabilna

* $\mbox{det} ( \begin{bmatrix} 0 & 1 \\ 0 & 0  \end{bmatrix} ) = 0$,<br>
$\mbox{det} ( \begin{bmatrix} -\lambda & 1 \\ 0 & -\lambda  \end{bmatrix} ) = 0 \Rightarrow \lambda = 0$ dvostruka nula.<br>
Za $\lambda = 0$:
$\begin{bmatrix} 0 & 1 \\ 0 & 0 \end{bmatrix}  \begin{bmatrix} x_1 \\ x_2  \end{bmatrix} = \begin{bmatrix} 0 \\ 0  \end{bmatrix} \Leftrightarrow x_2 = 0$ što daje jednodimenzionalni prostor $E_{0} = \mbox{span} ( [1, 0]^T )$.<br>
Nije  invertibilna, nije dijagonalizabilna

### 4.6
```{r E04.06a}
#########
#  4.6  #
#########

# a

A = matrix(c(2,1,0,3,4,0,0,3,1), ncol = 3); A
ev = eigen(A)
ev$values
ev$vectors
zapsmall(ev$vectors)
round(matrix(ev$vectors,ncol=3) %*% diag(-c(sqrt(2),sqrt(10),-sqrt(10))),digits=8)
```

a. Matrica $A$ nije dijagonazibilna. Sopstveni prostori koji odgovaraju karakterističnim vrednostima su:<br>
Za $\lambda = 5$: $E_5 = \mbox{span} ( [1, 1, 0 ]^T)$ i <br>
Za $\lambda = 1$ dvostruki algebarski: jednostruki geometrijski $E_1 = \mbox{span} ( [3, -1, 0 ]^T)$<br>

```{r E04.06b}
#########
#  4.6  #
#########

# b

A = matrix(c(1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0), ncol = 4, byrow = T ); A
ev=eigen(A)
round(ev$values, digits = 8)
round(ev$vectors, digits = 8)
evx = round(ev$vectors %*% diag(c(1,-sqrt(2),1,1)), digits = 8); evx
evx1 = solve(evx); round(evx1, digits = 8 )
evx %*% evx1
evx1 %*% diag(c(1,0,0,0)) %*% evx
```

b.  Matrica $A$ je dijagonazibilna. Sopstveni prostori koji odgovaraju karakterističnim vrednostima su:<br>
Za $\lambda = 1$: $E_5 = \mbox{span} ( [1, 0, 0, 0 ]^T)$ i <br>
Za $\lambda = 0$ trostruki algebarski i trostruki geometrijski: $E_0 = \mbox{span} ( [1, -1, 0, 0 ]^T, [ 0, 0, 1, 0 ]^T, [ 0, 0, 0, 1 ]^T )$<br>

### 4.7
```{r E04.07a}
#########
#  4.7  #
#########

# a

A = matrix(c(0,-8,1,4), ncol = 2); A
ev=eigen(A);
round(ev$values, digits = 8)
```
a. Matrica $A$ nije dijagolizabilna (nad $\R$) jer nema korene nad skupom $\R$.

```{r E04.07b}
#########
#  4.7  #
#########

# b

A = matrix(c(1,1,1,1,1,1,1,1,1), ncol = 3); A
ev=eigen(A)
round(ev$values, digits = 8)
round(ev$vectors, digits = 8)
p=round(ev$vectors %*% diag(c(-sqrt(3),sqrt(6),sqrt(2))), digits = 8); p
p1=solve(p); p1
zapsmall(6*p1)
p %*% diag(c(3,0,0))%*% p1
```
b. Matrica $A$ je dijagolizabilna:<br>
$\begin{bmatrix}
  1 & 1 & 1 \\ 
  1 & 1 & 1 \\ 
  1 & 1 & 1 \\ 
  \end{bmatrix} =
\begin{bmatrix}
  1 & 2 & 0 \\ 
  1 & -1 & -1 \\ 
  1 & -1 & 1 \\ 
  \end{bmatrix}
\begin{bmatrix}
  3 & 0 & 0 \\ 
  0 & 0 & 0 \\ 
  0 & 0 & 0 \\ 
\end{bmatrix} \frac16
\begin{bmatrix}
  2 & 2 & 2 \\ 
  2 & -1 & -1 \\ 
  0 & -3 & 3 \\ 
  \end{bmatrix}$,<br>
gde je
$\frac16
\begin{bmatrix}
  2 & 2 & 2 \\ 
  2 & -1 & -1 \\ 
  0 & -3 & 3 \\ 
  \end{bmatrix} = 
\begin{bmatrix}
  1 & 2 & 0 \\ 
  1 & -1 & -1 \\ 
  1 & -1 & 1 \\ 
  \end{bmatrix}^{-1}$.

```{r E04.07c}
#########
#  4.7  #
#########

# c

A = matrix(c(5,0,-1,1,4,1,-1,1,2,-1,3,-1,1,-1,0,2), ncol = 4); A
ev = eigen(A)
ev$values
ev$vectors
zapsmall(ev$vectors)
round(matrix(ev$vectors,ncol=4) %*% diag(c(-sqrt(3),sqrt(3),-sqrt(3),-sqrt(2))),digits=8)
```

c. Matrica $A$ nije dijagonazibilna. Sopstveni prostori koji odgovaraju karakterističnim vrednostima su:<br>
Za $\lambda = 4$ dvostruki algebarski: jednostruki geometrijski $E_4 = \mbox{span} ( [1, 0, -1, 1 ]^T)$,<br>
za $\lambda = 2$: $E_2 = \mbox{span} ( [ 1, 1, -1, 0, 1 ]^T)$ i <br>
za $\lambda = 1$: $E_1 = \mbox{span} ( [ 1, -1, 0, 0 ]^T)$.

```{r E04.07d}
#########
#  4.7  #
#########

# d

A = matrix(c(5,-1,3,-6,4,-6,-6,2,-4), ncol = 3); A
ev=eigen(A)
round(ev$values, digits = 8)
round(ev$vectors, digits = 8)
p=round(ev$vectors, digits = 8); p
p1=solve(p); p1
round(p %*% diag(c(2,2,1))%*% p1,digits=8)
```
d. Matrica $A$ je dijagolizabilna.

### 4.8
```{r E04.08}
#########
#  4.8  #
#########

A = matrix(c(3,2,2,3,2,-2), ncol = 3); A
svda = svd(A, nu=2, nv = 3); svda
U = svda$u
S = 0*A
for(k in 1:min(dim(A))) {S[k,k] = svda$d[k]}
V = svda$v
U%*% S %*% t(V)
zapsmall(V)
```
$\begin{bmatrix}
  2 & 3 & -2 \\ 
  3 & 2 & 2 \\ 
  \end{bmatrix} =
\begin{bmatrix}
  -\frac{\sqrt2}2 & -\frac{\sqrt2}2 \\ 
  -\frac{\sqrt2}2 &  \frac{\sqrt2}2 \\ 
\end{bmatrix}
\begin{bmatrix}
  5 & 0 & 0 \\ 
  0 & 3 & 0 \\ 
\end{bmatrix} 
\begin{bmatrix}
  -\frac{\sqrt2}2 & -\frac{\sqrt2}6  & -\frac23 \\ 
  -\frac{\sqrt2}2 &  \frac{\sqrt2}6  &  \frac23 \\ 
         0        & -\frac{2\sqrt2}3 &  \frac13 \\ 
\end{bmatrix}^T$

### 4.10
```{r E04.10}
#########
#  4.10 #
#########

U[,1] %*% t(V[,1])
zapsmall(5 * U[,1] %*% t(V[,1]))
```
$A_1 = u_1 v_1^T = 5\ 
\begin{bmatrix}
  -\frac{\sqrt2}2 \\ 
  -\frac{\sqrt2}2 \\ 
\end{bmatrix}
\begin{bmatrix}
  -\frac{\sqrt2}2 \\ 
  -\frac{\sqrt2}2 \\ 
         0        \\ 
\end{bmatrix}^T =
\begin{bmatrix}
  \frac52 & \frac52 & 0 \\ 
  \frac52 & \frac52 & 0 \\ 
\end{bmatrix}=
\begin{bmatrix}
  2.5 & 2.5 & 0 \\ 
  2.5 & 2.5 & 0 \\ 
\end{bmatrix}$

### 4.9
```{r E04.09}
#########
#  4.9  #
#########

A = matrix(c(2,-1,2,1), ncol = 2); A
svda = svd(A, nu=2, nv = 2); svda
U = svda$u
S = 0*A
for(k in 1:min(dim(A))) {S[k,k] = svda$d[k]}
V = svda$v
U%*% S %*% t(V)
zapsmall(U)
```
$\begin{bmatrix}
  2 & 2 \\ 
 -1 & 1 \\ 
\end{bmatrix} =
\begin{bmatrix}
  -1 & 0 \\ 
   0 & 1 \\ 
\end{bmatrix}
\begin{bmatrix}
  2 \sqrt2 & 0 \\ 
  0 & \sqrt2 \\ 
\end{bmatrix} 
\begin{bmatrix}
 -\frac{\sqrt2}2 & -\frac{\sqrt2}2 \\ 
 -\frac{\sqrt2}2 &  \frac{\sqrt2}2 \\ 
\end{bmatrix}^T$
