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
63 lines (48 loc) · 1.47 KB
/
cachematrix.R
File metadata and controls
63 lines (48 loc) · 1.47 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
## Create a cache enabled matric object that is able to cache the inverse of the matrix
## parameter x is the original matrix object
makeCacheMatrix <- function(x = matrix()) {
## m is used to store the inverse of the matrix
m <- NULL
## sets the alue of the original matrix and reinitalize m to NULL
set <- function(y) {
x <<- y
m <<- NULL
}
## gets the value of the original matrix
get <- function() {
x
}
## sets the inverse of the original matrix and assigns it to m
setInverse <- function(matrix) {
m <<- matrix
}
## gets the inverse of the original matrix
getInverse <- function() {
m
}
## lists the operation that can be done on the object
list(set = set, get = get,
setInverse = setInverse,
getInverse = getInverse)
}
## computes the cache of the matrix for the first time and then caches it
## for subsequent calls
## a paramter of makeCacheMatrix is passed
cacheSolve <- function(x, ...) {
## Return a matrix that is the inverse of 'x'
m <- x$getInverse()
## If the inverse is not null, that means we already computed it and we can return the
cached inverse
if(!is.null(m)) {
message("getting cached data")
return(m)
}
## at this point, it means we didn't compute the cache yet, so get the original matrix
data <- x$get()
## compute its inverse
m <- solve(data, ...)
## now store its inverse in the cache
x$setInverse(m)
## returns the inverse
m
}