Next: matrix() Up: MacAnova Help File Previous: matread_file   Contents

matrices

Usage:
Matrix transposition       x' or t(x)
Matrix multiplication      x %*% y, x %c% y, x %C% y
Matrix inversion           solve(a)
Linear equation solution   solve(a, b) or a %\% b, rsolve(a,b) or
                           b %/% a
Extract elements           x[i,j], x[,j], x[i,], i, j integer scalars or
                           vectors or LOGICAL vectors.
Eigen values and vectors   eigen(a), eigenvals(a), releigen(a,b),
                           releigenvals(a,b), trideigen(diag,subdiag...)
Other decompositions       qr(x [,pivot:T]), cholesky(x),
                           svd(x [all:T, right:T or F, left:T or F])
Other Functions of matrices
                           trace(x), det(x), diag(x), nrows(x), ncols(x)
Create matrices            matrix(x,nrows), hconcat(a,b,...),
                           vconcat(a,b,...), dmat(vec), dmat(x, n)



Keywords: matrix algebra, operations, variables
A matrix is a two dimensional array, that is, it has two subscripts.

If x is a REAL, LOGICAL, or CHARACTER vector of length m*n, matrix(x,m)
creates an m by n matrix from the elements of x.

A vector of length n is, in most contexts, equivalent to a n by 1
matrix.  See topic 'vectors'.

A generalized matrix is an array with more than two dimensions but which
has no more than 2 dimensions greater than 1.  With few exceptions, a
generalized matrix can be used wherever a matrix can be used.

A generalized matrix with exactly two dimensions with lengths m > 1 and
n > 1, is interpreted as a m by n matrix.  For example, array(run(20),1,
4,1,5) is considered for most purposes as if it were a 4 by 5 matrix.

A generalized matrix whose first dimension is n and all others are 1 is
iterpreted as an n by 1 matrix or, in some contexts, as a vector of
length n.  For example, array(run(7),7,1,1,1) is generally treated as
either a 7 by 1 matrix or a vector of length 7.

A generalized matrix whose first dimension is 1 and which has a single
dimension with length n > 1 is interpreted as a 1 by n matrix, that is,
as a row vector.  For example, array(run(5),1,1,5) is considered to be a
1 by 5 matrix.

A generalized matrix all of whose dimensions are 1 (example:
array(17,1,1,1,1)) is interpreted as a 1 by 1 matrix or, in most
contexts, a scalar.

If x is a generalized matrix, ismatrix(x) returns True and nrows(x) and
ncols(x) return the numbers of rows and columns as just described.

If x is a generalized matrix, matrix(x) is equivalent to matrix(x,
nrows(x)) and is an ordinary two dimensional matrix with the same
elements as x.

You can compute the transpose of a matrix x by either x' or t(x).  The
transpose of a generalized matrix is a generalized matrix with the same
dimensions in reverse order.

You can multiply two REAL matrices or generalized matrices with
conforming dimensions and no MISSING values as follows:
     Operator   Precedence  Meaning
      x %*% y      11       x MatMult y
      x %c% y      11       transpose(x) MatMult y
      x %C% y      11       x MatMult transpose(y)
where MatMult is ordinary matrix multiplication.  The result is always a
matrix with two dimensions, even if either x and.or y is a generalized
matrix.  Either or both operands can also be structures.  See
topic 'structures'.

It formerly was the case on some computers that, when x and y were
large, x' %c% y was considerably faster than x %*% y or x %C% y'.  That
is no longer the case; all three operations take about the same amount
of time.

You can "divide" one matrix by another (in the sense of multiplying by
an inverse) if they have conforming dimensions and no MISSING values as
follows:
     Operator   Precedence  Meaning
      x %/% y      11       x MatMult inverse(y) (same as rsolve(y,x)
      x %\% y      11       inverse(x) MatMult y (same as solve(x,y)

Neither %/% or %\% can be used with structures.

Note: These 5 matrix operations are "left associative", that is, for
example that x %*% y %\% z is equivalent to (x %*% y) %\% z, not x %*%
(y %\% z).


Precedence level 11 is just above the precedence level of '*', '/' and
'%%' and just below the precedence level of '^'.
  Examples:
    Expression          Interpretation     Required to be legal
    a %*% b + 3         (a %*% b) + 3      ncols(a) = nrows(b)
    3 / a %c% b^2       3 / (a %c% (b^2))  nrows(a) = nrows(b)
    a / 3 %C% b         a / (3 %C% b)      ncols(b) = 1

See topic 'precedence' for the precedence levels of other operators.

Here are some functions that are useful with matrices.  All treat
generalized matrices as matrices.
  cholesky()                       Compute Cholesky decomposition of x
  det(x)                           Compute the determinant of x
  det(x,mantexp:T)                 Compute the determinant of x in base
                                   10 mantissa and exponent form
  diag(x)                          Extract the diagonal of x.
  eigenvals(x) and eigen(x)        Compute eigenvalues and/or
                                   eigenvectors of x
  hconcat(x,y,...)                 Concatenate x, y, ... horizontally (y
                                   to the right of x, ...)
  nrows(x), ncols(x)               Find the number of rows or columns of
                                   x
  qr(x [,pivot:T])                 Compute QR decomposition of x
  releigenvals(a,b), releigen(a,b) Compute eigenvalues and/or
                                   eigenvectors of a relative to b
  rsolve(a, b)                     Solve x %*% a = b; equivalent to
                                   b %/% a.
  solve(x)                         Invert x
  solve(a,b)                       Solve a %*% x = b; equivalent to
                                   a %\% b.
  svd(x)                           Compute singular value decomposition
                                   of x
  swp(x,intvec)                    Apply Beaton SWP operator to rows
                                   and columns of x specified by intvec
  t(x)  or  x'                     Transpose of x
  trace(x)                         Compute the trace of x
  trideigen(diag,subdiag [,...])   Compute eigenvalues and/or
                                   eigenvectors of symetric tridiagonal
                                   matrix
  vconcat(x,y,...)                 Concatenate x, y, ... vertically (y
                                   below x, ...)

The following are also useful, but they do not treat a generalized
matrix x exactly like matrix(x).
  max(x)                           Maximum of each column of x
  min(x)                           Minimum of each column of x
  prod(x)                          Product down columns of x
  sum(x)                           Sum down columns of x

These all operate on the first actual dimension of x, producing a result
with the same number of dimensions as x, but with first dimension 1.  If
you want to treat a generalized matrix as if it were a matrix, use, say,
sum(matrix(x)).  See help on the functions for information on how to
operate on dimensions other than the first.

See also det(), trace(), swp(), eigen(), eigenvals(), releigen(),
releigenvals(), dim(), nrows(), ncols(), svd(), cholesky().


Gary Oehlert 2003-01-15