# Using LaTeX with WordPress

Updated 2017 November 17th

WordPress now supports LaTeX. To display LaTeX, just surround the code with "$latex" and "$", without the quotations. In this post, I show some formulae from this tutorial on SVD. I've shown the code for each formula; you can also mouseover the formula to see the code.

## Displaying a vector

\vec{x} = \begin{pmatrix}8\\6\\7\\5\\3\end{pmatrix} $\vec{x} = \begin{pmatrix}8\\6\\7\\5\\3\end{pmatrix}$

## Displaying a matrix

\begin{bmatrix} 17 & 18 & 5 & 5 & 45 & 1 \\ 42 & 28 & 30 & 15 & 115 & 3 \\ 10 & 10 & 10 & 21 & 51 & 2 \\ 28 & 5 & 65 & 39 & 132 & 5 \\ 24 & 26 & 45 & 21 & 116 & 4 \end{bmatrix} $\begin{bmatrix} 17 & 18 & 5 & 5 & 45 & 1 \\ 42 & 28 & 30 & 15 & 115 & 3 \\ 10 & 10 & 10 & 21 & 51 & 2 \\ 28 & 5 & 65 & 39 & 132 & 5 \\ 24 & 26 & 45 & 21 & 116 & 4 \end{bmatrix}$

A = \begin{bmatrix} a_{11} & \cdots & a_{1j} & \cdots & a_{1n} \\ \vdots & \ddots & \vdots & \ddots & \vdots \\ a_{i1} & \cdots & a_{ij} & \cdots & a_{in} \\ \vdots & \ddots & \vdots & \ddots & \vdots \\ a_{m1} & \cdots & a_{mj} & \cdots & a_{mn} \end{bmatrix} $A = \begin{bmatrix} a_{11} & \cdots & a_{1j} & \cdots & a_{1n} \\ \vdots & \ddots & \vdots & \ddots & \vdots \\ a_{i1} & \cdots & a_{ij} & \cdots & a_{in} \\ \vdots & \ddots & \vdots & \ddots & \vdots \\ a_{m1} & \cdots & a_{mj} & \cdots & a_{mn} \end{bmatrix}$

Above is an $m \times n$ matrix, where $a_{ij}$ are elements of the matrix $A$, $i$ = the $i_{th}$ row and $j$ = the $j_{th}$ column. The sequence of numbers:

A_{(i)} = (a_{i1}, \dotsc, a_{in}) $A_{(i)} = (a_{i1}, \dotsc, a_{in})$

is the $$i_{th}$$ row of A, and the sequence of numbers

A^{(j)} = (a_{1j}, \dotsc, a_{mj}) $A^{(j)} = (a_{1j}, \dotsc, a_{mj})$

is the $$j_{th}$$ column of A.

## Calculating vector length

The length of a vector is found by squaring each component, summing them, and taking the square root of the sum. If $\vec{v}$ is a vector, its length is ${\lvert}\vec{v}{\rvert}$.

{\lvert}\vec{v}{\rvert} = \sqrt{ \sum_{i=1}^{n}{x_i^2} } ${\lvert}\vec{v}{\rvert} = \sqrt{ \sum_{i=1}^{n}{x_i^2} }$

A = [a_{1}, a_{2}, \dotsc, a_{n}] $A = [a_{1}, a_{2}, \dotsc, a_{n}]$

B = [b_{1}, b_{2}, \dotsc, b_{n}] $B = [b_{1}, b_{2}, \dotsc, b_{n}]$

A + B = [a_{1} + b_{1}, a_{2} + b_{2}, \dotsc, a_{n} + b_{n}] $A + B = [a_{1} + b_{1}, a_{2} + b_{2}, \dotsc, a_{n} + b_{n}]$

## Scalar multiplication

Multiplying a scalar (real number) to a vector means multiplying every component by that real number to yield a new vector.

\vec{v} = [3, 6, 8, 4] \times 1.5 = [4.5, 9, 12, 6] $\vec{v} = [3, 6, 8, 4] \times 1.5 = [4.5, 9, 12, 6]$

