Appendix B — Linear Algebra

Author

Alejandro Morales & Joost van Heerwaarden

B.1 Vectors

A vector is a one-dimensional array of numbers. We denote vectors with lowercase, bold roman type such as \(\mathbf{v}\). The elements of a vector are indexed by a single subscript, for example \(v_i\) is the \(i\)-th element of the vector \(\mathbf{v}\). We will represent the elements of a vector with square brackets, for example \(\mathbf{v} = [v_1, v_2, \ldots, v_n]\).

We can create a vector in R using c() and access the \(i\)-th with square brackets::

v <- c(1, 2, 3)
v[2]
[1] 2

Vectors can be added or substracted element-wise. For example, if \(\mathbf{v} = [v_1, v_2, v_3]\) and \(\mathbf{w} = [w_1, w_2, w_3]\), then

\[ \mathbf{v} + \mathbf{w} = [v_1 + w_1, v_2 + w_2, v_3 + w_3]. \]

In R, we can also add or substract vectors and this will be done element-wise:

v <- c(1, 2, 3)
w <- c(4, 5, 6)
v + w
[1] 5 7 9
v - w
[1] -3 -3 -3

Similarly, scalar multiplication or division is done element-wise. For example, given the vector \(\mathbf{v} = [v_1, v_2, v_3]\) and constant \(k\), then

\[ k \mathbf{v} = [k \times v_1, k \times v_2, k \times v_3]. \]

In R, we can also multiply or divide a vector by a scalar:

v <- c(1, 2, 3)
k <- 2
k * v
[1] 2 4 6
v/k
[1] 0.5 1.0 1.5

There are two types of products between vectors: the dot product and the cross product. The cross product is important in discplines such as physics and engineering, but we will not use it in this course. The dot product of two vectors \(\mathbf{v}\) and \(\mathbf{w}\) is denoted by \(\mathbf{v} \cdot \mathbf{w}\) and is calculated as the sum of the products of the elements of the vectors. For example, given vectors \(\mathbf{v} = [v_1, v_2, v_3]\) and \(\mathbf{w} = [w_1, w_2, w_3]\), then

\[ \mathbf{v} \cdot \mathbf{w} = v_1 \times w_1 + v_2 \times w_2 + v_3 \times w_3. \]

In R, we can calculate the dot product of two vectors using the %*% operator:

v <- c(1, 2, 3)
w <- c(4, 5, 6)
v %*% w
     [,1]
[1,]   32

Note that R will not return a scalar but a matrix of size \(1 \times 1\) (see below for details). We can also calculate the dot product using the sum() function and element-wise multiplication:

sum(v * w)
[1] 32

which will return a scalar value.

B.2 Matrices

A matrix is a two-dimensional array of numbers. We denote matrices with uppercase, bold roman type such as \(\mathbf{A}\). The elements of a matrix are indexed by two subscripts, for example \(a_{ij}\) is the element in the \(i\)-th row and \(j\)-th column of the matrix \(\mathbf{A}\). We will represent the elements of a matrix with square brackets, for example a 2x2 matrix is represented as

\[ \mathbf{A} = \begin{bmatrix} a_{11} & a_{12} \\ a_{21} & a_{22} \end{bmatrix}. \]

We can make a matrix in R using the matrix() function by passing the elements and the dimensions of the matrix:

A <- matrix(c(1, 2, 3, 4), nrow = 2, ncol = 2)
A
     [,1] [,2]
[1,]    1    3
[2,]    2    4

We can retrieve the dimensions of the matrix using the dim() function:

dim(A)
[1] 2 2

Notice that the elements are filled column-wise. We can also fill the matrix by row:

B <- matrix(c(1, 2, 3, 4), nrow = 2, ncol = 2, byrow = TRUE)
B
     [,1] [,2]
[1,]    1    2
[2,]    3    4

A vector can be seen as special case of a matrix that only has one column (a column-vector) or one row (a row-vector). We can make a column-vector by specifying ncol = 1:

v <- c(1,2,3)
cv <- matrix(v, ncol = 1)
cv
     [,1]
[1,]    1
[2,]    2
[3,]    3

or a row-vector by specifying nrow = 1:

rv <- matrix(v, ncol = 1)
rv
     [,1]
[1,]    1
[2,]    2
[3,]    3

