forked from rdpeng/ProgrammingAssignment2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcachematrix.R
56 lines (48 loc) · 1.57 KB
/
cachematrix.R
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
## 'makeCacheMatrix' function creates a special 'list' which contains
## a function to
## - set the matrix
## - get the matrix
## - set the matrix inverse
## - get the matrix inverse
##
## 'cacheSolve' function takes the special 'list' and checks if inverse
## for the same is already calculated and stored in the cache. If found,
## it returns the inverse as it is without calculating again (Saving time
## and computation power). Else, the inverse is calculated again.
## 'makeCacheMatrix' function
##
## 'x' is a invertible matrix whose inverse is to be calculated
##
## Returns a list of required functions, which are used while calculating
## the matrix inverse
## NOTE: Ensure that the entered matrix is invertible
makeCacheMatrix <- function(x = matrix()) {
i <- NULL
set <- function(w){
x <<- w
i <<- NULL
}
get <- function() x
setinverse <- function(inverse) i<<- inverse
getinverse <- function() i
list(set = set, get=get, setinverse = setinverse, getinverse = getinverse)
}
## 'cacheSolve' function
##
## 'x' is a special list generated for the matrix whose inverse is to be calculated
## through 'makeCacheMatrix' function
##
## Returns the inverted matrix for the entered matrix
cacheSolve <- function(x, ...) {
i <- x$getinverse()
## checking if matrix inverse already calculated for the entered matrix, in cache
if (!is.null(i)) {
message("getting cached data")
return(i)
}
matrix <- x$get()
## if not found in cache, calculated
i <- solve(matrix, ...)
x$setinverse(i)
i
}