przetlumacz ten dokument latex na jezyk polski

\documentclass{article}
\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{enumitem}

\title{R Linear Algebra Cheatsheet}
\author{}
\date{}

\begin{document}
\maketitle

\section*{Solving Linear Equations}
To solve a system of linear equations Ax = b
:

\begin{verbatim}
# Define matrix A and vector b
A = matrix(c(…), nrow=…, ncol=…)
b = c(…)

# Solve for x
x = solve(A, b)
\end{verbatim}

\section*{Matrix Decompositions}
\begin{enumerate}[label=\textbf{\arabic*.}]
    \item \textbf{LU Decomposition:} Used when A is a square matrix and det != 0.
\begin{verbatim}
# LU decomposition
# A = LU
# det(A) != 0 && nrow(A) == ncol(A)
# 1.110223e-14 == 0
# library(pracma)

L = lu(A)$L
U = lu(A)$U

y = forwardsolve(L, b)
solve(U, y)
\end{verbatim}

    \item \textbf{Cholesky Decomposition:} Used when A is a square symmetric, positive-definite matrix.
\begin{verbatim}
# Cholesky decomposition
# A = Lt(L)
# nrow(A) == ncol(A) && all(A == t(A)) && all(eigen(A)$values > 0)

C = chol(A)
C # gornotrojkątna
tC = t(C) # dolnotrojkatna

y = forwardsolve(tC, b)
x = backsolve(C, y)
\end{verbatim}

    \item \textbf{QR Decomposition:} Used for full-rank matrices, rectangular and square.
\begin{verbatim}
# QR decomposition
# qr(A)$rank == ncol(A)
# A = QR

QR = qr(A)
# simpler, but doesn't demonstrate our understanding = solve.qr(QR, b)

Q = qr.Q(QR)
R = qr.R(QR)

x = backsolve(R, t(Q) %*% b)
\end{verbatim}

    \item \textbf{Singular Value Decomposition (SVD):} Used for general matrix decompositions, e.g. not full-rank rectangular matrices.
\begin{verbatim}
# SVD
# A = UEV*
a_svd = svd(A)

a_diag = diag(1/a_svd$d)
a_svd$v %*% a_diag %*% t(a_svd$u) %*% b
\end{verbatim}
\end{enumerate}

\section*{Vector and Matrix Norms}

\begin{enumerate}[label=\textbf{\arabic*.}]
    \item \textbf{Vector Norms:}
\begin{verbatim}
# Norm 1
l1_norm = sum(abs(v))

# Norm 2
l2_norm = sqrt(sum(v^2))

# Maximum infinity norm
max_norm = max(abs(v))
\end{verbatim}

    \item \textbf{Matrix norms:}
\begin{verbatim}
# Spectral Norm2
norm_spectral = function(x) {
  return (sqrt(max(eigen(t(x) %*% x)$values)))
}

# Norm1
norm1 = function(x) {
  sum = apply(A, 2, sum)
  return (max(sum))
}

# Norm Infinity
norm_inf = function(x) {
  sum = apply(A, 1, sum)
  return (max(sum))
}

# Frobenius Af Norm
frob = function(A) {
    return (sum(diag(A %*% t(A))))
}
# sum(trace(A %*% t(A)))
\end{verbatim}

\section*{Trace Function}
The trace of a square matrix (A) is the sum of its diagonal elements.

\begin{verbatim}
# Calculate trace
trace_A = sum(diag(A))
\end{verbatim}

\section*{Basic Matrix Algebra in R}
\begin{enumerate}[label=\textbf{\arabic*.}]
    \item \textbf{Matrix Creation:}
\begin{verbatim}
# Define matrices A and B
A = matrix(c(…), nrow=…, ncol=…)
B = matrix(c(…), nrow=…, ncol=…, byrow=TRUE)

matrix(c(1,2,3,4), 2)
>       [,1] [,2]
[1,]    1    3
[2,]    2    4

matrix(c(1,2,3,4), 2, byrow=TRUE)
>      [,1] [,2]
[1,]    1    2
[2,]    3    4
\end{verbatim}

    \item \textbf{Matrix Multiplication:}
\begin{verbatim}
# Matrix product
F = A %*% B
\end{verbatim}

    \item \textbf{Transpose:}
\begin{verbatim}
# Transpose of A
A_t = t(A)
\end{verbatim}

    \item \textbf{Inverse:}
\begin{verbatim}
# Inverse of A
A_inv = solve(A)
\end{verbatim}


    \item \textbf{Row, Column Operations:}
\begin{verbatim}
# Columns
norm1 = function(x) {
  cols = ncol(x)
  sum = 0
  sums = c()
  
  for (j in 1:cols) {
    sum = sum(abs(x[,j]))
    sums = c(sums, sum)
  }
  
  return (max(sums))
}

# Rows
norm_inf = function(x) {
  rows = nrow(x)
  sum = 0
  sums = c()
  
  for (i in 1:rows) {
    sum = sum(abs(x[i,]))
    sums = c(sums, sum)  
  }
  
  return (max(sums))
}
\end{verbatim}

\end{enumerate}

\end{document}