Repository navigation
Expand file tree
/
Copy pathcachematrix.R
More file actions
59 lines (54 loc) · 2.02 KB
/
Copy pathcachematrix.R
File metadata and controls
59 lines (54 loc) · 2.02 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
## Matrix inverse operation is generally an expensive
## operation, here we cache the result of the operation
## for a specific matrix
## This function creates a Cache Matrix object which
## has four operations, setting value for the matrix,
## getting value of the matrix, setting and getting
## inverse value of the matrix
makeCacheMatrix <- function(x = matrix()) {
## Returns list of operayions for the input matrix
## Sets the inverse value to NULL initially
s <- NULL
## Function to set the value of the matrix
set <- function(y) {
## Set the value in target environment
x <<- y
## Set inverse to NULL
s <<- NULL
}
## Getter function to get the matrix
get <- function() x
## Cache the inverse of given matrix
setinverse <- function(solve) s <<- solve
## Get the inverse of the matrix from cached value
getinverse <- function() s
## Return the special object created as list
list(set = set, get = get,
setinverse = setinverse,
getinverse = getinverse)
}
## This function takes a CacheMatrix object
## computes inverse of the matrix if not available
## and caches it else returns already cached inverse
cacheSolve <- function(x, ...) {
## Return a matrix that is the inverse of 'x'
## Get the inverse of the matrix x
m <- x$getinverse()
## Check for whether the value is NULL
if(!is.null(m)) {
## If m is not null, cached value available
message("getting cached data")
## return the cached inverse matrix
return(m)
}
## If cached inverse not available,
## compute inverse, cache it again
## Get the original matrix
data <- x$get()
## Compute inverse
m <- solve(data, ...)
## Cache the inverse of the matrix
x$setinverse(m)
## Return inverse of given CacheMatrix
m
}