The size of a matrix \(\mathbf{A}\) is denoted by \(m \times n\) where \(m\) is the number of rows and \(n\) is the number of columns. For example, the size of a matrix

\[ \mathbf{A} = \begin{bmatrix} a_{11} & a_{12} \\ a_{21} & a_{22} \\ a_{31} & a_{32} \end{bmatrix} \]

is \(3 \times 2\). A vector of length \(n\) will then be a \(n \times 1\) matrix (column-vector) or \(1 \times n\) matrix (row-vector).

In R, we control the number of rows and columns of a matrix using the nrow and ncol arguments. Only one of the two arguments is required, the other will be calculated from the number of elements used to construct the matrix. For example, we can create a 3x2 matrix by only specifying the number of rows:

A <- matrix(c(1, 2, 3, 4, 5, 6), nrow = 3)
A
     [,1] [,2]
[1,]    1    4
[2,]    2    5
[3,]    3    6

Be aware that if the number of elements is not a multiple of the number of rows or columns, R will recycle the elements from the start until a multiple is reached. For example, if we create a 3x2 matrix with 5 elements, the first element will be recycled:

A <- matrix(c(1, 2, 3, 4, 5), nrow = 3)
Warning in matrix(c(1, 2, 3, 4, 5), nrow = 3): data length [5] is not a
sub-multiple or multiple of the number of rows [3]
A
     [,1] [,2]
[1,]    1    4
[2,]    2    5
[3,]    3    1

At least R will show you a warning when this happens.

Matrices can be added or substracted element-wise. For example, if

\[ \mathbf{A} = \begin{bmatrix} a_{11} & a_{12} \\ a_{21} & a_{22} \end{bmatrix} \]

and

\[ \mathbf{B} = \begin{bmatrix} b_{11} & b_{12} \\ b_{21} & b_{22} \end{bmatrix}, \]

then

\[ \mathbf{A} + \mathbf{B} = \begin{bmatrix} a_{11} + b_{11} & a_{12} + b_{12} \\ a_{21} + b_{21} & a_{22} + b_{22} \end{bmatrix}. \]

In R, we can also add or substract matrices and this will be done element-wise:

A <- matrix(c(1, 2, 3, 4), nrow = 2)
B <- matrix(c(5, 6, 7, 8), nrow = 2)
A + B
     [,1] [,2]
[1,]    6   10
[2,]    8   12
A - B
     [,1] [,2]
[1,]   -4   -4
[2,]   -4   -4

Similarly, scalar multiplication and division is done element-wise. For example, if

\[ \mathbf{A} = \begin{bmatrix} a_{11} & a_{12} \\ a_{21} & a_{22} \end{bmatrix} \]

and given constant \(k\), then

\[k \mathbf{A} = \begin{bmatrix} k \times a_{11} & k \times a_{12} \\ k \times a_{21} & k \times a_{22} \end{bmatrix}. \]

In R, we can also multiply and divide a matrix by a scalar:

A <- matrix(c(1, 2, 3, 4), nrow = 2)
k <- 2
k * A
     [,1] [,2]
[1,]    2    6
[2,]    4    8
A / k
     [,1] [,2]
[1,]  0.5  1.5
[2,]  1.0  2.0

B.2.0.1 Matrix product

One can multiple two matrices but also a matrix and and a vector (interpreted as column- or row-vector). The matrix-matrix product is calculated as the sum of the products of the rows of the first matrix and the columns of the second matrix (you can think of each element of the new matrix being the result of a dot product between a row-vector and a column-vector). For example, if

\[ \mathbf{A} = \begin{bmatrix} a_{11} & a_{12} \\ a_{21} & a_{22} \end{bmatrix} \]

and

\[ \mathbf{B} = \begin{bmatrix} b_{11} & b_{12} \\ b_{21} & b_{22} \end{bmatrix}, \]

then

\[ \mathbf{A} \mathbf{B} = \begin{bmatrix} a_{11} \times b_{11} + a_{12} \times b_{21} & a_{11} \times b_{12} + a_{12} \times b_{22} \\ a_{21} \times b_{11} + a_{22} \times b_{21} & a_{21} \times b_{12} + a_{22} \times b_{22} \end{bmatrix}. \]

The result of the matrix-matrix product is a new matrix with dimensions equal to the number of rows of the first matrix and the number of columns of the second matrix. In R, we can multiply matrices using the %*% operator:

