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
71 lines (53 loc) · 1.72 KB
/
Copy pathcachematrix.R
File metadata and controls
71 lines (53 loc) · 1.72 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
## This function creates a special "matrix" object, which is really a list
## containing a function to:
## Set the value of the matrix
## Get the value of the matrix
## Set the value of the inverse
## Get the value of the inverse
makeCacheMatrix <- function(x = matrix()) {
## Initialize the inverse property to NULL
inv <- NULL
## Method to set the matrix
set <- function(y) {
x <<- y ## Assign new value to matrix in parent environment
inv <<- NULL ## Reset the inverse if a new matrix is set
}
## Method to get the matrix
get <- function() {
x
}
## Method to set the inverse of the matrix
setInverse <- function(inverse) {
inv <<- inverse
}
# Method to get the inverse of the matrix
getInverse <- function() {
inv
}
## Return a list of the methods
list(set = set,
get = get,
setInverse = setInverse,
getInverse = getInverse)
}
## This function computes the inverse of the special "matrix" returned by
## makeCacheMatrix.If the inverse has already been calculated (and the matrix
## has not changed), then cacheSolve will retrieve the inverse from the cache
## and skip the computation.
cacheSolve <- function(x, ...) {
## Attempt to get the inverse from the cache
inv <- x$getInverse()
## If the inverse is already calculated, return it
if (!is.null(inv)) {
message("getting cached data")
return(inv)
}
## If not cached, get the matrix itself
mat <- x$get()
## Calculate the inverse using the solve() function
inv <- solve(mat, ...)
## Cache the calculated inverse for future use
x$setInverse(inv)
## Return the inverse
inv
}