Write me a cheatsheet for an R linear algebra exam, where I'll have an exercise involving solving various linear equations, and I'll have to know what matrix decomposition is applicable (LU, Cholesky, QR, SVD). I'll also have to implement various vector and matrix norms and also implement the trace function. Some basic matrix algebra in R would also be useful. Write is as a LaTeX document.
Assistant
Certainly! Below is a LaTeX document for a cheatsheet that you might find useful for your R linear algebra exam. The document covers solving linear equations, matrix decompositions, various vector and matrix norms, the trace function, and basic matrix algebra in R.
latex
\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.
\begin{verbatim}
# LU decomposition
library(MASS)
lu <- lu(A)
L <- lu$L
U <- lu$U
\end{verbatim}
\item \textbf{Cholesky Decomposition:} Used when $A$ is a symmetric, positive-definite matrix.
\begin{verbatim}
# Cholesky decomposition
C <- chol(A)
\end{verbatim}
\item \textbf{QR Decomposition:} Used for least squares problems.
\begin{verbatim}
# QR decomposition
qr <- qr(A)
Q <- qr.Q(qr)
R <- qr.R(qr)
\end{verbatim}
\item \textbf{Singular Value Decomposition (SVD):} Used for general matrix decompositions.
\begin{verbatim}
# SVD
svd <- svd(A)
U <- svd$u
S <- diag(svd$d)
V <- svd$v
\end{verbatim}
\end{enumerate}
\section*{Vector and Matrix Norms}
\begin{enumerate}[label=\textbf{\arabic*.}]
\item \textbf{Vector Norms:}
\begin{verbatim}
# L1 norm
l1_norm <- sum(abs(v))
# L2 norm
l2_norm <- sqrt(sum(v^2))
# Maximum norm
max_norm <- max(abs(v))
\end{verbatim}
\item \textbf{Matrix Norms:}
\begin{verbatim}
# Frobenius norm
fro_norm <- norm(A, type = "F")
# Maximum absolute column sum norm (L1 norm)
max_col_sum_norm <- norm(A, type = "O")
# Spectral norm (L2 norm)
spectral_norm <- norm(A, type = "2")
\end{verbatim}
\end{enumerate}
\section*{Trace Function}
The trace of a square matrix \( A \) is the sum of its diagonal elements.
\begin{verbatim}
# Define matrix A
A <- matrix(c(...), nrow=..., ncol=...)
# Calculate trace
trace_A <- sum(diag(A))
\end{verbatim}
\section*{Basic Matrix Algebra in R}
\begin{enumerate}[label=\textbf{\arabic*.}]
\item \textbf{Matrix Addition/Subtraction:}
\begin{verbatim}
# Define matrices A and B
A <- matrix(c(...), nrow=..., ncol=...)
B <- matrix(c(...), nrow=..., ncol=...)
# Addition
C <- A + B
# Subtraction
D <- A - B
\end{verbatim}
\item \textbf{Matrix Multiplication:}
\begin{verbatim}
# Element-wise multiplication
E <- A * B
# 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}
\end{enumerate}
\end{document}
This document should provide you with a quick reference for the most important concepts and R code snippets you'll need for your exam. Good luck!