A <- matrix(c(1, 2, 3, 4), nrow = 2)
B <- matrix(c(5, 6, 7, 8), nrow = 2)
A %*% B
     [,1] [,2]
[1,]   23   31
[2,]   34   46

We can verify manually the first element of the new matrix:

1 * 5 + 3 * 6
[1] 23

Note that, unlike the dot product of vectors, the matrix-matrix product is not commutative. That is, in general, \(\mathbf{A} \mathbf{B} \neq \mathbf{B} \mathbf{A}\). We can quickly verify this in R:

A <- matrix(c(1, 2, 3, 4), nrow = 2)
B <- matrix(c(5, 6, 7, 8), nrow = 2)
A %*% B
     [,1] [,2]
[1,]   23   31
[2,]   34   46
B %*% A
     [,1] [,2]
[1,]   19   43
[2,]   22   50

Also, the matrix-matrix product is only defined when the number of columns of the first matrix is equal to the number of rows of the second matrix. This is denoted in R as the error message non-conformable arguments:

A <- matrix(c(1, 2, 3, 4), nrow = 2)
C <- matrix(c(9, 10, 11, 12, 13, 14), nrow = 3)
A %*% C
Error in A %*% C: non-conformable arguments

If we try to multiply a matrix by a vector, R will automally interpret the vector as a column-vector or row-vector depending on the context:

A <- matrix(c(1, 2, 3, 4), nrow = 2)
v <- c(5, 6)
A %*% v # Equivalent to A %*% matrix(v, ncol = 1)
     [,1]
[1,]   23
[2,]   34
v %*% A # Equivalent to matrix(v, nrow = 1) %*% A
     [,1] [,2]
[1,]   17   39

B.3 Determinant and inverse of a matrix

The determinant of a matrix is a scalar value that can be calculated from the elements of the matrix. There is no straightfoward defition of the determinant but it plays a role in inversing matrices (see below for details, see here for a list of properties of determinants). The determinant of a matrix \(\mathbf{A}\) is denoted by \(\det(\mathbf{A})\) and is calculated as a sum of products of the elements of the matrix (there are several formulae to compute the determinant, such as Leibniz formula). For example, the determinant of a \(2 \times 2\) matrix

\[ \mathbf{A} = \begin{bmatrix} a_{11} & a_{12} \\ a_{21} & a_{22} \end{bmatrix} \]

is \(a_{11}a_{22} - a_{12}a_{21}\).

In R, we can calculate the determinant of a matrix using the det() function:

A <- matrix(c(1, 2, 3, 4), nrow = 2)
det(A)
[1] -2

Note that the determinant of a matrix is only defined for square matrices (i.e., matrices with the same number of rows and columns). If we try to calculate the determinant of a non-square matrix, R will return an error:

A <- matrix(c(1, 2, 3, 4, 5, 6), nrow = 2)
det(A)
Error in determinant.matrix(x, logarithm = TRUE, ...): 'x' must be a square matrix

Elements of a matrix can be organized into three groups: the diagonal elements, the upper triangular elements, and the lower triangular elements:

  • The diagonal elements are the elements of the matrix that have the same row and column index.
  • The upper triangular elements are the elements of the matrix that have a row index smaller than the column index.
  • The lower triangular elements are the elements of the matrix that have a row index larger than the column index.

For example, in the matrix

\[ \mathbf{A} = \begin{bmatrix} a_{11} & a_{12} & a_{13} \\ a_{21} & a_{22} & a_{23} \\ a_{31} & a_{32} & a_{33} \end{bmatrix}, \]

the diagonal elements are \(a_{11}\), \(a_{22}\), and \(a_{33}\), the upper triangular elements are \(a_{12}\), \(a_{13}\), and \(a_{23}\), and the lower triangular elements are \(a_{21}\), \(a_{31}\), and \(a_{32}\).

B.4 Linear algebra operations

B.4.1 Transpose

The transpose of a matrix is a new matrix that is obtained by exchanging the rows and columns of the original matrix. The transpose of a matrix \(\mathbf{A}\) is denoted by \(\mathbf{A}^T\). For example, if

\[ \mathbf{A} = \begin{bmatrix} a_{11} & a_{12} \\ a_{21} & a_{22} \\ a_{31} & a_{32} \end{bmatrix}, \]

then

\[ \mathbf{A}^T = \begin{bmatrix} a_{11} & a_{21} & a_{31} \\ a_{12} & a_{22} & a_{32} \end{bmatrix}. \]

