Cholesky decomposition, also known as Cholesky factorization, is a method of decomposing the positive-definite matrix. A positive-definite matrix is defined as a symmetric matrix for all possible vectors.

**chol in R**

The chol() is a built-in R function that computes the **Cholesky** **factorization** of a real symmetric positive-definite square matrix. It takes the R object as an argument and returns the Cholesky factorization.

**Syntax**

`chol(x, pivot = FALSE, LINPACK = FALSE, tol = -1, …)`

**Parameters**

**:** It is an object for which a method exists. The default method applies to real symmetric, positive-definite matrices.

**pivot:** Should pivoting be used?

**LINPACK:** It is logical. Should **LINPACK** be used (now ignored)?

**tol:** A numeric tolerance for use with pivot = **TRUE**.

**…: **They are other optional arguments.

**Return Value**

It returns the upper triangular factor of the Cholesky decomposition.

**Example**

To create a matrix in R, use the matrix() function. For example, let’s create a 2 x 2 matrix.

```
vt_1 <- c(-1, 3, 5, -7)
mtrx_1 <- matrix(vt_1, nrow = 2, ncol = 2)
print(mtrx_1)
```

**Output**

```
[,1] [,2]
[1,] -1 5
[2,] 3 -7
```

Let’s calculate the **Cholesky** **factorization**.

```
vt_1 <- c(12, 2, 2, 6)
mtrx_1 <- matrix(vt_1, nrow = 2, ncol = 2)
chol_m <- chol(mtrx_1)
print(chol_m)
```

**Output**

```
[,1] [,2]
[1,] 3.464102 0.5773503
[2,] 0.000000 2.3804761
```

**Error in chol.default(x): the leading minor of order 2 is not positive definite**

Let’s take an example where we will face the leading minor of order 2 is not a positive definite error.

```
vt_1 <- 1:4
mtrx_1 <- matrix(vt_1, nrow = 2, ncol = 2)
chol_m <- chol(mtrx_1)
print(chol_m)
```

**Output**

```
Error in chol.default(mtrx_1) :
the leading minor of order 2 is not positive definite
Calls: chol -> chol.default
Execution halted
```

**Transposing the matrix**

To transpose the matrix in R, use the t() method. For example, we can use the t() method to transpose the resulting upper triangular to get the lower triangular matrix.

```
vt_1 <- c(12, 2, 2, 6)
mtrx_1 <- matrix(vt_1, nrow = 2, ncol = 2)
chol_m <- chol(mtrx_1)
print(chol_m)
cat("After apply transpose", "\n")
l_t <- t(chol_m)
print(l_t)
```

**Output**

```
[,1] [,2]
[1,] 3.464102 0.5773503
[2,] 0.000000 2.3804761
After apply transpose
[,1] [,2]
[1,] 3.4641016 0.000000
[2,] 0.5773503 2.380476
```

The chol.default calls LAPACK routine dpotrf for non-pivoted Cholesky factorization and **LAPACK** routine **dpstrf** for pivoted Cholesky factorization.

Both **LAPACK** routines allow users to choose whether to work with upper triangular or lower triangular, but R disables the lower triangular option and only returns upper triangular.

That is it for chol() function in R.

**See also**

Krunal Lathiya is an Information Technology Engineer by education and web developer by profession. He has worked with many back-end platforms, including Node.js, PHP, and Python. In addition, Krunal has excellent knowledge of Data Science and Machine Learning, and he is an expert in R Language. Krunal has written many programming blogs, which showcases his vast expertise in this field.