## SOURCE("fMultivar.C1-MatrixAddon")
## Create Pascal Matrix:
xmpMultivar("Start: Pascal Matrix > ")
P = pascal(3)
P
# Create lower triangle matrix
L = triang(P)
L
# Extract diagonal part
diag(P)
## Add/Subtract/Multiply/Divide:
xmpMultivar("Next: Math Operations > ")
X = P
# Multiply matrix with a constant
3 * X
# Multiply two matrices elementwise
X * P
# Multiplies rows/columns of a matrix by a vector
X %*% diag(P)
diag(P) %*% X
## Operate on Subsets of a Matrix:
xmpMultivar("Next: Matrix Subsets > ")
n = 3; i = 2; j = 3
D = diag(1:3)
# Return the dimension of a matrix
dim(P)
# Get the last colum of a matrix
P[, ncol(P)]
# Delete a column of a matrix
P[, -i]
# Permute the columns of a matrix
P[c(3, 1, 2), ]
# Augments matrix horizontally
cbind(P, D)
## Apply a function to all Elements of a Matrix:
xmpMultivar("Next: Operate Element by Element > ")
# Return square root for each element
sqrt(P)
# Exponentiate the matrix elementwise
exp(P)
# Compute the median of each column
apply(P, 2, "median")
# Test on all elements of a matrix
all( P > 2 )
# test on any element in a matrix
any( P > 2 )
## More Matrix Operations:
xmpMultivar("Next: More Matrix Operations > ")
# Return the product of two matrices
P %*% D
# Return the Kronecker Product
P %x% D
# Return the transposed matrix
t(P)
# Return the inverse of a matrix
inv(P)
# Return the norm of a matrix
norm(P)
# Return the determinante of a matrix
det(P)
# Return the rank of a matrix
rk(P)
# Return trace of a matrix
tr(P)
# Return the variance matrix
var(P)
# Return the covariance matrix
cov(P)
# Stack a matrix
vec(P)
# Stack the lower triangle
vech(P)
## Matrix Exponential:
xmpMultivar("Next: Linear Algebra > ")
# Test case 1 from Ward (1977)
test1 = t(matrix(c(
4, 2, 0,
1, 4, 1,
1, 1, 4), 3, 3))
mexp(test1)
# Results on Power Mac G3 under Mac OS 10.2.8
# [,1] [,2] [,3]
# [1,] 147.86662244637000 183.76513864636857 71.79703239999643
# [2,] 127.78108552318250 183.76513864636877 91.88256932318409
# [3,] 127.78108552318204 163.67960172318047 111.96810624637124
# -- these agree with ward (1977, p608)
# A naive alternative to mexp, using spectral decomposition:
mexp2 = function(matrix){
z = eigen(matrix, sym = FALSE)
Re(z$vectors %*% diag(exp(z$values)) %*% solve(z$vectors)) }
mexp2(test1)
# -- hopelessly inaccurate in all but the first column.
# Test case 4 from Ward (1977)
test4 <- structure(c(
0, 0, 0, 0, 0, 0, 0, 0, 0, 1e-10,
1, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 1, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 1, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 1, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 1, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 1, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 1, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 1, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 1, 0), .Dim = c(10, 10))
attributes(mexp(test4))
max(abs(mexp(test4) - mexp2(test4)))
#[1] 8.746826694186494e-08
# -- mexp2 is accurate to 7 d.p., whereas mexp to at least 14 d.p.
## More Linear Algebra:
xmpMultivar("Next: Linear Algebra > ")
X = P; b = c(1, 2, 3)
# Return the Cholesky factor matrix
chol(X)
# Return eigenvalues and eigenvectors
eigen(X)
# Return the singular value decomposition
svd(X)
# Return the condition number of a matrix
kappa(X)
# Return the QR decomposition of a matrix
qr(X)
# Solve a system of linear equations
# ... use backsolve when the matrix is upper triangular
# ... use forwardsolve when the matrix is lower triangular
solve(X, b)
backsolve(Triang(X), b)
solve(Triang(X), b)
forwardsolve(triang(X), b)
solve(triang(X), b)Run the code above in your browser using DataLab