## Inner product

The inner product of two vectors (also called the dot product or scalar product) defines multiplication of vectors. The inner product of two vectors is denoted $(\vec{v_{1}}, \vec{v_{2}})$ or $\vec{v_{1}} \cdot \vec{v_{2}}$.

(\vec{x}, \vec{y}) = \vec{x} \cdot \vec{y} = \sum_{i=1}^{n}{x_{i}y_{i}} $(\vec{x}, \vec{y}) = \vec{x} \cdot \vec{y} = \sum_{i=1}^{n}{x_{i}y_{i}}$

For example, if $\vec{x} = [1, 6, 7, 4]$ and $\vec{y} = [3, 2, 8, 3]$, then

\vec{x} \cdot \vec{y} = 1(3) + 6(2) + 7(8) + 3(4) = 83 $\vec{x} \cdot \vec{y} = 1(3) + 6(2) + 7(8) + 3(4) = 83$

## Orthogonality

Two vectors are orthogonal to each other if their inner product equals zero. For example, the vectors $[2, 1, -2, 4]$ and $[3, -6, 4, 2]$ are orthogonal because

[2, 1, -2, 4] \cdot [3, -6, 4, 2] = 2(3) + 1(-6) - 2(4) + 4(2) = 0 $[2, 1, -2, 4] \cdot [3, -6, 4, 2] = 2(3) + 1(-6) - 2(4) + 4(2) = 0$

## Normal vector

A normal vector (or unit vector) is a vector of length 1. If you divide each component by its vector length, you get the normal/unit vector.

If $\vec{v} = [2, 4, 1, 2]$, then

{\lvert}\vec{v}{\rvert} = \sqrt{2^2 + 4^2 + 1^2 + 2^2} = \sqrt{25} = 5 ${\lvert}\vec{v}{\rvert} = \sqrt{2^2 + 4^2 + 1^2 + 2^2} = \sqrt{25} = 5$

Then $\vec{u} = [2/5, 4/5, 1/5, 1/5]$ is a normal vector because

{\lvert}\vec{u}{\rvert} = \sqrt{(2/5)^2 + (4/5)^2 + (1/5)^2 + (2/5)^2} = \sqrt{25/25} = 1 ${\lvert}\vec{u}{\rvert} = \sqrt{(2/5)^2 + (4/5)^2 + (1/5)^2 + (2/5)^2} = \sqrt{25/25} = 1$

## Orthonormal vectors

Vectors of unit length that are orthogonal to each other are said to be orthonormal. For example

\vec{u} = [2/5, 1/5, -2/5, 4/5] $\vec{u} = [2/5, 1/5, -2/5, 4/5]$

and

\vec{v} = [3 / \sqrt{65}, -6 / \sqrt{65}, 4 / \sqrt{65}, 2 / \sqrt{65}] $\vec{v} = [3 / \sqrt{65}, -6 / \sqrt{65}, 4 / \sqrt{65}, 2 / \sqrt{65}]$

are orthonormal because

{\lvert}\vec{u}\rvert = \sqrt{(2/5)^2 + (1/5)^2 + (-2/5)^2 + (4/5)^2} = 1 ${\lvert}\vec{u}\rvert = \sqrt{(2/5)^2 + (1/5)^2 + (-2/5)^2 + (4/5)^2} = 1$

{\lvert}\vec{v}\rvert = \sqrt{(3 / \sqrt{65})^2 + (-6 / \sqrt{65})^2 + (4 / \sqrt{65})^2 + (2 / \sqrt{65})^2} = 1 ${\lvert}\vec{v}\rvert = \sqrt{(3 / \sqrt{65})^2 + (-6 / \sqrt{65})^2 + (4 / \sqrt{65})^2 + (2 / \sqrt{65})^2} = 1$

