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
50 lines (45 loc) · 1.49 KB
/
cachematrix.R
File metadata and controls
50 lines (45 loc) · 1.49 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
## These two functions make a special matrix object and cache the inverse of it.
##
## Firstly, call makeCacheMatrix(matrix) and make an object.
## example : mat <- makeCacheMatrix(matrix(c(1,3,4,5,6,7,8,9,10), nrow=3, ncol=3))
##
## The function cacheSolve(mat) is called for the first time,
## then computes inverse of matrix and stores the result.
## And if cacheSolve(mat) is called again, then returns cached result.
## This function makes a list of function: set() ,get(), setinverse(), and getinverse(),
## which is called by "cacheSolve" function.
makeCacheMatrix <- function(x = matrix()) {
inv <- NULL
set <- function(y) {
x <<- y
inv <<- NULL
}
get <- function() x
setinverse <- function(solve) inv <<- solve
getinverse <- function() inv
list(set = set, get = get,
setinverse = setinverse,
getinverse = getinverse)
}
## This function returns the inverse of "makeCacheMatrix" object.
## If matrix that is not invertible is passed, then it displays an error message and returns NULL.
cacheSolve <- function(x, ...) {
data <- x$get()
if(ncol(data) != nrow(data)) {
message("\'x\' must be square matrix")
return(NULL)
}
if(det(data) == 0) {
message("\'det(x)\' must not be zero")
return(NULL)
}
inv <- x$getinverse()
if(!is.null(inv)) {
message("getting cached data")
return(inv)
}
data <- x$get()
inv <- solve(data, ...)
x$setinverse(inv)
inv
}