In R, we can calculate the transpose of a matrix using the t() function:

A <- matrix(c(1, 2, 3, 4, 5, 6), nrow = 3)
A
     [,1] [,2]
[1,]    1    4
[2,]    2    5
[3,]    3    6
t(A)
     [,1] [,2] [,3]
[1,]    1    2    3
[2,]    4    5    6

B.4.2 Inverse

The inverse of a matrix is a new matrix that, when multiplied by the original matrix, results in the identity matrix (\(\mathbf{I}\), a square matrix with ones on the diagonal and zeros elsewhere). In R, the identity matrix can be created using the diag() function and passing the dimensions along:

I <- diag(3)
I # 3x3 identity matrix
     [,1] [,2] [,3]
[1,]    1    0    0
[2,]    0    1    0
[3,]    0    0    1

The inverse of a matrix \(\mathbf{A}\) is denoted by \(\mathbf{A}^{-1}\), such that \(\mathbf{A}^{-1} \mathbf{A} = \mathbf{A} \mathbf{A}^{-1} = \mathbf{I}\). Note that calculating the inverse of a matrix is not trivial and, in practice, multiple numerical methods exist to do so. We do not cover these methods in the course, but we will highlight some of the potential issues that may occur when trying to invert a matrix.

For example, if

\[ \mathbf{A} = \begin{bmatrix} 1 & 2 \\ 3 & 4 \end{bmatrix}, \]

then \[ \mathbf{A}^{-1} = \begin{bmatrix} -2 & 1 \\ 1.5 & -0.5 \end{bmatrix}. \]

Indeed we can see that

\[ \mathbf{A}^{-1} \mathbf{A} = \begin{bmatrix} -2 & 1 \\ 1.5 & -0.5 \end{bmatrix} \begin{bmatrix} 1 & 2 \\ 3 & 4 \end{bmatrix} = \begin{bmatrix} -2 \times 1 + 1 \times 3 & -2 \times 2 + 1 \times 4 \\ 1.5 \times 1 - 0.5 \times 3 & 1.5 \times 2 - 0.5 \times 4 \end{bmatrix} = \begin{bmatrix} 1 & 0 \\ 0 & 1 \end{bmatrix}. \]

In R, we can calculate the inverse of a matrix using the solve() function:

A <- matrix(c(1, 2, 3, 4), nrow = 2, byrow = TRUE)
A_inv <- solve(A)
A_inv
     [,1] [,2]
[1,] -2.0  1.0
[2,]  1.5 -0.5
A_inv %*% A # Notice tiny numerical errors
     [,1]         [,2]
[1,]    1 4.440892e-16
[2,]    0 1.000000e+00

Not all matrices have an inverse. Firstly, the must be a square matrix (i.e., same number of rows and columns) and its determinant should not be zero. A matrix that does not have an inverse is called a singular or determinant. A square matrix is singular if and only if its determinant is zero. Sometimes, a matrix may be reported to be singular due to limits to numerical accuracy when applying an algorithm to invert it, even though it is not.

For example, if we try to invert a singular matrix, R will return an error:

A <- matrix(c(1, 2, 2, 4), nrow = 2, byrow = TRUE)
det(A) # Determinant = 0 means singular matrix
[1] 0
solve(A)
Error in solve.default(A): Lapack routine dgesv: system is exactly singular: U[2,2] = 0

If we try to invert a matrix that is close to singular, R will return a (different) error:

A <- matrix(c(1, 2, 2, 4), nrow = 2, byrow = TRUE)
A[2, 2] <- A[2, 2] + 1e-15 # Make the matrix close to singular
det(A) # Not exactly zero but close
[1] 8.881784e-16
solve(A)
Error in solve.default(A): system is computationally singular: reciprocal condition number = 2.46716e-17
Exercise B.1

Given the following matrices:

n = 10
p = 2
x <- matrix(1, nrow = n, ncol = 1) # Column vector
y <- matrix(1:n, ncol = 1) # Column vector
X <- matrix((1:(n*p)), nrow = n, ncol = p) # n x p matrix
B <- matrix((1:p), nrow = p, ncol = 1) # 1 x p matrix
  1. Try to multiply X by B and B by X (not element-wise). What do you observe and can you explan it? What is the size of the resulting matrices?

  2. Verify manually at least one element of the resulting matrix.

  3. Multiply the transpose of x with y. How does this compare to the dot product of x and y?

  4. Compare the product of X and its transpose with the product of the transpose of X and X. Can you explain the size of the resulting matrices? What is special about the diagonal of the second resulting matrix?

