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
74 lines (55 loc) · 3.03 KB
/
cachematrix.R
File metadata and controls
74 lines (55 loc) · 3.03 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
## The assignment is to write a pair of functions that cache the inverse of a matrix.
## Matrix inversion is usually a costly computation and there may be some benefit to
## caching the inverse of a matrix rather than computing it repeatedly
## This function creates a special "matrix" object that is able to cache its inverse.
makeCacheMatrix <- function(x = matrix()) {
## the cache inverse variable is defined as NULL as no inverse is calculated yet
inverse <- NULL
## As the following functions are enclosed in the environment of makeCacheMatrix function
## they can access the matix variable x and the inverse variable inverse
## The operators <<- and ->> are normally only used in functions,
## and cause a search to made through parent environments for an existing definition
## of the variable being assigned. If such a variable is found (and its binding is not locked)
## then its value is redefined, otherwise assignment takes place in the global environment.
## defining the setter function to set a new matrix
set <- function(y) {
## Setting x to the new matrix
x <<- y
## As the matrix has changed, we need to clear any previous cache
inverse <<- NULL
}
## defining the get function to get the value of the matrix. Simply return variable x
get <- function() x
## Defining the setter function to set the inverse of the matrix so we can do caching
setinverse <- function(inv) inverse <<- inv
## Defining the function that will return the cached inverse which can be NULL if not cached.
getinverse <- function() inverse
## The following list will be returned as the output of this function
## It contains the 4 functions we have defined above for getting or setting
## the matix or the inverse. The users can then interact with this environment
list(set = set, get = get,
setinverse = setinverse,
getinverse = getinverse)
}
## This function computes the inverse of the special "matrix" returned by `makeCacheMatrix` above.
## If the inverse has already been calculated (and the matrix has not changed), then
## `cacheSolve` should retrieve the inverse from the cache.
cacheSolve <- function(x, ...) {
## We use the getinverse function we defined earlier in the makeCacheMatrix to get the cache inverse
inv <- x$getinverse()
## We check if the inverse has actually been computer before (i.e. it is not NULL)
if(!is.null(inv)) {
## Since the inverse was cached previously we can simply return and exit cacheSolve function
# message("getting cached inverse matrix") # For debugging
return(inv)
}
# It turns out that the inverse of the matrix was not cached previously
# So first we get the matrix using get function we defiend
matrix <- x$get()
# now we calculate the inverse of the above matrix using the solve function
inv <- solve(matrix)
# We update the cache using setinverse function define previously so we don't have to solve it again
x$setinverse(inv)
# Now we can return the calculated inverse to the user
inv
}