forked from rdpeng/ProgrammingAssignment2
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcachematrix.R
More file actions
94 lines (87 loc) · 3.08 KB
/
Copy pathcachematrix.R
File metadata and controls
94 lines (87 loc) · 3.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
## This program cache the inverse of matrix, by using
## two functions: makeCacheMatrix and cacheSolve.
## It is assumed that the matrix is real, square,
## and invertible. If the matrix is singular
## the program will terminate with an error message.
## This program is compiled with: source ("cachematrix.R")
###############################################################
## Function: makeCacheMatrix
###############################################################
## The input of this function is a real, square,
## invertible matrix.
## Return:
## A list with the following four functions:
## set: set the value of the matrix
## get: get the value of the matrix
## setinver: set the inverse of the matrix
## getinver: get the inverse of the matrix
makeCacheMatrix <- function(x = matrix()) {
inv <- NULL
set <- function(y) {
x <<- y
inv <<- NULL
}
get <- function() x
setinver <- function(inver) inv <<- inver
getinver <- function() inv
list(set = set, get = get,
setinver = setinver,
getinver = getinver)
}
###############################################################
## Function: cacheSolve
###############################################################
## Computes the inverse of a real, square, invertible
## matrix.
## Return:
## If the inverse has already been computed,returns
## the inverse. Else, the inverse of the matrix
## is computed, the value of the inverse is set
## in the cache via the setinver function, and the
## inverse of the matrix is returned with a message.
## If the matrix is singular, that is, not invertible
## the following error message is returned:
## Error in solve.default(data, ...) :
## Lapack routine dgesv: system is exactly singular.
cacheSolve <- function(x, ...) {
inv <- x$getinver()
if(!is.null(inv)) {
message("getting cached data")
return(inv)
}
data <- x$get()
inv <- solve(data, ...)
x$setinver (inv)
inv
}
###############################################################
## Example 1:
## Real, square, invertible, matrix.
## > mat <- makeCacheMatrix(matrix (c(5,4,3,2,5,5,3,6,7), nrow = 3, ncol =3))
## > cacheSolve (mat)
## [,1] [,2] [,3]
## [1,] 0.25 0.05 -0.15
## [2,] -0.50 1.30 -0.90
## [3,] 0.25 -0.95 0.85
## > cacheSolve (mat)
## getting cached data
## [,1] [,2] [,3]
## [1,] 0.25 0.05 -0.15
## [2,] -0.50 1.30 -0.90
## [3,] 0.25 -0.95 0.85
## Example 2:
## Checking the inverse using matrix multiplication
## A matrix time its inverse must be equal to the unit matrix
## > mat1 <- makeCacheMatrix (matrix (c(5,4,3,2,5,5,3,6,7), nrow = 3, ncol =3))
## > ma1 <- matrix (c(5,4,3,2,5,5,3,6,7), nrow = 3, ncol =3)
## > ma1%*%cacheSolve (mat1)
## [,1] [,2] [,3]
## [1,] 1.000000e+00 0 0
## [2,] -2.220446e-16 1 0
## [3,] 0.000000e+00 0 1
## Example 3:
## A real, singular matrix:
## > mat <- makeCacheMatrix (matrix (1:9, nrow = 3, ncol =3))
## > invma <- cacheSolve (mat)
## Error in solve.default(data, ...) :
## Lapack routine dgesv: system is exactly singular: U[3,3] = 0