\vec{u} \cdot \vec{v} = \frac{6}{5\sqrt{65}} - \frac{6}{5\sqrt{65}} - \frac{8}{5\sqrt{65}} + \frac{8}{5\sqrt{65}} = 0 $\vec{u} \cdot \vec{v} = \frac{6}{5\sqrt{65}} - \frac{6}{5\sqrt{65}} - \frac{8}{5\sqrt{65}} + \frac{8}{5\sqrt{65}} = 0$

## Square matrix

A matrix is square when $m = n$. To designate the size of a square matrix with n rows and columns, it is called n-square. A 3-square matrix

A = \begin{bmatrix} 1 & 2 & 3 \\ 4 & 5 & 6 \\ 7 & 8 & 9 \end{bmatrix} $A = \begin{bmatrix} 1 & 2 & 3 \\ 4 & 5 & 6 \\ 7 & 8 & 9 \end{bmatrix}$

## Transpose

The transpose of matrix $A$ is $A^T$

A = \begin{bmatrix} 1 & 2 & 3 \\ 4 & 5 & 6 \end{bmatrix} $A = \begin{bmatrix} 1 & 2 & 3 \\ 4 & 5 & 6 \end{bmatrix}$

A^T = \begin{bmatrix} 1 & 4 \\ 2 & 5 \\ 3 & 6 \end{bmatrix} $A^T = \begin{bmatrix} 1 & 4 \\ 2 & 5 \\ 3 & 6 \end{bmatrix}$

## Matrix multiplication

It is possible to multiply two matrices only when the number of rows of the first matrix matches the number of columns of the second matrix.

AB = \begin{bmatrix} 2 & 1 & 4 \\ 1 & 5 & 2 \end{bmatrix} \begin{bmatrix} 3 & 2 \\ -1 & 4 \\ 1 & 2 \end{bmatrix} = \begin{bmatrix} 9 & 16 \\ 0 & 26 \end{bmatrix}

Here's the working $AB = \begin{bmatrix} 2 & 1 & 4 \\ 1 & 5 & 2 \end{bmatrix} \begin{bmatrix} 3 & 2 \\ -1 & 4 \\ 1 & 2 \end{bmatrix} = \begin{bmatrix} 9 & 16 \\ 0 & 26 \end{bmatrix}$ $ab_{11} = \begin{bmatrix} 2 & 1 & 4 \end{bmatrix} \begin{bmatrix} 3 \\ -1 \\ 1 \end{bmatrix} = 2(3) + 1(-1) + 4(1) = 9$ $ab_{12} = \begin{bmatrix} 2 & 1 & 4 \end{bmatrix} \begin{bmatrix} 2 \\ 4 \\ 2 \end{bmatrix} = 2(2) + 1(4) + 4(2) = 16$ $ab_{21} = \begin{bmatrix} 1 & 5 & 2 \end{bmatrix} \begin{bmatrix} 3 \\ -1 \\ 1 \end{bmatrix} = 1(3) + 5(-1) + 2(1) = 0$ $ab_{22} = \begin{bmatrix} 1 & 5 & 2 \end{bmatrix} \begin{bmatrix} 2 \\ 4 \\ 2 \end{bmatrix} = 1(2) + 5(4) + 2(2) = 26$

## Identity matrix

The identity matrix ( $I$) is a square matrix where all components are 0 expect for components on the diagonal, which are equal to 1. If you multiple a matrix with an identity matrix, you end up with the matrix.

AI = \begin{bmatrix}2 & 4 & 6 \\ 1 & 3 & 5 \end{bmatrix} \begin{bmatrix} 1 & 0 & 0 \\ 0 & 1 & 0 \\ 0 & 0 & 1 \end{bmatrix} = \begin{bmatrix}2 & 4 & 6 \\ 1 & 3 & 5 \end{bmatrix} $AI = \begin{bmatrix}2 & 4 & 6 \\ 1 & 3 & 5 \end{bmatrix} \begin{bmatrix} 1 & 0 & 0 \\ 0 & 1 & 0 \\ 0 & 0 & 1 \end{bmatrix} = \begin{bmatrix}2 & 4 & 6 \\ 1 & 3 & 5 \end{bmatrix}$

## Orthogonal matrix

