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
62 lines (49 loc) · 1.64 KB
/
cachematrix.R
File metadata and controls
62 lines (49 loc) · 1.64 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
## This function creates a list of matrix which
## has functions for storing and retrieval of the
## matrix and its inverse. It maintains an internal
## cache to store the original data (matrix) and its
## computed inverse.
makeCacheMatrix <- function(x = matrix()) {
i <- NULL
## Setter function for input matrix data
set <- function(y) {
x <<- y
i <<- NULL
}
## Accessor funtion for input matrix data
get <- function() x
## Setter function for inverse matrix of 'x'
setinverse <- function(inverse) i <<- inverse
## Accessor funtion for inverse matrix of 'x'
getinverse <- function() i
## Returns the created cacheMatrix
list(set = set, get = get,
setinverse = setinverse,
getinverse = getinverse)
}
## This function takes a cached matrix created
## by function makeCacheMatrix and returns its
## inverse matrix by first checking the inverse
## is already calculated and store in the cache
## of the input matrix. If inverse value is already
## present, then the cached value is returned else
## inverse is calculated using the solve function
## store in the cache and returned as a result of
## this function.
## It is assumed that the matrix supplied is always invertible.
cacheSolve <- function(x, ...) {
## First check in cache for inverse stored value
i <- x$getinverse()
if(!is.null(i)) {
message("getting cached data")
return(i)
}
## Fetch the matrix whose inverse has to be calculated from input x
data <- x$get()
## Calculate inverse of 'x'
i <- solve(data, ...)
## Store the result into the cache
x$setinverse(i)
## Return a matrix that is the inverse of 'x'
i
}