16.1 Basic Operations

This section shows us how to create matrices and arrays and how to do simple matric calculations. You find all the following examples in 35575 XLGmatrix01.xpl and 35578 XLGmatrix02.xpl .


16.1.1 Creating Matrices and Arrays


z = 35770 matrix (d1 {, ..., dn})
generates an array z of ones up to eight dimensions
35773 randomize (seed)
sets the seed for the random number generator
z = 35776 uniform (d1 {, ..., dn})
generates an array z of uniform pseudorandom numbers up to eight dimensions
z = 35779 normal (d1 {, ..., dn})
generates an array z of standard pseudorandom numbers up to eight dimensions
z = 35782 diag (x)
creates a diagonal matrix z from a vector x
x = 35785 unit (d)
generates a d-dimensional identity matrix x
d = 35788 dim (x)
shows the dimension of an array x
n = 35791 rows (x)
shows the number of rows of an array x
p = 35794 cols (x)
shows the number of columns of an array x
z = x |y
concatenate two arrays x and y rowwise
z = x $ \sim$ y
concatenate two arrays x and y columnwise

We create a matrix or an array of dimensions $ d_1\times d_2\times \ldots\times d_8$, i.e. up to eight dimensions with the function 35801 matrix . All the elements of the created array are set to 1. The instruction

  mat=matrix(2,3)
  mat
generates a $ 2\times3$ matrix, the elements of which are all equal to one:
  Contents of mat
  [1,]        1        1        1 
  [2,]        1        1        1
The definition of higher-dimensional matrices is straightforward. However, as we cannot represent three-dimensional (or higher-dimensional) objects in the two-dimensional space of this page, or on your screen, the first two dimensions are displayed in the standard format, the projections of the higher ones are then displayed. Consider the following matrix of dimension $ 2\times 3 \times 2$:
  array = matrix(2,3,2)
  array
This matrix is displayed in the form of two submatrices, the layers of the matrix: The first layer contains the elements of array$ _{i,j,1}$, the second layer contains the elements of array$ _{i,j,2}$:
  Contents of array
  [,,1,1,1,1,1,1]
  [1,]        1        1        1 
  [2,]        1        1        1 
  [,,2,1,1,1,1,1]
  [1,]        1        1        1 
  [2,]        1        1        1

The functions 35804 uniform and 35807 normal have the same syntax as matrix. They are used to generate pseudorandom uniform or normal (Gaussian) numbers, respectively. The seed for the the random number generator can be set by 35810 randomize . For example,

  randomize(111222)
  normal(2,2,2)
produces
  Contents of rnorm
  [,,1,1,1,1,1,1]
  [1,]  0.23144   1.1671 
  [2,] -0.38991 -0.0089669 
  [,,2,1,1,1,1,1]
  [1,]  0.60053 -0.54146 
  [2,] -0.28422   1.0531

A matrix $ A$, with typical element $ a_{i,j}$, is said to be diagonal if all elements outside its main diagonal are equal to zero, i.e. $ a_{i,j} = 0,\, \forall i\neq j$. We create a diagonal matrix with the function 35813 diag , whose argument is the ordered sequence of elements in the main diagonal:

  x = #(1, 2, 3)
  diag (x)
which yields
[1,]        1        0        0
[2,]        0        2        0
[3,]        0        0        3

A particular diagonal matrix is the identity matrix denoted by $ I$: The elements of its main diagonal are all equal to one. We create a unit matrix with the function 35816 unit , which has as argument the dimension of the matrix. Since this function belongs to the xplore library, we need to load that library before using it. The commands

  library("xplore")
  id=unit(3)
create the three-dimensional identity matrix:
  Contents of id
  [1,]        1        0        0 
  [2,]        0        1        0 
  [3,]        0        0        1

We display the dimension of the matrix, i.e. the number of elements in each direction, with the function 35821 dim . This function has the matrix as its argument and returns its dimensions. Thus, the instruction

  dim(mat)
returns
  Contents of dim
  [1,]        2 
  [2,]        3
which means that the first dimension of mat is equal to 2, and the second dimension is equal to 3.

The function 35824 dim nests the functions 35827 rows and 35830 cols , which respectively return the number of rows and columns of a matrix. Thus,

  rows(mat)
yields
  Contents of rows
  [1,]        2
You can check that cols(mat) returns 3.

The operators |and $ \sim$ can be used to concatenate matrices or arrays in vertical or horizontal direction. We have used this already for creating simple data matrices in Descriptive Statistics (2). Both operators also work for matrices:

  unit(3)~matrix(3)
returns
  Contents of _tmp
  [1,]        1        0        0        1 
  [2,]        0        1        0        1 
  [3,]        0        0        1        1
whereas
  diag(1:3)|matrix(1,3)
gives
  Contents of _tmp
  [1,]        1        0        0 
  [2,]        0        2        0 
  [3,]        0        0        3 
  [4,]        1        1        1

The functions 35837 dim , 35840 rows , 35843 cols as well as the operators |and $ \sim$ can also be applied to alphanumeric matrices, i.e. multidimensional objects containing text. For example,

  textmat = #("aa","c") ~ #("b","d2")
  dim(textmat)
creates an alphanumeric matrix and prints its dimension
  Contents of dim
  [1,]        2 
  [2,]        2