Solution B.1
  1. The matrix-matrix product is not commutative, so X %*% B is not equal to B %*% X. X has dimensions \(10 \times 2\) and B has dimensions \(2 \times 1\). This means that X %*% B is possible and results in a matrix of dimensions \(10 \times 1\):
X %*% B
      [,1]
 [1,]   23
 [2,]   26
 [3,]   29
 [4,]   32
 [5,]   35
 [6,]   38
 [7,]   41
 [8,]   44
 [9,]   47
[10,]   50

However, the B %*% X is not possible:

B %*% X
Error in B %*% X: non-conformable arguments
  1. The first element of X %*% B is the dot product of the first row of X and B:
sum(X[1, ] * B) # or X[1, ] %*% B
[1] 23
  1. The transpose of x is a row-vector and y is a column-vector. This results in a matrix with only one element, which is the dot product of x and y (as vectors):
t(x) %*% y
     [,1]
[1,]   55
sum(x * y) # or x[,1] %*% y[,1]
[1] 55
  1. The product of X and its transpose is possible and results in a matrix of dimensions \(10 \times 10\):
X %*% t(X)
      [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
 [1,]  122  134  146  158  170  182  194  206  218   230
 [2,]  134  148  162  176  190  204  218  232  246   260
 [3,]  146  162  178  194  210  226  242  258  274   290
 [4,]  158  176  194  212  230  248  266  284  302   320
 [5,]  170  190  210  230  250  270  290  310  330   350
 [6,]  182  204  226  248  270  292  314  336  358   380
 [7,]  194  218  242  266  290  314  338  362  386   410
 [8,]  206  232  258  284  310  336  362  388  414   440
 [9,]  218  246  274  302  330  358  386  414  442   470
[10,]  230  260  290  320  350  380  410  440  470   500

The product of the transpose of X and X is also possible and results in a matrix of dimensions \(2 \times 2\):

t(X) %*% X
     [,1] [,2]
[1,]  385  935
[2,]  935 2485

The diagonal of the second matrix is the sum of the squares of the elements of the corresponding column of X.

sum(X[, 1]^2)
[1] 385
sum(X[, 2]^2)
[1] 2485

This is because the first row of t(X) is the first column of X (and so on) and the dot product of a vector with itself is just the sum of the squares of the elements.

Exercise B.2
  1. Make an identity matrix of size \(10 \times 10\).

  2. What is the transpose of the identity matrix?

  3. What is the inverse of the identity matrix?

Solution B.2
  1. The identity matrix of size \(10 \times 10\) is:
I <- diag(10)
I
      [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
 [1,]    1    0    0    0    0    0    0    0    0     0
 [2,]    0    1    0    0    0    0    0    0    0     0
 [3,]    0    0    1    0    0    0    0    0    0     0
 [4,]    0    0    0    1    0    0    0    0    0     0
 [5,]    0    0    0    0    1    0    0    0    0     0
 [6,]    0    0    0    0    0    1    0    0    0     0
 [7,]    0    0    0    0    0    0    1    0    0     0
 [8,]    0    0    0    0    0    0    0    1    0     0
 [9,]    0    0    0    0    0    0    0    0    1     0
[10,]    0    0    0    0    0    0    0    0    0     1
  1. The transpose of the identity matrix is the identity matrix itself:
t(I)
      [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
 [1,]    1    0    0    0    0    0    0    0    0     0
 [2,]    0    1    0    0    0    0    0    0    0     0
 [3,]    0    0    1    0    0    0    0    0    0     0
 [4,]    0    0    0    1    0    0    0    0    0     0
 [5,]    0    0    0    0    1    0    0    0    0     0
 [6,]    0    0    0    0    0    1    0    0    0     0
 [7,]    0    0    0    0    0    0    1    0    0     0
 [8,]    0    0    0    0    0    0    0    1    0     0
 [9,]    0    0    0    0    0    0    0    0    1     0
[10,]    0    0    0    0    0    0    0    0    0     1
  1. The inverse of the identity matrix is the identity matrix itself:
solve(I)
      [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
 [1,]    1    0    0    0    0    0    0    0    0     0
 [2,]    0    1    0    0    0    0    0    0    0     0
 [3,]    0    0    1    0    0    0    0    0    0     0
 [4,]    0    0    0    1    0    0    0    0    0     0
 [5,]    0    0    0    0    1    0    0    0    0     0
 [6,]    0    0    0    0    0    1    0    0    0     0
 [7,]    0    0    0    0    0    0    1    0    0     0
 [8,]    0    0    0    0    0    0    0    1    0     0
 [9,]    0    0    0    0    0    0    0    0    1     0
[10,]    0    0    0    0    0    0    0    0    0     1
Exercise B.3
  1. Make a matrix of size \(10 \times 10\) with random values inside of it (e.g., using rnorm(100) or rpois(100)).

  2. Calculate the inverse of the matrix.

  3. Multiply the matrix by its inverse. What do you observe?

Solution B.3
  1. There are many ways to create a matrix with random values. For example, using rnorm():
A <- matrix(rnorm(100), nrow = 10)
A
             [,1]       [,2]       [,3]        [,4]       [,5]       [,6]
 [1,]  0.95634262  0.3935960 -1.1138475 -0.51719777 -0.7347643 -0.5731081
 [2,] -1.66817933  0.6613139 -1.3730063  1.21913155  0.2355531  2.6491462
 [3,]  0.02980616  1.5530189 -1.0440907  0.09843743 -0.5984019  0.1271751
 [4,]  1.01748571 -0.6717381  1.0833426  1.32918893  1.2209639 -0.1428267
 [5,]  0.16335056  0.8470411 -0.2532366  2.16189000  2.6711978  0.2800027
 [6,] -0.93097066  0.9745176  1.2583060 -2.84386865  1.3611031 -1.5053700
 [7,] -0.79038807 -1.5246831 -0.9350806  2.25229218 -0.9292145  0.8429486
 [8,] -0.09271131  0.5302093  0.7831707 -0.48069646 -1.9378625  1.1671326
 [9,] -0.38318801  0.2566994 -0.1191065  1.13040869 -1.7427978 -0.1319157
[10,] -0.93029664 -0.1940101  0.9540830 -0.12606562 -1.3132739  0.7723685
             [,7]        [,8]        [,9]      [,10]
 [1,] -1.65449010  0.15813777 -1.00290695  0.9338939
 [2,] -0.30654031 -1.35461640  0.07636844  0.1835736
 [3,] -0.50202884 -1.16854424  1.15333061 -0.3553271
 [4,] -0.32830498 -0.05315744  0.36360392  0.1982206
 [5,] -0.65695599  0.67812544 -0.69670014 -1.3631197
 [6,]  1.07855465  0.32276672 -1.15990588  0.9411791
 [7,] -1.63055757 -0.50746139 -1.11094384  0.4935972
 [8,] -1.50672022 -0.62142205  0.60324258 -1.3771061
 [9,]  0.21450182 -0.80344181 -2.07628536 -0.8286787
[10,]  0.08788821  0.78265715 -0.69624619  0.9652674
  1. The inverse of the matrix is:
A_inv <- solve(A)
A_inv
             [,1]         [,2]        [,3]        [,4]         [,5]        [,6]
 [1,]  0.37921455  0.189809349 -0.32833704  0.31010630 -0.125321648 -0.24795345
 [2,]  0.10073839  0.006442368  0.38668802  0.07687742  0.167489285  0.03164058
 [3,] -0.10470712 -0.043127968  0.06315007  0.36302099 -0.006705992  0.14592803
 [4,] -0.13047046 -0.124252934  0.31983273  0.14259185  0.113463240 -0.08315616
 [5,]  0.01391669  0.124250232 -0.13924151  0.08464462  0.078803692  0.12351854
 [6,]  0.20830887  0.418346580 -0.45683291  0.08940520 -0.055140825 -0.17763803
 [7,] -0.09940942  0.112026501 -0.11962723 -0.01197592 -0.115259319 -0.15691831
 [8,]  0.01214638 -0.219840610 -0.02736688 -0.35642720  0.258765964 -0.18916286
 [9,] -0.22642728 -0.169768171  0.34403347 -0.04179525 -0.018113463 -0.05851157
[10,]  0.16031235  0.090527226  0.19796155  0.27769558 -0.126717708  0.04794102
              [,7]         [,8]        [,9]        [,10]
 [1,] -0.473991025 -0.006336762  0.17480065 -0.139338229
 [2,] -0.201227966 -0.058681676  0.08683601  0.327269679
 [3,] -0.010381251  0.161451154  0.05682544  0.190876888
 [4,]  0.107813277 -0.168943705  0.07855513  0.250906402
 [5,]  0.011248548  0.003660688 -0.09812475 -0.199654056
 [6,] -0.369089842  0.119034231  0.02413100  0.006986802
 [7,] -0.308050715 -0.233915591  0.20031685  0.019311222
 [8,] -0.050510753 -0.081208450 -0.19779646  0.383204960
 [9,]  0.124896453 -0.023150493 -0.26182678  0.096380204
[10,] -0.003196327 -0.219367344 -0.05951391  0.291399247
  1. The product of the matrix and its inverse is always the identity matrix:
A %*% A_inv
               [,1]          [,2]          [,3]          [,4]          [,5]
 [1,]  1.000000e+00  9.714451e-17 -1.665335e-16 -5.551115e-17 -6.938894e-17
 [2,]  1.526557e-16  1.000000e+00  5.551115e-17 -4.857226e-17  1.040834e-17
 [3,]  3.330669e-16  0.000000e+00  1.000000e+00 -4.163336e-17 -4.857226e-17
 [4,] -3.608225e-16 -3.816392e-17 -9.714451e-17  1.000000e+00 -9.020562e-17
 [5,] -5.551115e-17 -1.110223e-16  5.551115e-17 -1.110223e-16  1.000000e+00
 [6,]  8.326673e-17  1.387779e-16 -3.053113e-16 -5.551115e-17  1.387779e-17
 [7,]  4.163336e-17  9.714451e-17  4.163336e-17 -1.110223e-16  0.000000e+00
 [8,]  5.551115e-17  4.163336e-17 -1.110223e-16  0.000000e+00  0.000000e+00
 [9,] -2.775558e-17  1.387779e-17  2.220446e-16 -2.775558e-17  4.163336e-17
[10,]  2.775558e-16  9.714451e-17 -8.326673e-17  0.000000e+00  1.110223e-16
               [,6]          [,7]          [,8]          [,9]         [,10]
 [1,] -2.081668e-17 -1.092876e-16 -1.110223e-16 -2.081668e-17 -1.110223e-16
 [2,]  2.255141e-17 -1.080950e-16  0.000000e+00 -1.561251e-17  6.938894e-18
 [3,] -7.979728e-17 -4.336809e-18 -1.942890e-16 -3.469447e-18  6.938894e-17
 [4,] -1.700029e-16 -2.552212e-16  2.081668e-17  3.816392e-17 -1.873501e-16
 [5,] -9.714451e-17 -9.540979e-18 -1.110223e-16  6.938894e-17 -1.110223e-16
 [6,]  1.000000e+00  1.812786e-16  2.775558e-17  9.020562e-17 -1.665335e-16
 [7,]  3.469447e-18  1.000000e+00  8.326673e-17 -5.204170e-17  1.387779e-16
 [8,] -6.938894e-17 -8.673617e-18  1.000000e+00  2.775558e-17 -5.551115e-17
 [9,]  5.551115e-17  3.226586e-16  0.000000e+00  1.000000e+00  0.000000e+00
[10,]  4.163336e-17  1.543904e-16  2.220446e-16  1.110223e-16  1.000000e+00

Note that there may be tiny numerical errors due to the limits of numerical accuracy. To get a clearer view of the result, we can round the values:

round(A %*% A_inv)
      [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
 [1,]    1    0    0    0    0    0    0    0    0     0
 [2,]    0    1    0    0    0    0    0    0    0     0
 [3,]    0    0    1    0    0    0    0    0    0     0
 [4,]    0    0    0    1    0    0    0    0    0     0
 [5,]    0    0    0    0    1    0    0    0    0     0
 [6,]    0    0    0    0    0    1    0    0    0     0
 [7,]    0    0    0    0    0    0    1    0    0     0
 [8,]    0    0    0    0    0    0    0    1    0     0
 [9,]    0    0    0    0    0    0    0    0    1     0
[10,]    0    0    0    0    0    0    0    0    0     1

We can verify that the result is the identity matrix:

all(round(A %*% A_inv) == diag(10))
[1] TRUE