Matrix $A$ is orthogonal if $AA^T = A^TA = I$. Matrix $A$ is a symmetric matrix since it is equal to its transpose.

A = \begin{bmatrix} 1 & 0 & 0 \\ 0 & 3/5 & -4/5 \\ 0 & 4/5 & 3/5 \end{bmatrix} $A = \begin{bmatrix} 1 & 0 & 0 \\ 0 & 3/5 & -4/5 \\ 0 & 4/5 & 3/5 \end{bmatrix}$

is orthogonal because

AA^T = \begin{bmatrix} 1 & 0 & 0 \\ 0 & 3/5 & -4/5 \\ 0 & 4/5 & 3/5 \end{bmatrix} \begin{bmatrix} 1 & 0 & 0 \\ 0 & 3/5 & 4/5 \\ 0 & -4/5 & 3/5 \end{bmatrix} = \begin{bmatrix} 1 & 0 & 0 \\ 0 & 1 & 0 \\ 0 & 0 & 1 \end{bmatrix} $AA^T = \begin{bmatrix} 1 & 0 & 0 \\ 0 & 3/5 & -4/5 \\ 0 & 4/5 & 3/5 \end{bmatrix} \begin{bmatrix} 1 & 0 & 0 \\ 0 & 3/5 & 4/5 \\ 0 & -4/5 & 3/5 \end{bmatrix} = \begin{bmatrix} 1 & 0 & 0 \\ 0 & 1 & 0 \\ 0 & 0 & 1 \end{bmatrix}$

## Diagonal matrix

A diagonal matrix $A$ is a square matrix where all the entries $a_{ij}$ are 0 when $i \neq j$.

A = \begin{bmatrix} a_{11} & 0 & 0 & 0 \\ 0 & a_{22} & 0 & 0 \\ 0 & 0 & a_{33} & 0 \\ 0 & 0 & 0 & a_{mm} \end{bmatrix} $A = \begin{bmatrix} a_{11} & 0 & 0 & 0 \\ 0 & a_{22} & 0 & 0 \\ 0 & 0 & a_{33} & 0 \\ 0 & 0 & 0 & a_{mm} \end{bmatrix}$

The trace of an $n \times n$ square matrix is defined to be the sum of the elements on the main diagonal.

tr(A) = \sum^{n}_{i=1} a_{ii} = a_{11} + a_{22} + \dots + a_{nn} $tr(A) = \sum^{n}_{i=1} a_{ii} = a_{11} + a_{22} + \dots + a_{nn}$

## Determinant

A determinant is a function of a square matrix that reduces it to a single number. The determinant of a matrix $A$ is denoted ${\lvert}A\rvert$ or $det(A)$. If $A$ consists of one element $a$, then ${\lvert}A\rvert = a$. If $A$ is a 2 x 2 matrix, then

{\lvert}A\rvert = \left| \begin{array}{cc} a & b \\ c & d \end{array} \right| = ad - bc ${\lvert}A\rvert = \left| \begin{array}{cc} a & b \\ c & d \end{array} \right| = ad - bc$

The determinant of

A = \begin{bmatrix} 4 & 1 \\ 1 & 2 \end{bmatrix} $A = \begin{bmatrix} 4 & 1 \\ 1 & 2 \end{bmatrix}$

is

{\lvert}A\rvert = \left| \begin{array}{cc} 4 & 1 \\ 1 & 2 \end{array} \right| = 4(2) - 1(1) = 7 ${\lvert}A\rvert = \left| \begin{array}{cc} 4 & 1 \\ 1 & 2 \end{array} \right| = 4(2) - 1(1) = 7$

When the determinant of a matrix is 0, the inverse of the matrix does not exist.

## Eigenvectors and eigenvalues

An eigenvector is a nonzero vector that satisfies the equation

A\vec{v} = \lambda\vec{v} $A\vec{v} = \lambda\vec{v}$

where $A$ is a square matrix, $\lambda$ is a scalar, and $\vec{v}$ is the eigenvector. $\lambda$ is called an eigenvalue. 