However, matrices cannot mix alphanumeric and numeric elements. We will see in Section 16.7 that numeric and alphanumeric elements can only be mixed in lists.


16.1.2 Operators for Numeric Matrices


x + y
elementwise addition of x and y
x - y
elementwise subtraction of x and y
x .* y
elementwise multiplication of x by y
x / y
elementwise division of x by y
x $ \wedge$ y
elementwise exponentiation of x by y
x % y
modulo division of x by y
x * y
matrix-wise multiplication of x and y
y = 36237 inv (x)
computes y as the inverse matrix of x
y = 36240 trans (x) or y = x'
computes the transpose of an array x

Let $ A$ and $ B$ be two matrices of the same dimension $ k\times n$, with typical elements $ a_{ij}$ and $ b_{ij}$. Given that these two matrices are of the same dimension, they are said to be conformable for addition and subtraction. The matrix $ C$, of dimension $ k\times n$, defined as $ C = A+ B$, obtained by the addition of matrices $ A$ and $ B$, is the matrix with typical element $ c_{ij}$ such that $ c_{ij} =
a_{ij}+b_{ij}$. The matrix $ D$, of dimension $ k\times n$, defined as $ D = A-B$, is the matrix with typical element $ d_{ij}$ such that $ d_{ij} = a_{ij}-b_{ij}$. For example

  A = (2|4)~(8|6)
  B = (5|6)~(7|8)
  A + B
returns
  Contents of _tmp
  [1,]        7       15 
  [2,]       10       14
You can use the elementwise operators also for adding and subtracting scalars and conformable vectors. For example
  A + 3
  A - (1|1)
gives
  Contents of _tmp
  [1,]        5       11 
  [2,]        7        9 
  Contents of _tmp
  [1,]        1        7 
  [2,]        3        5

Two matrices $ E$ and $ F$ are said to be conformable for the matrix multiplication $ EF$, if the number of columns of $ E$ is equal to the number of rows $ F$. The matrix $ G$ defined as $ G = EF$ has as typical element $ g_{ij}$, with $ g_{ij} = \sum_k e_{ik}f_{kj}$.

Let's define the following matrices:

  E = #(2, 4)~#(8, 6)
  F = #(1, 2)~#(3, 4)
Since both matrices are conformable for the two multiplications $ EF$ and $ FE$, we can verify that matrix multiplication is not commutative:
  mult1 =  F * F
  mult2 =  E * E
  mult1
  mult2
shows
  Contents of mult1
  [1,]       18       38 
  [2,]       16       36 
  Contents of mult2
  [1,]       14       26 
  [2,]       20       40
The two matrices mult1 and mult2 are indeed different.

Matrix multiplication is different from matrix elementwise multiplication, the operator of which is the compound operator .* obtained by concatenating the dot and star symbols. Two matrices are said conformable for elementwise multiplication if they are of the same dimension. The matrix $ M$, defined by $ M = A.*B$, has typical element $ m_{ij}$, where $ m_{ij} = a_{ij} b_{ij}$. Matrix elementwise multiplication is obviously commutative:

  mult3 = E.*F
  mult4 = F.*E
  mult3
  mult4
shows
  Contents of mult3
  [1,]        2       24 
  [2,]        8       24 
  Contents of mult4
  [1,]        2       24 
  [2,]        8       24
The matrices mult3 and mult4 are the same, and differ from both mult1 and mult2.

Note that all elementwise operations extend to arrays in a natural way. Typical matrix operations, such as matrix multiplication, are performed for each layer of an array. This holds also for the determinant, the inverse and the transpose which we will consider now.

A square matrix is such that its number of rows is equal to its number of columns. A square matrix $ A$ is said to be invertible if its determinant, denoted by $ \textrm{det}(A)$ or $ \vert A\vert$, is different from zero. The determinant of a matrix measures the ``volume'' of the space spanned by its column vectors. The determinant of a matrix is thus equal to zero if its rows, or column vectors, are collinear.

We calculate the determinant of a matrix with the function 36247 det , which has the matrix as its argument. Thus,

  det(A)
returns
  Contents of det
  [1,]      -20

The inverse of an invertible matrix $ A$ is the unique matrix, denoted by $ A^{-1}$ of the same dimension of $ A$, such that $ A^{-1}A$ = $ AA^{-1} =I$, the identity matrix. We evaluate the inverse of a matrix with the function 36250 inv . The matrix $ A$ is invertible since its determinant is different from zero. We calculate this inverse as follows:

  inv(A)
yields
  Contents of cinv
  [1,]     -0.3      0.4 
  [2,]      0.2     -0.1
We verify that the product of $ A$ and its inverse is the two-dimensional identity matrix:
  A*inv(A)
gives
  Contents of _tmp
  [1,]           1        0 
  [2,] -1.1102e-16        1
which is numerically close to the two-dimensional identity matrix. The difference in the element in the second row and the first column is caused by rounding errors.

The transpose of a matrix $ A$, with typical element $ a_{ij}$, is the matrix denoted by $ A^T$: the element in the $ i$th row and $ j$th column of $ A^T$ is $ a_{ji}$, the element in the $ j$th row and $ i$th column of $ A$. The matrix $ A$ is said to be symmetric if and only if $ A^T = A$.

We calculate the transpose of a matrix with either the function 36253 trans or the operator '. The following two instructions are equivalent:

  trans(A)
  A'
and return the transpose of the matrix A.